"Todo" comment.
[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
ea1cb2bd 978 (format "%d:%d:"
b0aa2c65
RS
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)
008e2c2a 1013 (with-current-buffer (get-buffer-create "*Compile-Log*")
ca96ae0b 1014 (goto-char (point-max))
997011eb
RS
1015 (let* ((inhibit-read-only t)
1016 (dir (and byte-compile-current-file
4390021b
RS
1017 (file-name-directory byte-compile-current-file)))
1018 (was-same (equal default-directory dir))
1019 pt)
1020 (when dir
1021 (unless was-same
1022 (insert (format "Leaving directory `%s'\n" default-directory))))
1023 (unless (bolp)
1024 (insert "\n"))
1025 (setq pt (point-marker))
1026 (if byte-compile-current-file
1027 (insert "\f\nCompiling "
1028 (if (stringp byte-compile-current-file)
1029 (concat "file " byte-compile-current-file)
1030 (concat "buffer " (buffer-name byte-compile-current-file)))
1031 " at " (current-time-string) "\n")
1032 (insert "\f\nCompiling no file at " (current-time-string) "\n"))
1033 (when dir
1034 (setq default-directory dir)
1035 (unless was-same
1036 (insert (format "Entering directory `%s'\n" default-directory))))
9dbd7d66
RS
1037 (setq byte-compile-last-logged-file byte-compile-current-file
1038 byte-compile-last-warned-form nil)
977f31f8
RS
1039 ;; Do this after setting default-directory.
1040 (unless (eq major-mode 'compilation-mode)
1041 (compilation-mode))
b88a41d0 1042 (compilation-forget-errors)
22788fb8
RS
1043 pt))))
1044
1045;; Log a message STRING in *Compile-Log*.
1046;; Also log the current function and file if not already done.
1047(defun byte-compile-log-warning (string &optional fill level)
1048 (let ((warning-prefix-function 'byte-compile-warning-prefix)
9e31e67b 1049 (warning-type-format "")
997011eb
RS
1050 (warning-fill-prefix (if fill " "))
1051 (inhibit-read-only t))
22788fb8 1052 (display-warning 'bytecomp string level "*Compile-Log*")))
144b2637 1053
1c393159 1054(defun byte-compile-warn (format &rest args)
22788fb8 1055 "Issue a byte compiler warning; use (format FORMAT ARGS...) for message."
1c393159
JB
1056 (setq format (apply 'format format args))
1057 (if byte-compile-error-on-warn
1058 (error "%s" format) ; byte-compile-file catches and logs it
22788fb8
RS
1059 (byte-compile-log-warning format t :warning)))
1060
0b030df7 1061(defun byte-compile-report-error (error-info)
22788fb8 1062 "Report Lisp error in compilation. ERROR-INFO is the error data."
ab94e6e7 1063 (setq byte-compiler-error-flag t)
22788fb8
RS
1064 (byte-compile-log-warning
1065 (error-message-string error-info)
1066 nil :error))
0b030df7 1067
1c393159
JB
1068;;; Used by make-obsolete.
1069(defun byte-compile-obsolete (form)
20482b70
SM
1070 (let* ((new (get (car form) 'byte-obsolete-info))
1071 (handler (nth 1 new))
1072 (when (nth 2 new)))
ccb3c8de 1073 (byte-compile-set-symbol-position (car form))
e0b93488 1074 (if (memq 'obsolete byte-compile-warnings)
1d5c17c0 1075 (byte-compile-warn "`%s' is an obsolete function%s; %s" (car form)
f1953880 1076 (if when (concat " (as of Emacs " when ")") "")
9290191f
RS
1077 (if (stringp (car new))
1078 (car new)
1d5c17c0 1079 (format "use `%s' instead." (car new)))))
20482b70 1080 (funcall (or handler 'byte-compile-normal-call) form)))
1c393159
JB
1081\f
1082;; Compiler options
1083
52799cb8
RS
1084;; (defvar byte-compiler-valid-options
1085;; '((optimize byte-optimize (t nil source byte) val)
1086;; (file-format byte-compile-compatibility (emacs18 emacs19)
1087;; (eq val 'emacs18))
1088;; ;; (new-bytecodes byte-compile-generate-emacs19-bytecodes (t nil) val)
1089;; (delete-errors byte-compile-delete-errors (t nil) val)
1090;; (verbose byte-compile-verbose (t nil) val)
1091;; (warnings byte-compile-warnings ((callargs redefine free-vars unresolved))
1092;; val)))
1c393159
JB
1093
1094;; Inhibit v18/v19 selectors if the version is hardcoded.
c5091f25 1095;; #### This should print a warning if the user tries to change something
1c393159 1096;; than can't be changed because the running compiler doesn't support it.
52799cb8
RS
1097;; (cond
1098;; ((byte-compile-single-version)
1099;; (setcar (cdr (cdr (assq 'new-bytecodes byte-compiler-valid-options)))
1100;; (list (byte-compile-version-cond
1101;; byte-compile-generate-emacs19-bytecodes)))
1102;; (setcar (cdr (cdr (assq 'file-format byte-compiler-valid-options)))
1103;; (if (byte-compile-version-cond byte-compile-compatibility)
1104;; '(emacs18) '(emacs19)))))
1105
1106;; (defun byte-compiler-options-handler (&rest args)
1107;; (let (key val desc choices)
1108;; (while args
1109;; (if (or (atom (car args)) (nthcdr 2 (car args)) (null (cdr (car args))))
1110;; (error "Malformed byte-compiler option `%s'" (car args)))
1111;; (setq key (car (car args))
1112;; val (car (cdr (car args)))
1113;; desc (assq key byte-compiler-valid-options))
1114;; (or desc
1115;; (error "Unknown byte-compiler option `%s'" key))
1116;; (setq choices (nth 2 desc))
1117;; (if (consp (car choices))
1118;; (let (this
1119;; (handler 'cons)
1120;; (ret (and (memq (car val) '(+ -))
1121;; (copy-sequence (if (eq t (symbol-value (nth 1 desc)))
1122;; choices
1123;; (symbol-value (nth 1 desc)))))))
1124;; (setq choices (car choices))
1125;; (while val
1126;; (setq this (car val))
1127;; (cond ((memq this choices)
1128;; (setq ret (funcall handler this ret)))
1129;; ((eq this '+) (setq handler 'cons))
1130;; ((eq this '-) (setq handler 'delq))
1131;; ((error "`%s' only accepts %s" key choices)))
1132;; (setq val (cdr val)))
1133;; (set (nth 1 desc) ret))
1134;; (or (memq val choices)
1135;; (error "`%s' must be one of `%s'" key choices))
1136;; (set (nth 1 desc) (eval (nth 3 desc))))
1137;; (setq args (cdr args)))
1138;; nil))
1c393159
JB
1139\f
1140;;; sanity-checking arglists
1141
a7a7ddf1
RS
1142;; If a function has an entry saying (FUNCTION . t).
1143;; that means we know it is defined but we don't know how.
1144;; If a function has an entry saying (FUNCTION . nil),
1145;; that means treat it as not defined.
1c393159
JB
1146(defun byte-compile-fdefinition (name macro-p)
1147 (let* ((list (if macro-p
1148 byte-compile-macro-environment
5286a842 1149 byte-compile-function-environment))
1c393159
JB
1150 (env (cdr (assq name list))))
1151 (or env
1152 (let ((fn name))
1153 (while (and (symbolp fn)
1154 (fboundp fn)
1155 (or (symbolp (symbol-function fn))
1156 (consp (symbol-function fn))
1157 (and (not macro-p)
ed015bdd 1158 (byte-code-function-p (symbol-function fn)))))
1c393159 1159 (setq fn (symbol-function fn)))
ed015bdd 1160 (if (and (not macro-p) (byte-code-function-p fn))
1c393159
JB
1161 fn
1162 (and (consp fn)
1163 (if (eq 'macro (car fn))
1164 (cdr fn)
1165 (if macro-p
1166 nil
1167 (if (eq 'autoload (car fn))
1168 nil
1169 fn)))))))))
1170
1171(defun byte-compile-arglist-signature (arglist)
1172 (let ((args 0)
1173 opts
1174 restp)
1175 (while arglist
1176 (cond ((eq (car arglist) '&optional)
1177 (or opts (setq opts 0)))
1178 ((eq (car arglist) '&rest)
1179 (if (cdr arglist)
1180 (setq restp t
1181 arglist nil)))
1182 (t
1183 (if opts
1184 (setq opts (1+ opts))
1185 (setq args (1+ args)))))
1186 (setq arglist (cdr arglist)))
1187 (cons args (if restp nil (if opts (+ args opts) args)))))
1188
1189
1190(defun byte-compile-arglist-signatures-congruent-p (old new)
1191 (not (or
1192 (> (car new) (car old)) ; requires more args now
a7acbbe4 1193 (and (null (cdr old)) ; took rest-args, doesn't any more
1c393159
JB
1194 (cdr new))
1195 (and (cdr new) (cdr old) ; can't take as many args now
1196 (< (cdr new) (cdr old)))
1197 )))
1198
1199(defun byte-compile-arglist-signature-string (signature)
1200 (cond ((null (cdr signature))
1201 (format "%d+" (car signature)))
1202 ((= (car signature) (cdr signature))
1203 (format "%d" (car signature)))
1204 (t (format "%d-%d" (car signature) (cdr signature)))))
1205
1206
52799cb8 1207;; Warn if the form is calling a function with the wrong number of arguments.
1c393159 1208(defun byte-compile-callargs-warn (form)
1c393159
JB
1209 (let* ((def (or (byte-compile-fdefinition (car form) nil)
1210 (byte-compile-fdefinition (car form) t)))
a7a7ddf1 1211 (sig (if (and def (not (eq def t)))
ed62683d
DL
1212 (byte-compile-arglist-signature
1213 (if (eq 'lambda (car-safe def))
1214 (nth 1 def)
1215 (if (byte-code-function-p def)
1216 (aref def 0)
1217 '(&rest def))))
1218 (if (and (fboundp (car form))
1219 (subrp (symbol-function (car form))))
1220 (subr-arity (symbol-function (car form))))))
1c393159 1221 (ncall (length (cdr form))))
ed62683d
DL
1222 ;; Check many or unevalled from subr-arity.
1223 (if (and (cdr-safe sig)
1224 (not (numberp (cdr sig))))
1225 (setcdr sig nil))
1c393159 1226 (if sig
ccb3c8de 1227 (when (or (< ncall (car sig))
1c393159 1228 (and (cdr sig) (> ncall (cdr sig))))
ccb3c8de
CW
1229 (byte-compile-set-symbol-position (car form))
1230 (byte-compile-warn
1231 "%s called with %d argument%s, but %s %s"
1232 (car form) ncall
1233 (if (= 1 ncall) "" "s")
1234 (if (< ncall (car sig))
1235 "requires"
1236 "accepts only")
ba76e7fa 1237 (byte-compile-arglist-signature-string sig))))
ab43c850 1238 (byte-compile-format-warn form)
ba76e7fa
SM
1239 ;; Check to see if the function will be available at runtime
1240 ;; and/or remember its arity if it's unknown.
a7a7ddf1 1241 (or (and (or def (fboundp (car form))) ; might be a subr or autoload.
ab43c850 1242 (not (memq (car form) byte-compile-noruntime-functions)))
ba76e7fa
SM
1243 (eq (car form) byte-compile-current-form) ; ## this doesn't work
1244 ; with recursion.
1245 ;; It's a currently-undefined function.
1246 ;; Remember number of args in call.
1247 (let ((cons (assq (car form) byte-compile-unresolved-functions))
1248 (n (length (cdr form))))
1249 (if cons
1250 (or (memq n (cdr cons))
1251 (setcdr cons (cons n (cdr cons))))
977b50fb
SM
1252 (push (list (car form) n)
1253 byte-compile-unresolved-functions))))))
1c393159 1254
ab43c850
RS
1255(defun byte-compile-format-warn (form)
1256 "Warn if FORM is `format'-like with inconsistent args.
1257Applies if head of FORM is a symbol with non-nil property
1258`byte-compile-format-like' and first arg is a constant string.
1259Then check the number of format fields matches the number of
1260extra args."
1261 (when (and (symbolp (car form))
1262 (stringp (nth 1 form))
1263 (get (car form) 'byte-compile-format-like))
1264 (let ((nfields (with-temp-buffer
1265 (insert (nth 1 form))
b8104a2b 1266 (goto-char (point-min))
ab43c850
RS
1267 (let ((n 0))
1268 (while (re-search-forward "%." nil t)
1269 (unless (eq ?% (char-after (1+ (match-beginning 0))))
1270 (setq n (1+ n))))
1271 n)))
1272 (nargs (- (length form) 2)))
1273 (unless (= nargs nfields)
1274 (byte-compile-warn
1275 "`%s' called with %d args to fill %d format field(s)" (car form)
1276 nargs nfields)))))
1277
1278(dolist (elt '(format message error))
1279 (put elt 'byte-compile-format-like t))
1280
11efeb9b
RS
1281;; Warn if a custom definition fails to specify :group.
1282(defun byte-compile-nogroup-warn (form)
3ab6a7ae
SM
1283 (let ((keyword-args (cdr (cdr (cdr (cdr form)))))
1284 (name (cadr form)))
1285 (or (not (eq (car-safe name) 'quote))
1286 (and (eq (car form) 'custom-declare-group)
1287 (equal name ''emacs))
1288 (plist-get keyword-args :group)
1289 (not (and (consp name) (eq (car name) 'quote)))
1290 (byte-compile-warn
1291 "%s for `%s' fails to specify containing group"
1292 (cdr (assq (car form)
1293 '((custom-declare-group . defgroup)
1294 (custom-declare-face . defface)
1295 (custom-declare-variable . defcustom))))
1296 (cadr name)))))
11efeb9b 1297
52799cb8
RS
1298;; Warn if the function or macro is being redefined with a different
1299;; number of arguments.
1c393159 1300(defun byte-compile-arglist-warn (form macrop)
1c393159 1301 (let ((old (byte-compile-fdefinition (nth 1 form) macrop)))
a7a7ddf1 1302 (if (and old (not (eq old t)))
1c393159
JB
1303 (let ((sig1 (byte-compile-arglist-signature
1304 (if (eq 'lambda (car-safe old))
1305 (nth 1 old)
d82e848c 1306 (if (byte-code-function-p old)
5286a842
RS
1307 (aref old 0)
1308 '(&rest def)))))
1c393159 1309 (sig2 (byte-compile-arglist-signature (nth 2 form))))
ccb3c8de
CW
1310 (unless (byte-compile-arglist-signatures-congruent-p sig1 sig2)
1311 (byte-compile-set-symbol-position (nth 1 form))
1312 (byte-compile-warn
1313 "%s %s used to take %s %s, now takes %s"
1314 (if (eq (car form) 'defun) "function" "macro")
1315 (nth 1 form)
1316 (byte-compile-arglist-signature-string sig1)
1317 (if (equal sig1 '(1 . 1)) "argument" "arguments")
1318 (byte-compile-arglist-signature-string sig2))))
1c393159
JB
1319 ;; This is the first definition. See if previous calls are compatible.
1320 (let ((calls (assq (nth 1 form) byte-compile-unresolved-functions))
1321 nums sig min max)
1322 (if calls
1323 (progn
1324 (setq sig (byte-compile-arglist-signature (nth 2 form))
1325 nums (sort (copy-sequence (cdr calls)) (function <))
1326 min (car nums)
1327 max (car (nreverse nums)))
ccb3c8de 1328 (when (or (< min (car sig))
1c393159 1329 (and (cdr sig) (> max (cdr sig))))
ccb3c8de
CW
1330 (byte-compile-set-symbol-position (nth 1 form))
1331 (byte-compile-warn
1332 "%s being defined to take %s%s, but was previously called with %s"
1333 (nth 1 form)
1334 (byte-compile-arglist-signature-string sig)
1335 (if (equal sig '(1 . 1)) " arg" " args")
1336 (byte-compile-arglist-signature-string (cons min max))))
1f006824 1337
1c393159
JB
1338 (setq byte-compile-unresolved-functions
1339 (delq calls byte-compile-unresolved-functions)))))
1340 )))
1341
95c997fa
RS
1342(defvar byte-compile-cl-functions nil
1343 "List of functions defined in CL.")
1344
1345(defun byte-compile-find-cl-functions ()
1346 (unless byte-compile-cl-functions
1347 (dolist (elt load-history)
c43008c4 1348 (when (and (stringp (car elt))
7940bf7d
MR
1349 (string-match
1350 "^cl\\>" (file-name-nondirectory (car elt))))
95c997fa
RS
1351 (setq byte-compile-cl-functions
1352 (append byte-compile-cl-functions
1353 (cdr elt)))))
1354 (let ((tail byte-compile-cl-functions))
1355 (while tail
1356 (if (and (consp (car tail))
1357 (eq (car (car tail)) 'autoload))
1358 (setcar tail (cdr (car tail))))
1359 (setq tail (cdr tail))))))
1360
4795d1c7
RS
1361(defun byte-compile-cl-warn (form)
1362 "Warn if FORM is a call of a function from the CL package."
95c997fa
RS
1363 (let ((func (car-safe form)))
1364 (if (and byte-compile-cl-functions
1365 (memq func byte-compile-cl-functions)
ab43c850 1366 ;; Aliases which won't have been expanded at this point.
4795d1c7
RS
1367 ;; These aren't all aliases of subrs, so not trivial to
1368 ;; avoid hardwiring the list.
1369 (not (memq func
9cb9a7bc
RS
1370 '(cl-block-wrapper cl-block-throw
1371 multiple-value-call nth-value
95c997fa 1372 copy-seq first second rest endp cl-member
d1a57439
RS
1373 ;; These are included in generated code
1374 ;; that can't be called except at compile time
1375 ;; or unless cl is loaded anyway.
1376 cl-defsubst-expand cl-struct-setf-expander
8f876842
RS
1377 ;; These would sometimes be warned about
1378 ;; but such warnings are never useful,
1379 ;; so don't warn about them.
118861df
DL
1380 macroexpand cl-macroexpand-all
1381 cl-compiling-file)))
1382 ;; Avoid warnings for things which are safe because they
1383 ;; have suitable compiler macros, but those aren't
1384 ;; expanded at this stage. There should probably be more
1385 ;; here than caaar and friends.
1386 (not (and (eq (get func 'byte-compile)
1387 'cl-byte-compile-compiler-macro)
6640c250 1388 (string-match "\\`c[ad]+r\\'" (symbol-name func)))))
4795d1c7
RS
1389 (byte-compile-warn "Function `%s' from cl package called at runtime"
1390 func)))
1391 form)
1392
a586093f 1393(defun byte-compile-print-syms (str1 strn syms)
ccb3c8de
CW
1394 (when syms
1395 (byte-compile-set-symbol-position (car syms) t))
b8175fe6
GM
1396 (cond ((and (cdr syms) (not noninteractive))
1397 (let* ((str strn)
1398 (L (length str))
1399 s)
1400 (while syms
1401 (setq s (symbol-name (pop syms))
1402 L (+ L (length s) 2))
1403 (if (< L (1- fill-column))
1404 (setq str (concat str " " s (and syms ",")))
1405 (setq str (concat str "\n " s (and syms ","))
1406 L (+ (length s) 4))))
1407 (byte-compile-warn "%s" str)))
1408 ((cdr syms)
1f006824 1409 (byte-compile-warn "%s %s"
b8175fe6
GM
1410 strn
1411 (mapconcat #'symbol-name syms ", ")))
1412
1413 (syms
1414 (byte-compile-warn str1 (car syms)))))
a586093f 1415
1f006824 1416;; If we have compiled any calls to functions which are not known to be
52799cb8
RS
1417;; defined, issue a warning enumerating them.
1418;; `unresolved' in the list `byte-compile-warnings' disables this.
1c393159 1419(defun byte-compile-warn-about-unresolved-functions ()
a586093f 1420 (when (memq 'unresolved byte-compile-warnings)
b8175fe6 1421 (let ((byte-compile-current-form :end)
a586093f
SM
1422 (noruntime nil)
1423 (unresolved nil))
1424 ;; Separate the functions that will not be available at runtime
1425 ;; from the truly unresolved ones.
1426 (dolist (f byte-compile-unresolved-functions)
1427 (setq f (car f))
1428 (if (fboundp f) (push f noruntime) (push f unresolved)))
1429 ;; Complain about the no-run-time functions
1430 (byte-compile-print-syms
b8175fe6
GM
1431 "the function `%s' might not be defined at runtime."
1432 "the following functions might not be defined at runtime:"
a586093f
SM
1433 noruntime)
1434 ;; Complain about the unresolved functions
1435 (byte-compile-print-syms
b8175fe6
GM
1436 "the function `%s' is not known to be defined."
1437 "the following functions are not known to be defined:"
a586093f 1438 unresolved)))
1c393159
JB
1439 nil)
1440
1441\f
582a857c 1442(defsubst byte-compile-const-symbol-p (symbol &optional any-value)
6c2161c4 1443 "Non-nil if SYMBOL is constant.
582a857c 1444If ANY-VALUE is nil, only return non-nil if the value of the symbol is the
6c2161c4 1445symbol itself."
1639b803 1446 (or (memq symbol '(nil t))
6c2161c4 1447 (keywordp symbol)
582a857c 1448 (if any-value (memq symbol byte-compile-const-variables))))
1639b803 1449
1c393159 1450(defmacro byte-compile-constp (form)
c5091f25 1451 "Return non-nil if FORM is a constant."
1639b803
DL
1452 `(cond ((consp ,form) (eq (car ,form) 'quote))
1453 ((not (symbolp ,form)))
1454 ((byte-compile-const-symbol-p ,form))))
1c393159
JB
1455
1456(defmacro byte-compile-close-variables (&rest body)
1457 (cons 'let
1458 (cons '(;;
1459 ;; Close over these variables to encapsulate the
1460 ;; compilation state
1461 ;;
1462 (byte-compile-macro-environment
1463 ;; Copy it because the compiler may patch into the
1464 ;; macroenvironment.
1465 (copy-alist byte-compile-initial-macro-environment))
1466 (byte-compile-function-environment nil)
1467 (byte-compile-bound-variables nil)
6c2161c4 1468 (byte-compile-const-variables nil)
1c393159
JB
1469 (byte-compile-free-references nil)
1470 (byte-compile-free-assignments nil)
1471 ;;
1472 ;; Close over these variables so that `byte-compiler-options'
1473 ;; can change them on a per-file basis.
1474 ;;
1475 (byte-compile-verbose byte-compile-verbose)
1476 (byte-optimize byte-optimize)
d82e848c
RS
1477 (byte-compile-compatibility byte-compile-compatibility)
1478 (byte-compile-dynamic byte-compile-dynamic)
1479 (byte-compile-dynamic-docstrings
1480 byte-compile-dynamic-docstrings)
52799cb8
RS
1481;; (byte-compile-generate-emacs19-bytecodes
1482;; byte-compile-generate-emacs19-bytecodes)
1c393159
JB
1483 (byte-compile-warnings (if (eq byte-compile-warnings t)
1484 byte-compile-warning-types
1485 byte-compile-warnings))
1486 )
1487 body)))
1488
1c393159 1489(defmacro displaying-byte-compile-warnings (&rest body)
4390021b
RS
1490 `(let* ((--displaying-byte-compile-warnings-fn (lambda () ,@body))
1491 (warning-series-started
1492 (and (markerp warning-series)
1493 (eq (marker-buffer warning-series)
1494 (get-buffer "*Compile-Log*")))))
95c997fa 1495 (byte-compile-find-cl-functions)
4390021b
RS
1496 (if (or (eq warning-series 'byte-compile-warning-series)
1497 warning-series-started)
1498 ;; warning-series does come from compilation,
1499 ;; so don't bind it, but maybe do set it.
1500 (let (tem)
1501 ;; Log the file name. Record position of that text.
1502 (setq tem (byte-compile-log-file))
1503 (unless warning-series-started
1504 (setq warning-series (or tem 'byte-compile-warning-series)))
1505 (if byte-compile-debug
1506 (funcall --displaying-byte-compile-warnings-fn)
1507 (condition-case error-info
1508 (funcall --displaying-byte-compile-warnings-fn)
1509 (error (byte-compile-report-error error-info)))))
1510 ;; warning-series does not come from compilation, so bind it.
1511 (let ((warning-series
1512 ;; Log the file name. Record position of that text.
1513 (or (byte-compile-log-file) 'byte-compile-warning-series)))
1514 (if byte-compile-debug
22788fb8 1515 (funcall --displaying-byte-compile-warnings-fn)
4390021b
RS
1516 (condition-case error-info
1517 (funcall --displaying-byte-compile-warnings-fn)
1518 (error (byte-compile-report-error error-info))))))))
1c393159 1519\f
fd5285f3 1520;;;###autoload
9742dbc0
RS
1521(defun byte-force-recompile (directory)
1522 "Recompile every `.el' file in DIRECTORY that already has a `.elc' file.
1523Files in subdirectories of DIRECTORY are processed also."
c0f43df5 1524 (interactive "DByte force recompile (directory): ")
9742dbc0
RS
1525 (byte-recompile-directory directory nil t))
1526
1527;;;###autoload
1528(defun byte-recompile-directory (directory &optional arg force)
1c393159
JB
1529 "Recompile every `.el' file in DIRECTORY that needs recompilation.
1530This is if a `.elc' file exists but is older than the `.el' file.
691e7e76 1531Files in subdirectories of DIRECTORY are processed also.
1c393159 1532
c4f2cabd
RS
1533If the `.elc' file does not exist, normally this function *does not*
1534compile the corresponding `.el' file. However,
1535if ARG (the prefix argument) is 0, that means do compile all those files.
1536A nonzero ARG means ask the user, for each such `.el' file,
1537whether to compile it.
691e7e76 1538
c4f2cabd 1539A nonzero ARG also means ask about each subdirectory before scanning it.
9742dbc0
RS
1540
1541If the third argument FORCE is non-nil,
1542recompile every `.el' file that already has a `.elc' file."
1c393159 1543 (interactive "DByte recompile directory: \nP")
0dea0bbe
RM
1544 (if arg
1545 (setq arg (prefix-numeric-value arg)))
e27c3564
JB
1546 (if noninteractive
1547 nil
1548 (save-some-buffers)
ba901388 1549 (force-mode-line-update))
008e2c2a 1550 (with-current-buffer (get-buffer-create "*Compile-Log*")
6c2161c4 1551 (setq default-directory (expand-file-name directory))
977f31f8
RS
1552 ;; compilation-mode copies value of default-directory.
1553 (unless (eq major-mode 'compilation-mode)
1554 (compilation-mode))
ea1cb2bd 1555 (let ((directories (list default-directory))
4eb4926c
RS
1556 (default-directory default-directory)
1557 (skip-count 0)
1558 (fail-count 0)
1559 (file-count 0)
1560 (dir-count 0)
1561 last-dir)
1562 (displaying-byte-compile-warnings
1563 (while directories
1564 (setq directory (car directories))
1565 (message "Checking %s..." directory)
1566 (let ((files (directory-files directory))
1567 source dest)
1568 (dolist (file files)
1569 (setq source (expand-file-name file directory))
b122f3fb
SM
1570 (if (and (not (member file '("RCS" "CVS")))
1571 (not (eq ?\. (aref file 0)))
4eb4926c
RS
1572 (file-directory-p source)
1573 (not (file-symlink-p source)))
1574 ;; This file is a subdirectory. Handle them differently.
1575 (when (or (null arg)
1576 (eq 0 arg)
1577 (y-or-n-p (concat "Check " source "? ")))
1578 (setq directories
1579 (nconc directories (list source))))
1580 ;; It is an ordinary file. Decide whether to compile it.
1581 (if (and (string-match emacs-lisp-file-regexp source)
1582 (file-readable-p source)
1583 (not (auto-save-file-name-p source))
1584 (setq dest (byte-compile-dest-file source))
1585 (if (file-exists-p dest)
1586 ;; File was already compiled.
1587 (or force (file-newer-than-file-p source dest))
1588 ;; No compiled file exists yet.
1589 (and arg
1590 (or (eq 0 arg)
1591 (y-or-n-p (concat "Compile " source "? "))))))
1592 (progn (if (and noninteractive (not byte-compile-verbose))
1593 (message "Compiling %s..." source))
1594 (let ((res (byte-compile-file source)))
1595 (cond ((eq res 'no-byte-compile)
1596 (setq skip-count (1+ skip-count)))
1597 ((eq res t)
1598 (setq file-count (1+ file-count)))
1599 ((eq res nil)
1600 (setq fail-count (1+ fail-count)))))
1601 (or noninteractive
1602 (message "Checking %s..." directory))
1603 (if (not (eq last-dir directory))
1604 (setq last-dir directory
1605 dir-count (1+ dir-count)))
1606 )))))
1607 (setq directories (cdr directories))))
1608 (message "Done (Total of %d file%s compiled%s%s%s)"
1609 file-count (if (= file-count 1) "" "s")
1610 (if (> fail-count 0) (format ", %d failed" fail-count) "")
1611 (if (> skip-count 0) (format ", %d skipped" skip-count) "")
1612 (if (> dir-count 1) (format " in %d directories" dir-count) "")))))
1c393159 1613
fef3407e
SM
1614(defvar no-byte-compile nil
1615 "Non-nil to prevent byte-compiling of emacs-lisp code.
1616This is normally set in local file variables at the end of the elisp file:
1617
1618;; Local Variables:\n;; no-byte-compile: t\n;; End: ")
631c8020 1619;;;###autoload(put 'no-byte-compile 'safe-local-variable 'booleanp)
fef3407e 1620
fd5285f3 1621;;;###autoload
1c393159
JB
1622(defun byte-compile-file (filename &optional load)
1623 "Compile a file of Lisp code named FILENAME into a file of byte code.
f9b4b5d8
JB
1624The output file's name is generated by passing FILENAME to the
1625`byte-compile-dest-file' function (which see).
3614fc84 1626With prefix arg (noninteractively: 2nd arg), LOAD the file after compiling.
d90a41e8 1627The value is non-nil if there were no errors, nil if errors."
1c393159
JB
1628;; (interactive "fByte compile file: \nP")
1629 (interactive
1630 (let ((file buffer-file-name)
1631 (file-name nil)
1632 (file-dir nil))
1633 (and file
1634 (eq (cdr (assq 'major-mode (buffer-local-variables)))
1635 'emacs-lisp-mode)
1636 (setq file-name (file-name-nondirectory file)
1637 file-dir (file-name-directory file)))
52799cb8
RS
1638 (list (read-file-name (if current-prefix-arg
1639 "Byte compile and load file: "
1640 "Byte compile file: ")
79c6071d 1641 file-dir file-name nil)
fd5285f3 1642 current-prefix-arg)))
1c393159
JB
1643 ;; Expand now so we get the current buffer's defaults
1644 (setq filename (expand-file-name filename))
1645
1646 ;; If we're compiling a file that's in a buffer and is modified, offer
1647 ;; to save it first.
1648 (or noninteractive
1649 (let ((b (get-file-buffer (expand-file-name filename))))
1650 (if (and b (buffer-modified-p b)
a586093f 1651 (y-or-n-p (format "Save buffer %s first? " (buffer-name b))))
008e2c2a 1652 (with-current-buffer b (save-buffer)))))
1c393159 1653
4390021b
RS
1654 ;; Force logging of the file name for each file compiled.
1655 (setq byte-compile-last-logged-file nil)
3ea1f391 1656 (let ((byte-compile-current-file filename)
dc14ae36 1657 (set-auto-coding-for-load t)
d82e848c
RS
1658 target-file input-buffer output-buffer
1659 byte-compile-dest-file)
1660 (setq target-file (byte-compile-dest-file filename))
1661 (setq byte-compile-dest-file target-file)
ea1cb2bd 1662 (with-current-buffer
008e2c2a 1663 (setq input-buffer (get-buffer-create " *Compiler Input*"))
1c393159 1664 (erase-buffer)
7a28e3b1 1665 (setq buffer-file-coding-system nil)
746dd298 1666 ;; Always compile an Emacs Lisp file as multibyte
b92dd692 1667 ;; unless the file itself forces unibyte with -*-coding: raw-text;-*-
746dd298 1668 (set-buffer-multibyte t)
1c393159 1669 (insert-file-contents filename)
844da0ff
KH
1670 ;; Mimic the way after-insert-file-set-coding can make the
1671 ;; buffer unibyte when visiting this file.
7a28e3b1
RS
1672 (when (or (eq last-coding-system-used 'no-conversion)
1673 (eq (coding-system-type last-coding-system-used) 5))
1674 ;; For coding systems no-conversion and raw-text...,
1675 ;; edit the buffer as unibyte.
1676 (set-buffer-multibyte nil))
1c393159
JB
1677 ;; Run hooks including the uncompression hook.
1678 ;; If they change the file name, then change it for the output also.
d82e848c 1679 (let ((buffer-file-name filename)
a096f13f 1680 (default-major-mode 'emacs-lisp-mode)
aa9addfa
RS
1681 ;; Ignore unsafe local variables.
1682 ;; We only care about a few of them for our purposes.
1683 (enable-local-variables :safe)
d82e848c 1684 (enable-local-eval nil))
aa9addfa
RS
1685 ;; Arg of t means don't alter enable-local-variables.
1686 (normal-mode t)
cd891e68
RS
1687 (setq filename buffer-file-name))
1688 ;; Set the default directory, in case an eval-when-compile uses it.
1689 (setq default-directory (file-name-directory filename)))
3614fc84
GM
1690 ;; Check if the file's local variables explicitly specify not to
1691 ;; compile this file.
fef3407e 1692 (if (with-current-buffer input-buffer no-byte-compile)
3614fc84 1693 (progn
b122f3fb
SM
1694 ;; (message "%s not compiled because of `no-byte-compile: %s'"
1695 ;; (file-relative-name filename)
1696 ;; (with-current-buffer input-buffer no-byte-compile))
1697 (when (file-exists-p target-file)
1698 (message "%s deleted because of `no-byte-compile: %s'"
1699 (file-relative-name target-file)
1700 (buffer-local-value 'no-byte-compile input-buffer))
1701 (condition-case nil (delete-file target-file) (error nil)))
82345a9a 1702 ;; We successfully didn't compile this file.
d90a41e8 1703 'no-byte-compile)
ccb3c8de
CW
1704 (when byte-compile-verbose
1705 (message "Compiling %s..." filename))
82345a9a
SM
1706 (setq byte-compiler-error-flag nil)
1707 ;; It is important that input-buffer not be current at this call,
1708 ;; so that the value of point set in input-buffer
1709 ;; within byte-compile-from-buffer lingers in that buffer.
4f6d5bf0
SM
1710 (setq output-buffer
1711 (save-current-buffer
1712 (byte-compile-from-buffer input-buffer filename)))
82345a9a
SM
1713 (if byte-compiler-error-flag
1714 nil
ccb3c8de
CW
1715 (when byte-compile-verbose
1716 (message "Compiling %s...done" filename))
82345a9a
SM
1717 (kill-buffer input-buffer)
1718 (with-current-buffer output-buffer
1719 (goto-char (point-max))
1720 (insert "\n") ; aaah, unix.
1721 (let ((vms-stmlf-recfm t))
1722 (if (file-writable-p target-file)
1723 ;; We must disable any code conversion here.
1724 (let ((coding-system-for-write 'no-conversion))
1725 (if (memq system-type '(ms-dos 'windows-nt))
1726 (setq buffer-file-type t))
1727 (when (file-exists-p target-file)
1728 ;; Remove the target before writing it, so that any
1729 ;; hard-links continue to point to the old file (this makes
1730 ;; it possible for installed files to share disk space with
1731 ;; the build tree, without causing problems when emacs-lisp
1732 ;; files in the build tree are recompiled).
1733 (delete-file target-file))
ba76e7fa 1734 (write-region (point-min) (point-max) target-file))
82345a9a
SM
1735 ;; This is just to give a better error message than write-region
1736 (signal 'file-error
1737 (list "Opening output file"
1738 (if (file-exists-p target-file)
1739 "cannot overwrite file"
1740 "directory not writable or nonexistent")
1741 target-file))))
1742 (kill-buffer (current-buffer)))
1743 (if (and byte-compile-generate-call-tree
1744 (or (eq t byte-compile-generate-call-tree)
1745 (y-or-n-p (format "Report call tree for %s? " filename))))
1746 (save-excursion
1747 (display-call-tree filename)))
1748 (if load
1749 (load target-file))
1750 t))))
1c393159 1751
52799cb8
RS
1752;;(defun byte-compile-and-load-file (&optional filename)
1753;; "Compile a file of Lisp code named FILENAME into a file of byte code,
c5091f25 1754;;and then load it. The output file's name is made by appending \"c\" to
52799cb8
RS
1755;;the end of FILENAME."
1756;; (interactive)
1757;; (if filename ; I don't get it, (interactive-p) doesn't always work
1758;; (byte-compile-file filename t)
1759;; (let ((current-prefix-arg '(4)))
1760;; (call-interactively 'byte-compile-file))))
1761
1762;;(defun byte-compile-buffer (&optional buffer)
1763;; "Byte-compile and evaluate contents of BUFFER (default: the current buffer)."
1764;; (interactive "bByte compile buffer: ")
1765;; (setq buffer (if buffer (get-buffer buffer) (current-buffer)))
1766;; (message "Compiling %s..." (buffer-name buffer))
1767;; (let* ((filename (or (buffer-file-name buffer)
1768;; (concat "#<buffer " (buffer-name buffer) ">")))
1769;; (byte-compile-current-file buffer))
8a5dd086 1770;; (byte-compile-from-buffer buffer nil))
52799cb8
RS
1771;; (message "Compiling %s...done" (buffer-name buffer))
1772;; t)
1c393159
JB
1773
1774;;; compiling a single function
fd5285f3 1775;;;###autoload
52799cb8 1776(defun compile-defun (&optional arg)
1c393159 1777 "Compile and evaluate the current top-level form.
7618fe8e 1778Print the result in the echo area.
1c393159
JB
1779With argument, insert value in current buffer after the form."
1780 (interactive "P")
1781 (save-excursion
1782 (end-of-defun)
1783 (beginning-of-defun)
1784 (let* ((byte-compile-current-file nil)
ccb3c8de
CW
1785 (byte-compile-current-buffer (current-buffer))
1786 (byte-compile-read-position (point))
1787 (byte-compile-last-position byte-compile-read-position)
1c393159 1788 (byte-compile-last-warned-form 'nothing)
ccb3c8de 1789 (value (eval
9cb9a7bc 1790 (let ((read-with-symbol-positions (current-buffer))
ccb3c8de
CW
1791 (read-symbol-positions-list nil))
1792 (displaying-byte-compile-warnings
1793 (byte-compile-sexp (read (current-buffer))))))))
1c393159
JB
1794 (cond (arg
1795 (message "Compiling from buffer... done.")
1796 (prin1 value (current-buffer))
1797 (insert "\n"))
1798 ((message "%s" (prin1-to-string value)))))))
1799
1800
8a5dd086
RS
1801(defun byte-compile-from-buffer (inbuffer &optional filename)
1802 ;; Filename is used for the loading-into-Emacs-18 error message.
d82e848c 1803 (let (outbuffer
ccb3c8de
CW
1804 (byte-compile-current-buffer inbuffer)
1805 (byte-compile-read-position nil)
1806 (byte-compile-last-position nil)
d82e848c
RS
1807 ;; Prevent truncation of flonums and lists as we read and print them
1808 (float-output-format nil)
1809 (case-fold-search nil)
1810 (print-length nil)
95e7d933 1811 (print-level nil)
74dfd056
RS
1812 ;; Prevent edebug from interfering when we compile
1813 ;; and put the output into a file.
ccb3c8de
CW
1814;; (edebug-all-defs nil)
1815;; (edebug-all-forms nil)
d82e848c
RS
1816 ;; Simulate entry to byte-compile-top-level
1817 (byte-compile-constants nil)
1818 (byte-compile-variables nil)
1819 (byte-compile-tag-number 0)
1820 (byte-compile-depth 0)
1821 (byte-compile-maxdepth 0)
1822 (byte-compile-output nil)
ccb3c8de 1823 ;; This allows us to get the positions of symbols read; it's
bf247b6e 1824 ;; new in Emacs 22.1.
ccb3c8de
CW
1825 (read-with-symbol-positions inbuffer)
1826 (read-symbol-positions-list nil)
d82e848c
RS
1827 ;; #### This is bound in b-c-close-variables.
1828 ;; (byte-compile-warnings (if (eq byte-compile-warnings t)
1829 ;; byte-compile-warning-types
1830 ;; byte-compile-warnings))
1831 )
1832 (byte-compile-close-variables
b8104a2b
SM
1833 (with-current-buffer
1834 (setq outbuffer (get-buffer-create " *Compiler Output*"))
08b59cd3 1835 (set-buffer-multibyte t)
d82e848c
RS
1836 (erase-buffer)
1837 ;; (emacs-lisp-mode)
1838 (setq case-fold-search nil)
d82e848c
RS
1839 ;; This is a kludge. Some operating systems (OS/2, DOS) need to
1840 ;; write files containing binary information specially.
1841 ;; Under most circumstances, such files will be in binary
1842 ;; overwrite mode, so those OS's use that flag to guess how
1843 ;; they should write their data. Advise them that .elc files
1844 ;; need to be written carefully.
1845 (setq overwrite-mode 'overwrite-mode-binary))
1846 (displaying-byte-compile-warnings
fb639443 1847 (and filename (byte-compile-insert-header filename inbuffer outbuffer))
b8104a2b
SM
1848 (with-current-buffer inbuffer
1849 (goto-char (point-min))
2cb63a7c
AM
1850 ;; Should we always do this? When calling multiple files, it
1851 ;; would be useful to delay this warning until all have been
1852 ;; compiled. A: Yes! b-c-u-f might contain dross from a
1853 ;; previous byte-compile.
1854 (setq byte-compile-unresolved-functions nil)
d82e848c
RS
1855
1856 ;; Compile the forms from the input buffer.
1857 (while (progn
1858 (while (progn (skip-chars-forward " \t\n\^l")
1859 (looking-at ";"))
1860 (forward-line 1))
1861 (not (eobp)))
ccb3c8de
CW
1862 (setq byte-compile-read-position (point)
1863 byte-compile-last-position byte-compile-read-position)
36e65f70
SM
1864 (let* ((old-style-backquotes nil)
1865 (form (read inbuffer)))
1866 ;; Warn about the use of old-style backquotes.
1867 (when old-style-backquotes
1868 (byte-compile-warn "!! The file uses old-style backquotes !!
1869This functionality has been obsolete for more than 10 years already
1870and will be removed soon. See (elisp)Backquote in the manual."))
ccb3c8de 1871 (byte-compile-file-form form)))
d82e848c
RS
1872 ;; Compile pending forms at end of file.
1873 (byte-compile-flush-pending)
977f31f8
RS
1874 ;; Make warnings about unresolved functions
1875 ;; give the end of the file as their position.
1876 (setq byte-compile-last-position (point-max))
2cb63a7c 1877 (byte-compile-warn-about-unresolved-functions))
fb639443
RS
1878 ;; Fix up the header at the front of the output
1879 ;; if the buffer contains multibyte characters.
1880 (and filename (byte-compile-fix-header filename inbuffer outbuffer))))
285cdf4e 1881 outbuffer))
8a5dd086 1882
fb639443 1883(defun byte-compile-fix-header (filename inbuffer outbuffer)
6c2161c4 1884 (with-current-buffer outbuffer
fb639443 1885 ;; See if the buffer has any multibyte characters.
447a052b 1886 (when (< (point-max) (position-bytes (point-max)))
fb639443
RS
1887 (when (byte-compile-version-cond byte-compile-compatibility)
1888 (error "Version-18 compatibility not valid with multibyte characters"))
1889 (goto-char (point-min))
1890 ;; Find the comment that describes the version test.
1891 (search-forward "\n;;; This file")
1892 (beginning-of-line)
1893 (narrow-to-region (point) (point-max))
1894 ;; Find the line of ballast semicolons.
1895 (search-forward ";;;;;;;;;;")
1896 (beginning-of-line)
1897
1898 (narrow-to-region (point-min) (point))
1899 (let ((old-header-end (point))
1900 delta)
1901 (goto-char (point-min))
1902 (delete-region (point) (progn (re-search-forward "^(")
1903 (beginning-of-line)
1904 (point)))
1905 (insert ";;; This file contains multibyte non-ASCII characters\n"
1906 ";;; and therefore cannot be loaded into Emacs 19.\n")
1907 ;; Replace "19" or "19.29" with "20", twice.
1908 (re-search-forward "19\\(\\.[0-9]+\\)")
1909 (replace-match "20")
1910 (re-search-forward "19\\(\\.[0-9]+\\)")
1911 (replace-match "20")
1912 ;; Now compensate for the change in size,
1913 ;; to make sure all positions in the file remain valid.
1914 (setq delta (- (point-max) old-header-end))
1915 (goto-char (point-max))
1916 (widen)
1917 (delete-char delta)))))
1918
f1f32df9
RS
1919(defun byte-compile-insert-header (filename inbuffer outbuffer)
1920 (set-buffer inbuffer)
1e098504 1921 (let ((dynamic-docstrings byte-compile-dynamic-docstrings)
fb639443 1922 (dynamic byte-compile-dynamic))
f1f32df9 1923 (set-buffer outbuffer)
b8104a2b 1924 (goto-char (point-min))
1596952e
RS
1925 ;; The magic number of .elc files is ";ELC", or 0x3B454C43. After
1926 ;; that is the file-format version number (18, 19 or 20) as a
1927 ;; byte, followed by some nulls. The primary motivation for doing
1928 ;; this is to get some binary characters up in the first line of
1929 ;; the file so that `diff' will simply say "Binary files differ"
1930 ;; instead of actually doing a diff of two .elc files. An extra
1931 ;; benefit is that you can add this to /etc/magic:
1932
f1f32df9
RS
1933 ;; 0 string ;ELC GNU Emacs Lisp compiled file,
1934 ;; >4 byte x version %d
1596952e 1935
f1f32df9
RS
1936 (insert
1937 ";ELC"
1596952e 1938 (if (byte-compile-version-cond byte-compile-compatibility) 18 20)
f1f32df9
RS
1939 "\000\000\000\n"
1940 )
1e098504 1941 (insert ";;; Compiled by "
f1f32df9
RS
1942 (or (and (boundp 'user-mail-address) user-mail-address)
1943 (concat (user-login-name) "@" (system-name)))
1944 " on "
1945 (current-time-string) "\n;;; from file " filename "\n")
1e098504 1946 (insert ";;; in Emacs version " emacs-version "\n")
1f3ddf11 1947 (insert ";;; "
f1f32df9 1948 (cond
1e098504
RS
1949 ((eq byte-optimize 'source) "with source-level optimization only")
1950 ((eq byte-optimize 'byte) "with byte-level optimization only")
1951 (byte-optimize "with all optimizations")
1952 (t "without optimization"))
f1f32df9
RS
1953 (if (byte-compile-version-cond byte-compile-compatibility)
1954 "; compiled with Emacs 18 compatibility.\n"
1955 ".\n"))
1e098504
RS
1956 (if dynamic
1957 (insert ";;; Function definitions are lazy-loaded.\n"))
fb639443 1958 (if (not (byte-compile-version-cond byte-compile-compatibility))
1596952e
RS
1959 (let (intro-string minimum-version)
1960 ;; Figure out which Emacs version to require,
1961 ;; and what comment to use to explain why.
fb639443
RS
1962 ;; Note that this fails to take account of whether
1963 ;; the buffer contains multibyte characters. We may have to
1964 ;; compensate at the end in byte-compile-fix-header.
1965 (if dynamic-docstrings
1596952e 1966 (setq intro-string
fb639443
RS
1967 ";;; This file uses dynamic docstrings, first added in Emacs 19.29.\n"
1968 minimum-version "19.29")
1969 (setq intro-string
1970 ";;; This file uses opcodes which do not exist in Emacs 18.\n"
1971 minimum-version "19"))
1596952e
RS
1972 ;; Now insert the comment and the error check.
1973 (insert
fb639443 1974 "\n"
1596952e
RS
1975 intro-string
1976 ;; Have to check if emacs-version is bound so that this works
1977 ;; in files loaded early in loadup.el.
fb639443 1978 "(if (and (boundp 'emacs-version)\n"
1596952e
RS
1979 ;; If there is a name at the end of emacs-version,
1980 ;; don't try to check the version number.
1981 "\t (< (aref emacs-version (1- (length emacs-version))) ?A)\n"
1982 "\t (or (and (boundp 'epoch::version) epoch::version)\n"
1983 (format "\t (string-lessp emacs-version \"%s\")))\n"
1984 minimum-version)
1985 " (error \"`"
1986 ;; prin1-to-string is used to quote backslashes.
1987 (substring (prin1-to-string (file-name-nondirectory filename))
1988 1 -1)
1989 (format "' was compiled for Emacs %s or later\"))\n\n"
fb639443
RS
1990 minimum-version)
1991 ;; Insert semicolons as ballast, so that byte-compile-fix-header
1992 ;; can delete them so as to keep the buffer positions
1993 ;; constant for the actual compiled code.
1994 ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n\n"))
1596952e 1995 ;; Here if we want Emacs 18 compatibility.
fb639443
RS
1996 (when dynamic-docstrings
1997 (error "Version-18 compatibility doesn't support dynamic doc strings"))
1998 (when byte-compile-dynamic
1999 (error "Version-18 compatibility doesn't support dynamic byte code"))
f1f32df9 2000 (insert "(or (boundp 'current-load-list) (setq current-load-list nil))\n"
1596952e 2001 "\n"))))
1c393159
JB
2002
2003(defun byte-compile-output-file-form (form)
2004 ;; writes the given form to the output buffer, being careful of docstrings
36b7e523
RS
2005 ;; in defun, defmacro, defvar, defconst, autoload and
2006 ;; custom-declare-variable because make-docfile is so amazingly stupid.
c36881cf
ER
2007 ;; defalias calls are output directly by byte-compile-file-form-defmumble;
2008 ;; it does not pay to first build the defalias in defmumble and then parse
2009 ;; it here.
36b7e523
RS
2010 (if (and (memq (car-safe form) '(defun defmacro defvar defconst autoload
2011 custom-declare-variable))
1c393159 2012 (stringp (nth 3 form)))
d82e848c 2013 (byte-compile-output-docform nil nil '("\n(" 3 ")") form nil
36b7e523
RS
2014 (memq (car form)
2015 '(autoload custom-declare-variable)))
1c393159 2016 (let ((print-escape-newlines t)
37c29340
KH
2017 (print-length nil)
2018 (print-level nil)
77308fd7 2019 (print-quoted t)
5e51de79 2020 (print-gensym t)
0e66b003
KH
2021 (print-circle ; handle circular data structures
2022 (not byte-compile-disable-print-circle)))
1c393159
JB
2023 (princ "\n" outbuffer)
2024 (prin1 form outbuffer)
2025 nil)))
2026
6c2161c4
SM
2027(defvar print-gensym-alist) ;Used before print-circle existed.
2028
d82e848c 2029(defun byte-compile-output-docform (preface name info form specindex quoted)
dac6f673
RS
2030 "Print a form with a doc string. INFO is (prefix doc-index postfix).
2031If PREFACE and NAME are non-nil, print them too,
2032before INFO and the FORM but after the doc string itself.
2033If SPECINDEX is non-nil, it is the index in FORM
2034of the function bytecode string. In that case,
2035we output that argument and the following argument (the constants vector)
2036together, for lazy loading.
2037QUOTED says that we have to put a quote before the
2038list that represents a doc string reference.
36b7e523 2039`autoload' and `custom-declare-variable' need that."
dac6f673
RS
2040 ;; We need to examine byte-compile-dynamic-docstrings
2041 ;; in the input buffer (now current), not in the output buffer.
2042 (let ((dynamic-docstrings byte-compile-dynamic-docstrings))
9ec5dfe6
SM
2043 (with-current-buffer outbuffer
2044 (let (position)
2045
2046 ;; Insert the doc string, and make it a comment with #@LENGTH.
2047 (and (>= (nth 1 info) 0)
2048 dynamic-docstrings
2049 (not byte-compile-compatibility)
2050 (progn
2051 ;; Make the doc string start at beginning of line
2052 ;; for make-docfile's sake.
2053 (insert "\n")
2054 (setq position
2055 (byte-compile-output-as-comment
2056 (nth (nth 1 info) form) nil))
2057 (setq position (- (position-bytes position) (point-min) -1))
2058 ;; If the doc string starts with * (a user variable),
2059 ;; negate POSITION.
2060 (if (and (stringp (nth (nth 1 info) form))
2061 (> (length (nth (nth 1 info) form)) 0)
2062 (eq (aref (nth (nth 1 info) form) 0) ?*))
2063 (setq position (- position)))))
2064
2065 (if preface
2066 (progn
2067 (insert preface)
2068 (prin1 name outbuffer)))
2069 (insert (car info))
2070 (let ((print-escape-newlines t)
2071 (print-quoted t)
2072 ;; For compatibility with code before print-circle,
2073 ;; use a cons cell to say that we want
2074 ;; print-gensym-alist not to be cleared
2075 ;; between calls to print functions.
2076 (print-gensym '(t))
2077 (print-circle ; handle circular data structures
2078 (not byte-compile-disable-print-circle))
2079 print-gensym-alist ; was used before print-circle existed.
2080 (print-continuous-numbering t)
2081 print-number-table
2082 (index 0))
2083 (prin1 (car form) outbuffer)
2084 (while (setq form (cdr form))
2085 (setq index (1+ index))
2086 (insert " ")
2087 (cond ((and (numberp specindex) (= index specindex)
2088 ;; Don't handle the definition dynamically
2089 ;; if it refers (or might refer)
2090 ;; to objects already output
2091 ;; (for instance, gensyms in the arg list).
2092 (let (non-nil)
2093 (dotimes (i (length print-number-table))
2094 (if (aref print-number-table i)
2095 (setq non-nil t)))
2096 (not non-nil)))
2097 ;; Output the byte code and constants specially
2098 ;; for lazy dynamic loading.
2099 (let ((position
2100 (byte-compile-output-as-comment
2101 (cons (car form) (nth 1 form))
2102 t)))
2103 (setq position (- (position-bytes position) (point-min) -1))
2104 (princ (format "(#$ . %d) nil" position) outbuffer)
2105 (setq form (cdr form))
2106 (setq index (1+ index))))
2107 ((= index (nth 1 info))
2108 (if position
2109 (princ (format (if quoted "'(#$ . %d)" "(#$ . %d)")
2110 position)
2111 outbuffer)
2112 (let ((print-escape-newlines nil))
2113 (goto-char (prog1 (1+ (point))
2114 (prin1 (car form) outbuffer)))
2115 (insert "\\\n")
2116 (goto-char (point-max)))))
2117 (t
2118 (prin1 (car form) outbuffer)))))
2119 (insert (nth 2 info)))))
1c393159
JB
2120 nil)
2121
2122(defun byte-compile-keep-pending (form &optional handler)
2123 (if (memq byte-optimize '(t source))
2124 (setq form (byte-optimize-form form t)))
2125 (if handler
2126 (let ((for-effect t))
2127 ;; To avoid consing up monstrously large forms at load time, we split
2128 ;; the output regularly.
b4ff4a23
RS
2129 (and (memq (car-safe form) '(fset defalias))
2130 (nthcdr 300 byte-compile-output)
1c393159
JB
2131 (byte-compile-flush-pending))
2132 (funcall handler form)
2133 (if for-effect
2134 (byte-compile-discard)))
2135 (byte-compile-form form t))
2136 nil)
2137
2138(defun byte-compile-flush-pending ()
2139 (if byte-compile-output
2140 (let ((form (byte-compile-out-toplevel t 'file)))
2141 (cond ((eq (car-safe form) 'progn)
ed62683d 2142 (mapc 'byte-compile-output-file-form (cdr form)))
1c393159
JB
2143 (form
2144 (byte-compile-output-file-form form)))
2145 (setq byte-compile-constants nil
2146 byte-compile-variables nil
2147 byte-compile-depth 0
2148 byte-compile-maxdepth 0
2149 byte-compile-output nil))))
2150
2151(defun byte-compile-file-form (form)
2152 (let ((byte-compile-current-form nil) ; close over this for warnings.
2153 handler)
2154 (cond
2155 ((not (consp form))
2156 (byte-compile-keep-pending form))
2157 ((and (symbolp (car form))
2158 (setq handler (get (car form) 'byte-hunk-handler)))
2159 (cond ((setq form (funcall handler form))
2160 (byte-compile-flush-pending)
2161 (byte-compile-output-file-form form))))
2162 ((eq form (setq form (macroexpand form byte-compile-macro-environment)))
2163 (byte-compile-keep-pending form))
2164 (t
2165 (byte-compile-file-form form)))))
2166
2167;; Functions and variables with doc strings must be output separately,
2168;; so make-docfile can recognise them. Most other things can be output
2169;; as byte-code.
2170
2171(put 'defsubst 'byte-hunk-handler 'byte-compile-file-form-defsubst)
2172(defun byte-compile-file-form-defsubst (form)
6c2161c4
SM
2173 (when (assq (nth 1 form) byte-compile-unresolved-functions)
2174 (setq byte-compile-current-form (nth 1 form))
1d5c17c0 2175 (byte-compile-warn "defsubst `%s' was used before it was defined"
6c2161c4 2176 (nth 1 form)))
1c393159
JB
2177 (byte-compile-file-form
2178 (macroexpand form byte-compile-macro-environment))
2179 ;; Return nil so the form is not output twice.
2180 nil)
2181
2182(put 'autoload 'byte-hunk-handler 'byte-compile-file-form-autoload)
2183(defun byte-compile-file-form-autoload (form)
2184 (and (let ((form form))
2185 (while (if (setq form (cdr form)) (byte-compile-constp (car form))))
2186 (null form)) ;Constants only
2187 (eval (nth 5 form)) ;Macro
2188 (eval form)) ;Define the autoload.
c5091f25 2189 ;; Avoid undefined function warnings for the autoload.
c3b2784b 2190 (if (and (consp (nth 1 form))
c5091f25
DL
2191 (eq (car (nth 1 form)) 'quote)
2192 (consp (cdr (nth 1 form)))
2193 (symbolp (nth 1 (nth 1 form))))
977b50fb
SM
2194 (push (cons (nth 1 (nth 1 form))
2195 (cons 'autoload (cdr (cdr form))))
2196 byte-compile-function-environment))
1c393159
JB
2197 (if (stringp (nth 3 form))
2198 form
2199 ;; No doc string, so we can compile this as a normal form.
2200 (byte-compile-keep-pending form 'byte-compile-normal-call)))
2201
2202(put 'defvar 'byte-hunk-handler 'byte-compile-file-form-defvar)
2203(put 'defconst 'byte-hunk-handler 'byte-compile-file-form-defvar)
2204(defun byte-compile-file-form-defvar (form)
2205 (if (null (nth 3 form))
2206 ;; Since there is no doc string, we can compile this as a normal form,
2207 ;; and not do a file-boundary.
2208 (byte-compile-keep-pending form)
6c2161c4 2209 (when (memq 'free-vars byte-compile-warnings)
8898f44e 2210 (push (nth 1 form) byte-compile-bound-variables)
6c2161c4
SM
2211 (if (eq (car form) 'defconst)
2212 (push (nth 1 form) byte-compile-const-variables)))
1c393159
JB
2213 (cond ((consp (nth 2 form))
2214 (setq form (copy-sequence form))
2215 (setcar (cdr (cdr form))
2216 (byte-compile-top-level (nth 2 form) nil 'file))))
2217 form))
2218
8c731d3d
RS
2219(put 'custom-declare-variable 'byte-hunk-handler
2220 'byte-compile-file-form-custom-declare-variable)
2221(defun byte-compile-file-form-custom-declare-variable (form)
fe33e7c8
RS
2222 (when (memq 'callargs byte-compile-warnings)
2223 (byte-compile-nogroup-warn form))
6c2161c4
SM
2224 (when (memq 'free-vars byte-compile-warnings)
2225 (push (nth 1 (nth 1 form)) byte-compile-bound-variables))
347a36bc
RS
2226 (let ((tail (nthcdr 4 form)))
2227 (while tail
2228 ;; If there are any (function (lambda ...)) expressions, compile
2229 ;; those functions.
2230 (if (and (consp (car tail))
2231 (eq (car (car tail)) 'function)
2232 (consp (nth 1 (car tail))))
2233 (setcar tail (byte-compile-lambda (nth 1 (car tail))))
2234 ;; Likewise for a bare lambda.
2235 (if (and (consp (car tail))
2236 (eq (car (car tail)) 'lambda))
2237 (setcar tail (byte-compile-lambda (car tail)))))
2238 (setq tail (cdr tail))))
8c731d3d
RS
2239 form)
2240
997011eb
RS
2241(put 'require 'byte-hunk-handler 'byte-compile-file-form-require)
2242(defun byte-compile-file-form-require (form)
b8104a2b 2243 (let ((args (mapcar 'eval (cdr form))))
997011eb 2244 (apply 'require args)
4db98503 2245 ;; Detect (require 'cl) in a way that works even if cl is already loaded.
997011eb
RS
2246 (if (member (car args) '("cl" cl))
2247 (setq byte-compile-warnings
2248 (remq 'cl-functions byte-compile-warnings))))
1c393159
JB
2249 (byte-compile-keep-pending form 'byte-compile-normal-call))
2250
2251(put 'progn 'byte-hunk-handler 'byte-compile-file-form-progn)
2252(put 'prog1 'byte-hunk-handler 'byte-compile-file-form-progn)
2253(put 'prog2 'byte-hunk-handler 'byte-compile-file-form-progn)
2254(defun byte-compile-file-form-progn (form)
ed62683d 2255 (mapc 'byte-compile-file-form (cdr form))
1c393159
JB
2256 ;; Return nil so the forms are not output twice.
2257 nil)
2258
2259;; This handler is not necessary, but it makes the output from dont-compile
2260;; and similar macros cleaner.
2261(put 'eval 'byte-hunk-handler 'byte-compile-file-form-eval)
2262(defun byte-compile-file-form-eval (form)
2263 (if (eq (car-safe (nth 1 form)) 'quote)
2264 (nth 1 (nth 1 form))
2265 (byte-compile-keep-pending form)))
2266
2267(put 'defun 'byte-hunk-handler 'byte-compile-file-form-defun)
2268(defun byte-compile-file-form-defun (form)
2269 (byte-compile-file-form-defmumble form nil))
2270
2271(put 'defmacro 'byte-hunk-handler 'byte-compile-file-form-defmacro)
2272(defun byte-compile-file-form-defmacro (form)
2273 (byte-compile-file-form-defmumble form t))
2274
2275(defun byte-compile-file-form-defmumble (form macrop)
2276 (let* ((name (car (cdr form)))
2277 (this-kind (if macrop 'byte-compile-macro-environment
2278 'byte-compile-function-environment))
2279 (that-kind (if macrop 'byte-compile-function-environment
2280 'byte-compile-macro-environment))
2281 (this-one (assq name (symbol-value this-kind)))
2282 (that-one (assq name (symbol-value that-kind)))
2283 (byte-compile-free-references nil)
2284 (byte-compile-free-assignments nil))
ccb3c8de 2285 (byte-compile-set-symbol-position name)
1c393159
JB
2286 ;; When a function or macro is defined, add it to the call tree so that
2287 ;; we can tell when functions are not used.
2288 (if byte-compile-generate-call-tree
2289 (or (assq name byte-compile-call-tree)
2290 (setq byte-compile-call-tree
2291 (cons (list name nil nil) byte-compile-call-tree))))
2292
2293 (setq byte-compile-current-form name) ; for warnings
2294 (if (memq 'redefine byte-compile-warnings)
2295 (byte-compile-arglist-warn form macrop))
2296 (if byte-compile-verbose
47cf9d3a 2297 (message "Compiling %s... (%s)" (or filename "") (nth 1 form)))
1c393159
JB
2298 (cond (that-one
2299 (if (and (memq 'redefine byte-compile-warnings)
52799cb8 2300 ;; don't warn when compiling the stubs in byte-run...
1c393159
JB
2301 (not (assq (nth 1 form)
2302 byte-compile-initial-macro-environment)))
2303 (byte-compile-warn
1d5c17c0 2304 "`%s' defined multiple times, as both function and macro"
1c393159
JB
2305 (nth 1 form)))
2306 (setcdr that-one nil))
2307 (this-one
ccb3c8de 2308 (when (and (memq 'redefine byte-compile-warnings)
1c393159 2309 ;; hack: don't warn when compiling the magic internal
52799cb8 2310 ;; byte-compiler macros in byte-run.el...
1c393159
JB
2311 (not (assq (nth 1 form)
2312 byte-compile-initial-macro-environment)))
1d5c17c0 2313 (byte-compile-warn "%s `%s' defined multiple times in this file"
ccb3c8de
CW
2314 (if macrop "macro" "function")
2315 (nth 1 form))))
1c393159
JB
2316 ((and (fboundp name)
2317 (eq (car-safe (symbol-function name))
2318 (if macrop 'lambda 'macro)))
ccb3c8de 2319 (when (memq 'redefine byte-compile-warnings)
1d5c17c0 2320 (byte-compile-warn "%s `%s' being redefined as a %s"
ccb3c8de
CW
2321 (if macrop "function" "macro")
2322 (nth 1 form)
2323 (if macrop "macro" "function")))
1c393159
JB
2324 ;; shadow existing definition
2325 (set this-kind
2326 (cons (cons name nil) (symbol-value this-kind))))
2327 )
2328 (let ((body (nthcdr 3 form)))
ccb3c8de
CW
2329 (when (and (stringp (car body))
2330 (symbolp (car-safe (cdr-safe body)))
2331 (car-safe (cdr-safe body))
2332 (stringp (car-safe (cdr-safe (cdr-safe body)))))
2333 (byte-compile-set-symbol-position (nth 1 form))
2334 (byte-compile-warn "probable `\"' without `\\' in doc string of %s"
2335 (nth 1 form))))
6b8c2efc 2336
985b4686
GM
2337 ;; Generate code for declarations in macro definitions.
2338 ;; Remove declarations from the body of the macro definition.
2339 (when macrop
2340 (let ((tail (nthcdr 2 form)))
2341 (when (stringp (car (cdr tail)))
2342 (setq tail (cdr tail)))
2343 (while (and (consp (car (cdr tail)))
2344 (eq (car (car (cdr tail))) 'declare))
2345 (let ((declaration (car (cdr tail))))
2346 (setcdr tail (cdr (cdr tail)))
8bd91fd0 2347 (prin1 `(if macro-declaration-function
985b4686
GM
2348 (funcall macro-declaration-function
2349 ',name ',declaration))
2350 outbuffer)))))
6b8c2efc 2351
4ec5239c 2352 (let* ((new-one (byte-compile-lambda (nthcdr 2 form) t))
1c393159
JB
2353 (code (byte-compile-byte-code-maker new-one)))
2354 (if this-one
2355 (setcdr this-one new-one)
2356 (set this-kind
2357 (cons (cons name new-one) (symbol-value this-kind))))
2358 (if (and (stringp (nth 3 form))
2359 (eq 'quote (car-safe code))
2360 (eq 'lambda (car-safe (nth 1 code))))
2361 (cons (car form)
2362 (cons name (cdr (nth 1 code))))
d82e848c 2363 (byte-compile-flush-pending)
1c393159 2364 (if (not (stringp (nth 3 form)))
d82e848c
RS
2365 ;; No doc string. Provide -1 as the "doc string index"
2366 ;; so that no element will be treated as a doc string.
2367 (byte-compile-output-docform
2368 (if (byte-compile-version-cond byte-compile-compatibility)
2369 "\n(fset '" "\n(defalias '")
2370 name
2371 (cond ((atom code)
2372 (if macrop '(" '(macro . #[" -1 "])") '(" #[" -1 "]")))
2373 ((eq (car code) 'quote)
2374 (setq code new-one)
2375 (if macrop '(" '(macro " -1 ")") '(" '(" -1 ")")))
2376 ((if macrop '(" (cons 'macro (" -1 "))") '(" (" -1 ")"))))
2377 (append code nil)
2378 (and (atom code) byte-compile-dynamic
2379 1)
2380 nil)
1c393159 2381 ;; Output the form by hand, that's much simpler than having
c36881cf 2382 ;; b-c-output-file-form analyze the defalias.
1c393159 2383 (byte-compile-output-docform
d82e848c
RS
2384 (if (byte-compile-version-cond byte-compile-compatibility)
2385 "\n(fset '" "\n(defalias '")
2386 name
1c393159
JB
2387 (cond ((atom code)
2388 (if macrop '(" '(macro . #[" 4 "])") '(" #[" 4 "]")))
2389 ((eq (car code) 'quote)
2390 (setq code new-one)
2391 (if macrop '(" '(macro " 2 ")") '(" '(" 2 ")")))
2392 ((if macrop '(" (cons 'macro (" 5 "))") '(" (" 5 ")"))))
d82e848c
RS
2393 (append code nil)
2394 (and (atom code) byte-compile-dynamic
2395 1)
2396 nil))
2397 (princ ")" outbuffer)
2398 nil))))
2399
2400;; Print Lisp object EXP in the output file, inside a comment,
2401;; and return the file position it will have.
2402;; If QUOTED is non-nil, print with quoting; otherwise, print without quoting.
2403(defun byte-compile-output-as-comment (exp quoted)
2d5975fa 2404 (let ((position (point)))
9ec5dfe6
SM
2405 (with-current-buffer outbuffer
2406
2407 ;; Insert EXP, and make it a comment with #@LENGTH.
2408 (insert " ")
2409 (if quoted
2410 (prin1 exp outbuffer)
2411 (princ exp outbuffer))
2412 (goto-char position)
2413 ;; Quote certain special characters as needed.
2414 ;; get_doc_string in doc.c does the unquoting.
2415 (while (search-forward "\^A" nil t)
2416 (replace-match "\^A\^A" t t))
2417 (goto-char position)
2418 (while (search-forward "\000" nil t)
2419 (replace-match "\^A0" t t))
2420 (goto-char position)
2421 (while (search-forward "\037" nil t)
2422 (replace-match "\^A_" t t))
2423 (goto-char (point-max))
2424 (insert "\037")
2425 (goto-char position)
2426 (insert "#@" (format "%d" (- (position-bytes (point-max))
2427 (position-bytes position))))
2428
2429 ;; Save the file position of the object.
2430 ;; Note we should add 1 to skip the space
2431 ;; that we inserted before the actual doc string,
2432 ;; and subtract 1 to convert from an 1-origin Emacs position
2433 ;; to a file position; they cancel.
2434 (setq position (point))
2435 (goto-char (point-max)))
d82e848c
RS
2436 position))
2437
1c393159
JB
2438
2439\f
fd5285f3 2440;;;###autoload
1c393159
JB
2441(defun byte-compile (form)
2442 "If FORM is a symbol, byte-compile its function definition.
2443If FORM is a lambda or a macro, byte-compile it as a function."
2444 (displaying-byte-compile-warnings
2445 (byte-compile-close-variables
2446 (let* ((fun (if (symbolp form)
2447 (and (fboundp form) (symbol-function form))
2448 form))
2449 (macro (eq (car-safe fun) 'macro)))
2450 (if macro
2451 (setq fun (cdr fun)))
2452 (cond ((eq (car-safe fun) 'lambda)
2453 (setq fun (if macro
2454 (cons 'macro (byte-compile-lambda fun))
2455 (byte-compile-lambda fun)))
2456 (if (symbolp form)
c36881cf 2457 (defalias form fun)
1c393159
JB
2458 fun)))))))
2459
2460(defun byte-compile-sexp (sexp)
2461 "Compile and return SEXP."
2462 (displaying-byte-compile-warnings
2463 (byte-compile-close-variables
2464 (byte-compile-top-level sexp))))
2465
2466;; Given a function made by byte-compile-lambda, make a form which produces it.
2467(defun byte-compile-byte-code-maker (fun)
2468 (cond
52799cb8 2469 ((byte-compile-version-cond byte-compile-compatibility)
1c393159
JB
2470 ;; Return (quote (lambda ...)).
2471 (list 'quote (byte-compile-byte-code-unmake fun)))
2472 ;; ## atom is faster than compiled-func-p.
2473 ((atom fun) ; compiled function.
2474 ;; generate-emacs19-bytecodes must be on, otherwise byte-compile-lambda
2475 ;; would have produced a lambda.
469414a0 2476 fun)
1c393159 2477 ;; b-c-lambda didn't produce a compiled-function, so it's either a trivial
52799cb8 2478 ;; function, or this is Emacs 18, or generate-emacs19-bytecodes is off.
1c393159
JB
2479 ((let (tmp)
2480 (if (and (setq tmp (assq 'byte-code (cdr-safe (cdr fun))))
2481 (null (cdr (memq tmp fun))))
2482 ;; Generate a make-byte-code call.
2483 (let* ((interactive (assq 'interactive (cdr (cdr fun)))))
2484 (nconc (list 'make-byte-code
2485 (list 'quote (nth 1 fun)) ;arglist
2486 (nth 1 tmp) ;bytes
2487 (nth 2 tmp) ;consts
2488 (nth 3 tmp)) ;depth
2489 (cond ((stringp (nth 2 fun))
2490 (list (nth 2 fun))) ;doc
2491 (interactive
2492 (list nil)))
2493 (cond (interactive
2494 (list (if (or (null (nth 1 interactive))
2495 (stringp (nth 1 interactive)))
2496 (nth 1 interactive)
2497 ;; Interactive spec is a list or a variable
2498 ;; (if it is correct).
2499 (list 'quote (nth 1 interactive))))))))
2500 ;; a non-compiled function (probably trivial)
2501 (list 'quote fun))))))
2502
2503;; Turn a function into an ordinary lambda. Needed for v18 files.
2504(defun byte-compile-byte-code-unmake (function)
2505 (if (consp function)
2506 function;;It already is a lambda.
2507 (setq function (append function nil)) ; turn it into a list
2508 (nconc (list 'lambda (nth 0 function))
2509 (and (nth 4 function) (list (nth 4 function)))
2510 (if (nthcdr 5 function)
2511 (list (cons 'interactive (if (nth 5 function)
2512 (nthcdr 5 function)))))
2513 (list (list 'byte-code
2514 (nth 1 function) (nth 2 function)
2515 (nth 3 function))))))
2516
2517
eadd6444
GM
2518(defun byte-compile-check-lambda-list (list)
2519 "Check lambda-list LIST for errors."
2520 (let (vars)
2521 (while list
2522 (let ((arg (car list)))
ccb3c8de
CW
2523 (when (symbolp arg)
2524 (byte-compile-set-symbol-position arg))
1f006824 2525 (cond ((or (not (symbolp arg))
6c2161c4 2526 (byte-compile-const-symbol-p arg t))
eadd6444
GM
2527 (error "Invalid lambda variable %s" arg))
2528 ((eq arg '&rest)
2529 (unless (cdr list)
2530 (error "&rest without variable name"))
2531 (when (cddr list)
2532 (error "Garbage following &rest VAR in lambda-list")))
2533 ((eq arg '&optional)
2534 (unless (cdr list)
2535 (error "Variable name missing after &optional")))
2536 ((memq arg vars)
e34fd2f2 2537 (byte-compile-warn "repeated variable %s in lambda-list" arg))
1f006824 2538 (t
eadd6444
GM
2539 (push arg vars))))
2540 (setq list (cdr list)))))
2541
2542
1c393159
JB
2543;; Byte-compile a lambda-expression and return a valid function.
2544;; The value is usually a compiled function but may be the original
2545;; lambda-expression.
4ec5239c
LH
2546;; When ADD-LAMBDA is non-nil, the symbol `lambda' is added as head
2547;; of the list FUN and `byte-compile-set-symbol-position' is not called.
2548;; Use this feature to avoid calling `byte-compile-set-symbol-position'
2549;; for symbols generated by the byte compiler itself.
2550(defun byte-compile-lambda (fun &optional add-lambda)
2551 (if add-lambda
2552 (setq fun (cons 'lambda fun))
2553 (unless (eq 'lambda (car-safe fun))
2554 (error "Not a lambda list: %S" fun))
2555 (byte-compile-set-symbol-position 'lambda))
eadd6444 2556 (byte-compile-check-lambda-list (nth 1 fun))
1c393159
JB
2557 (let* ((arglist (nth 1 fun))
2558 (byte-compile-bound-variables
2559 (nconc (and (memq 'free-vars byte-compile-warnings)
2560 (delq '&rest (delq '&optional (copy-sequence arglist))))
2561 byte-compile-bound-variables))
2562 (body (cdr (cdr fun)))
2563 (doc (if (stringp (car body))
2564 (prog1 (car body)
d8f59f56
RS
2565 ;; Discard the doc string
2566 ;; unless it is the last element of the body.
b9edcafb 2567 (if (cdr body)
d8f59f56 2568 (setq body (cdr body))))))
1c393159 2569 (int (assq 'interactive body)))
6c2161c4
SM
2570 ;; Process the interactive spec.
2571 (when int
2572 (byte-compile-set-symbol-position 'interactive)
2573 ;; Skip (interactive) if it is in front (the most usual location).
2574 (if (eq int (car body))
2575 (setq body (cdr body)))
2576 (cond ((consp (cdr int))
2577 (if (cdr (cdr int))
2578 (byte-compile-warn "malformed interactive spec: %s"
2579 (prin1-to-string int)))
ab43c850
RS
2580 ;; If the interactive spec is a call to `list', don't
2581 ;; compile it, because `call-interactively' looks at the
2582 ;; args of `list'. Actually, compile it to get warnings,
2583 ;; but don't use the result.
6c2161c4
SM
2584 (let ((form (nth 1 int)))
2585 (while (memq (car-safe form) '(let let* progn save-excursion))
2586 (while (consp (cdr form))
2587 (setq form (cdr form)))
2588 (setq form (car form)))
ab43c850
RS
2589 (if (eq (car-safe form) 'list)
2590 (byte-compile-top-level (nth 1 int))
2591 (setq int (list 'interactive
2592 (byte-compile-top-level (nth 1 int)))))))
6c2161c4
SM
2593 ((cdr int)
2594 (byte-compile-warn "malformed interactive spec: %s"
2595 (prin1-to-string int)))))
2596 ;; Process the body.
1c393159 2597 (let ((compiled (byte-compile-top-level (cons 'progn body) nil 'lambda)))
6c2161c4 2598 ;; Build the actual byte-coded function.
1c393159 2599 (if (and (eq 'byte-code (car-safe compiled))
b890df1a
RS
2600 (not (byte-compile-version-cond
2601 byte-compile-compatibility)))
1c393159
JB
2602 (apply 'make-byte-code
2603 (append (list arglist)
2604 ;; byte-string, constants-vector, stack depth
2605 (cdr compiled)
2606 ;; optionally, the doc string.
2607 (if (or doc int)
2608 (list doc))
2609 ;; optionally, the interactive spec.
2610 (if int
2611 (list (nth 1 int)))))
2612 (setq compiled
2613 (nconc (if int (list int))
2614 (cond ((eq (car-safe compiled) 'progn) (cdr compiled))
2615 (compiled (list compiled)))))
2616 (nconc (list 'lambda arglist)
2617 (if (or doc (stringp (car compiled)))
2618 (cons doc (cond (compiled)
2619 (body (list nil))))
2620 compiled))))))
2621
2622(defun byte-compile-constants-vector ()
2623 ;; Builds the constants-vector from the current variables and constants.
2624 ;; This modifies the constants from (const . nil) to (const . offset).
2625 ;; To keep the byte-codes to look up the vector as short as possible:
2626 ;; First 6 elements are vars, as there are one-byte varref codes for those.
2627 ;; Next up to byte-constant-limit are constants, still with one-byte codes.
2628 ;; Next variables again, to get 2-byte codes for variable lookup.
2629 ;; The rest of the constants and variables need 3-byte byte-codes.
2630 (let* ((i -1)
2631 (rest (nreverse byte-compile-variables)) ; nreverse because the first
2632 (other (nreverse byte-compile-constants)) ; vars often are used most.
2633 ret tmp
2634 (limits '(5 ; Use the 1-byte varref codes,
2635 63 ; 1-constlim ; 1-byte byte-constant codes,
2636 255 ; 2-byte varref codes,
2637 65535)) ; 3-byte codes for the rest.
2638 limit)
2639 (while (or rest other)
2640 (setq limit (car limits))
2641 (while (and rest (not (eq i limit)))
2642 (if (setq tmp (assq (car (car rest)) ret))
2643 (setcdr (car rest) (cdr tmp))
2644 (setcdr (car rest) (setq i (1+ i)))
2645 (setq ret (cons (car rest) ret)))
2646 (setq rest (cdr rest)))
2647 (setq limits (cdr limits)
2648 rest (prog1 other
2649 (setq other rest))))
2650 (apply 'vector (nreverse (mapcar 'car ret)))))
2651
2652;; Given an expression FORM, compile it and return an equivalent byte-code
2653;; expression (a call to the function byte-code).
2654(defun byte-compile-top-level (form &optional for-effect output-type)
2655 ;; OUTPUT-TYPE advises about how form is expected to be used:
2656 ;; 'eval or nil -> a single form,
2657 ;; 'progn or t -> a list of forms,
2658 ;; 'lambda -> body of a lambda,
2659 ;; 'file -> used at file-level.
285cdf4e
RS
2660 (let ((byte-compile-constants nil)
2661 (byte-compile-variables nil)
2662 (byte-compile-tag-number 0)
2663 (byte-compile-depth 0)
2664 (byte-compile-maxdepth 0)
2665 (byte-compile-output nil))
d9e42bcf
RS
2666 (if (memq byte-optimize '(t source))
2667 (setq form (byte-optimize-form form for-effect)))
2668 (while (and (eq (car-safe form) 'progn) (null (cdr (cdr form))))
2669 (setq form (nth 1 form)))
2670 (if (and (eq 'byte-code (car-safe form))
2671 (not (memq byte-optimize '(t byte)))
2672 (stringp (nth 1 form)) (vectorp (nth 2 form))
2673 (natnump (nth 3 form)))
2674 form
2675 (byte-compile-form form for-effect)
285cdf4e 2676 (byte-compile-out-toplevel for-effect output-type))))
1c393159
JB
2677
2678(defun byte-compile-out-toplevel (&optional for-effect output-type)
2679 (if for-effect
2680 ;; The stack is empty. Push a value to be returned from (byte-code ..).
2681 (if (eq (car (car byte-compile-output)) 'byte-discard)
2682 (setq byte-compile-output (cdr byte-compile-output))
2683 (byte-compile-push-constant
2684 ;; Push any constant - preferably one which already is used, and
2685 ;; a number or symbol - ie not some big sequence. The return value
2686 ;; isn't returned, but it would be a shame if some textually large
2687 ;; constant was not optimized away because we chose to return it.
2688 (and (not (assq nil byte-compile-constants)) ; Nil is often there.
2689 (let ((tmp (reverse byte-compile-constants)))
ba76e7fa
SM
2690 (while (and tmp (not (or (symbolp (caar tmp))
2691 (numberp (caar tmp)))))
1c393159 2692 (setq tmp (cdr tmp)))
ba76e7fa 2693 (caar tmp))))))
1c393159
JB
2694 (byte-compile-out 'byte-return 0)
2695 (setq byte-compile-output (nreverse byte-compile-output))
2696 (if (memq byte-optimize '(t byte))
2697 (setq byte-compile-output
2698 (byte-optimize-lapcode byte-compile-output for-effect)))
1f006824 2699
1c393159
JB
2700 ;; Decompile trivial functions:
2701 ;; only constants and variables, or a single funcall except in lambdas.
2702 ;; Except for Lisp_Compiled objects, forms like (foo "hi")
2703 ;; are still quicker than (byte-code "..." [foo "hi"] 2).
2704 ;; Note that even (quote foo) must be parsed just as any subr by the
2705 ;; interpreter, so quote should be compiled into byte-code in some contexts.
2706 ;; What to leave uncompiled:
69dc83fd
KH
2707 ;; lambda -> never. we used to leave it uncompiled if the body was
2708 ;; a single atom, but that causes confusion if the docstring
2709 ;; uses the (file . pos) syntax. Besides, now that we have
2710 ;; the Lisp_Compiled type, the compiled form is faster.
1c393159
JB
2711 ;; eval -> atom, quote or (function atom atom atom)
2712 ;; progn -> as <<same-as-eval>> or (progn <<same-as-eval>> atom)
2713 ;; file -> as progn, but takes both quotes and atoms, and longer forms.
2714 (let (rest
2715 (maycall (not (eq output-type 'lambda))) ; t if we may make a funcall.
2716 tmp body)
2717 (cond
2718 ;; #### This should be split out into byte-compile-nontrivial-function-p.
69dc83fd
KH
2719 ((or (eq output-type 'lambda)
2720 (nthcdr (if (eq output-type 'file) 50 8) byte-compile-output)
1c393159
JB
2721 (assq 'TAG byte-compile-output) ; Not necessary, but speeds up a bit.
2722 (not (setq tmp (assq 'byte-return byte-compile-output)))
2723 (progn
2724 (setq rest (nreverse
2725 (cdr (memq tmp (reverse byte-compile-output)))))
2726 (while (cond
2727 ((memq (car (car rest)) '(byte-varref byte-constant))
2728 (setq tmp (car (cdr (car rest))))
469414a0
RS
2729 (if (if (eq (car (car rest)) 'byte-constant)
2730 (or (consp tmp)
2731 (and (symbolp tmp)
1639b803 2732 (not (byte-compile-const-symbol-p tmp)))))
469414a0
RS
2733 (if maycall
2734 (setq body (cons (list 'quote tmp) body)))
2735 (setq body (cons tmp body))))
1c393159
JB
2736 ((and maycall
2737 ;; Allow a funcall if at most one atom follows it.
2738 (null (nthcdr 3 rest))
2739 (setq tmp (get (car (car rest)) 'byte-opcode-invert))
2740 (or (null (cdr rest))
2741 (and (memq output-type '(file progn t))
2742 (cdr (cdr rest))
2743 (eq (car (nth 1 rest)) 'byte-discard)
2744 (progn (setq rest (cdr rest)) t))))
2745 (setq maycall nil) ; Only allow one real function call.
2746 (setq body (nreverse body))
2747 (setq body (list
2748 (if (and (eq tmp 'funcall)
2749 (eq (car-safe (car body)) 'quote))
2750 (cons (nth 1 (car body)) (cdr body))
2751 (cons tmp body))))
2752 (or (eq output-type 'file)
2753 (not (delq nil (mapcar 'consp (cdr (car body))))))))
2754 (setq rest (cdr rest)))
69dc83fd 2755 rest))
1c393159
JB
2756 (let ((byte-compile-vector (byte-compile-constants-vector)))
2757 (list 'byte-code (byte-compile-lapcode byte-compile-output)
2758 byte-compile-vector byte-compile-maxdepth)))
2759 ;; it's a trivial function
2760 ((cdr body) (cons 'progn (nreverse body)))
2761 ((car body)))))
2762
2763;; Given BODY, compile it and return a new body.
2764(defun byte-compile-top-level-body (body &optional for-effect)
2765 (setq body (byte-compile-top-level (cons 'progn body) for-effect t))
2766 (cond ((eq (car-safe body) 'progn)
2767 (cdr body))
2768 (body
2769 (list body))))
2770\f
c5091f25 2771;; This is the recursive entry point for compiling each subform of an
1c393159
JB
2772;; expression.
2773;; If for-effect is non-nil, byte-compile-form will output a byte-discard
2774;; before terminating (ie no value will be left on the stack).
2775;; A byte-compile handler may, when for-effect is non-nil, choose output code
2776;; which does not leave a value on the stack, and then set for-effect to nil
2777;; (to prevent byte-compile-form from outputting the byte-discard).
2778;; If a handler wants to call another handler, it should do so via
2779;; byte-compile-form, or take extreme care to handle for-effect correctly.
2780;; (Use byte-compile-form-do-effect to reset the for-effect flag too.)
2781;;
2782(defun byte-compile-form (form &optional for-effect)
2783 (setq form (macroexpand form byte-compile-macro-environment))
2784 (cond ((not (consp form))
1639b803 2785 (cond ((or (not (symbolp form)) (byte-compile-const-symbol-p form))
0b46acbf
RS
2786 (when (symbolp form)
2787 (byte-compile-set-symbol-position form))
1c393159
JB
2788 (byte-compile-constant form))
2789 ((and for-effect byte-compile-delete-errors)
0b46acbf
RS
2790 (when (symbolp form)
2791 (byte-compile-set-symbol-position form))
1c393159
JB
2792 (setq for-effect nil))
2793 (t (byte-compile-variable-ref 'byte-varref form))))
2794 ((symbolp (car form))
2795 (let* ((fn (car form))
2796 (handler (get fn 'byte-compile)))
ccb3c8de 2797 (when (byte-compile-const-symbol-p fn)
1d5c17c0 2798 (byte-compile-warn "`%s' called as a function" fn))
086af77c 2799 (and (memq 'interactive-only byte-compile-warnings)
11efeb9b 2800 (memq fn byte-compile-interactive-only-functions)
086af77c
RS
2801 (byte-compile-warn "`%s' used from Lisp code\n\
2802That command is designed for interactive use only" fn))
1c393159 2803 (if (and handler
67438f77
SM
2804 ;; Make sure that function exists. This is important
2805 ;; for CL compiler macros since the symbol may be
2806 ;; `cl-byte-compile-compiler-macro' but if CL isn't
2807 ;; loaded, this function doesn't exist.
2808 (or (not (memq handler '(cl-byte-compile-compiler-macro)))
e0b4be6c 2809 (functionp handler))
67438f77
SM
2810 (not (and (byte-compile-version-cond
2811 byte-compile-compatibility)
2812 (get (get fn 'byte-opcode) 'emacs19-opcode))))
4ec5239c 2813 (funcall handler form)
11efeb9b
RS
2814 (when (memq 'callargs byte-compile-warnings)
2815 (if (memq fn '(custom-declare-group custom-declare-variable custom-declare-face))
2816 (byte-compile-nogroup-warn form))
2817 (byte-compile-callargs-warn form))
4795d1c7
RS
2818 (byte-compile-normal-call form))
2819 (if (memq 'cl-functions byte-compile-warnings)
2820 (byte-compile-cl-warn form))))
ed015bdd 2821 ((and (or (byte-code-function-p (car form))
1c393159
JB
2822 (eq (car-safe (car form)) 'lambda))
2823 ;; if the form comes out the same way it went in, that's
2824 ;; because it was malformed, and we couldn't unfold it.
2825 (not (eq form (setq form (byte-compile-unfold-lambda form)))))
2826 (byte-compile-form form for-effect)
2827 (setq for-effect nil))
2828 ((byte-compile-normal-call form)))
2829 (if for-effect
2830 (byte-compile-discard)))
2831
2832(defun byte-compile-normal-call (form)
2833 (if byte-compile-generate-call-tree
2834 (byte-compile-annotate-call-tree form))
2835 (byte-compile-push-constant (car form))
ed62683d 2836 (mapc 'byte-compile-form (cdr form)) ; wasteful, but faster.
1c393159
JB
2837 (byte-compile-out 'byte-call (length (cdr form))))
2838
2839(defun byte-compile-variable-ref (base-op var)
ccb3c8de
CW
2840 (when (symbolp var)
2841 (byte-compile-set-symbol-position var))
6c2161c4
SM
2842 (if (or (not (symbolp var))
2843 (byte-compile-const-symbol-p var (not (eq base-op 'byte-varref))))
2844 (byte-compile-warn
1d5c17c0
RS
2845 (cond ((eq base-op 'byte-varbind) "attempt to let-bind %s `%s'")
2846 ((eq base-op 'byte-varset) "variable assignment to %s `%s'")
2847 (t "variable reference to %s `%s'"))
6c2161c4
SM
2848 (if (symbolp var) "constant" "nonvariable")
2849 (prin1-to-string var))
9290191f 2850 (if (and (get var 'byte-obsolete-variable)
9e31e67b
RS
2851 (memq 'obsolete byte-compile-warnings)
2852 (not (eq var byte-compile-not-obsolete-var)))
20482b70
SM
2853 (let* ((ob (get var 'byte-obsolete-variable))
2854 (when (cdr ob)))
1d5c17c0 2855 (byte-compile-warn "`%s' is an obsolete variable%s; %s" var
f1953880 2856 (if when (concat " (as of Emacs " when ")") "")
20482b70
SM
2857 (if (stringp (car ob))
2858 (car ob)
1d5c17c0 2859 (format "use `%s' instead." (car ob))))))
1c393159
JB
2860 (if (memq 'free-vars byte-compile-warnings)
2861 (if (eq base-op 'byte-varbind)
6c2161c4 2862 (push var byte-compile-bound-variables)
1c393159
JB
2863 (or (boundp var)
2864 (memq var byte-compile-bound-variables)
2865 (if (eq base-op 'byte-varset)
2866 (or (memq var byte-compile-free-assignments)
2867 (progn
1d5c17c0 2868 (byte-compile-warn "assignment to free variable `%s'" var)
6c2161c4 2869 (push var byte-compile-free-assignments)))
1c393159
JB
2870 (or (memq var byte-compile-free-references)
2871 (progn
1d5c17c0 2872 (byte-compile-warn "reference to free variable `%s'" var)
6c2161c4 2873 (push var byte-compile-free-references))))))))
1c393159 2874 (let ((tmp (assq var byte-compile-variables)))
6c2161c4
SM
2875 (unless tmp
2876 (setq tmp (list var))
2877 (push tmp byte-compile-variables))
1c393159
JB
2878 (byte-compile-out base-op tmp)))
2879
2880(defmacro byte-compile-get-constant (const)
1639b803 2881 `(or (if (stringp ,const)
7fb4fa10
RS
2882 ;; In a string constant, treat properties as significant.
2883 (let (result)
2884 (dolist (elt byte-compile-constants)
2885 (if (equal-including-properties (car elt) ,const)
2886 (setq result elt)))
2887 result)
1639b803
DL
2888 (assq ,const byte-compile-constants))
2889 (car (setq byte-compile-constants
2890 (cons (list ,const) byte-compile-constants)))))
1c393159
JB
2891
2892;; Use this when the value of a form is a constant. This obeys for-effect.
2893(defun byte-compile-constant (const)
2894 (if for-effect
2895 (setq for-effect nil)
ccb3c8de
CW
2896 (when (symbolp const)
2897 (byte-compile-set-symbol-position const))
1c393159
JB
2898 (byte-compile-out 'byte-constant (byte-compile-get-constant const))))
2899
2900;; Use this for a constant that is not the value of its containing form.
2901;; This ignores for-effect.
2902(defun byte-compile-push-constant (const)
2903 (let ((for-effect nil))
2904 (inline (byte-compile-constant const))))
2905
2906\f
2907;; Compile those primitive ordinary functions
2908;; which have special byte codes just for speed.
2909
2910(defmacro byte-defop-compiler (function &optional compile-handler)
2911 ;; add a compiler-form for FUNCTION.
2912 ;; If function is a symbol, then the variable "byte-SYMBOL" must name
2913 ;; the opcode to be used. If function is a list, the first element
2914 ;; is the function and the second element is the bytecode-symbol.
3c9dc1cf 2915 ;; The second element may be nil, meaning there is no opcode.
1c393159
JB
2916 ;; COMPILE-HANDLER is the function to use to compile this byte-op, or
2917 ;; may be the abbreviations 0, 1, 2, 3, 0-1, or 1-2.
2918 ;; If it is nil, then the handler is "byte-compile-SYMBOL."
2919 (let (opcode)
2920 (if (symbolp function)
2921 (setq opcode (intern (concat "byte-" (symbol-name function))))
2922 (setq opcode (car (cdr function))
2923 function (car function)))
2924 (let ((fnform
2925 (list 'put (list 'quote function) ''byte-compile
2926 (list 'quote
2927 (or (cdr (assq compile-handler
2928 '((0 . byte-compile-no-args)
2929 (1 . byte-compile-one-arg)
2930 (2 . byte-compile-two-args)
2931 (3 . byte-compile-three-args)
2932 (0-1 . byte-compile-zero-or-one-arg)
2933 (1-2 . byte-compile-one-or-two-args)
2934 (2-3 . byte-compile-two-or-three-args)
2935 )))
2936 compile-handler
2937 (intern (concat "byte-compile-"
2938 (symbol-name function))))))))
2939 (if opcode
2940 (list 'progn fnform
2941 (list 'put (list 'quote function)
2942 ''byte-opcode (list 'quote opcode))
2943 (list 'put (list 'quote opcode)
2944 ''byte-opcode-invert (list 'quote function)))
2945 fnform))))
2946
2947(defmacro byte-defop-compiler19 (function &optional compile-handler)
2948 ;; Just like byte-defop-compiler, but defines an opcode that will only
e27c3564 2949 ;; be used when byte-compile-compatibility is false.
1c393159 2950 (if (and (byte-compile-single-version)
e27c3564 2951 byte-compile-compatibility)
9e2b097b
JB
2952 ;; #### instead of doing nothing, this should do some remprops,
2953 ;; #### to protect against the case where a single-version compiler
2954 ;; #### is loaded into a world that has contained a multi-version one.
1c393159
JB
2955 nil
2956 (list 'progn
2957 (list 'put
2958 (list 'quote
2959 (or (car (cdr-safe function))
2960 (intern (concat "byte-"
2961 (symbol-name (or (car-safe function) function))))))
2962 ''emacs19-opcode t)
2963 (list 'byte-defop-compiler function compile-handler))))
2964
2965(defmacro byte-defop-compiler-1 (function &optional compile-handler)
2966 (list 'byte-defop-compiler (list function nil) compile-handler))
2967
2968\f
2969(put 'byte-call 'byte-opcode-invert 'funcall)
2970(put 'byte-list1 'byte-opcode-invert 'list)
2971(put 'byte-list2 'byte-opcode-invert 'list)
2972(put 'byte-list3 'byte-opcode-invert 'list)
2973(put 'byte-list4 'byte-opcode-invert 'list)
2974(put 'byte-listN 'byte-opcode-invert 'list)
2975(put 'byte-concat2 'byte-opcode-invert 'concat)
2976(put 'byte-concat3 'byte-opcode-invert 'concat)
2977(put 'byte-concat4 'byte-opcode-invert 'concat)
2978(put 'byte-concatN 'byte-opcode-invert 'concat)
2979(put 'byte-insertN 'byte-opcode-invert 'insert)
2980
1c393159
JB
2981(byte-defop-compiler point 0)
2982;;(byte-defop-compiler mark 0) ;; obsolete
2983(byte-defop-compiler point-max 0)
2984(byte-defop-compiler point-min 0)
2985(byte-defop-compiler following-char 0)
2986(byte-defop-compiler preceding-char 0)
2987(byte-defop-compiler current-column 0)
2988(byte-defop-compiler eolp 0)
2989(byte-defop-compiler eobp 0)
2990(byte-defop-compiler bolp 0)
2991(byte-defop-compiler bobp 0)
2992(byte-defop-compiler current-buffer 0)
2993;;(byte-defop-compiler read-char 0) ;; obsolete
2994(byte-defop-compiler interactive-p 0)
2995(byte-defop-compiler19 widen 0)
2996(byte-defop-compiler19 end-of-line 0-1)
2997(byte-defop-compiler19 forward-char 0-1)
2998(byte-defop-compiler19 forward-line 0-1)
2999(byte-defop-compiler symbolp 1)
3000(byte-defop-compiler consp 1)
3001(byte-defop-compiler stringp 1)
3002(byte-defop-compiler listp 1)
3003(byte-defop-compiler not 1)
3004(byte-defop-compiler (null byte-not) 1)
3005(byte-defop-compiler car 1)
3006(byte-defop-compiler cdr 1)
3007(byte-defop-compiler length 1)
3008(byte-defop-compiler symbol-value 1)
3009(byte-defop-compiler symbol-function 1)
3010(byte-defop-compiler (1+ byte-add1) 1)
3011(byte-defop-compiler (1- byte-sub1) 1)
3012(byte-defop-compiler goto-char 1)
b8ae93ad 3013(byte-defop-compiler char-after 0-1)
1c393159
JB
3014(byte-defop-compiler set-buffer 1)
3015;;(byte-defop-compiler set-mark 1) ;; obsolete
78642e03 3016(byte-defop-compiler19 forward-word 0-1)
1c393159
JB
3017(byte-defop-compiler19 char-syntax 1)
3018(byte-defop-compiler19 nreverse 1)
3019(byte-defop-compiler19 car-safe 1)
3020(byte-defop-compiler19 cdr-safe 1)
3021(byte-defop-compiler19 numberp 1)
3022(byte-defop-compiler19 integerp 1)
3023(byte-defop-compiler19 skip-chars-forward 1-2)
3024(byte-defop-compiler19 skip-chars-backward 1-2)
1c393159
JB
3025(byte-defop-compiler eq 2)
3026(byte-defop-compiler memq 2)
3027(byte-defop-compiler cons 2)
3028(byte-defop-compiler aref 2)
3029(byte-defop-compiler set 2)
3030(byte-defop-compiler (= byte-eqlsign) 2)
3031(byte-defop-compiler (< byte-lss) 2)
3032(byte-defop-compiler (> byte-gtr) 2)
3033(byte-defop-compiler (<= byte-leq) 2)
3034(byte-defop-compiler (>= byte-geq) 2)
3035(byte-defop-compiler get 2)
3036(byte-defop-compiler nth 2)
3037(byte-defop-compiler substring 2-3)
9e2b097b 3038(byte-defop-compiler19 (move-marker byte-set-marker) 2-3)
1c393159
JB
3039(byte-defop-compiler19 set-marker 2-3)
3040(byte-defop-compiler19 match-beginning 1)
3041(byte-defop-compiler19 match-end 1)
3042(byte-defop-compiler19 upcase 1)
3043(byte-defop-compiler19 downcase 1)
3044(byte-defop-compiler19 string= 2)
3045(byte-defop-compiler19 string< 2)
9e2b097b
JB
3046(byte-defop-compiler19 (string-equal byte-string=) 2)
3047(byte-defop-compiler19 (string-lessp byte-string<) 2)
1c393159
JB
3048(byte-defop-compiler19 equal 2)
3049(byte-defop-compiler19 nthcdr 2)
3050(byte-defop-compiler19 elt 2)
3051(byte-defop-compiler19 member 2)
3052(byte-defop-compiler19 assq 2)
9e2b097b
JB
3053(byte-defop-compiler19 (rplaca byte-setcar) 2)
3054(byte-defop-compiler19 (rplacd byte-setcdr) 2)
1c393159
JB
3055(byte-defop-compiler19 setcar 2)
3056(byte-defop-compiler19 setcdr 2)
3057(byte-defop-compiler19 buffer-substring 2)
3058(byte-defop-compiler19 delete-region 2)
3059(byte-defop-compiler19 narrow-to-region 2)
1c393159
JB
3060(byte-defop-compiler19 (% byte-rem) 2)
3061(byte-defop-compiler aset 3)
3062
3063(byte-defop-compiler max byte-compile-associative)
3064(byte-defop-compiler min byte-compile-associative)
3065(byte-defop-compiler (+ byte-plus) byte-compile-associative)
3066(byte-defop-compiler19 (* byte-mult) byte-compile-associative)
3067
3068;;####(byte-defop-compiler19 move-to-column 1)
3069(byte-defop-compiler-1 interactive byte-compile-noop)
3070
3071\f
3072(defun byte-compile-subr-wrong-args (form n)
ccb3c8de 3073 (byte-compile-set-symbol-position (car form))
1d5c17c0 3074 (byte-compile-warn "`%s' called with %d arg%s, but requires %s"
1c393159
JB
3075 (car form) (length (cdr form))
3076 (if (= 1 (length (cdr form))) "" "s") n)
3077 ;; get run-time wrong-number-of-args error.
3078 (byte-compile-normal-call form))
3079
3080(defun byte-compile-no-args (form)
3081 (if (not (= (length form) 1))
3082 (byte-compile-subr-wrong-args form "none")
3083 (byte-compile-out (get (car form) 'byte-opcode) 0)))
3084
3085(defun byte-compile-one-arg (form)
3086 (if (not (= (length form) 2))
3087 (byte-compile-subr-wrong-args form 1)
3088 (byte-compile-form (car (cdr form))) ;; Push the argument
3089 (byte-compile-out (get (car form) 'byte-opcode) 0)))
3090
3091(defun byte-compile-two-args (form)
3092 (if (not (= (length form) 3))
3093 (byte-compile-subr-wrong-args form 2)
3094 (byte-compile-form (car (cdr form))) ;; Push the arguments
3095 (byte-compile-form (nth 2 form))
3096 (byte-compile-out (get (car form) 'byte-opcode) 0)))
3097
3098(defun byte-compile-three-args (form)
3099 (if (not (= (length form) 4))
3100 (byte-compile-subr-wrong-args form 3)
3101 (byte-compile-form (car (cdr form))) ;; Push the arguments
3102 (byte-compile-form (nth 2 form))
3103 (byte-compile-form (nth 3 form))
3104 (byte-compile-out (get (car form) 'byte-opcode) 0)))
3105
3106(defun byte-compile-zero-or-one-arg (form)
3107 (let ((len (length form)))
3108 (cond ((= len 1) (byte-compile-one-arg (append form '(nil))))
3109 ((= len 2) (byte-compile-one-arg form))
3110 (t (byte-compile-subr-wrong-args form "0-1")))))
3111
3112(defun byte-compile-one-or-two-args (form)
3113 (let ((len (length form)))
3114 (cond ((= len 2) (byte-compile-two-args (append form '(nil))))
3115 ((= len 3) (byte-compile-two-args form))
3116 (t (byte-compile-subr-wrong-args form "1-2")))))
3117
3118(defun byte-compile-two-or-three-args (form)
3119 (let ((len (length form)))
3120 (cond ((= len 3) (byte-compile-three-args (append form '(nil))))
3121 ((= len 4) (byte-compile-three-args form))
3122 (t (byte-compile-subr-wrong-args form "2-3")))))
3123
3124(defun byte-compile-noop (form)
3125 (byte-compile-constant nil))
3126
3127(defun byte-compile-discard ()
3128 (byte-compile-out 'byte-discard 0))
3129
3130
3131;; Compile a function that accepts one or more args and is right-associative.
c0f43df5
RS
3132;; We do it by left-associativity so that the operations
3133;; are done in the same order as in interpreted code.
10809e0f
RS
3134;; We treat the one-arg case, as in (+ x), like (+ x 0).
3135;; in order to convert markers to numbers, and trigger expected errors.
1c393159
JB
3136(defun byte-compile-associative (form)
3137 (if (cdr form)
c0f43df5
RS
3138 (let ((opcode (get (car form) 'byte-opcode))
3139 (args (copy-sequence (cdr form))))
3140 (byte-compile-form (car args))
3141 (setq args (cdr args))
10809e0f
RS
3142 (or args (setq args '(0)
3143 opcode (get '+ 'byte-opcode)))
6c2161c4
SM
3144 (dolist (arg args)
3145 (byte-compile-form arg)
3146 (byte-compile-out opcode 0)))
1c393159
JB
3147 (byte-compile-constant (eval form))))
3148
3149\f
3150;; more complicated compiler macros
3151
ec448ae2 3152(byte-defop-compiler char-before)
a746fb65
GM
3153(byte-defop-compiler backward-char)
3154(byte-defop-compiler backward-word)
1c393159
JB
3155(byte-defop-compiler list)
3156(byte-defop-compiler concat)
3157(byte-defop-compiler fset)
3158(byte-defop-compiler (indent-to-column byte-indent-to) byte-compile-indent-to)
3159(byte-defop-compiler indent-to)
3160(byte-defop-compiler insert)
3161(byte-defop-compiler-1 function byte-compile-function-form)
3162(byte-defop-compiler-1 - byte-compile-minus)
3163(byte-defop-compiler19 (/ byte-quo) byte-compile-quo)
3164(byte-defop-compiler19 nconc)
1c393159 3165
ec448ae2
GM
3166(defun byte-compile-char-before (form)
3167 (cond ((= 2 (length form))
a746fb65
GM
3168 (byte-compile-form (list 'char-after (if (numberp (nth 1 form))
3169 (1- (nth 1 form))
3170 `(1- ,(nth 1 form))))))
3171 ((= 1 (length form))
3172 (byte-compile-form '(char-after (1- (point)))))
3173 (t (byte-compile-subr-wrong-args form "0-1"))))
3174
3175;; backward-... ==> forward-... with negated argument.
3176(defun byte-compile-backward-char (form)
3177 (cond ((= 2 (length form))
3178 (byte-compile-form (list 'forward-char (if (numberp (nth 1 form))
3179 (- (nth 1 form))
3180 `(- ,(nth 1 form))))))
3181 ((= 1 (length form))
3182 (byte-compile-form '(forward-char -1)))
3183 (t (byte-compile-subr-wrong-args form "0-1"))))
3184
3185(defun byte-compile-backward-word (form)
3186 (cond ((= 2 (length form))
3187 (byte-compile-form (list 'forward-word (if (numberp (nth 1 form))
3188 (- (nth 1 form))
3189 `(- ,(nth 1 form))))))
3190 ((= 1 (length form))
3191 (byte-compile-form '(forward-word -1)))
3192 (t (byte-compile-subr-wrong-args form "0-1"))))
ec448ae2 3193
1c393159
JB
3194(defun byte-compile-list (form)
3195 (let ((count (length (cdr form))))
3196 (cond ((= count 0)
3197 (byte-compile-constant nil))
3198 ((< count 5)
ed62683d 3199 (mapc 'byte-compile-form (cdr form))
1c393159
JB
3200 (byte-compile-out
3201 (aref [byte-list1 byte-list2 byte-list3 byte-list4] (1- count)) 0))
e27c3564
JB
3202 ((and (< count 256) (not (byte-compile-version-cond
3203 byte-compile-compatibility)))
ed62683d 3204 (mapc 'byte-compile-form (cdr form))
1c393159
JB
3205 (byte-compile-out 'byte-listN count))
3206 (t (byte-compile-normal-call form)))))
3207
3208(defun byte-compile-concat (form)
3209 (let ((count (length (cdr form))))
3210 (cond ((and (< 1 count) (< count 5))
ed62683d 3211 (mapc 'byte-compile-form (cdr form))
1c393159
JB
3212 (byte-compile-out
3213 (aref [byte-concat2 byte-concat3 byte-concat4] (- count 2))
3214 0))
3215 ;; Concat of one arg is not a no-op if arg is not a string.
3216 ((= count 0)
3217 (byte-compile-form ""))
e27c3564
JB
3218 ((and (< count 256) (not (byte-compile-version-cond
3219 byte-compile-compatibility)))
ed62683d 3220 (mapc 'byte-compile-form (cdr form))
1c393159
JB
3221 (byte-compile-out 'byte-concatN count))
3222 ((byte-compile-normal-call form)))))
3223
3224(defun byte-compile-minus (form)
3225 (if (null (setq form (cdr form)))
3226 (byte-compile-constant 0)
3227 (byte-compile-form (car form))
3228 (if (cdr form)
3229 (while (setq form (cdr form))
3230 (byte-compile-form (car form))
3231 (byte-compile-out 'byte-diff 0))
3232 (byte-compile-out 'byte-negate 0))))
3233
3234(defun byte-compile-quo (form)
3235 (let ((len (length form)))
3236 (cond ((<= len 2)
3237 (byte-compile-subr-wrong-args form "2 or more"))
3238 (t
3239 (byte-compile-form (car (setq form (cdr form))))
3240 (while (setq form (cdr form))
3241 (byte-compile-form (car form))
3242 (byte-compile-out 'byte-quo 0))))))
3243
3244(defun byte-compile-nconc (form)
3245 (let ((len (length form)))
3246 (cond ((= len 1)
3247 (byte-compile-constant nil))
3248 ((= len 2)
3249 ;; nconc of one arg is a noop, even if that arg isn't a list.
3250 (byte-compile-form (nth 1 form)))
3251 (t
3252 (byte-compile-form (car (setq form (cdr form))))
3253 (while (setq form (cdr form))
3254 (byte-compile-form (car form))
3255 (byte-compile-out 'byte-nconc 0))))))
3256
3257(defun byte-compile-fset (form)
3258 ;; warn about forms like (fset 'foo '(lambda () ...))
3259 ;; (where the lambda expression is non-trivial...)
3260 (let ((fn (nth 2 form))
3261 body)
3262 (if (and (eq (car-safe fn) 'quote)
3263 (eq (car-safe (setq fn (nth 1 fn))) 'lambda))
3264 (progn
3265 (setq body (cdr (cdr fn)))
3266 (if (stringp (car body)) (setq body (cdr body)))
3267 (if (eq 'interactive (car-safe (car body))) (setq body (cdr body)))
3268 (if (and (consp (car body))
3269 (not (eq 'byte-code (car (car body)))))
3270 (byte-compile-warn
1d5c17c0 3271 "A quoted lambda form is the second argument of `fset'. This is probably
1c393159
JB
3272 not what you want, as that lambda cannot be compiled. Consider using
3273 the syntax (function (lambda (...) ...)) instead.")))))
3274 (byte-compile-two-args form))
3275
3276(defun byte-compile-funarg (form)
3277 ;; (mapcar '(lambda (x) ..) ..) ==> (mapcar (function (lambda (x) ..)) ..)
eb8c3be9 3278 ;; for cases where it's guaranteed that first arg will be used as a lambda.
1c393159
JB
3279 (byte-compile-normal-call
3280 (let ((fn (nth 1 form)))
3281 (if (and (eq (car-safe fn) 'quote)
3282 (eq (car-safe (nth 1 fn)) 'lambda))
3283 (cons (car form)
3284 (cons (cons 'function (cdr fn))
3285 (cdr (cdr form))))
3286 form))))
3287
5a6037bb
RS
3288(defun byte-compile-funarg-2 (form)
3289 ;; (sort ... '(lambda (x) ..)) ==> (sort ... (function (lambda (x) ..)))
3290 ;; for cases where it's guaranteed that second arg will be used as a lambda.
3291 (byte-compile-normal-call
3292 (let ((fn (nth 2 form)))
3293 (if (and (eq (car-safe fn) 'quote)
3294 (eq (car-safe (nth 1 fn)) 'lambda))
3295 (cons (car form)
3296 (cons (nth 1 form)
3297 (cons (cons 'function (cdr fn))
3298 (cdr (cdr (cdr form))))))
3299 form))))
3300
1c393159
JB
3301;; (function foo) must compile like 'foo, not like (symbol-function 'foo).
3302;; Otherwise it will be incompatible with the interpreter,
3303;; and (funcall (function foo)) will lose with autoloads.
3304
3305(defun byte-compile-function-form (form)
3306 (byte-compile-constant
3307 (cond ((symbolp (nth 1 form))
3308 (nth 1 form))
3309 ;; If we're not allowed to use #[] syntax, then output a form like
3310 ;; '(lambda (..) (byte-code ..)) instead of a call to make-byte-code.
3311 ;; In this situation, calling make-byte-code at run-time will usually
3312 ;; be less efficient than processing a call to byte-code.
52799cb8 3313 ((byte-compile-version-cond byte-compile-compatibility)
1c393159
JB
3314 (byte-compile-byte-code-unmake (byte-compile-lambda (nth 1 form))))
3315 ((byte-compile-lambda (nth 1 form))))))
3316
3317(defun byte-compile-indent-to (form)
3318 (let ((len (length form)))
3319 (cond ((= len 2)
3320 (byte-compile-form (car (cdr form)))
3321 (byte-compile-out 'byte-indent-to 0))
3322 ((= len 3)
3323 ;; no opcode for 2-arg case.
3324 (byte-compile-normal-call form))
3325 (t
3326 (byte-compile-subr-wrong-args form "1-2")))))
3327
3328(defun byte-compile-insert (form)
3329 (cond ((null (cdr form))
3330 (byte-compile-constant nil))
e27c3564
JB
3331 ((and (not (byte-compile-version-cond
3332 byte-compile-compatibility))
1c393159 3333 (<= (length form) 256))
ed62683d 3334 (mapc 'byte-compile-form (cdr form))
1c393159
JB
3335 (if (cdr (cdr form))
3336 (byte-compile-out 'byte-insertN (length (cdr form)))
3337 (byte-compile-out 'byte-insert 0)))
3338 ((memq t (mapcar 'consp (cdr (cdr form))))
3339 (byte-compile-normal-call form))
3340 ;; We can split it; there is no function call after inserting 1st arg.
3341 (t
3342 (while (setq form (cdr form))
3343 (byte-compile-form (car form))
3344 (byte-compile-out 'byte-insert 0)
3345 (if (cdr form)
3346 (byte-compile-discard))))))
3347
1c393159
JB
3348\f
3349(byte-defop-compiler-1 setq)
3350(byte-defop-compiler-1 setq-default)
3351(byte-defop-compiler-1 quote)
3352(byte-defop-compiler-1 quote-form)
3353
3354(defun byte-compile-setq (form)
3355 (let ((args (cdr form)))
3356 (if args
3357 (while args
3358 (byte-compile-form (car (cdr args)))
3359 (or for-effect (cdr (cdr args))
3360 (byte-compile-out 'byte-dup 0))
3361 (byte-compile-variable-ref 'byte-varset (car args))
3362 (setq args (cdr (cdr args))))
3363 ;; (setq), with no arguments.
3364 (byte-compile-form nil for-effect))
3365 (setq for-effect nil)))
3366
3367(defun byte-compile-setq-default (form)
ca38179a
RS
3368 (let ((args (cdr form))
3369 setters)
3370 (while args
3371 (setq setters
3372 (cons (list 'set-default (list 'quote (car args)) (car (cdr args)))
3373 setters))
3374 (setq args (cdr (cdr args))))
3375 (byte-compile-form (cons 'progn (nreverse setters)))))
1c393159
JB
3376
3377(defun byte-compile-quote (form)
3378 (byte-compile-constant (car (cdr form))))
3379
3380(defun byte-compile-quote-form (form)
3381 (byte-compile-constant (byte-compile-top-level (nth 1 form))))
3382
3383\f
3384;;; control structures
3385
3386(defun byte-compile-body (body &optional for-effect)
3387 (while (cdr body)
3388 (byte-compile-form (car body) t)
3389 (setq body (cdr body)))
3390 (byte-compile-form (car body) for-effect))
3391
52799cb8 3392(defsubst byte-compile-body-do-effect (body)
1c393159
JB
3393 (byte-compile-body body for-effect)
3394 (setq for-effect nil))
3395
52799cb8 3396(defsubst byte-compile-form-do-effect (form)
1c393159
JB
3397 (byte-compile-form form for-effect)
3398 (setq for-effect nil))
3399
3400(byte-defop-compiler-1 inline byte-compile-progn)
3401(byte-defop-compiler-1 progn)
3402(byte-defop-compiler-1 prog1)
3403(byte-defop-compiler-1 prog2)
3404(byte-defop-compiler-1 if)
3405(byte-defop-compiler-1 cond)
3406(byte-defop-compiler-1 and)
3407(byte-defop-compiler-1 or)
3408(byte-defop-compiler-1 while)
3409(byte-defop-compiler-1 funcall)
3410(byte-defop-compiler-1 apply byte-compile-funarg)
3411(byte-defop-compiler-1 mapcar byte-compile-funarg)
3412(byte-defop-compiler-1 mapatoms byte-compile-funarg)
3413(byte-defop-compiler-1 mapconcat byte-compile-funarg)
28bb2cef 3414(byte-defop-compiler-1 mapc byte-compile-funarg)
def9389a
DL
3415(byte-defop-compiler-1 maphash byte-compile-funarg)
3416(byte-defop-compiler-1 map-char-table byte-compile-funarg)
5a6037bb 3417(byte-defop-compiler-1 sort byte-compile-funarg-2)
1c393159
JB
3418(byte-defop-compiler-1 let)
3419(byte-defop-compiler-1 let*)
3420
3421(defun byte-compile-progn (form)
3422 (byte-compile-body-do-effect (cdr form)))
3423
3424(defun byte-compile-prog1 (form)
3425 (byte-compile-form-do-effect (car (cdr form)))
3426 (byte-compile-body (cdr (cdr form)) t))
3427
3428(defun byte-compile-prog2 (form)
3429 (byte-compile-form (nth 1 form) t)
3430 (byte-compile-form-do-effect (nth 2 form))
3431 (byte-compile-body (cdr (cdr (cdr form))) t))
3432
3433(defmacro byte-compile-goto-if (cond discard tag)
1639b803
DL
3434 `(byte-compile-goto
3435 (if ,cond
3436 (if ,discard 'byte-goto-if-not-nil 'byte-goto-if-not-nil-else-pop)
3437 (if ,discard 'byte-goto-if-nil 'byte-goto-if-nil-else-pop))
3438 ,tag))
1c393159 3439
ab43c850
RS
3440(defmacro byte-compile-maybe-guarded (condition &rest body)
3441 "Execute forms in BODY, potentially guarded by CONDITION.
82a726b4
RS
3442CONDITION is a variable whose value is a test in an `if' or `cond'.
3443BODY is the code to compile first arm of the if or the body of the
b2e948ee 3444cond clause. If CONDITION's value is of the form (fboundp 'foo)
ad50a502 3445or (boundp 'foo), the relevant warnings from BODY about foo's
82a726b4
RS
3446being undefined will be suppressed.
3447
b2e948ee 3448If CONDITION's value is (not (featurep 'emacs)) or (featurep 'xemacs),
ad50a502 3449that suppresses all warnings during execution of BODY."
ab43c850 3450 (declare (indent 1) (debug t))
8195ef65
CY
3451 `(let* ((fbound
3452 (if (eq 'fboundp (car-safe ,condition))
3453 (and (eq 'quote (car-safe (nth 1 ,condition)))
3454 ;; Ignore if the symbol is already on the
3455 ;; unresolved list.
3456 (not (assq (nth 1 (nth 1 ,condition)) ; the relevant symbol
3457 byte-compile-unresolved-functions))
3458 (nth 1 (nth 1 ,condition)))))
3459 (bound (if (or (eq 'boundp (car-safe ,condition))
3460 (eq 'default-boundp (car-safe ,condition)))
3461 (and (eq 'quote (car-safe (nth 1 ,condition)))
3462 (nth 1 (nth 1 ,condition)))))
3463 ;; Maybe add to the bound list.
3464 (byte-compile-bound-variables
3465 (if bound
3466 (cons bound byte-compile-bound-variables)
3467 byte-compile-bound-variables))
3468 ;; Suppress all warnings, for code not used in Emacs.
3469 (byte-compile-warnings
83b0af6e
RS
3470 (if (member ,condition '((featurep 'xemacs)
3471 (not (featurep 'emacs))))
8195ef65 3472 nil byte-compile-warnings)))
82a726b4
RS
3473 (unwind-protect
3474 (progn ,@body)
3475 ;; Maybe remove the function symbol from the unresolved list.
8195ef65
CY
3476 (if fbound
3477 (setq byte-compile-unresolved-functions
3478 (delq (assq fbound byte-compile-unresolved-functions)
3479 byte-compile-unresolved-functions))))))
ab43c850 3480
1c393159
JB
3481(defun byte-compile-if (form)
3482 (byte-compile-form (car (cdr form)))
b8234c84
DL
3483 ;; Check whether we have `(if (fboundp ...' or `(if (boundp ...'
3484 ;; and avoid warnings about the relevent symbols in the consequent.
ab43c850
RS
3485 (let ((clause (nth 1 form))
3486 (donetag (byte-compile-make-tag)))
b8234c84
DL
3487 (if (null (nthcdr 3 form))
3488 ;; No else-forms
3489 (progn
3490 (byte-compile-goto-if nil for-effect donetag)
ab43c850 3491 (byte-compile-maybe-guarded clause
b8234c84 3492 (byte-compile-form (nth 2 form) for-effect))
b8234c84
DL
3493 (byte-compile-out-tag donetag))
3494 (let ((elsetag (byte-compile-make-tag)))
3495 (byte-compile-goto 'byte-goto-if-nil elsetag)
ab43c850
RS
3496 (byte-compile-maybe-guarded clause
3497 (byte-compile-form (nth 2 form) for-effect))
b8234c84
DL
3498 (byte-compile-goto 'byte-goto donetag)
3499 (byte-compile-out-tag elsetag)
300f994a
RS
3500 (byte-compile-maybe-guarded (list 'not clause)
3501 (byte-compile-body (cdr (cdr (cdr form))) for-effect))
b8234c84 3502 (byte-compile-out-tag donetag))))
1c393159
JB
3503 (setq for-effect nil))
3504
3505(defun byte-compile-cond (clauses)
3506 (let ((donetag (byte-compile-make-tag))
3507 nexttag clause)
3508 (while (setq clauses (cdr clauses))
3509 (setq clause (car clauses))
3510 (cond ((or (eq (car clause) t)
3511 (and (eq (car-safe (car clause)) 'quote)
3512 (car-safe (cdr-safe (car clause)))))
3513 ;; Unconditional clause
3514 (setq clause (cons t clause)
3515 clauses nil))
3516 ((cdr clauses)
3517 (byte-compile-form (car clause))
3518 (if (null (cdr clause))
3519 ;; First clause is a singleton.
3520 (byte-compile-goto-if t for-effect donetag)
82a726b4
RS
3521 (setq nexttag (byte-compile-make-tag))
3522 (byte-compile-goto 'byte-goto-if-nil nexttag)
3523 (byte-compile-maybe-guarded (car clause)
3524 (byte-compile-body (cdr clause) for-effect))
3525 (byte-compile-goto 'byte-goto donetag)
3526 (byte-compile-out-tag nexttag)))))
1c393159 3527 ;; Last clause
7d0e0955
SM
3528 (let ((guard (car clause)))
3529 (and (cdr clause) (not (eq guard t))
3530 (progn (byte-compile-form guard)
3531 (byte-compile-goto-if nil for-effect donetag)
3532 (setq clause (cdr clause))))
3533 (byte-compile-maybe-guarded guard
3534 (byte-compile-body-do-effect clause)))
1c393159
JB
3535 (byte-compile-out-tag donetag)))
3536
3537(defun byte-compile-and (form)
3538 (let ((failtag (byte-compile-make-tag))
3539 (args (cdr form)))
3540 (if (null args)
3541 (byte-compile-form-do-effect t)
8877fa6f
RS
3542 (byte-compile-and-recursion args failtag))))
3543
83b0af6e 3544;; Handle compilation of a nontrivial `and' call.
8877fa6f
RS
3545;; We use tail recursion so we can use byte-compile-maybe-guarded.
3546(defun byte-compile-and-recursion (rest failtag)
3547 (if (cdr rest)
3548 (progn
3549 (byte-compile-form (car rest))
1c393159 3550 (byte-compile-goto-if nil for-effect failtag)
8877fa6f
RS
3551 (byte-compile-maybe-guarded (car rest)
3552 (byte-compile-and-recursion (cdr rest) failtag)))
3553 (byte-compile-form-do-effect (car rest))
3554 (byte-compile-out-tag failtag)))
1c393159
JB
3555
3556(defun byte-compile-or (form)
3557 (let ((wintag (byte-compile-make-tag))
3558 (args (cdr form)))
3559 (if (null args)
3560 (byte-compile-form-do-effect nil)
83b0af6e
RS
3561 (byte-compile-or-recursion args wintag))))
3562
3563;; Handle compilation of a nontrivial `or' call.
3564;; We use tail recursion so we can use byte-compile-maybe-guarded.
3565(defun byte-compile-or-recursion (rest wintag)
3566 (if (cdr rest)
3567 (progn
3568 (byte-compile-form (car rest))
1c393159 3569 (byte-compile-goto-if t for-effect wintag)
83b0af6e
RS
3570 (byte-compile-maybe-guarded (list 'not (car rest))
3571 (byte-compile-or-recursion (cdr rest) wintag)))
3572 (byte-compile-form-do-effect (car rest))
3573 (byte-compile-out-tag wintag)))
1c393159
JB
3574
3575(defun byte-compile-while (form)
3576 (let ((endtag (byte-compile-make-tag))
3577 (looptag (byte-compile-make-tag)))
3578 (byte-compile-out-tag looptag)
3579 (byte-compile-form (car (cdr form)))
3580 (byte-compile-goto-if nil for-effect endtag)
3581 (byte-compile-body (cdr (cdr form)) t)
3582 (byte-compile-goto 'byte-goto looptag)
3583 (byte-compile-out-tag endtag)
3584 (setq for-effect nil)))
3585
3586(defun byte-compile-funcall (form)
ed62683d 3587 (mapc 'byte-compile-form (cdr form))
1c393159
JB
3588 (byte-compile-out 'byte-call (length (cdr (cdr form)))))
3589
3590
3591(defun byte-compile-let (form)
3592 ;; First compute the binding values in the old scope.
3593 (let ((varlist (car (cdr form))))
6c2161c4
SM
3594 (dolist (var varlist)
3595 (if (consp var)
3596 (byte-compile-form (car (cdr var)))
3597 (byte-compile-push-constant nil))))
1c393159
JB
3598 (let ((byte-compile-bound-variables byte-compile-bound-variables) ;new scope
3599 (varlist (reverse (car (cdr form)))))
6c2161c4
SM
3600 (dolist (var varlist)
3601 (byte-compile-variable-ref 'byte-varbind (if (consp var) (car var) var)))
1c393159
JB
3602 (byte-compile-body-do-effect (cdr (cdr form)))
3603 (byte-compile-out 'byte-unbind (length (car (cdr form))))))
3604
3605(defun byte-compile-let* (form)
3606 (let ((byte-compile-bound-variables byte-compile-bound-variables) ;new scope
3607 (varlist (copy-sequence (car (cdr form)))))
6c2161c4
SM
3608 (dolist (var varlist)
3609 (if (atom var)
1c393159 3610 (byte-compile-push-constant nil)
6c2161c4
SM
3611 (byte-compile-form (car (cdr var)))
3612 (setq var (car var)))
3613 (byte-compile-variable-ref 'byte-varbind var))
1c393159
JB
3614 (byte-compile-body-do-effect (cdr (cdr form)))
3615 (byte-compile-out 'byte-unbind (length (car (cdr form))))))
3616
3617
3618(byte-defop-compiler-1 /= byte-compile-negated)
3619(byte-defop-compiler-1 atom byte-compile-negated)
3620(byte-defop-compiler-1 nlistp byte-compile-negated)
3621
3622(put '/= 'byte-compile-negated-op '=)
3623(put 'atom 'byte-compile-negated-op 'consp)
3624(put 'nlistp 'byte-compile-negated-op 'listp)
3625
3626(defun byte-compile-negated (form)
3627 (byte-compile-form-do-effect (byte-compile-negation-optimizer form)))
3628
3629;; Even when optimization is off, /= is optimized to (not (= ...)).
3630(defun byte-compile-negation-optimizer (form)
3631 ;; an optimizer for forms where <form1> is less efficient than (not <form2>)
ccb3c8de 3632 (byte-compile-set-symbol-position (car form))
1c393159
JB
3633 (list 'not
3634 (cons (or (get (car form) 'byte-compile-negated-op)
3635 (error
52799cb8 3636 "Compiler error: `%s' has no `byte-compile-negated-op' property"
1c393159
JB
3637 (car form)))
3638 (cdr form))))
3639\f
3640;;; other tricky macro-like special-forms
3641
3642(byte-defop-compiler-1 catch)
3643(byte-defop-compiler-1 unwind-protect)
3644(byte-defop-compiler-1 condition-case)
3645(byte-defop-compiler-1 save-excursion)
f3e472b0 3646(byte-defop-compiler-1 save-current-buffer)
1c393159
JB
3647(byte-defop-compiler-1 save-restriction)
3648(byte-defop-compiler-1 save-window-excursion)
3649(byte-defop-compiler-1 with-output-to-temp-buffer)
6e8d0db7 3650(byte-defop-compiler-1 track-mouse)
1c393159
JB
3651
3652(defun byte-compile-catch (form)
3653 (byte-compile-form (car (cdr form)))
3654 (byte-compile-push-constant
3655 (byte-compile-top-level (cons 'progn (cdr (cdr form))) for-effect))
3656 (byte-compile-out 'byte-catch 0))
3657
3658(defun byte-compile-unwind-protect (form)
3659 (byte-compile-push-constant
3660 (byte-compile-top-level-body (cdr (cdr form)) t))
3661 (byte-compile-out 'byte-unwind-protect 0)
3662 (byte-compile-form-do-effect (car (cdr form)))
3663 (byte-compile-out 'byte-unbind 1))
3664
6e8d0db7 3665(defun byte-compile-track-mouse (form)
d7846e08 3666 (byte-compile-form
6c2161c4
SM
3667 `(funcall '(lambda nil
3668 (track-mouse ,@(byte-compile-top-level-body (cdr form)))))))
6e8d0db7 3669
1c393159
JB
3670(defun byte-compile-condition-case (form)
3671 (let* ((var (nth 1 form))
3672 (byte-compile-bound-variables
3673 (if var (cons var byte-compile-bound-variables)
3674 byte-compile-bound-variables)))
ccb3c8de
CW
3675 (byte-compile-set-symbol-position 'condition-case)
3676 (unless (symbolp var)
3677 (byte-compile-warn
1d5c17c0 3678 "`%s' is not a variable-name or nil (in condition-case)" var))
1c393159
JB
3679 (byte-compile-push-constant var)
3680 (byte-compile-push-constant (byte-compile-top-level
3681 (nth 2 form) for-effect))
3682 (let ((clauses (cdr (cdr (cdr form))))
3683 compiled-clauses)
3684 (while clauses
e27c3564
JB
3685 (let* ((clause (car clauses))
3686 (condition (car clause)))
2abcddce
RS
3687 (cond ((not (or (symbolp condition)
3688 (and (listp condition)
3689 (let ((syms condition) (ok t))
3690 (while syms
3691 (if (not (symbolp (car syms)))
3692 (setq ok nil))
3693 (setq syms (cdr syms)))
3694 ok))))
e27c3564 3695 (byte-compile-warn
1d5c17c0 3696 "`%s' is not a condition name or list of such (in condition-case)"
e27c3564 3697 (prin1-to-string condition)))
2abcddce
RS
3698;; ((not (or (eq condition 't)
3699;; (and (stringp (get condition 'error-message))
3700;; (consp (get condition 'error-conditions)))))
3701;; (byte-compile-warn
1d5c17c0 3702;; "`%s' is not a known condition name (in condition-case)"
2abcddce
RS
3703;; condition))
3704 )
1c393159 3705 (setq compiled-clauses
e27c3564 3706 (cons (cons condition
1c393159
JB
3707 (byte-compile-top-level-body
3708 (cdr clause) for-effect))
3709 compiled-clauses)))
3710 (setq clauses (cdr clauses)))
3711 (byte-compile-push-constant (nreverse compiled-clauses)))
3712 (byte-compile-out 'byte-condition-case 0)))
3713
3714
3715(defun byte-compile-save-excursion (form)
3716 (byte-compile-out 'byte-save-excursion 0)
3717 (byte-compile-body-do-effect (cdr form))
3718 (byte-compile-out 'byte-unbind 1))
3719
3720(defun byte-compile-save-restriction (form)
3721 (byte-compile-out 'byte-save-restriction 0)
3722 (byte-compile-body-do-effect (cdr form))
3723 (byte-compile-out 'byte-unbind 1))
3724
f3e472b0
RS
3725(defun byte-compile-save-current-buffer (form)
3726 (byte-compile-out 'byte-save-current-buffer 0)
3727 (byte-compile-body-do-effect (cdr form))
3728 (byte-compile-out 'byte-unbind 1))
3729
1c393159
JB
3730(defun byte-compile-save-window-excursion (form)
3731 (byte-compile-push-constant
3732 (byte-compile-top-level-body (cdr form) for-effect))
3733 (byte-compile-out 'byte-save-window-excursion 0))
3734
3735(defun byte-compile-with-output-to-temp-buffer (form)
3736 (byte-compile-form (car (cdr form)))
3737 (byte-compile-out 'byte-temp-output-buffer-setup 0)
3738 (byte-compile-body (cdr (cdr form)))
3739 (byte-compile-out 'byte-temp-output-buffer-show 0))
1c393159
JB
3740\f
3741;;; top-level forms elsewhere
3742
3743(byte-defop-compiler-1 defun)
3744(byte-defop-compiler-1 defmacro)
3745(byte-defop-compiler-1 defvar)
3746(byte-defop-compiler-1 defconst byte-compile-defvar)
3747(byte-defop-compiler-1 autoload)
3748(byte-defop-compiler-1 lambda byte-compile-lambda-form)
3749
3750(defun byte-compile-defun (form)
3751 ;; This is not used for file-level defuns with doc strings.
ccb3c8de
CW
3752 (if (symbolp (car form))
3753 (byte-compile-set-symbol-position (car form))
3754 (byte-compile-set-symbol-position 'defun)
eadd6444 3755 (error "defun name must be a symbol, not %s" (car form)))
d8c91258
RS
3756 (if (byte-compile-version-cond byte-compile-compatibility)
3757 (progn
3758 (byte-compile-two-args ; Use this to avoid byte-compile-fset's warning.
3759 (list 'fset
3760 (list 'quote (nth 1 form))
3761 (byte-compile-byte-code-maker
4ec5239c 3762 (byte-compile-lambda (cdr (cdr form)) t))))
d8c91258 3763 (byte-compile-discard))
a81edd91
RS
3764 ;; We prefer to generate a defalias form so it will record the function
3765 ;; definition just like interpreting a defun.
d8c91258
RS
3766 (byte-compile-form
3767 (list 'defalias
3768 (list 'quote (nth 1 form))
3769 (byte-compile-byte-code-maker
4ec5239c 3770 (byte-compile-lambda (cdr (cdr form)) t)))
d8c91258 3771 t))
1c393159
JB
3772 (byte-compile-constant (nth 1 form)))
3773
3774(defun byte-compile-defmacro (form)
3775 ;; This is not used for file-level defmacros with doc strings.
3776 (byte-compile-body-do-effect
3777 (list (list 'fset (list 'quote (nth 1 form))
3778 (let ((code (byte-compile-byte-code-maker
4ec5239c 3779 (byte-compile-lambda (cdr (cdr form)) t))))
1c393159
JB
3780 (if (eq (car-safe code) 'make-byte-code)
3781 (list 'cons ''macro code)
3782 (list 'quote (cons 'macro (eval code))))))
3783 (list 'quote (nth 1 form)))))
3784
3785(defun byte-compile-defvar (form)
3786 ;; This is not used for file-level defvar/consts with doc strings.
1bc20d83
GM
3787 (let ((fun (nth 0 form))
3788 (var (nth 1 form))
1c393159
JB
3789 (value (nth 2 form))
3790 (string (nth 3 form)))
ccb3c8de 3791 (byte-compile-set-symbol-position fun)
6c2161c4
SM
3792 (when (or (> (length form) 4)
3793 (and (eq fun 'defconst) (null (cddr form))))
d0e07261
SM
3794 (let ((ncall (length (cdr form))))
3795 (byte-compile-warn
1d5c17c0 3796 "`%s' called with %d argument%s, but %s %s"
d0e07261
SM
3797 fun ncall
3798 (if (= 1 ncall) "" "s")
3799 (if (< ncall 2) "requires" "accepts only")
3800 "2-3")))
1bc20d83 3801 (when (memq 'free-vars byte-compile-warnings)
8898f44e 3802 (push var byte-compile-bound-variables)
6c2161c4
SM
3803 (if (eq fun 'defconst)
3804 (push var byte-compile-const-variables)))
1c393159 3805 (byte-compile-body-do-effect
1bc20d83
GM
3806 (list
3807 ;; Put the defined variable in this library's load-history entry
3808 ;; just as a real defvar would, but only in top-level forms.
3614fc84 3809 (when (and (cddr form) (null byte-compile-current-form))
1bc20d83
GM
3810 `(push ',var current-load-list))
3811 (when (> (length form) 3)
3812 (when (and string (not (stringp string)))
1d5c17c0 3813 (byte-compile-warn "third arg to `%s %s' is not a string: %s"
1bc20d83
GM
3814 fun var string))
3815 `(put ',var 'variable-documentation ,string))
fef3407e 3816 (if (cddr form) ; `value' provided
9e31e67b
RS
3817 (let ((byte-compile-not-obsolete-var var))
3818 (if (eq fun 'defconst)
3819 ;; `defconst' sets `var' unconditionally.
3820 (let ((tmp (make-symbol "defconst-tmp-var")))
3821 `(funcall '(lambda (,tmp) (defconst ,var ,tmp))
3822 ,value))
3823 ;; `defvar' sets `var' only when unbound.
3824 `(if (not (default-boundp ',var)) (setq-default ,var ,value))))
6c2161c4
SM
3825 (when (eq fun 'defconst)
3826 ;; This will signal an appropriate error at runtime.
3827 `(eval ',form)))
1bc20d83 3828 `',var))))
1c393159
JB
3829
3830(defun byte-compile-autoload (form)
ccb3c8de 3831 (byte-compile-set-symbol-position 'autoload)
1c393159
JB
3832 (and (byte-compile-constp (nth 1 form))
3833 (byte-compile-constp (nth 5 form))
3834 (eval (nth 5 form)) ; macro-p
3835 (not (fboundp (eval (nth 1 form))))
3836 (byte-compile-warn
c5091f25 3837 "The compiler ignores `autoload' except at top level. You should
1c393159
JB
3838 probably put the autoload of the macro `%s' at top-level."
3839 (eval (nth 1 form))))
3840 (byte-compile-normal-call form))
3841
c5091f25 3842;; Lambdas in valid places are handled as special cases by various code.
1c393159
JB
3843;; The ones that remain are errors.
3844(defun byte-compile-lambda-form (form)
ccb3c8de 3845 (byte-compile-set-symbol-position 'lambda)
1c393159
JB
3846 (error "`lambda' used as function name is invalid"))
3847
5286a842 3848;; Compile normally, but deal with warnings for the function being defined.
977b50fb
SM
3849(put 'defalias 'byte-hunk-handler 'byte-compile-file-form-defalias)
3850(defun byte-compile-file-form-defalias (form)
5286a842
RS
3851 (if (and (consp (cdr form)) (consp (nth 1 form))
3852 (eq (car (nth 1 form)) 'quote)
3853 (consp (cdr (nth 1 form)))
a7a7ddf1
RS
3854 (symbolp (nth 1 (nth 1 form))))
3855 (let ((constant
3856 (and (consp (nthcdr 2 form))
3857 (consp (nth 2 form))
3858 (eq (car (nth 2 form)) 'quote)
3859 (consp (cdr (nth 2 form)))
3860 (symbolp (nth 1 (nth 2 form))))))
6c2161c4 3861 (byte-compile-defalias-warn (nth 1 (nth 1 form)))
977b50fb
SM
3862 (push (cons (nth 1 (nth 1 form))
3863 (if constant (nth 1 (nth 2 form)) t))
3864 byte-compile-function-environment)))
b7a5a208
SM
3865 ;; We used to jus do: (byte-compile-normal-call form)
3866 ;; But it turns out that this fails to optimize the code.
3867 ;; So instead we now do the same as what other byte-hunk-handlers do,
3868 ;; which is to call back byte-compile-file-form and then return nil.
3869 ;; Except that we can't just call byte-compile-file-form since it would
3870 ;; call us right back.
3871 (byte-compile-keep-pending form)
3872 ;; Return nil so the form is not output twice.
3873 nil)
5286a842
RS
3874
3875;; Turn off warnings about prior calls to the function being defalias'd.
3876;; This could be smarter and compare those calls with
3877;; the function it is being aliased to.
6c2161c4 3878(defun byte-compile-defalias-warn (new)
5286a842
RS
3879 (let ((calls (assq new byte-compile-unresolved-functions)))
3880 (if calls
3881 (setq byte-compile-unresolved-functions
3882 (delq calls byte-compile-unresolved-functions)))))
3c9dc1cf
RS
3883
3884(byte-defop-compiler-1 with-no-warnings byte-compile-no-warnings)
3885(defun byte-compile-no-warnings (form)
3886 (let (byte-compile-warnings)
a4f66531 3887 (byte-compile-form (cons 'progn (cdr form)))))
01e4a4fa
SM
3888
3889;; Warn about misuses of make-variable-buffer-local.
3890(byte-defop-compiler-1 make-variable-buffer-local byte-compile-make-variable-buffer-local)
3891(defun byte-compile-make-variable-buffer-local (form)
15ce9dcf
GM
3892 (if (and (eq (car-safe (car-safe (cdr-safe form))) 'quote)
3893 (memq 'make-local byte-compile-warnings))
01e4a4fa
SM
3894 (byte-compile-warn
3895 "`make-variable-buffer-local' should be called at toplevel"))
3896 (byte-compile-normal-call form))
3897(put 'make-variable-buffer-local
3898 'byte-hunk-handler 'byte-compile-form-make-variable-buffer-local)
3899(defun byte-compile-form-make-variable-buffer-local (form)
3900 (byte-compile-keep-pending form 'byte-compile-normal-call))
3901
1c393159
JB
3902\f
3903;;; tags
3904
3905;; Note: Most operations will strip off the 'TAG, but it speeds up
3906;; optimization to have the 'TAG as a part of the tag.
3907;; Tags will be (TAG . (tag-number . stack-depth)).
3908(defun byte-compile-make-tag ()
3909 (list 'TAG (setq byte-compile-tag-number (1+ byte-compile-tag-number))))
3910
3911
3912(defun byte-compile-out-tag (tag)
3913 (setq byte-compile-output (cons tag byte-compile-output))
3914 (if (cdr (cdr tag))
3915 (progn
3916 ;; ## remove this someday
3917 (and byte-compile-depth
3918 (not (= (cdr (cdr tag)) byte-compile-depth))
52799cb8 3919 (error "Compiler bug: depth conflict at tag %d" (car (cdr tag))))
1c393159
JB
3920 (setq byte-compile-depth (cdr (cdr tag))))
3921 (setcdr (cdr tag) byte-compile-depth)))
3922
3923(defun byte-compile-goto (opcode tag)
6c2161c4 3924 (push (cons opcode tag) byte-compile-output)
1c393159
JB
3925 (setcdr (cdr tag) (if (memq opcode byte-goto-always-pop-ops)
3926 (1- byte-compile-depth)
3927 byte-compile-depth))
3928 (setq byte-compile-depth (and (not (eq opcode 'byte-goto))
3929 (1- byte-compile-depth))))
3930
3931(defun byte-compile-out (opcode offset)
6c2161c4 3932 (push (cons opcode offset) byte-compile-output)
1c393159
JB
3933 (cond ((eq opcode 'byte-call)
3934 (setq byte-compile-depth (- byte-compile-depth offset)))
3935 ((eq opcode 'byte-return)
3936 ;; This is actually an unnecessary case, because there should be
3937 ;; no more opcodes behind byte-return.
3938 (setq byte-compile-depth nil))
3939 (t
3940 (setq byte-compile-depth (+ byte-compile-depth
3941 (or (aref byte-stack+-info
3942 (symbol-value opcode))
3943 (- (1- offset))))
3944 byte-compile-maxdepth (max byte-compile-depth
3945 byte-compile-maxdepth))))
52799cb8 3946 ;;(if (< byte-compile-depth 0) (error "Compiler error: stack underflow"))
1c393159
JB
3947 )
3948
3949\f
3950;;; call tree stuff
3951
3952(defun byte-compile-annotate-call-tree (form)
3953 (let (entry)
3954 ;; annotate the current call
3955 (if (setq entry (assq (car form) byte-compile-call-tree))
3956 (or (memq byte-compile-current-form (nth 1 entry)) ;callers
3957 (setcar (cdr entry)
3958 (cons byte-compile-current-form (nth 1 entry))))
3959 (setq byte-compile-call-tree
3960 (cons (list (car form) (list byte-compile-current-form) nil)
3961 byte-compile-call-tree)))
3962 ;; annotate the current function
3963 (if (setq entry (assq byte-compile-current-form byte-compile-call-tree))
3964 (or (memq (car form) (nth 2 entry)) ;called
3965 (setcar (cdr (cdr entry))
3966 (cons (car form) (nth 2 entry))))
3967 (setq byte-compile-call-tree
3968 (cons (list byte-compile-current-form nil (list (car form)))
3969 byte-compile-call-tree)))
3970 ))
3971
52799cb8
RS
3972;; Renamed from byte-compile-report-call-tree
3973;; to avoid interfering with completion of byte-compile-file.
fd5285f3 3974;;;###autoload
52799cb8
RS
3975(defun display-call-tree (&optional filename)
3976 "Display a call graph of a specified file.
3977This lists which functions have been called, what functions called
3978them, and what functions they call. The list includes all functions
3979whose definitions have been compiled in this Emacs session, as well as
3980all functions called by those functions.
1c393159 3981
52799cb8
RS
3982The call graph does not include macros, inline functions, or
3983primitives that the byte-code interpreter knows about directly \(eq,
3984cons, etc.\).
1c393159
JB
3985
3986The call tree also lists those functions which are not known to be called
52799cb8
RS
3987\(that is, to which no calls have been compiled\), and which cannot be
3988invoked interactively."
1c393159
JB
3989 (interactive)
3990 (message "Generating call tree...")
3991 (with-output-to-temp-buffer "*Call-Tree*"
3992 (set-buffer "*Call-Tree*")
3993 (erase-buffer)
47cf9d3a 3994 (message "Generating call tree... (sorting on %s)"
1c393159
JB
3995 byte-compile-call-tree-sort)
3996 (insert "Call tree for "
3997 (cond ((null byte-compile-current-file) (or filename "???"))
3998 ((stringp byte-compile-current-file)
3999 byte-compile-current-file)
4000 (t (buffer-name byte-compile-current-file)))
4001 " sorted on "
4002 (prin1-to-string byte-compile-call-tree-sort)
4003 ":\n\n")
4004 (if byte-compile-call-tree-sort
4005 (setq byte-compile-call-tree
4006 (sort byte-compile-call-tree
4007 (cond ((eq byte-compile-call-tree-sort 'callers)
4008 (function (lambda (x y) (< (length (nth 1 x))
4009 (length (nth 1 y))))))
4010 ((eq byte-compile-call-tree-sort 'calls)
4011 (function (lambda (x y) (< (length (nth 2 x))
4012 (length (nth 2 y))))))
4013 ((eq byte-compile-call-tree-sort 'calls+callers)
4014 (function (lambda (x y) (< (+ (length (nth 1 x))
4015 (length (nth 2 x)))
4016 (+ (length (nth 1 y))
4017 (length (nth 2 y)))))))
4018 ((eq byte-compile-call-tree-sort 'name)
4019 (function (lambda (x y) (string< (car x)
4020 (car y)))))
52799cb8 4021 (t (error "`byte-compile-call-tree-sort': `%s' - unknown sort mode"
1c393159
JB
4022 byte-compile-call-tree-sort))))))
4023 (message "Generating call tree...")
4024 (let ((rest byte-compile-call-tree)
4025 (b (current-buffer))
4026 f p
4027 callers calls)
4028 (while rest
4029 (prin1 (car (car rest)) b)
4030 (setq callers (nth 1 (car rest))
4031 calls (nth 2 (car rest)))
4032 (insert "\t"
4033 (cond ((not (fboundp (setq f (car (car rest)))))
4034 (if (null f)
4035 " <top level>";; shouldn't insert nil then, actually -sk
4036 " <not defined>"))
4037 ((subrp (setq f (symbol-function f)))
4038 " <subr>")
4039 ((symbolp f)
4040 (format " ==> %s" f))
ed015bdd 4041 ((byte-code-function-p f)
1c393159
JB
4042 "<compiled function>")
4043 ((not (consp f))
4044 "<malformed function>")
4045 ((eq 'macro (car f))
ed015bdd 4046 (if (or (byte-code-function-p (cdr f))
1c393159
JB
4047 (assq 'byte-code (cdr (cdr (cdr f)))))
4048 " <compiled macro>"
4049 " <macro>"))
4050 ((assq 'byte-code (cdr (cdr f)))
4051 "<compiled lambda>")
4052 ((eq 'lambda (car f))
4053 "<function>")
4054 (t "???"))
4055 (format " (%d callers + %d calls = %d)"
4056 ;; Does the optimizer eliminate common subexpressions?-sk
4057 (length callers)
4058 (length calls)
4059 (+ (length callers) (length calls)))
4060 "\n")
4061 (if callers
4062 (progn
4063 (insert " called by:\n")
4064 (setq p (point))
4065 (insert " " (if (car callers)
4066 (mapconcat 'symbol-name callers ", ")
4067 "<top level>"))
4068 (let ((fill-prefix " "))
78bba1c8
TTN
4069 (fill-region-as-paragraph p (point)))
4070 (unless (= 0 (current-column))
4071 (insert "\n"))))
1c393159
JB
4072 (if calls
4073 (progn
4074 (insert " calls:\n")
4075 (setq p (point))
4076 (insert " " (mapconcat 'symbol-name calls ", "))
4077 (let ((fill-prefix " "))
78bba1c8
TTN
4078 (fill-region-as-paragraph p (point)))
4079 (unless (= 0 (current-column))
4080 (insert "\n"))))
1c393159
JB
4081 (setq rest (cdr rest)))
4082
4083 (message "Generating call tree...(finding uncalled functions...)")
4084 (setq rest byte-compile-call-tree)
4085 (let ((uncalled nil))
4086 (while rest
4087 (or (nth 1 (car rest))
4088 (null (setq f (car (car rest))))
a7a7ddf1 4089 (functionp (byte-compile-fdefinition f t))
1c393159
JB
4090 (commandp (byte-compile-fdefinition f nil))
4091 (setq uncalled (cons f uncalled)))
4092 (setq rest (cdr rest)))
4093 (if uncalled
4094 (let ((fill-prefix " "))
4095 (insert "Noninteractive functions not known to be called:\n ")
4096 (setq p (point))
4097 (insert (mapconcat 'symbol-name (nreverse uncalled) ", "))
4098 (fill-region-as-paragraph p (point)))))
4099 )
4100 (message "Generating call tree...done.")
4101 ))
4102
4103\f
814c447f 4104;;;###autoload
7e7d0f8b
RS
4105(defun batch-byte-compile-if-not-done ()
4106 "Like `byte-compile-file' but doesn't recompile if already up to date.
4107Use this from the command line, with `-batch';
4108it won't work in an interactive Emacs."
4109 (batch-byte-compile t))
4110
1c393159
JB
4111;;; by crl@newton.purdue.edu
4112;;; Only works noninteractively.
fd5285f3 4113;;;###autoload
7e7d0f8b 4114(defun batch-byte-compile (&optional noforce)
52799cb8
RS
4115 "Run `byte-compile-file' on the files remaining on the command line.
4116Use this from the command line, with `-batch';
4117it won't work in an interactive Emacs.
4118Each file is processed even if an error occurred previously.
7e7d0f8b
RS
4119For example, invoke \"emacs -batch -f batch-byte-compile $emacs/ ~/*.el\".
4120If NOFORCE is non-nil, don't recompile a file that seems to be
4121already up-to-date."
1c393159
JB
4122 ;; command-line-args-left is what is left of the command line (from startup.el)
4123 (defvar command-line-args-left) ;Avoid 'free variable' warning
4124 (if (not noninteractive)
52799cb8 4125 (error "`batch-byte-compile' is to be used only with -batch"))
1c393159
JB
4126 (let ((error nil))
4127 (while command-line-args-left
4128 (if (file-directory-p (expand-file-name (car command-line-args-left)))
7e7d0f8b 4129 ;; Directory as argument.
1c393159
JB
4130 (let ((files (directory-files (car command-line-args-left)))
4131 source dest)
d90a41e8
SS
4132 (dolist (file files)
4133 (if (and (string-match emacs-lisp-file-regexp file)
4134 (not (auto-save-file-name-p file))
4135 (setq source (expand-file-name file
1c393159
JB
4136 (car command-line-args-left)))
4137 (setq dest (byte-compile-dest-file source))
4138 (file-exists-p dest)
4139 (file-newer-than-file-p source dest))
4140 (if (null (batch-byte-compile-file source))
d90a41e8 4141 (setq error t)))))
7e7d0f8b
RS
4142 ;; Specific file argument
4143 (if (or (not noforce)
4144 (let* ((source (car command-line-args-left))
4145 (dest (byte-compile-dest-file source)))
4146 (or (not (file-exists-p dest))
4147 (file-newer-than-file-p source dest))))
4148 (if (null (batch-byte-compile-file (car command-line-args-left)))
4149 (setq error t))))
1c393159 4150 (setq command-line-args-left (cdr command-line-args-left)))
1c393159
JB
4151 (kill-emacs (if error 1 0))))
4152
4153(defun batch-byte-compile-file (file)
a080ff23 4154 (if debug-on-error
1c57117e 4155 (byte-compile-file file)
a080ff23
SM
4156 (condition-case err
4157 (byte-compile-file file)
4158 (file-error
4159 (message (if (cdr err)
4160 ">>Error occurred processing %s: %s (%s)"
d09b1c02 4161 ">>Error occurred processing %s: %s")
a080ff23
SM
4162 file
4163 (get (car err) 'error-message)
4164 (prin1-to-string (cdr err)))
4165 (let ((destfile (byte-compile-dest-file file)))
4166 (if (file-exists-p destfile)
4167 (delete-file destfile)))
4168 nil)
4169 (error
4170 (message (if (cdr err)
4171 ">>Error occurred processing %s: %s (%s)"
1c393159 4172 ">>Error occurred processing %s: %s")
a080ff23
SM
4173 file
4174 (get (car err) 'error-message)
4175 (prin1-to-string (cdr err)))
4176 nil))))
1c393159 4177
e9681c45 4178;;;###autoload
6f8e3590 4179(defun batch-byte-recompile-directory (&optional arg)
4f6d5bf0 4180 "Run `byte-recompile-directory' on the dirs remaining on the command line.
79c6071d 4181Must be used only with `-batch', and kills Emacs on completion.
defe3b41
EZ
4182For example, invoke `emacs -batch -f batch-byte-recompile-directory .'.
4183
4184Optional argument ARG is passed as second argument ARG to
4185`batch-recompile-directory'; see there for its possible values
4186and corresponding effects."
e27c3564
JB
4187 ;; command-line-args-left is what is left of the command line (startup.el)
4188 (defvar command-line-args-left) ;Avoid 'free variable' warning
4189 (if (not noninteractive)
4190 (error "batch-byte-recompile-directory is to be used only with -batch"))
4191 (or command-line-args-left
4192 (setq command-line-args-left '(".")))
4193 (while command-line-args-left
6f8e3590 4194 (byte-recompile-directory (car command-line-args-left) arg)
e27c3564
JB
4195 (setq command-line-args-left (cdr command-line-args-left)))
4196 (kill-emacs 0))
4197
1c393159 4198(provide 'byte-compile)
200503bb 4199(provide 'bytecomp)
1c393159
JB
4200
4201\f
4202;;; report metering (see the hacks in bytecode.c)
4203
08d21785 4204(defvar byte-code-meter)
52799cb8 4205(defun byte-compile-report-ops ()
52799cb8
RS
4206 (with-output-to-temp-buffer "*Meter*"
4207 (set-buffer "*Meter*")
4208 (let ((i 0) n op off)
4209 (while (< i 256)
4210 (setq n (aref (aref byte-code-meter 0) i)
4211 off nil)
4212 (if t ;(not (zerop n))
4213 (progn
4214 (setq op i)
4215 (setq off nil)
4216 (cond ((< op byte-nth)
4217 (setq off (logand op 7))
4218 (setq op (logand op 248)))
4219 ((>= op byte-constant)
4220 (setq off (- op byte-constant)
4221 op byte-constant)))
4222 (setq op (aref byte-code-vector op))
4223 (insert (format "%-4d" i))
4224 (insert (symbol-name op))
4225 (if off (insert " [" (int-to-string off) "]"))
4226 (indent-to 40)
4227 (insert (int-to-string n) "\n")))
4228 (setq i (1+ i))))))
1c393159
JB
4229\f
4230;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when bytecomp compiles
4231;; itself, compile some of its most used recursive functions (at load time).
4232;;
4233(eval-when-compile
591655c7
SM
4234 (or (byte-code-function-p (symbol-function 'byte-compile-form))
4235 (assq 'byte-code (symbol-function 'byte-compile-form))
4236 (let ((byte-optimize nil) ; do it fast
4237 (byte-compile-warnings nil))
d686d354
SM
4238 (mapcar (lambda (x)
4239 (or noninteractive (message "compiling %s..." x))
4240 (byte-compile x)
4241 (or noninteractive (message "compiling %s...done" x)))
591655c7
SM
4242 '(byte-compile-normal-call
4243 byte-compile-form
4244 byte-compile-body
4245 ;; Inserted some more than necessary, to speed it up.
4246 byte-compile-top-level
4247 byte-compile-out-toplevel
4248 byte-compile-constant
4249 byte-compile-variable-ref))))
4250 nil)
fd5285f3 4251
3433c43f
DL
4252(run-hooks 'bytecomp-load-hook)
4253
977b50fb 4254;; arch-tag: 9c97b0f0-8745-4571-bfc3-8dceb677292a
fd5285f3 4255;;; bytecomp.el ends here