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