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