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