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