Merge from emacs-24; up to 2012-12-26T22:30:58Z!yamaoka@jpl.org
[bpt/emacs.git] / lisp / emacs-lisp / bytecomp.el
CommitLineData
9ace101c 1;;; bytecomp.el --- compilation of Lisp code into byte code -*- lexical-binding: t -*-
fd5285f3 2
ab422c4d
PE
3;; Copyright (C) 1985-1987, 1992, 1994, 1998, 2000-2013 Free Software
4;; 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
bd78fa1d 10;; Package: emacs
1c393159 11
1c393159
JB
12;; This file is part of GNU Emacs.
13
d6cba7ae 14;; GNU Emacs is free software: you can redistribute it and/or modify
1c393159 15;; it under the terms of the GNU General Public License as published by
d6cba7ae
GM
16;; the Free Software Foundation, either version 3 of the License, or
17;; (at your option) any later version.
1c393159
JB
18
19;; GNU Emacs is distributed in the hope that it will be useful,
20;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22;; GNU General Public License for more details.
23
24;; You should have received a copy of the GNU General Public License
d6cba7ae 25;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
1c393159 26
e41b2db1
ER
27;;; Commentary:
28
29;; The Emacs Lisp byte compiler. This crunches lisp source into a sort
a586093f
SM
30;; of p-code (`lapcode') which takes up less space and can be interpreted
31;; faster. [`LAP' == `Lisp Assembly Program'.]
e41b2db1
ER
32;; The user entry points are byte-compile-file and byte-recompile-directory.
33
fd5285f3
RS
34;;; Code:
35
b578f267
EN
36;; ========================================================================
37;; Entry points:
38;; byte-recompile-directory, byte-compile-file,
430e7297 39;; byte-recompile-file,
b578f267
EN
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
416d3588 71;; User customization variables: M-x customize-group bytecomp
b578f267
EN
72
73;; New Features:
74;;
75;; o The form `defsubst' is just like `defun', except that the function
76;; generated will be open-coded in compiled code which uses it. This
77;; means that no function call will be generated, it will simply be
78;; spliced in. Lisp functions calls are very slow, so this can be a
79;; big win.
80;;
81;; You can generally accomplish the same thing with `defmacro', but in
82;; that case, the defined procedure can't be used as an argument to
83;; mapcar, etc.
84;;
85;; o You can also open-code one particular call to a function without
86;; open-coding all calls. Use the 'inline' form to do this, like so:
87;;
88;; (inline (foo 1 2 3)) ;; `foo' will be open-coded
89;; or...
c5091f25 90;; (inline ;; `foo' and `baz' will be
b578f267
EN
91;; (foo 1 2 3 (bar 5)) ;; open-coded, but `bar' will not.
92;; (baz 0))
93;;
94;; o It is possible to open-code a function in the same file it is defined
6b61353c 95;; in without having to load that file before compiling it. The
b578f267
EN
96;; byte-compiler has been modified to remember function definitions in
97;; the compilation environment in the same way that it remembers macro
98;; definitions.
99;;
100;; o Forms like ((lambda ...) ...) are open-coded.
101;;
102;; o The form `eval-when-compile' is like progn, except that the body
103;; is evaluated at compile-time. When it appears at top-level, this
104;; is analogous to the Common Lisp idiom (eval-when (compile) ...).
105;; When it does not appear at top-level, it is similar to the
106;; Common Lisp #. reader macro (but not in interpreted code).
107;;
108;; o The form `eval-and-compile' is similar to eval-when-compile, but
109;; the whole form is evalled both at compile-time and at run-time.
110;;
111;; o The command compile-defun is analogous to eval-defun.
112;;
c5091f25 113;; o If you run byte-compile-file on a filename which is visited in a
b578f267
EN
114;; buffer, and that buffer is modified, you are asked whether you want
115;; to save the buffer before compiling.
116;;
117;; o byte-compiled files now start with the string `;ELC'.
118;; Some versions of `file' can be customized to recognize that.
1c393159 119
79d52eea 120(require 'backquote)
b9598260 121(require 'macroexp)
94d11cb5 122(require 'cconv)
f80efb86 123(eval-when-compile (require 'cl-lib))
79d52eea 124
1c393159
JB
125(or (fboundp 'defsubst)
126 ;; This really ought to be loaded already!
6c2161c4 127 (load "byte-run"))
1c393159 128
b9598260
SM
129;; The feature of compiling in a specific target Emacs version
130;; has been turned off because compile time options are a bad idea.
713ea1de 131(defgroup bytecomp nil
25d1fc94 132 "Emacs Lisp byte-compiler."
713ea1de
RS
133 :group 'lisp)
134
5692cc8c 135(defcustom emacs-lisp-file-regexp "\\.el\\'"
2b9c3b12 136 "Regexp which matches Emacs Lisp source files.
3f12e5bd 137If you change this, you might want to set `byte-compile-dest-file-function'."
713ea1de
RS
138 :group 'bytecomp
139 :type 'regexp)
1c393159 140
3f12e5bd
GM
141(defcustom byte-compile-dest-file-function nil
142 "Function for the function `byte-compile-dest-file' to call.
143It should take one argument, the name of an Emacs Lisp source
144file name, and return the name of the compiled file."
145 :group 'bytecomp
146 :type '(choice (const nil) function)
147 :version "23.2")
148
2140206e
RS
149;; This enables file name handlers such as jka-compr
150;; to remove parts of the file name that should not be copied
151;; through to the output file name.
152(defun byte-compiler-base-file-name (filename)
153 (let ((handler (find-file-name-handler filename
154 'byte-compiler-base-file-name)))
155 (if handler
156 (funcall handler 'byte-compiler-base-file-name filename)
157 filename)))
158
1c393159 159(or (fboundp 'byte-compile-dest-file)
e27c3564 160 ;; The user may want to redefine this along with emacs-lisp-file-regexp,
1c393159 161 ;; so only define it if it is undefined.
3f12e5bd
GM
162 ;; Note - redefining this function is obsolete as of 23.2.
163 ;; Customize byte-compile-dest-file-function instead.
1c393159 164 (defun byte-compile-dest-file (filename)
f9b4b5d8 165 "Convert an Emacs Lisp source file name to a compiled file name.
3f12e5bd
GM
166If `byte-compile-dest-file-function' is non-nil, uses that
167function to do the work. Otherwise, if FILENAME matches
168`emacs-lisp-file-regexp' (by default, files with the extension `.el'),
169adds `c' to it; otherwise adds `.elc'."
170 (if byte-compile-dest-file-function
171 (funcall byte-compile-dest-file-function filename)
172 (setq filename (file-name-sans-versions
173 (byte-compiler-base-file-name filename)))
174 (cond ((string-match emacs-lisp-file-regexp filename)
175 (concat (substring filename 0 (match-beginning 0)) ".elc"))
176 (t (concat filename ".elc"))))))
1c393159
JB
177
178;; This can be the 'byte-compile property of any symbol.
52799cb8 179(autoload 'byte-compile-inline-expand "byte-opt")
1c393159 180
e4769531 181;; This is the entry point to the lapcode optimizer pass1.
52799cb8 182(autoload 'byte-optimize-form "byte-opt")
e4769531 183;; This is the entry point to the lapcode optimizer pass2.
52799cb8
RS
184(autoload 'byte-optimize-lapcode "byte-opt")
185(autoload 'byte-compile-unfold-lambda "byte-opt")
1c393159 186
ed015bdd
JB
187;; This is the entry point to the decompiler, which is used by the
188;; disassembler. The disassembler just requires 'byte-compile, but
189;; that doesn't define this function, so this seems to be a reasonable
190;; thing to do.
191(autoload 'byte-decompile-bytecode "byte-opt")
192
713ea1de 193(defcustom byte-compile-verbose
1c393159 194 (and (not noninteractive) (> baud-rate search-slow-speed))
2b9c3b12 195 "Non-nil means print messages describing progress of byte-compiler."
713ea1de
RS
196 :group 'bytecomp
197 :type 'boolean)
1c393159 198
713ea1de 199(defcustom byte-optimize t
2b9c3b12 200 "Enable optimization in the byte compiler.
9bb2e9f8
JB
201Possible values are:
202 nil - no optimization
203 t - all optimizations
204 `source' - source-level optimizations only
205 `byte' - code-level optimizations only"
713ea1de
RS
206 :group 'bytecomp
207 :type '(choice (const :tag "none" nil)
208 (const :tag "all" t)
209 (const :tag "source-level" source)
210 (const :tag "byte-level" byte)))
211
cd91e34c 212(defcustom byte-compile-delete-errors nil
2b9c3b12 213 "If non-nil, the optimizer may delete forms that may signal an error.
713ea1de
RS
214This includes variable references and calls to functions such as `car'."
215 :group 'bytecomp
216 :type 'boolean)
1c393159 217
d82e848c 218(defvar byte-compile-dynamic nil
713ea1de 219 "If non-nil, compile function bodies so they load lazily.
458f70dc
RS
220They are hidden in comments in the compiled file,
221and each one is brought into core when the
d82e848c
RS
222function is called.
223
224To enable this option, make it a file-local variable
225in the source file you want it to apply to.
226For example, add -*-byte-compile-dynamic: t;-*- on the first line.
227
228When this option is true, if you load the compiled file and then move it,
229the functions you loaded will not be able to run.")
631c8020 230;;;###autoload(put 'byte-compile-dynamic 'safe-local-variable 'booleanp)
d82e848c 231
0e66b003
KH
232(defvar byte-compile-disable-print-circle nil
233 "If non-nil, disable `print-circle' on printing a byte-compiled code.")
53cfe624 234(make-obsolete-variable 'byte-compile-disable-print-circle nil "24.1")
0e66b003
KH
235;;;###autoload(put 'byte-compile-disable-print-circle 'safe-local-variable 'booleanp)
236
713ea1de 237(defcustom byte-compile-dynamic-docstrings t
2b9c3b12
JB
238 "If non-nil, compile doc strings for lazy access.
239We bury the doc strings of functions and variables inside comments in
240the file, and bring them into core only when they are actually needed.
d82e848c
RS
241
242When this option is true, if you load the compiled file and then move it,
243you won't be able to find the documentation of anything in that file.
244
1c660f5a
KH
245To disable this option for a certain file, make it a file-local variable
246in the source file. For example, add this to the first line:
247 -*-byte-compile-dynamic-docstrings:nil;-*-
248You can also set the variable globally.
249
713ea1de
RS
250This option is enabled by default because it reduces Emacs memory usage."
251 :group 'bytecomp
252 :type 'boolean)
631c8020 253;;;###autoload(put 'byte-compile-dynamic-docstrings 'safe-local-variable 'booleanp)
d82e848c 254
7847454a
GM
255(defconst byte-compile-log-buffer "*Compile-Log*"
256 "Name of the byte-compiler's log buffer.")
257
713ea1de 258(defcustom byte-optimize-log nil
7847454a 259 "If non-nil, the byte-compiler will log its optimizations.
1c393159 260If this is 'source, then only source-level optimizations will be logged.
7847454a
GM
261If it is 'byte, then only byte-level optimizations will be logged.
262The information is logged to `byte-compile-log-buffer'."
713ea1de
RS
263 :group 'bytecomp
264 :type '(choice (const :tag "none" nil)
265 (const :tag "all" t)
266 (const :tag "source-level" source)
267 (const :tag "byte-level" byte)))
268
269(defcustom byte-compile-error-on-warn nil
2b9c3b12 270 "If true, the byte-compiler reports warnings with `error'."
713ea1de
RS
271 :group 'bytecomp
272 :type 'boolean)
1c393159 273
9290191f 274(defconst byte-compile-warning-types
086af77c 275 '(redefine callargs free-vars unresolved
8accceac 276 obsolete noruntime cl-functions interactive-only
4f1e9960 277 make-local mapcar constants suspicious lexical)
4795d1c7 278 "The list of warning types used when `byte-compile-warnings' is t.")
713ea1de 279(defcustom byte-compile-warnings t
2b9c3b12 280 "List of warnings that the byte-compiler should issue (t for all).
4795d1c7 281
9bb2e9f8 282Elements of the list may be:
9e2b097b
JB
283
284 free-vars references to variables not in the current lexical scope.
285 unresolved calls to unknown functions.
11efeb9b
RS
286 callargs function calls with args that don't match the definition.
287 redefine function name redefined from a macro to ordinary function or vice
9e2b097b 288 versa, or redefined to take a different number of arguments.
4795d1c7
RS
289 obsolete obsolete variables and functions.
290 noruntime functions that may not be defined at runtime (typically
291 defined only under `eval-when-compile').
6b8c2efc 292 cl-functions calls to runtime functions from the CL package (as
086af77c
RS
293 distinguished from macros and aliases).
294 interactive-only
15ce9dcf 295 commands that normally shouldn't be called from Lisp code.
86da2828 296 make-local calls to make-variable-buffer-local that may be incorrect.
cf637a34 297 mapcar mapcar called for effect.
416d3588 298 constants let-binding of, or assignment to, constants/nonvariables.
62a258a7 299 suspicious constructs that usually don't do what the coder wanted.
cf637a34
GM
300
301If the list begins with `not', then the remaining elements specify warnings to
302suppress. For example, (not mapcar) will suppress warnings about mapcar."
713ea1de 303 :group 'bytecomp
4795d1c7 304 :type `(choice (const :tag "All" t)
aa635691 305 (set :menu-tag "Some"
62a258a7
SM
306 ,@(mapcar (lambda (x) `(const ,x))
307 byte-compile-warning-types))))
6a831405 308
0027258d 309;;;###autoload
acef0722
SM
310(put 'byte-compile-warnings 'safe-local-variable
311 (lambda (v)
312 (or (symbolp v)
313 (null (delq nil (mapcar (lambda (x) (not (symbolp x))) v))))))
086af77c 314
cf637a34
GM
315(defun byte-compile-warning-enabled-p (warning)
316 "Return non-nil if WARNING is enabled, according to `byte-compile-warnings'."
317 (or (eq byte-compile-warnings t)
318 (if (eq (car byte-compile-warnings) 'not)
319 (not (memq warning byte-compile-warnings))
320 (memq warning byte-compile-warnings))))
321
322;;;###autoload
323(defun byte-compile-disable-warning (warning)
324 "Change `byte-compile-warnings' to disable WARNING.
325If `byte-compile-warnings' is t, set it to `(not WARNING)'.
798bd437
GM
326Otherwise, if the first element is `not', add WARNING, else remove it.
327Normally you should let-bind `byte-compile-warnings' before calling this,
328else the global value will be modified."
cf637a34
GM
329 (setq byte-compile-warnings
330 (cond ((eq byte-compile-warnings t)
331 (list 'not warning))
332 ((eq (car byte-compile-warnings) 'not)
333 (if (memq warning byte-compile-warnings)
334 byte-compile-warnings
335 (append byte-compile-warnings (list warning))))
336 (t
337 (delq warning byte-compile-warnings)))))
338
339;;;###autoload
340(defun byte-compile-enable-warning (warning)
341 "Change `byte-compile-warnings' to enable WARNING.
342If `byte-compile-warnings' is `t', do nothing. Otherwise, if the
798bd437
GM
343first element is `not', remove WARNING, else add it.
344Normally you should let-bind `byte-compile-warnings' before calling this,
345else the global value will be modified."
cf637a34
GM
346 (or (eq byte-compile-warnings t)
347 (setq byte-compile-warnings
348 (cond ((eq (car byte-compile-warnings) 'not)
349 (delq warning byte-compile-warnings))
350 ((memq warning byte-compile-warnings)
351 byte-compile-warnings)
352 (t
353 (append byte-compile-warnings (list warning)))))))
354
086af77c
RS
355(defvar byte-compile-interactive-only-functions
356 '(beginning-of-buffer end-of-buffer replace-string replace-regexp
dfd4e693 357 insert-file insert-buffer insert-file-literally previous-line next-line
b68b3337 358 goto-line comint-run delete-backward-char)
086af77c 359 "List of commands that are not meant to be called from Lisp.")
1c393159 360
8480fc7c 361(defvar byte-compile-not-obsolete-vars nil
bac7ff22
SM
362 "List of variables that shouldn't be reported as obsolete.")
363(defvar byte-compile-global-not-obsolete-vars nil
364 "Global list of variables that shouldn't be reported as obsolete.")
8480fc7c
GM
365
366(defvar byte-compile-not-obsolete-funcs nil
bac7ff22 367 "List of functions that shouldn't be reported as obsolete.")
6b61353c 368
713ea1de 369(defcustom byte-compile-generate-call-tree nil
2b9c3b12 370 "Non-nil means collect call-graph information when compiling.
78bba1c8 371This records which functions were called and from where.
52799cb8
RS
372If the value is t, compilation displays the call graph when it finishes.
373If the value is neither t nor nil, compilation asks you whether to display
374the graph.
1c393159
JB
375
376The call tree only lists functions called, not macros used. Those functions
377which the byte-code interpreter knows about directly (eq, cons, etc.) are
378not reported.
379
380The call tree also lists those functions which are not known to be called
5023d9a0 381\(that is, to which no calls have been compiled). Functions which can be
713ea1de
RS
382invoked interactively are excluded from this list."
383 :group 'bytecomp
384 :type '(choice (const :tag "Yes" t) (const :tag "No" nil)
778c7576 385 (other :tag "Ask" lambda)))
1c393159 386
2b9c3b12
JB
387(defvar byte-compile-call-tree nil
388 "Alist of functions and their call tree.
1c393159
JB
389Each element looks like
390
391 \(FUNCTION CALLERS CALLS\)
392
393where CALLERS is a list of functions that call FUNCTION, and CALLS
394is a list of functions for which calls were generated while compiling
395FUNCTION.")
396
713ea1de 397(defcustom byte-compile-call-tree-sort 'name
2b9c3b12 398 "If non-nil, sort the call tree.
52799cb8 399The values `name', `callers', `calls', `calls+callers'
713ea1de
RS
400specify different fields to sort on."
401 :group 'bytecomp
402 :type '(choice (const name) (const callers) (const calls)
403 (const calls+callers) (const nil)))
52799cb8 404
ccb3c8de 405(defvar byte-compile-debug nil)
1c393159 406(defvar byte-compile-constants nil
a586093f 407 "List of all constants encountered during compilation of this form.")
1c393159 408(defvar byte-compile-variables nil
a586093f 409 "List of all variables encountered during compilation of this form.")
1c393159 410(defvar byte-compile-bound-variables nil
ce5b520a 411 "List of dynamic variables bound in the context of the current form.
b92dd692 412This list lives partly on the stack.")
6c2161c4
SM
413(defvar byte-compile-const-variables nil
414 "List of variables declared as constants during compilation of this file.")
1c393159
JB
415(defvar byte-compile-free-references)
416(defvar byte-compile-free-assignments)
417
ab94e6e7
RS
418(defvar byte-compiler-error-flag)
419
1c393159 420(defconst byte-compile-initial-macro-environment
52799cb8 421 '(
e703069f
SM
422 ;; (byte-compiler-options . (lambda (&rest forms)
423 ;; (apply 'byte-compiler-options-handler forms)))
a9de04fa 424 (declare-function . byte-compile-macroexpand-declare-function)
1c393159 425 (eval-when-compile . (lambda (&rest body)
b9598260
SM
426 (list
427 'quote
428 (byte-compile-eval
ba83908c 429 (byte-compile-top-level
2ec42da9 430 (byte-compile-preprocess (cons 'progn body)))))))
1c393159 431 (eval-and-compile . (lambda (&rest body)
e703069f
SM
432 ;; Byte compile before running it. Do it piece by
433 ;; piece, in case further expressions need earlier
434 ;; ones to be evaluated already, as is the case in
435 ;; eieio.el.
436 `(progn
437 ,@(mapcar (lambda (exp)
438 (let ((cexp
439 (byte-compile-top-level
440 (byte-compile-preprocess
441 exp))))
442 (eval cexp)
443 cexp))
444 body)))))
1c393159
JB
445 "The default macro-environment passed to macroexpand by the compiler.
446Placing a macro here will cause a macro to have different semantics when
447expanded by the compiler as when expanded by the interpreter.")
448
449(defvar byte-compile-macro-environment byte-compile-initial-macro-environment
52799cb8
RS
450 "Alist of macros defined in the file being compiled.
451Each element looks like (MACRONAME . DEFINITION). It is
e27c3564 452\(MACRONAME . nil) when a macro is redefined as a function.")
1c393159
JB
453
454(defvar byte-compile-function-environment nil
52799cb8
RS
455 "Alist of functions defined in the file being compiled.
456This is so we can inline them when necessary.
457Each element looks like (FUNCTIONNAME . DEFINITION). It is
a7a7ddf1
RS
458\(FUNCTIONNAME . nil) when a function is redefined as a macro.
459It is \(FUNCTIONNAME . t) when all we know is that it was defined,
cb4fb1d0
GM
460and we don't know the definition. For an autoloaded function, DEFINITION
461has the form (autoload . FILENAME).")
1c393159
JB
462
463(defvar byte-compile-unresolved-functions nil
a586093f 464 "Alist of undefined functions to which calls have been compiled.
2cb63a7c
AM
465This variable is only significant whilst compiling an entire buffer.
466Used for warnings when a function is not known to be defined or is later
a586093f 467defined with incorrect args.")
1c393159 468
6b61353c
KH
469(defvar byte-compile-noruntime-functions nil
470 "Alist of functions called that may not be defined when the compiled code is run.
471Used for warnings about calling a function that is defined during compilation
472but won't necessarily be defined when the compiled file is loaded.")
473
b9598260 474;; Variables for lexical binding
7200d79c 475(defvar byte-compile--lexical-environment nil
b9598260 476 "The current lexical environment.")
b9598260 477
1c393159
JB
478(defvar byte-compile-tag-number 0)
479(defvar byte-compile-output nil
480 "Alist describing contents to put in byte code string.
481Each element is (INDEX . VALUE)")
482(defvar byte-compile-depth 0 "Current depth of execution stack.")
483(defvar byte-compile-maxdepth 0 "Maximum depth of execution stack.")
484
485\f
486;;; The byte codes; this information is duplicated in bytecomp.c
487
fef3407e 488(defvar byte-code-vector nil
1c393159
JB
489 "An array containing byte-code names indexed by byte-code values.")
490
fef3407e 491(defvar byte-stack+-info nil
1c393159
JB
492 "An array with the stack adjustment for each byte-code.")
493
494(defmacro byte-defop (opcode stack-adjust opname &optional docstring)
495 ;; This is a speed-hack for building the byte-code-vector at compile-time.
496 ;; We fill in the vector at macroexpand-time, and then after the last call
497 ;; to byte-defop, we write the vector out as a constant instead of writing
498 ;; out a bunch of calls to aset.
499 ;; Actually, we don't fill in the vector itself, because that could make
500 ;; it problematic to compile big changes to this compiler; we store the
501 ;; values on its plist, and remove them later in -extrude.
502 (let ((v1 (or (get 'byte-code-vector 'tmp-compile-time-value)
503 (put 'byte-code-vector 'tmp-compile-time-value
504 (make-vector 256 nil))))
505 (v2 (or (get 'byte-stack+-info 'tmp-compile-time-value)
506 (put 'byte-stack+-info 'tmp-compile-time-value
507 (make-vector 256 nil)))))
508 (aset v1 opcode opname)
509 (aset v2 opcode stack-adjust))
510 (if docstring
511 (list 'defconst opname opcode (concat "Byte code opcode " docstring "."))
512 (list 'defconst opname opcode)))
513
514(defmacro byte-extrude-byte-code-vectors ()
515 (prog1 (list 'setq 'byte-code-vector
516 (get 'byte-code-vector 'tmp-compile-time-value)
517 'byte-stack+-info
518 (get 'byte-stack+-info 'tmp-compile-time-value))
fd9b0a6b
DL
519 (put 'byte-code-vector 'tmp-compile-time-value nil)
520 (put 'byte-stack+-info 'tmp-compile-time-value nil)))
1c393159
JB
521
522
1c393159
JB
523;; These opcodes are special in that they pack their argument into the
524;; opcode word.
525;;
b9598260 526(byte-defop 0 1 byte-stack-ref "for stack reference")
1c393159
JB
527(byte-defop 8 1 byte-varref "for variable reference")
528(byte-defop 16 -1 byte-varset "for setting a variable")
529(byte-defop 24 -1 byte-varbind "for binding a variable")
530(byte-defop 32 0 byte-call "for calling a function")
531(byte-defop 40 0 byte-unbind "for unbinding special bindings")
eb8c3be9 532;; codes 8-47 are consumed by the preceding opcodes
1c393159
JB
533
534;; unused: 48-55
535
536(byte-defop 56 -1 byte-nth)
537(byte-defop 57 0 byte-symbolp)
538(byte-defop 58 0 byte-consp)
539(byte-defop 59 0 byte-stringp)
540(byte-defop 60 0 byte-listp)
541(byte-defop 61 -1 byte-eq)
542(byte-defop 62 -1 byte-memq)
543(byte-defop 63 0 byte-not)
544(byte-defop 64 0 byte-car)
545(byte-defop 65 0 byte-cdr)
546(byte-defop 66 -1 byte-cons)
547(byte-defop 67 0 byte-list1)
548(byte-defop 68 -1 byte-list2)
549(byte-defop 69 -2 byte-list3)
550(byte-defop 70 -3 byte-list4)
551(byte-defop 71 0 byte-length)
552(byte-defop 72 -1 byte-aref)
553(byte-defop 73 -2 byte-aset)
554(byte-defop 74 0 byte-symbol-value)
555(byte-defop 75 0 byte-symbol-function) ; this was commented out
556(byte-defop 76 -1 byte-set)
557(byte-defop 77 -1 byte-fset) ; this was commented out
558(byte-defop 78 -1 byte-get)
559(byte-defop 79 -2 byte-substring)
560(byte-defop 80 -1 byte-concat2)
561(byte-defop 81 -2 byte-concat3)
562(byte-defop 82 -3 byte-concat4)
563(byte-defop 83 0 byte-sub1)
564(byte-defop 84 0 byte-add1)
565(byte-defop 85 -1 byte-eqlsign)
566(byte-defop 86 -1 byte-gtr)
567(byte-defop 87 -1 byte-lss)
568(byte-defop 88 -1 byte-leq)
569(byte-defop 89 -1 byte-geq)
570(byte-defop 90 -1 byte-diff)
571(byte-defop 91 0 byte-negate)
572(byte-defop 92 -1 byte-plus)
573(byte-defop 93 -1 byte-max)
574(byte-defop 94 -1 byte-min)
575(byte-defop 95 -1 byte-mult) ; v19 only
576(byte-defop 96 1 byte-point)
1c393159
JB
577(byte-defop 98 0 byte-goto-char)
578(byte-defop 99 0 byte-insert)
579(byte-defop 100 1 byte-point-max)
580(byte-defop 101 1 byte-point-min)
581(byte-defop 102 0 byte-char-after)
582(byte-defop 103 1 byte-following-char)
583(byte-defop 104 1 byte-preceding-char)
584(byte-defop 105 1 byte-current-column)
585(byte-defop 106 0 byte-indent-to)
586(byte-defop 107 0 byte-scan-buffer-OBSOLETE) ; no longer generated as of v18
587(byte-defop 108 1 byte-eolp)
588(byte-defop 109 1 byte-eobp)
589(byte-defop 110 1 byte-bolp)
590(byte-defop 111 1 byte-bobp)
591(byte-defop 112 1 byte-current-buffer)
592(byte-defop 113 0 byte-set-buffer)
78943c8a
RS
593(byte-defop 114 0 byte-save-current-buffer
594 "To make a binding to record the current buffer")
1c393159 595(byte-defop 115 0 byte-set-mark-OBSOLETE)
8b05752a 596(byte-defop 116 1 byte-interactive-p-OBSOLETE)
1c393159
JB
597
598;; These ops are new to v19
599(byte-defop 117 0 byte-forward-char)
600(byte-defop 118 0 byte-forward-word)
601(byte-defop 119 -1 byte-skip-chars-forward)
602(byte-defop 120 -1 byte-skip-chars-backward)
603(byte-defop 121 0 byte-forward-line)
604(byte-defop 122 0 byte-char-syntax)
605(byte-defop 123 -1 byte-buffer-substring)
606(byte-defop 124 -1 byte-delete-region)
607(byte-defop 125 -1 byte-narrow-to-region)
608(byte-defop 126 1 byte-widen)
609(byte-defop 127 0 byte-end-of-line)
610
611;; unused: 128
612
613;; These store their argument in the next two bytes
614(byte-defop 129 1 byte-constant2
615 "for reference to a constant with vector index >= byte-constant-limit")
616(byte-defop 130 0 byte-goto "for unconditional jump")
617(byte-defop 131 -1 byte-goto-if-nil "to pop value and jump if it's nil")
618(byte-defop 132 -1 byte-goto-if-not-nil "to pop value and jump if it's not nil")
619(byte-defop 133 -1 byte-goto-if-nil-else-pop
c5091f25 620 "to examine top-of-stack, jump and don't pop it if it's nil,
1c393159
JB
621otherwise pop it")
622(byte-defop 134 -1 byte-goto-if-not-nil-else-pop
c5091f25 623 "to examine top-of-stack, jump and don't pop it if it's non nil,
1c393159
JB
624otherwise pop it")
625
626(byte-defop 135 -1 byte-return "to pop a value and return it from `byte-code'")
627(byte-defop 136 -1 byte-discard "to discard one value from stack")
628(byte-defop 137 1 byte-dup "to duplicate the top of the stack")
629
630(byte-defop 138 0 byte-save-excursion
631 "to make a binding to record the buffer, point and mark")
8b05752a
SM
632(byte-defop 139 0 byte-save-window-excursion-OBSOLETE
633 "to make a binding to record entire window configuration")
1c393159
JB
634(byte-defop 140 0 byte-save-restriction
635 "to make a binding to record the current buffer clipping restrictions")
636(byte-defop 141 -1 byte-catch
637 "for catch. Takes, on stack, the tag and an expression for the body")
638(byte-defop 142 -1 byte-unwind-protect
639 "for unwind-protect. Takes, on stack, an expression for the unwind-action")
640
c5091f25 641;; For condition-case. Takes, on stack, the variable to bind,
52799cb8
RS
642;; an expression for the body, and a list of clauses.
643(byte-defop 143 -2 byte-condition-case)
1c393159 644
8b05752a
SM
645(byte-defop 144 0 byte-temp-output-buffer-setup-OBSOLETE)
646(byte-defop 145 -1 byte-temp-output-buffer-show-OBSOLETE)
1c393159
JB
647
648;; these ops are new to v19
52799cb8
RS
649
650;; To unbind back to the beginning of this frame.
69dc83fd 651;; Not used yet, but will be needed for tail-recursion elimination.
52799cb8 652(byte-defop 146 0 byte-unbind-all)
1c393159
JB
653
654;; these ops are new to v19
655(byte-defop 147 -2 byte-set-marker)
656(byte-defop 148 0 byte-match-beginning)
657(byte-defop 149 0 byte-match-end)
658(byte-defop 150 0 byte-upcase)
659(byte-defop 151 0 byte-downcase)
660(byte-defop 152 -1 byte-string=)
661(byte-defop 153 -1 byte-string<)
662(byte-defop 154 -1 byte-equal)
663(byte-defop 155 -1 byte-nthcdr)
664(byte-defop 156 -1 byte-elt)
665(byte-defop 157 -1 byte-member)
666(byte-defop 158 -1 byte-assq)
667(byte-defop 159 0 byte-nreverse)
668(byte-defop 160 -1 byte-setcar)
669(byte-defop 161 -1 byte-setcdr)
670(byte-defop 162 0 byte-car-safe)
671(byte-defop 163 0 byte-cdr-safe)
672(byte-defop 164 -1 byte-nconc)
673(byte-defop 165 -1 byte-quo)
674(byte-defop 166 -1 byte-rem)
675(byte-defop 167 0 byte-numberp)
676(byte-defop 168 0 byte-integerp)
677
3eac9910 678;; unused: 169-174
1c393159
JB
679(byte-defop 175 nil byte-listN)
680(byte-defop 176 nil byte-concatN)
681(byte-defop 177 nil byte-insertN)
682
7200d79c
SM
683(byte-defop 178 -1 byte-stack-set) ; Stack offset in following one byte.
684(byte-defop 179 -1 byte-stack-set2) ; Stack offset in following two bytes.
b9598260 685
7200d79c 686;; If (following one byte & 0x80) == 0
b9598260
SM
687;; discard (following one byte & 0x7F) stack entries
688;; else
2ec42da9 689;; discard (following one byte & 0x7F) stack entries _underneath_ TOS
b9598260
SM
690;; (that is, if the operand = 0x83, ... X Y Z T => ... T)
691(byte-defop 182 nil byte-discardN)
692;; `byte-discardN-preserve-tos' is a pseudo-op that gets turned into
693;; `byte-discardN' with the high bit in the operand set (by
694;; `byte-compile-lapcode').
695(defconst byte-discardN-preserve-tos byte-discardN)
696
697;; unused: 182-191
1c393159
JB
698
699(byte-defop 192 1 byte-constant "for reference to a constant")
700;; codes 193-255 are consumed by byte-constant.
701(defconst byte-constant-limit 64
702 "Exclusive maximum index usable in the `byte-constant' opcode.")
703
704(defconst byte-goto-ops '(byte-goto byte-goto-if-nil byte-goto-if-not-nil
705 byte-goto-if-nil-else-pop
706 byte-goto-if-not-nil-else-pop)
52799cb8 707 "List of byte-codes whose offset is a pc.")
1c393159
JB
708
709(defconst byte-goto-always-pop-ops '(byte-goto-if-nil byte-goto-if-not-nil))
710
1c393159
JB
711(byte-extrude-byte-code-vectors)
712\f
713;;; lapcode generator
3614fc84
GM
714;;
715;; the byte-compiler now does source -> lapcode -> bytecode instead of
716;; source -> bytecode, because it's a lot easier to make optimizations
717;; on lapcode than on bytecode.
718;;
719;; Elements of the lapcode list are of the form (<instruction> . <parameter>)
720;; where instruction is a symbol naming a byte-code instruction,
721;; and parameter is an argument to that instruction, if any.
722;;
723;; The instruction can be the pseudo-op TAG, which means that this position
724;; in the instruction stream is a target of a goto. (car PARAMETER) will be
725;; the PC for this location, and the whole instruction "(TAG pc)" will be the
726;; parameter for some goto op.
727;;
728;; If the operation is varbind, varref, varset or push-constant, then the
729;; parameter is (variable/constant . index_in_constant_vector).
730;;
731;; First, the source code is macroexpanded and optimized in various ways.
732;; Then the resultant code is compiled into lapcode. Another set of
733;; optimizations are then run over the lapcode. Then the variables and
734;; constants referenced by the lapcode are collected and placed in the
735;; constants-vector. (This happens now so that variables referenced by dead
736;; code don't consume space.) And finally, the lapcode is transformed into
737;; compacted byte-code.
738;;
739;; A distinction is made between variables and constants because the variable-
740;; referencing instructions are more sensitive to the variables being near the
741;; front of the constants-vector than the constant-referencing instructions.
742;; Also, this lets us notice references to free variables.
1c393159 743
b9598260 744(defmacro byte-compile-push-bytecodes (&rest args)
e703069f
SM
745 "Push bytes onto BVAR, and increment CVAR by the number of bytes pushed.
746BVAR and CVAR are variables which are updated after evaluating
747all the arguments.
748
749\(fn BYTE1 BYTE2 ... BYTEn BVAR CVAR)"
b9598260
SM
750 (let ((byte-exprs (butlast args 2))
751 (bytes-var (car (last args 2)))
752 (pc-var (car (last args))))
753 `(setq ,bytes-var ,(if (null (cdr byte-exprs))
f80efb86 754 `(progn (cl-assert (<= 0 ,(car byte-exprs)))
b38b1ec0
SM
755 (cons ,@byte-exprs ,bytes-var))
756 `(nconc (list ,@(reverse byte-exprs)) ,bytes-var))
757 ,pc-var (+ ,(length byte-exprs) ,pc-var))))
b9598260
SM
758
759(defmacro byte-compile-push-bytecode-const2 (opcode const2 bytes pc)
760 "Push OPCODE and the two-byte constant CONST2 onto BYTES, and add 3 to PC.
4c36be58 761CONST2 may be evaluated multiple times."
b9598260
SM
762 `(byte-compile-push-bytecodes ,opcode (logand ,const2 255) (lsh ,const2 -8)
763 ,bytes ,pc))
764
1c393159
JB
765(defun byte-compile-lapcode (lap)
766 "Turns lapcode into bytecode. The lapcode is destroyed."
767 ;; Lapcode modifications: changes the ID of a tag to be the tag's PC.
768 (let ((pc 0) ; Program counter
769 op off ; Operation & offset
b9598260 770 opcode ; numeric value of OP
1c393159 771 (bytes '()) ; Put the output bytes here
b9598260
SM
772 (patchlist nil)) ; List of gotos to patch
773 (dolist (lap-entry lap)
774 (setq op (car lap-entry)
775 off (cdr lap-entry))
ce5b520a
SM
776 (cond
777 ((not (symbolp op))
778 (error "Non-symbolic opcode `%s'" op))
779 ((eq op 'TAG)
780 (setcar off pc))
ce5b520a
SM
781 (t
782 (setq opcode
783 (if (eq op 'byte-discardN-preserve-tos)
784 ;; byte-discardN-preserve-tos is a pseudo op, which
785 ;; is actually the same as byte-discardN
786 ;; with a modified argument.
787 byte-discardN
788 (symbol-value op)))
789 (cond ((memq op byte-goto-ops)
790 ;; goto
791 (byte-compile-push-bytecodes opcode nil (cdr off) bytes pc)
7200d79c 792 (push bytes patchlist))
876c194c
SM
793 ((or (and (consp off)
794 ;; Variable or constant reference
795 (progn
796 (setq off (cdr off))
797 (eq op 'byte-constant)))
7200d79c 798 (and (eq op 'byte-constant)
876c194c 799 (integerp off)))
ce5b520a
SM
800 ;; constant ref
801 (if (< off byte-constant-limit)
802 (byte-compile-push-bytecodes (+ byte-constant off)
803 bytes pc)
804 (byte-compile-push-bytecode-const2 byte-constant2 off
805 bytes pc)))
806 ((and (= opcode byte-stack-set)
807 (> off 255))
808 ;; Use the two-byte version of byte-stack-set if the
809 ;; offset is too large for the normal version.
810 (byte-compile-push-bytecode-const2 byte-stack-set2 off
811 bytes pc))
812 ((and (>= opcode byte-listN)
813 (< opcode byte-discardN))
814 ;; These insns all put their operand into one extra byte.
815 (byte-compile-push-bytecodes opcode off bytes pc))
816 ((= opcode byte-discardN)
b38b1ec0 817 ;; byte-discardN is weird in that it encodes a flag in the
ce5b520a
SM
818 ;; top bit of its one-byte argument. If the argument is
819 ;; too large to fit in 7 bits, the opcode can be repeated.
820 (let ((flag (if (eq op 'byte-discardN-preserve-tos) #x80 0)))
821 (while (> off #x7f)
2ec42da9
SM
822 (byte-compile-push-bytecodes opcode (logior #x7f flag)
823 bytes pc)
ce5b520a 824 (setq off (- off #x7f)))
2ec42da9
SM
825 (byte-compile-push-bytecodes opcode (logior off flag)
826 bytes pc)))
ce5b520a
SM
827 ((null off)
828 ;; opcode that doesn't use OFF
829 (byte-compile-push-bytecodes opcode bytes pc))
3e21b6a7
SM
830 ((and (eq opcode byte-stack-ref) (eq off 0))
831 ;; (stack-ref 0) is really just another name for `dup'.
832 (debug) ;FIXME: When would this happen?
833 (byte-compile-push-bytecodes byte-dup bytes pc))
ce5b520a
SM
834 ;; The following three cases are for the special
835 ;; insns that encode their operand into 0, 1, or 2
836 ;; extra bytes depending on its magnitude.
837 ((< off 6)
838 (byte-compile-push-bytecodes (+ opcode off) bytes pc))
839 ((< off 256)
840 (byte-compile-push-bytecodes (+ opcode 6) off bytes pc))
841 (t
842 (byte-compile-push-bytecode-const2 (+ opcode 7) off
843 bytes pc))))))
1c393159 844 ;;(if (not (= pc (length bytes)))
52799cb8 845 ;; (error "Compiler error: pc mismatch - %s %s" pc (length bytes)))
7200d79c 846 ;; Patch tag PCs into absolute jumps.
b9598260 847 (dolist (bytes-tail patchlist)
7200d79c 848 (setq pc (caar bytes-tail)) ; Pick PC from goto's tag.
b9598260
SM
849 (setcar (cdr bytes-tail) (logand pc 255))
850 (setcar bytes-tail (lsh pc -8))
851 ;; FIXME: Replace this by some workaround.
d9bbf400 852 (if (> (car bytes-tail) 255) (error "Bytecode overflow")))
b9598260 853
da9e269f 854 (apply 'unibyte-string (nreverse bytes))))
1c393159
JB
855
856\f
a586093f
SM
857;;; compile-time evaluation
858
3f12e5bd
GM
859(defun byte-compile-cl-file-p (file)
860 "Return non-nil if FILE is one of the CL files."
861 (and (stringp file)
e60b51ab 862 (string-match "^cl\\.el" (file-name-nondirectory file))))
3f12e5bd 863
ea4b0ca3
SM
864(defun byte-compile-eval (form)
865 "Eval FORM and mark the functions defined therein.
6b61353c 866Each function's symbol gets added to `byte-compile-noruntime-functions'."
a586093f
SM
867 (let ((hist-orig load-history)
868 (hist-nil-orig current-load-list))
2ec42da9 869 (prog1 (eval form lexical-binding)
cf637a34 870 (when (byte-compile-warning-enabled-p 'noruntime)
a586093f
SM
871 (let ((hist-new load-history)
872 (hist-nil-new current-load-list))
ea4b0ca3
SM
873 ;; Go through load-history, look for newly loaded files
874 ;; and mark all the functions defined therein.
875 (while (and hist-new (not (eq hist-new hist-orig)))
d1a57439
RS
876 (let ((xs (pop hist-new))
877 old-autoloads)
ea4b0ca3 878 ;; Make sure the file was not already loaded before.
3a6851d9 879 (unless (assoc (car xs) hist-orig)
ea4b0ca3
SM
880 (dolist (s xs)
881 (cond
d1a57439 882 ((and (consp s) (eq t (car s)))
6c2161c4 883 (push (cdr s) old-autoloads))
5253a5fd
SM
884 ((and (consp s) (memq (car s) '(autoload defun)))
885 (unless (memq (cdr s) old-autoloads)
886 (push (cdr s) byte-compile-noruntime-functions))))))))
ea4b0ca3 887 ;; Go through current-load-list for the locally defined funs.
d1a57439
RS
888 (let (old-autoloads)
889 (while (and hist-nil-new (not (eq hist-nil-new hist-nil-orig)))
890 (let ((s (pop hist-nil-new)))
891 (when (and (symbolp s) (not (memq s old-autoloads)))
6b61353c 892 (push s byte-compile-noruntime-functions))
d1a57439 893 (when (and (consp s) (eq t (car s)))
997011eb 894 (push (cdr s) old-autoloads)))))))
cf637a34 895 (when (byte-compile-warning-enabled-p 'cl-functions)
b8104a2b 896 (let ((hist-new load-history))
3f12e5bd
GM
897 ;; Go through load-history, looking for the cl files.
898 ;; Since new files are added at the start of load-history,
899 ;; we scan the new history until the tail matches the old.
900 (while (and (not byte-compile-cl-functions)
901 hist-new (not (eq hist-new hist-orig)))
902 ;; We used to check if the file had already been loaded,
903 ;; but it is better to check non-nil byte-compile-cl-functions.
904 (and (byte-compile-cl-file-p (car (pop hist-new)))
905 (byte-compile-find-cl-functions))))))))
a586093f 906
4795d1c7
RS
907(defun byte-compile-eval-before-compile (form)
908 "Evaluate FORM for `eval-and-compile'."
909 (let ((hist-nil-orig current-load-list))
2ec42da9 910 (prog1 (eval form lexical-binding)
4795d1c7 911 ;; (eval-and-compile (require 'cl) turns off warnings for cl functions.
3f12e5bd
GM
912 ;; FIXME Why does it do that - just as a hack?
913 ;; There are other ways to do this nowadays.
4795d1c7
RS
914 (let ((tem current-load-list))
915 (while (not (eq tem hist-nil-orig))
916 (when (equal (car tem) '(require . cl))
cf637a34 917 (byte-compile-disable-warning 'cl-functions))
4795d1c7 918 (setq tem (cdr tem)))))))
a586093f 919\f
1c393159
JB
920;;; byte compiler messages
921
d82e848c 922(defvar byte-compile-current-form nil)
d82e848c 923(defvar byte-compile-dest-file nil)
9985d391 924(defvar byte-compile-current-file nil)
ab5111e3 925(defvar byte-compile-current-group nil)
ccb3c8de 926(defvar byte-compile-current-buffer nil)
6a619620 927
22788fb8 928;; Log something that isn't a warning.
1c393159 929(defmacro byte-compile-log (format-string &rest args)
b88a41d0
SM
930 `(and
931 byte-optimize
932 (memq byte-optimize-log '(t source))
933 (let ((print-escape-newlines t)
934 (print-level 4)
935 (print-length 4))
936 (byte-compile-log-1
937 (format
938 ,format-string
939 ,@(mapcar
940 (lambda (x) (if (symbolp x) (list 'prin1-to-string x) x))
941 args))))))
1c393159 942
22788fb8
RS
943;; Log something that isn't a warning.
944(defun byte-compile-log-1 (string)
7847454a 945 (with-current-buffer byte-compile-log-buffer
997011eb
RS
946 (let ((inhibit-read-only t))
947 (goto-char (point-max))
948 (byte-compile-warning-prefix nil nil)
949 (cond (noninteractive
950 (message " %s" string))
951 (t
952 (insert (format "%s\n" string)))))))
1c393159 953
ccb3c8de
CW
954(defvar byte-compile-read-position nil
955 "Character position we began the last `read' from.")
956(defvar byte-compile-last-position nil
957 "Last known character position in the input.")
958
959;; copied from gnus-util.el
1fd592a0 960(defsubst byte-compile-delete-first (elt list)
ccb3c8de
CW
961 (if (eq (car list) elt)
962 (cdr list)
963 (let ((total list))
964 (while (and (cdr list)
965 (not (eq (cadr list) elt)))
966 (setq list (cdr list)))
967 (when (cdr list)
968 (setcdr list (cddr list)))
969 total)))
970
971;; The purpose of this function is to iterate through the
972;; `read-symbol-positions-list'. Each time we process, say, a
973;; function definition (`defun') we remove `defun' from
974;; `read-symbol-positions-list', and set `byte-compile-last-position'
975;; to that symbol's character position. Similarly, if we encounter a
976;; variable reference, like in (1+ foo), we remove `foo' from the
977;; list. If our current position is after the symbol's position, we
978;; assume we've already passed that point, and look for the next
6b8c2efc 979;; occurrence of the symbol.
4ec5239c
LH
980;;
981;; This function should not be called twice for the same occurrence of
982;; a symbol, and it should not be called for symbols generated by the
983;; byte compiler itself; because rather than just fail looking up the
984;; symbol, we may find an occurrence of the symbol further ahead, and
985;; then `byte-compile-last-position' as advanced too far.
986;;
6b8c2efc 987;; So your're probably asking yourself: Isn't this function a
ccb3c8de
CW
988;; gross hack? And the answer, of course, would be yes.
989(defun byte-compile-set-symbol-position (sym &optional allow-previous)
990 (when byte-compile-read-position
1fd592a0 991 (let (last entry)
ccb3c8de 992 (while (progn
0b46acbf
RS
993 (setq last byte-compile-last-position
994 entry (assq sym read-symbol-positions-list))
995 (when entry
996 (setq byte-compile-last-position
997 (+ byte-compile-read-position (cdr entry))
998 read-symbol-positions-list
999 (byte-compile-delete-first
1000 entry read-symbol-positions-list)))
2ec42da9
SM
1001 (or (and allow-previous
1002 (not (= last byte-compile-last-position)))
ccb3c8de 1003 (> last byte-compile-last-position)))))))
b8175fe6 1004
22788fb8
RS
1005(defvar byte-compile-last-warned-form nil)
1006(defvar byte-compile-last-logged-file nil)
3858bfe7
SM
1007(defvar byte-compile-root-dir nil
1008 "Directory relative to which file names in error messages are written.")
22788fb8 1009
375e49d4
SM
1010;; FIXME: We should maybe extend abbreviate-file-name with an optional DIR
1011;; argument to try and use a relative file-name.
1012(defun byte-compile-abbreviate-file (file &optional dir)
1013 (let ((f1 (abbreviate-file-name file))
1014 (f2 (file-relative-name file dir)))
1015 (if (< (length f2) (length f1)) f2 f1)))
1016
22788fb8 1017;; This is used as warning-prefix for the compiler.
4390021b 1018;; It is always called with the warnings buffer current.
22788fb8 1019(defun byte-compile-warning-prefix (level entry)
997011eb 1020 (let* ((inhibit-read-only t)
3858bfe7 1021 (dir (or byte-compile-root-dir default-directory))
4390021b 1022 (file (cond ((stringp byte-compile-current-file)
375e49d4 1023 (format "%s:" (byte-compile-abbreviate-file
2ec42da9 1024 byte-compile-current-file dir)))
b8175fe6 1025 ((bufferp byte-compile-current-file)
1f006824 1026 (format "Buffer %s:"
b8175fe6 1027 (buffer-name byte-compile-current-file)))
2168fe4f
GM
1028 ;; We might be simply loading a file that
1029 ;; contains explicit calls to byte-compile functions.
1030 ((stringp load-file-name)
375e49d4
SM
1031 (format "%s:" (byte-compile-abbreviate-file
1032 load-file-name dir)))
b8175fe6 1033 (t "")))
1f006824 1034 (pos (if (and byte-compile-current-file
ccb3c8de
CW
1035 (integerp byte-compile-read-position))
1036 (with-current-buffer byte-compile-current-buffer
ea1cb2bd 1037 (format "%d:%d:"
b0aa2c65
RS
1038 (save-excursion
1039 (goto-char byte-compile-last-position)
1040 (1+ (count-lines (point-min) (point-at-bol))))
ccb3c8de
CW
1041 (save-excursion
1042 (goto-char byte-compile-last-position)
1043 (1+ (current-column)))))
b8175fe6 1044 ""))
4390021b
RS
1045 (form (if (eq byte-compile-current-form :end) "end of data"
1046 (or byte-compile-current-form "toplevel form"))))
1047 (when (or (and byte-compile-current-file
1048 (not (equal byte-compile-current-file
1049 byte-compile-last-logged-file)))
6b61353c 1050 (and byte-compile-current-form
4390021b
RS
1051 (not (eq byte-compile-current-form
1052 byte-compile-last-warned-form))))
22788fb8 1053 (insert (format "\nIn %s:\n" form)))
4390021b
RS
1054 (when level
1055 (insert (format "%s%s" file pos))))
cb3069bb 1056 (setq byte-compile-last-logged-file byte-compile-current-file
22788fb8
RS
1057 byte-compile-last-warned-form byte-compile-current-form)
1058 entry)
1c393159 1059
4390021b
RS
1060;; This no-op function is used as the value of warning-series
1061;; to tell inner calls to displaying-byte-compile-warnings
1062;; not to bind warning-series.
9ace101c 1063(defun byte-compile-warning-series (&rest _ignore)
4390021b
RS
1064 nil)
1065
db283402 1066;; (compile-mode) will cause this to be loaded.
2c52d7a3 1067(declare-function compilation-forget-errors "compile" ())
db283402 1068
7847454a 1069;; Log the start of a file in `byte-compile-log-buffer', and mark it as done.
22788fb8 1070;; Return the position of the start of the page in the log buffer.
144b2637
RS
1071;; But do nothing in batch mode.
1072(defun byte-compile-log-file ()
4390021b 1073 (and (not (equal byte-compile-current-file byte-compile-last-logged-file))
cb3069bb 1074 (not noninteractive)
7847454a 1075 (with-current-buffer (get-buffer-create byte-compile-log-buffer)
ca96ae0b 1076 (goto-char (point-max))
997011eb
RS
1077 (let* ((inhibit-read-only t)
1078 (dir (and byte-compile-current-file
4390021b
RS
1079 (file-name-directory byte-compile-current-file)))
1080 (was-same (equal default-directory dir))
1081 pt)
1082 (when dir
1083 (unless was-same
1084 (insert (format "Leaving directory `%s'\n" default-directory))))
1085 (unless (bolp)
1086 (insert "\n"))
1087 (setq pt (point-marker))
1088 (if byte-compile-current-file
1089 (insert "\f\nCompiling "
1090 (if (stringp byte-compile-current-file)
1091 (concat "file " byte-compile-current-file)
2ec42da9
SM
1092 (concat "buffer "
1093 (buffer-name byte-compile-current-file)))
4390021b
RS
1094 " at " (current-time-string) "\n")
1095 (insert "\f\nCompiling no file at " (current-time-string) "\n"))
1096 (when dir
1097 (setq default-directory dir)
1098 (unless was-same
2ec42da9
SM
1099 (insert (format "Entering directory `%s'\n"
1100 default-directory))))
6b61353c
KH
1101 (setq byte-compile-last-logged-file byte-compile-current-file
1102 byte-compile-last-warned-form nil)
977f31f8 1103 ;; Do this after setting default-directory.
5c7ffa04 1104 (unless (derived-mode-p 'compilation-mode) (compilation-mode))
b88a41d0 1105 (compilation-forget-errors)
22788fb8
RS
1106 pt))))
1107
7847454a 1108;; Log a message STRING in `byte-compile-log-buffer'.
22788fb8
RS
1109;; Also log the current function and file if not already done.
1110(defun byte-compile-log-warning (string &optional fill level)
1111 (let ((warning-prefix-function 'byte-compile-warning-prefix)
6b61353c 1112 (warning-type-format "")
e703069f 1113 (warning-fill-prefix (if fill " ")))
7847454a 1114 (display-warning 'bytecomp string level byte-compile-log-buffer)))
144b2637 1115
1c393159 1116(defun byte-compile-warn (format &rest args)
22788fb8 1117 "Issue a byte compiler warning; use (format FORMAT ARGS...) for message."
1c393159
JB
1118 (setq format (apply 'format format args))
1119 (if byte-compile-error-on-warn
1120 (error "%s" format) ; byte-compile-file catches and logs it
22788fb8
RS
1121 (byte-compile-log-warning format t :warning)))
1122
5791bedf
GM
1123(defun byte-compile-warn-obsolete (symbol)
1124 "Warn that SYMBOL (a variable or function) is obsolete."
1125 (when (byte-compile-warning-enabled-p 'obsolete)
1126 (let* ((funcp (get symbol 'byte-obsolete-info))
95b9712e
SM
1127 (msg (macroexp--obsolete-warning
1128 symbol
1129 (or funcp (get symbol 'byte-obsolete-variable))
1130 (if funcp "function" "variable"))))
8480fc7c 1131 (unless (and funcp (memq symbol byte-compile-not-obsolete-funcs))
95b9712e 1132 (byte-compile-warn "%s" msg)))))
5791bedf 1133
0b030df7 1134(defun byte-compile-report-error (error-info)
22788fb8 1135 "Report Lisp error in compilation. ERROR-INFO is the error data."
ab94e6e7 1136 (setq byte-compiler-error-flag t)
22788fb8
RS
1137 (byte-compile-log-warning
1138 (error-message-string error-info)
1139 nil :error))
1c393159 1140\f
1c393159
JB
1141;;; sanity-checking arglists
1142
1143(defun byte-compile-fdefinition (name macro-p)
ced10a4c
SM
1144 ;; If a function has an entry saying (FUNCTION . t).
1145 ;; that means we know it is defined but we don't know how.
1146 ;; If a function has an entry saying (FUNCTION . nil),
1147 ;; that means treat it as not defined.
1c393159
JB
1148 (let* ((list (if macro-p
1149 byte-compile-macro-environment
5286a842 1150 byte-compile-function-environment))
1c393159
JB
1151 (env (cdr (assq name list))))
1152 (or env
1153 (let ((fn name))
1154 (while (and (symbolp fn)
1155 (fboundp fn)
1156 (or (symbolp (symbol-function fn))
1157 (consp (symbol-function fn))
1158 (and (not macro-p)
ed015bdd 1159 (byte-code-function-p (symbol-function fn)))))
1c393159 1160 (setq fn (symbol-function fn)))
9d28c33e
SM
1161 (let ((advertised (gethash (if (and (symbolp fn) (fboundp fn))
1162 ;; Could be a subr.
1163 (symbol-function fn)
1164 fn)
1165 advertised-signature-table t)))
ced10a4c
SM
1166 (cond
1167 ((listp advertised)
1168 (if macro-p
1169 `(macro lambda ,advertised)
1170 `(lambda ,advertised)))
1171 ((and (not macro-p) (byte-code-function-p fn)) fn)
1172 ((not (consp fn)) nil)
1173 ((eq 'macro (car fn)) (cdr fn))
1174 (macro-p nil)
1175 ((eq 'autoload (car fn)) nil)
1176 (t fn)))))))
1c393159
JB
1177
1178(defun byte-compile-arglist-signature (arglist)
61b108cc
SM
1179 (cond
1180 ;; New style byte-code arglist.
1181 ((integerp arglist)
1182 (cons (logand arglist 127) ;Mandatory.
1183 (if (zerop (logand arglist 128)) ;No &rest.
1184 (lsh arglist -8)))) ;Nonrest.
1185 ;; Old style byte-code, or interpreted function.
1186 ((listp arglist)
e2abe5a1
SM
1187 (let ((args 0)
1188 opts
1189 restp)
1190 (while arglist
1191 (cond ((eq (car arglist) '&optional)
1192 (or opts (setq opts 0)))
1193 ((eq (car arglist) '&rest)
1194 (if (cdr arglist)
1195 (setq restp t
1196 arglist nil)))
1197 (t
1198 (if opts
1199 (setq opts (1+ opts))
1c393159 1200 (setq args (1+ args)))))
e2abe5a1 1201 (setq arglist (cdr arglist)))
61b108cc
SM
1202 (cons args (if restp nil (if opts (+ args opts) args)))))
1203 ;; Unknown arglist.
1204 (t '(0))))
1c393159
JB
1205
1206
1207(defun byte-compile-arglist-signatures-congruent-p (old new)
1208 (not (or
1209 (> (car new) (car old)) ; requires more args now
a7acbbe4 1210 (and (null (cdr old)) ; took rest-args, doesn't any more
1c393159
JB
1211 (cdr new))
1212 (and (cdr new) (cdr old) ; can't take as many args now
1213 (< (cdr new) (cdr old)))
1214 )))
1215
1216(defun byte-compile-arglist-signature-string (signature)
1217 (cond ((null (cdr signature))
1218 (format "%d+" (car signature)))
1219 ((= (car signature) (cdr signature))
1220 (format "%d" (car signature)))
1221 (t (format "%d-%d" (car signature) (cdr signature)))))
1222
1223
52799cb8 1224;; Warn if the form is calling a function with the wrong number of arguments.
1c393159 1225(defun byte-compile-callargs-warn (form)
1c393159
JB
1226 (let* ((def (or (byte-compile-fdefinition (car form) nil)
1227 (byte-compile-fdefinition (car form) t)))
a7a7ddf1 1228 (sig (if (and def (not (eq def t)))
416d3588
GM
1229 (progn
1230 (and (eq (car-safe def) 'macro)
1231 (eq (car-safe (cdr-safe def)) 'lambda)
1232 (setq def (cdr def)))
1233 (byte-compile-arglist-signature
1234 (if (memq (car-safe def) '(declared lambda))
1235 (nth 1 def)
1236 (if (byte-code-function-p def)
1237 (aref def 0)
1238 '(&rest def)))))
ed62683d
DL
1239 (if (and (fboundp (car form))
1240 (subrp (symbol-function (car form))))
1241 (subr-arity (symbol-function (car form))))))
1c393159 1242 (ncall (length (cdr form))))
ed62683d
DL
1243 ;; Check many or unevalled from subr-arity.
1244 (if (and (cdr-safe sig)
1245 (not (numberp (cdr sig))))
1246 (setcdr sig nil))
1c393159 1247 (if sig
ccb3c8de 1248 (when (or (< ncall (car sig))
1c393159 1249 (and (cdr sig) (> ncall (cdr sig))))
ccb3c8de
CW
1250 (byte-compile-set-symbol-position (car form))
1251 (byte-compile-warn
1252 "%s called with %d argument%s, but %s %s"
1253 (car form) ncall
1254 (if (= 1 ncall) "" "s")
1255 (if (< ncall (car sig))
1256 "requires"
1257 "accepts only")
ba76e7fa 1258 (byte-compile-arglist-signature-string sig))))
6b61353c 1259 (byte-compile-format-warn form)
ba76e7fa
SM
1260 ;; Check to see if the function will be available at runtime
1261 ;; and/or remember its arity if it's unknown.
a7a7ddf1 1262 (or (and (or def (fboundp (car form))) ; might be a subr or autoload.
6b61353c 1263 (not (memq (car form) byte-compile-noruntime-functions)))
61b108cc
SM
1264 (eq (car form) byte-compile-current-form) ; ## This doesn't work
1265 ; with recursion.
ba76e7fa
SM
1266 ;; It's a currently-undefined function.
1267 ;; Remember number of args in call.
1268 (let ((cons (assq (car form) byte-compile-unresolved-functions))
1269 (n (length (cdr form))))
1270 (if cons
1271 (or (memq n (cdr cons))
1272 (setcdr cons (cons n (cdr cons))))
977b50fb
SM
1273 (push (list (car form) n)
1274 byte-compile-unresolved-functions))))))
1c393159 1275
6b61353c
KH
1276(defun byte-compile-format-warn (form)
1277 "Warn if FORM is `format'-like with inconsistent args.
1278Applies if head of FORM is a symbol with non-nil property
1279`byte-compile-format-like' and first arg is a constant string.
1280Then check the number of format fields matches the number of
1281extra args."
1282 (when (and (symbolp (car form))
1283 (stringp (nth 1 form))
1284 (get (car form) 'byte-compile-format-like))
1285 (let ((nfields (with-temp-buffer
1286 (insert (nth 1 form))
b8104a2b 1287 (goto-char (point-min))
6b61353c
KH
1288 (let ((n 0))
1289 (while (re-search-forward "%." nil t)
1290 (unless (eq ?% (char-after (1+ (match-beginning 0))))
1291 (setq n (1+ n))))
1292 n)))
1293 (nargs (- (length form) 2)))
1294 (unless (= nargs nfields)
1295 (byte-compile-warn
1296 "`%s' called with %d args to fill %d format field(s)" (car form)
1297 nargs nfields)))))
1298
1299(dolist (elt '(format message error))
1300 (put elt 'byte-compile-format-like t))
1301
11efeb9b
RS
1302;; Warn if a custom definition fails to specify :group.
1303(defun byte-compile-nogroup-warn (form)
ab5111e3
SM
1304 (if (and (memq (car form) '(custom-declare-face custom-declare-variable))
1305 byte-compile-current-group)
1306 ;; The group will be provided implicitly.
1307 nil
1308 (let ((keyword-args (cdr (cdr (cdr (cdr form)))))
1309 (name (cadr form)))
1310 (or (not (eq (car-safe name) 'quote))
3ab6a7ae
SM
1311 (and (eq (car form) 'custom-declare-group)
1312 (equal name ''emacs))
1313 (plist-get keyword-args :group)
1314 (not (and (consp name) (eq (car name) 'quote)))
1315 (byte-compile-warn
1316 "%s for `%s' fails to specify containing group"
1317 (cdr (assq (car form)
ab5111e3
SM
1318 '((custom-declare-group . defgroup)
1319 (custom-declare-face . defface)
1320 (custom-declare-variable . defcustom))))
1321 (cadr name)))
1322 ;; Update the current group, if needed.
2ec42da9 1323 (if (and byte-compile-current-file ;Only when compiling a whole file.
ab5111e3
SM
1324 (eq (car form) 'custom-declare-group)
1325 (eq (car-safe name) 'quote))
1326 (setq byte-compile-current-group (cadr name))))))
11efeb9b 1327
52799cb8
RS
1328;; Warn if the function or macro is being redefined with a different
1329;; number of arguments.
61b108cc
SM
1330(defun byte-compile-arglist-warn (name arglist macrop)
1331 (let* ((old (byte-compile-fdefinition name macrop))
7d3b9d44
GM
1332 (initial (and macrop
1333 (cdr (assq name
1334 byte-compile-initial-macro-environment)))))
1335 ;; Assumes an element of b-c-i-macro-env that is a symbol points
1336 ;; to a defined function. (Bug#8646)
1337 (and initial (symbolp initial)
410c42c5 1338 (setq old (byte-compile-fdefinition initial nil)))
a7a7ddf1 1339 (if (and old (not (eq old t)))
416d3588
GM
1340 (progn
1341 (and (eq 'macro (car-safe old))
1342 (eq 'lambda (car-safe (cdr-safe old)))
1343 (setq old (cdr old)))
1344 (let ((sig1 (byte-compile-arglist-signature
b38b1ec0
SM
1345 (pcase old
1346 (`(lambda ,args . ,_) args)
23aba0ea 1347 (`(closure ,_ ,args . ,_) args)
b38b1ec0
SM
1348 ((pred byte-code-function-p) (aref old 0))
1349 (t '(&rest def)))))
61b108cc 1350 (sig2 (byte-compile-arglist-signature arglist)))
416d3588 1351 (unless (byte-compile-arglist-signatures-congruent-p sig1 sig2)
a9de04fa 1352 (byte-compile-set-symbol-position name)
416d3588
GM
1353 (byte-compile-warn
1354 "%s %s used to take %s %s, now takes %s"
61b108cc 1355 (if macrop "macro" "function")
a9de04fa 1356 name
416d3588
GM
1357 (byte-compile-arglist-signature-string sig1)
1358 (if (equal sig1 '(1 . 1)) "argument" "arguments")
1359 (byte-compile-arglist-signature-string sig2)))))
1c393159 1360 ;; This is the first definition. See if previous calls are compatible.
a9de04fa 1361 (let ((calls (assq name byte-compile-unresolved-functions))
1c393159 1362 nums sig min max)
a9de04fa
SM
1363 (when calls
1364 (when (and (symbolp name)
7abaf5cc 1365 (eq (function-get name 'byte-optimizer)
a9de04fa
SM
1366 'byte-compile-inline-expand))
1367 (byte-compile-warn "defsubst `%s' was used before it was defined"
1368 name))
61b108cc 1369 (setq sig (byte-compile-arglist-signature arglist)
a9de04fa
SM
1370 nums (sort (copy-sequence (cdr calls)) (function <))
1371 min (car nums)
1372 max (car (nreverse nums)))
1373 (when (or (< min (car sig))
1374 (and (cdr sig) (> max (cdr sig))))
1375 (byte-compile-set-symbol-position name)
1376 (byte-compile-warn
1377 "%s being defined to take %s%s, but was previously called with %s"
1378 name
1379 (byte-compile-arglist-signature-string sig)
1380 (if (equal sig '(1 . 1)) " arg" " args")
1381 (byte-compile-arglist-signature-string (cons min max))))
1382
1383 (setq byte-compile-unresolved-functions
1384 (delq calls byte-compile-unresolved-functions)))))))
1c393159 1385
95c997fa
RS
1386(defvar byte-compile-cl-functions nil
1387 "List of functions defined in CL.")
1388
3f12e5bd
GM
1389;; Can't just add this to cl-load-hook, because that runs just before
1390;; the forms from cl.el get added to load-history.
95c997fa
RS
1391(defun byte-compile-find-cl-functions ()
1392 (unless byte-compile-cl-functions
1393 (dolist (elt load-history)
3f12e5bd
GM
1394 (and (byte-compile-cl-file-p (car elt))
1395 (dolist (e (cdr elt))
1396 ;; Includes the cl-foo functions that cl autoloads.
1397 (when (memq (car-safe e) '(autoload defun))
1398 (push (cdr e) byte-compile-cl-functions)))))))
95c997fa 1399
4795d1c7
RS
1400(defun byte-compile-cl-warn (form)
1401 "Warn if FORM is a call of a function from the CL package."
95c997fa
RS
1402 (let ((func (car-safe form)))
1403 (if (and byte-compile-cl-functions
1404 (memq func byte-compile-cl-functions)
6b61353c 1405 ;; Aliases which won't have been expanded at this point.
4795d1c7
RS
1406 ;; These aren't all aliases of subrs, so not trivial to
1407 ;; avoid hardwiring the list.
1408 (not (memq func
bb3faf5b 1409 '(cl--block-wrapper cl--block-throw
9cb9a7bc 1410 multiple-value-call nth-value
95c997fa 1411 copy-seq first second rest endp cl-member
d1a57439
RS
1412 ;; These are included in generated code
1413 ;; that can't be called except at compile time
1414 ;; or unless cl is loaded anyway.
bb3faf5b 1415 cl--defsubst-expand cl-struct-setf-expander
8f876842
RS
1416 ;; These would sometimes be warned about
1417 ;; but such warnings are never useful,
1418 ;; so don't warn about them.
118861df 1419 macroexpand cl-macroexpand-all
bb3faf5b 1420 cl--compiling-file))))
7a16788b 1421 (byte-compile-warn "function `%s' from cl package called at runtime"
4795d1c7
RS
1422 func)))
1423 form)
1424
a586093f 1425(defun byte-compile-print-syms (str1 strn syms)
ccb3c8de
CW
1426 (when syms
1427 (byte-compile-set-symbol-position (car syms) t))
b8175fe6
GM
1428 (cond ((and (cdr syms) (not noninteractive))
1429 (let* ((str strn)
1430 (L (length str))
1431 s)
1432 (while syms
1433 (setq s (symbol-name (pop syms))
1434 L (+ L (length s) 2))
1435 (if (< L (1- fill-column))
1436 (setq str (concat str " " s (and syms ",")))
1437 (setq str (concat str "\n " s (and syms ","))
1438 L (+ (length s) 4))))
1439 (byte-compile-warn "%s" str)))
1440 ((cdr syms)
1f006824 1441 (byte-compile-warn "%s %s"
b8175fe6
GM
1442 strn
1443 (mapconcat #'symbol-name syms ", ")))
1444
1445 (syms
1446 (byte-compile-warn str1 (car syms)))))
a586093f 1447
1f006824 1448;; If we have compiled any calls to functions which are not known to be
52799cb8
RS
1449;; defined, issue a warning enumerating them.
1450;; `unresolved' in the list `byte-compile-warnings' disables this.
1c393159 1451(defun byte-compile-warn-about-unresolved-functions ()
cf637a34 1452 (when (byte-compile-warning-enabled-p 'unresolved)
b8175fe6 1453 (let ((byte-compile-current-form :end)
a586093f
SM
1454 (noruntime nil)
1455 (unresolved nil))
1456 ;; Separate the functions that will not be available at runtime
1457 ;; from the truly unresolved ones.
1458 (dolist (f byte-compile-unresolved-functions)
1459 (setq f (car f))
1460 (if (fboundp f) (push f noruntime) (push f unresolved)))
1461 ;; Complain about the no-run-time functions
1462 (byte-compile-print-syms
b8175fe6
GM
1463 "the function `%s' might not be defined at runtime."
1464 "the following functions might not be defined at runtime:"
a586093f
SM
1465 noruntime)
1466 ;; Complain about the unresolved functions
1467 (byte-compile-print-syms
b8175fe6
GM
1468 "the function `%s' is not known to be defined."
1469 "the following functions are not known to be defined:"
a586093f 1470 unresolved)))
1c393159
JB
1471 nil)
1472
1473\f
6876a58d
SM
1474;; Dynamically bound in byte-compile-from-buffer.
1475;; NB also used in cl.el and cl-macs.el.
1476(defvar byte-compile--outbuffer)
1477
1c393159 1478(defmacro byte-compile-close-variables (&rest body)
876c194c 1479 (declare (debug t))
6876a58d
SM
1480 `(let (;;
1481 ;; Close over these variables to encapsulate the
1482 ;; compilation state
1483 ;;
1484 (byte-compile-macro-environment
1485 ;; Copy it because the compiler may patch into the
1486 ;; macroenvironment.
1487 (copy-alist byte-compile-initial-macro-environment))
1488 (byte-compile--outbuffer nil)
1489 (byte-compile-function-environment nil)
1490 (byte-compile-bound-variables nil)
1491 (byte-compile-const-variables nil)
1492 (byte-compile-free-references nil)
1493 (byte-compile-free-assignments nil)
1494 ;;
1495 ;; Close over these variables so that `byte-compiler-options'
1496 ;; can change them on a per-file basis.
1497 ;;
1498 (byte-compile-verbose byte-compile-verbose)
1499 (byte-optimize byte-optimize)
1500 (byte-compile-dynamic byte-compile-dynamic)
1501 (byte-compile-dynamic-docstrings
1502 byte-compile-dynamic-docstrings)
1503 ;; (byte-compile-generate-emacs19-bytecodes
1504 ;; byte-compile-generate-emacs19-bytecodes)
1505 (byte-compile-warnings byte-compile-warnings)
1506 )
1507 ,@body))
1c393159 1508
1c393159 1509(defmacro displaying-byte-compile-warnings (&rest body)
876c194c 1510 (declare (debug t))
4390021b
RS
1511 `(let* ((--displaying-byte-compile-warnings-fn (lambda () ,@body))
1512 (warning-series-started
1513 (and (markerp warning-series)
1514 (eq (marker-buffer warning-series)
7847454a 1515 (get-buffer byte-compile-log-buffer)))))
95c997fa 1516 (byte-compile-find-cl-functions)
4390021b
RS
1517 (if (or (eq warning-series 'byte-compile-warning-series)
1518 warning-series-started)
1519 ;; warning-series does come from compilation,
1520 ;; so don't bind it, but maybe do set it.
1521 (let (tem)
1522 ;; Log the file name. Record position of that text.
1523 (setq tem (byte-compile-log-file))
1524 (unless warning-series-started
1525 (setq warning-series (or tem 'byte-compile-warning-series)))
1526 (if byte-compile-debug
1527 (funcall --displaying-byte-compile-warnings-fn)
1528 (condition-case error-info
1529 (funcall --displaying-byte-compile-warnings-fn)
1530 (error (byte-compile-report-error error-info)))))
1531 ;; warning-series does not come from compilation, so bind it.
1532 (let ((warning-series
1533 ;; Log the file name. Record position of that text.
1534 (or (byte-compile-log-file) 'byte-compile-warning-series)))
1535 (if byte-compile-debug
22788fb8 1536 (funcall --displaying-byte-compile-warnings-fn)
4390021b
RS
1537 (condition-case error-info
1538 (funcall --displaying-byte-compile-warnings-fn)
1539 (error (byte-compile-report-error error-info))))))))
1c393159 1540\f
fd5285f3 1541;;;###autoload
9742dbc0
RS
1542(defun byte-force-recompile (directory)
1543 "Recompile every `.el' file in DIRECTORY that already has a `.elc' file.
1544Files in subdirectories of DIRECTORY are processed also."
c0f43df5 1545 (interactive "DByte force recompile (directory): ")
9742dbc0
RS
1546 (byte-recompile-directory directory nil t))
1547
1548;;;###autoload
ca105506
SM
1549(defun byte-recompile-directory (directory &optional arg force)
1550 "Recompile every `.el' file in DIRECTORY that needs recompilation.
2b9c3b12 1551This happens when a `.elc' file exists but is older than the `.el' file.
ca105506 1552Files in subdirectories of DIRECTORY are processed also.
1c393159 1553
c4f2cabd 1554If the `.elc' file does not exist, normally this function *does not*
1c3b663f 1555compile the corresponding `.el' file. However, if the prefix argument
ca105506
SM
1556ARG is 0, that means do compile all those files. A nonzero
1557ARG means ask the user, for each such `.el' file, whether to
1558compile it. A nonzero ARG also means ask about each subdirectory
1c3b663f
GM
1559before scanning it.
1560
ca105506 1561If the third argument FORCE is non-nil, recompile every `.el' file
1c3b663f 1562that already has a `.elc' file."
1c393159 1563 (interactive "DByte recompile directory: \nP")
ca105506 1564 (if arg (setq arg (prefix-numeric-value arg)))
e27c3564
JB
1565 (if noninteractive
1566 nil
1567 (save-some-buffers)
ba901388 1568 (force-mode-line-update))
7847454a 1569 (with-current-buffer (get-buffer-create byte-compile-log-buffer)
ca105506 1570 (setq default-directory (expand-file-name directory))
977f31f8
RS
1571 ;; compilation-mode copies value of default-directory.
1572 (unless (eq major-mode 'compilation-mode)
1573 (compilation-mode))
ca105506 1574 (let ((directories (list default-directory))
4eb4926c
RS
1575 (default-directory default-directory)
1576 (skip-count 0)
1577 (fail-count 0)
1578 (file-count 0)
1579 (dir-count 0)
1580 last-dir)
1581 (displaying-byte-compile-warnings
ca105506
SM
1582 (while directories
1583 (setq directory (car directories))
1584 (message "Checking %s..." directory)
1585 (dolist (file (directory-files directory))
1586 (let ((source (expand-file-name file directory)))
1587 (if (and (not (member file '("RCS" "CVS")))
1588 (not (eq ?\. (aref file 0)))
1589 (file-directory-p source)
1590 (not (file-symlink-p source)))
1591 ;; This file is a subdirectory. Handle them differently.
1592 (when (or (null arg) (eq 0 arg)
1593 (y-or-n-p (concat "Check " source "? ")))
1594 (setq directories (nconc directories (list source))))
1595 ;; It is an ordinary file. Decide whether to compile it.
1596 (if (and (string-match emacs-lisp-file-regexp source)
343a2aef 1597 ;; The next 2 tests avoid compiling lock files
ca105506 1598 (file-readable-p source)
343a2aef 1599 (not (string-match "\\`\\.#" file))
ca105506
SM
1600 (not (auto-save-file-name-p source))
1601 (not (string-equal dir-locals-file
1602 (file-name-nondirectory source))))
56bc1586 1603 (progn (cl-incf
f58e0fd5
SM
1604 (pcase (byte-recompile-file source force arg)
1605 (`no-byte-compile skip-count)
1606 (`t file-count)
1607 (_ fail-count)))
ca105506
SM
1608 (or noninteractive
1609 (message "Checking %s..." directory))
1610 (if (not (eq last-dir directory))
1611 (setq last-dir directory
1612 dir-count (1+ dir-count)))
1613 )))))
1614 (setq directories (cdr directories))))
4eb4926c
RS
1615 (message "Done (Total of %d file%s compiled%s%s%s)"
1616 file-count (if (= file-count 1) "" "s")
1617 (if (> fail-count 0) (format ", %d failed" fail-count) "")
1618 (if (> skip-count 0) (format ", %d skipped" skip-count) "")
1c3b663f
GM
1619 (if (> dir-count 1)
1620 (format " in %d directories" dir-count) "")))))
1c393159 1621
fef3407e 1622(defvar no-byte-compile nil
2b9c3b12 1623 "Non-nil to prevent byte-compiling of Emacs Lisp code.
fef3407e
SM
1624This is normally set in local file variables at the end of the elisp file:
1625
ce5b520a 1626\;; Local Variables:\n;; no-byte-compile: t\n;; End: ") ;Backslash for compile-main.
631c8020 1627;;;###autoload(put 'no-byte-compile 'safe-local-variable 'booleanp)
fef3407e 1628
ca105506
SM
1629(defun byte-recompile-file (filename &optional force arg load)
1630 "Recompile FILENAME file if it needs recompilation.
430e7297
JD
1631This happens when its `.elc' file is older than itself.
1632
3c3cda1a
GM
1633If the `.elc' file exists and is up-to-date, normally this function
1634*does not* compile FILENAME. If the prefix argument FORCE is non-nil,
1635however, it compiles FILENAME even if the destination already
1636exists and is up-to-date.
430e7297 1637
3c3cda1a
GM
1638If the `.elc' file does not exist, normally this function *does not*
1639compile FILENAME. If optional argument ARG is 0, it compiles
1640the input file even if the `.elc' file does not exist.
1641Any other non-nil value of ARG means to ask the user.
430e7297 1642
3c3cda1a 1643If optional argument LOAD is non-nil, loads the file after compiling.
430e7297 1644
3c3cda1a
GM
1645If compilation is needed, this functions returns the result of
1646`byte-compile-file'; otherwise it returns 'no-byte-compile."
430e7297 1647 (interactive
ca105506
SM
1648 (let ((file buffer-file-name)
1649 (file-name nil)
1650 (file-dir nil))
1651 (and file
1652 (derived-mode-p 'emacs-lisp-mode)
1653 (setq file-name (file-name-nondirectory file)
1654 file-dir (file-name-directory file)))
430e7297
JD
1655 (list (read-file-name (if current-prefix-arg
1656 "Byte compile file: "
1657 "Byte recompile file: ")
ca105506 1658 file-dir file-name nil)
430e7297 1659 current-prefix-arg)))
ca105506 1660 (let ((dest (byte-compile-dest-file filename))
430e7297 1661 ;; Expand now so we get the current buffer's defaults
ca105506
SM
1662 (filename (expand-file-name filename)))
1663 (if (if (file-exists-p dest)
430e7297
JD
1664 ;; File was already compiled
1665 ;; Compile if forced to, or filename newer
ca105506
SM
1666 (or force
1667 (file-newer-than-file-p filename dest))
1668 (and arg
1669 (or (eq 0 arg)
fa14dc18 1670 (y-or-n-p (concat "Compile "
ca105506 1671 filename "? ")))))
430e7297
JD
1672 (progn
1673 (if (and noninteractive (not byte-compile-verbose))
ca105506
SM
1674 (message "Compiling %s..." filename))
1675 (byte-compile-file filename load))
e5b19827
AS
1676 (when load
1677 (load (if (file-exists-p dest) dest filename)))
430e7297
JD
1678 'no-byte-compile)))
1679
fd743628
GM
1680(defvar byte-compile-level 0 ; bug#13787
1681 "Depth of a recursive byte compilation.")
1682
fd5285f3 1683;;;###autoload
ca105506
SM
1684(defun byte-compile-file (filename &optional load)
1685 "Compile a file of Lisp code named FILENAME into a file of byte code.
1686The output file's name is generated by passing FILENAME to the
f279aaab 1687function `byte-compile-dest-file' (which see).
3614fc84 1688With prefix arg (noninteractively: 2nd arg), LOAD the file after compiling.
d90a41e8 1689The value is non-nil if there were no errors, nil if errors."
1c393159
JB
1690;; (interactive "fByte compile file: \nP")
1691 (interactive
ca105506
SM
1692 (let ((file buffer-file-name)
1693 (file-name nil)
1694 (file-dir nil))
1695 (and file
9ace101c 1696 (derived-mode-p 'emacs-lisp-mode)
ca105506
SM
1697 (setq file-name (file-name-nondirectory file)
1698 file-dir (file-name-directory file)))
52799cb8
RS
1699 (list (read-file-name (if current-prefix-arg
1700 "Byte compile and load file: "
1701 "Byte compile file: ")
ca105506 1702 file-dir file-name nil)
fd5285f3 1703 current-prefix-arg)))
1c393159 1704 ;; Expand now so we get the current buffer's defaults
ca105506 1705 (setq filename (expand-file-name filename))
1c393159
JB
1706
1707 ;; If we're compiling a file that's in a buffer and is modified, offer
1708 ;; to save it first.
1709 (or noninteractive
ca105506 1710 (let ((b (get-file-buffer (expand-file-name filename))))
1c393159 1711 (if (and b (buffer-modified-p b)
a586093f 1712 (y-or-n-p (format "Save buffer %s first? " (buffer-name b))))
008e2c2a 1713 (with-current-buffer b (save-buffer)))))
1c393159 1714
4390021b
RS
1715 ;; Force logging of the file name for each file compiled.
1716 (setq byte-compile-last-logged-file nil)
ca105506 1717 (let ((byte-compile-current-file filename)
ab5111e3 1718 (byte-compile-current-group nil)
dc14ae36 1719 (set-auto-coding-for-load t)
d82e848c
RS
1720 target-file input-buffer output-buffer
1721 byte-compile-dest-file)
ca105506 1722 (setq target-file (byte-compile-dest-file filename))
d82e848c 1723 (setq byte-compile-dest-file target-file)
ea1cb2bd 1724 (with-current-buffer
fd743628
GM
1725 ;; It would be cleaner to use a temp buffer, but if there was
1726 ;; an error, we leave this buffer around for diagnostics.
1727 ;; Its name is documented in the lispref.
1728 (setq input-buffer (get-buffer-create
1729 (concat " *Compiler Input*"
1730 (if (zerop byte-compile-level) ""
1731 (format "-%s" byte-compile-level)))))
1c393159 1732 (erase-buffer)
7a28e3b1 1733 (setq buffer-file-coding-system nil)
746dd298 1734 ;; Always compile an Emacs Lisp file as multibyte
b92dd692 1735 ;; unless the file itself forces unibyte with -*-coding: raw-text;-*-
746dd298 1736 (set-buffer-multibyte t)
ca105506 1737 (insert-file-contents filename)
844da0ff
KH
1738 ;; Mimic the way after-insert-file-set-coding can make the
1739 ;; buffer unibyte when visiting this file.
7a28e3b1
RS
1740 (when (or (eq last-coding-system-used 'no-conversion)
1741 (eq (coding-system-type last-coding-system-used) 5))
1742 ;; For coding systems no-conversion and raw-text...,
1743 ;; edit the buffer as unibyte.
1744 (set-buffer-multibyte nil))
1c393159
JB
1745 ;; Run hooks including the uncompression hook.
1746 ;; If they change the file name, then change it for the output also.
2ee3d7f0
SM
1747 (let ((buffer-file-name filename)
1748 (dmm (default-value 'major-mode))
1749 ;; Ignore unsafe local variables.
1750 ;; We only care about a few of them for our purposes.
1751 (enable-local-variables :safe)
1752 (enable-local-eval nil))
1753 (unwind-protect
1754 (progn
1755 (setq-default major-mode 'emacs-lisp-mode)
1756 ;; Arg of t means don't alter enable-local-variables.
1757 (normal-mode t))
1758 (setq-default major-mode dmm))
7d5944b9
GM
1759 ;; There may be a file local variable setting (bug#10419).
1760 (setq buffer-read-only nil
1761 filename buffer-file-name))
002c019c
SM
1762 ;; Don't inherit lexical-binding from caller (bug#12938).
1763 (unless (local-variable-p 'lexical-binding)
1764 (setq-local lexical-binding nil))
cd891e68 1765 ;; Set the default directory, in case an eval-when-compile uses it.
ca105506 1766 (setq default-directory (file-name-directory filename)))
3614fc84
GM
1767 ;; Check if the file's local variables explicitly specify not to
1768 ;; compile this file.
fef3407e 1769 (if (with-current-buffer input-buffer no-byte-compile)
3614fc84 1770 (progn
6b61353c 1771 ;; (message "%s not compiled because of `no-byte-compile: %s'"
375e49d4 1772 ;; (byte-compile-abbreviate-file filename)
6b61353c
KH
1773 ;; (with-current-buffer input-buffer no-byte-compile))
1774 (when (file-exists-p target-file)
1775 (message "%s deleted because of `no-byte-compile: %s'"
375e49d4 1776 (byte-compile-abbreviate-file target-file)
6b61353c
KH
1777 (buffer-local-value 'no-byte-compile input-buffer))
1778 (condition-case nil (delete-file target-file) (error nil)))
82345a9a 1779 ;; We successfully didn't compile this file.
d90a41e8 1780 'no-byte-compile)
ccb3c8de 1781 (when byte-compile-verbose
ca105506 1782 (message "Compiling %s..." filename))
82345a9a
SM
1783 (setq byte-compiler-error-flag nil)
1784 ;; It is important that input-buffer not be current at this call,
1785 ;; so that the value of point set in input-buffer
1786 ;; within byte-compile-from-buffer lingers in that buffer.
4f6d5bf0
SM
1787 (setq output-buffer
1788 (save-current-buffer
4c514b0f
SM
1789 (let ((byte-compile-level (1+ byte-compile-level)))
1790 (byte-compile-from-buffer input-buffer))))
82345a9a
SM
1791 (if byte-compiler-error-flag
1792 nil
ccb3c8de 1793 (when byte-compile-verbose
ca105506 1794 (message "Compiling %s...done" filename))
82345a9a
SM
1795 (kill-buffer input-buffer)
1796 (with-current-buffer output-buffer
1797 (goto-char (point-max))
1798 (insert "\n") ; aaah, unix.
94d4c7dc
AS
1799 (if (file-writable-p target-file)
1800 ;; We must disable any code conversion here.
1801 (let* ((coding-system-for-write 'no-conversion)
1802 ;; Write to a tempfile so that if another Emacs
1803 ;; process is trying to load target-file (eg in a
1804 ;; parallel bootstrap), it does not risk getting a
1805 ;; half-finished file. (Bug#4196)
1806 (tempfile (make-temp-name target-file))
1807 (kill-emacs-hook
1808 (cons (lambda () (ignore-errors (delete-file tempfile)))
1809 kill-emacs-hook)))
94d4c7dc
AS
1810 (write-region (point-min) (point-max) tempfile nil 1)
1811 ;; This has the intentional side effect that any
1812 ;; hard-links to target-file continue to
1813 ;; point to the old file (this makes it possible
1814 ;; for installed files to share disk space with
1815 ;; the build tree, without causing problems when
1816 ;; emacs-lisp files in the build tree are
1817 ;; recompiled). Previously this was accomplished by
1818 ;; deleting target-file before writing it.
1819 (rename-file tempfile target-file t)
1820 (message "Wrote %s" target-file))
1821 ;; This is just to give a better error message than write-region
1822 (signal 'file-error
1823 (list "Opening output file"
1824 (if (file-exists-p target-file)
1825 "cannot overwrite file"
1826 "directory not writable or nonexistent")
1827 target-file)))
82345a9a
SM
1828 (kill-buffer (current-buffer)))
1829 (if (and byte-compile-generate-call-tree
1830 (or (eq t byte-compile-generate-call-tree)
1c3b663f 1831 (y-or-n-p (format "Report call tree for %s? "
ca105506 1832 filename))))
82345a9a 1833 (save-excursion
ca105506 1834 (display-call-tree filename)))
82345a9a
SM
1835 (if load
1836 (load target-file))
1837 t))))
1c393159 1838
1c393159 1839;;; compiling a single function
fd5285f3 1840;;;###autoload
52799cb8 1841(defun compile-defun (&optional arg)
1c393159 1842 "Compile and evaluate the current top-level form.
6b61353c 1843Print the result in the echo area.
2b9c3b12 1844With argument ARG, insert value in current buffer after the form."
1c393159
JB
1845 (interactive "P")
1846 (save-excursion
1847 (end-of-defun)
1848 (beginning-of-defun)
1849 (let* ((byte-compile-current-file nil)
ccb3c8de
CW
1850 (byte-compile-current-buffer (current-buffer))
1851 (byte-compile-read-position (point))
1852 (byte-compile-last-position byte-compile-read-position)
1c393159 1853 (byte-compile-last-warned-form 'nothing)
ccb3c8de 1854 (value (eval
9cb9a7bc 1855 (let ((read-with-symbol-positions (current-buffer))
ccb3c8de
CW
1856 (read-symbol-positions-list nil))
1857 (displaying-byte-compile-warnings
2ec42da9
SM
1858 (byte-compile-sexp (read (current-buffer)))))
1859 lexical-binding)))
1c393159
JB
1860 (cond (arg
1861 (message "Compiling from buffer... done.")
1862 (prin1 value (current-buffer))
1863 (insert "\n"))
1864 ((message "%s" (prin1-to-string value)))))))
1865
ca105506 1866(defun byte-compile-from-buffer (inbuffer)
6876a58d 1867 (let ((byte-compile-current-buffer inbuffer)
ccb3c8de
CW
1868 (byte-compile-read-position nil)
1869 (byte-compile-last-position nil)
d82e848c
RS
1870 ;; Prevent truncation of flonums and lists as we read and print them
1871 (float-output-format nil)
1872 (case-fold-search nil)
1873 (print-length nil)
95e7d933 1874 (print-level nil)
74dfd056
RS
1875 ;; Prevent edebug from interfering when we compile
1876 ;; and put the output into a file.
ccb3c8de
CW
1877;; (edebug-all-defs nil)
1878;; (edebug-all-forms nil)
d82e848c
RS
1879 ;; Simulate entry to byte-compile-top-level
1880 (byte-compile-constants nil)
1881 (byte-compile-variables nil)
1882 (byte-compile-tag-number 0)
1883 (byte-compile-depth 0)
1884 (byte-compile-maxdepth 0)
1885 (byte-compile-output nil)
ccb3c8de 1886 ;; This allows us to get the positions of symbols read; it's
bf247b6e 1887 ;; new in Emacs 22.1.
ca105506 1888 (read-with-symbol-positions inbuffer)
ccb3c8de 1889 (read-symbol-positions-list nil)
d82e848c 1890 ;; #### This is bound in b-c-close-variables.
cf637a34 1891 ;; (byte-compile-warnings byte-compile-warnings)
d82e848c
RS
1892 )
1893 (byte-compile-close-variables
b8104a2b 1894 (with-current-buffer
7200d79c 1895 (setq byte-compile--outbuffer
fd743628
GM
1896 (get-buffer-create
1897 (concat " *Compiler Output*"
1898 (if (<= byte-compile-level 1) ""
1899 (format "-%s" (1- byte-compile-level))))))
08b59cd3 1900 (set-buffer-multibyte t)
d82e848c
RS
1901 (erase-buffer)
1902 ;; (emacs-lisp-mode)
96bcef2e 1903 (setq case-fold-search nil))
d82e848c 1904 (displaying-byte-compile-warnings
ca105506 1905 (with-current-buffer inbuffer
9ace101c
SM
1906 (and byte-compile-current-file
1907 (byte-compile-insert-header byte-compile-current-file
7200d79c 1908 byte-compile--outbuffer))
b8104a2b 1909 (goto-char (point-min))
2cb63a7c
AM
1910 ;; Should we always do this? When calling multiple files, it
1911 ;; would be useful to delay this warning until all have been
1912 ;; compiled. A: Yes! b-c-u-f might contain dross from a
1913 ;; previous byte-compile.
1914 (setq byte-compile-unresolved-functions nil)
d82e848c
RS
1915
1916 ;; Compile the forms from the input buffer.
1917 (while (progn
1918 (while (progn (skip-chars-forward " \t\n\^l")
1919 (looking-at ";"))
1920 (forward-line 1))
1921 (not (eobp)))
ccb3c8de
CW
1922 (setq byte-compile-read-position (point)
1923 byte-compile-last-position byte-compile-read-position)
36e65f70 1924 (let* ((old-style-backquotes nil)
ca105506 1925 (form (read inbuffer)))
36e65f70
SM
1926 ;; Warn about the use of old-style backquotes.
1927 (when old-style-backquotes
1928 (byte-compile-warn "!! The file uses old-style backquotes !!
1929This functionality has been obsolete for more than 10 years already
1930and will be removed soon. See (elisp)Backquote in the manual."))
876c194c 1931 (byte-compile-toplevel-file-form form)))
d82e848c
RS
1932 ;; Compile pending forms at end of file.
1933 (byte-compile-flush-pending)
977f31f8
RS
1934 ;; Make warnings about unresolved functions
1935 ;; give the end of the file as their position.
1936 (setq byte-compile-last-position (point-max))
2cb63a7c 1937 (byte-compile-warn-about-unresolved-functions))
fb639443
RS
1938 ;; Fix up the header at the front of the output
1939 ;; if the buffer contains multibyte characters.
9ace101c 1940 (and byte-compile-current-file
7200d79c 1941 (with-current-buffer byte-compile--outbuffer
6876a58d
SM
1942 (byte-compile-fix-header byte-compile-current-file))))
1943 byte-compile--outbuffer)))
8a5dd086 1944
dd41169b 1945(defun byte-compile-fix-header (_filename)
775adc51
GM
1946 "If the current buffer has any multibyte characters, insert a version test."
1947 (when (< (point-max) (position-bytes (point-max)))
1948 (goto-char (point-min))
1949 ;; Find the comment that describes the version condition.
1950 (search-forward "\n;;; This file uses")
1951 (narrow-to-region (line-beginning-position) (point-max))
1952 ;; Find the first line of ballast semicolons.
1953 (search-forward ";;;;;;;;;;")
1954 (beginning-of-line)
1955 (narrow-to-region (point-min) (point))
1956 (let ((old-header-end (point))
1957 (minimum-version "23")
1958 delta)
1959 (delete-region (point-min) (point-max))
1960 (insert
1961 ";;; This file contains utf-8 non-ASCII characters,\n"
1962 ";;; and so cannot be loaded into Emacs 22 or earlier.\n"
1963 ;; Have to check if emacs-version is bound so that this works
1964 ;; in files loaded early in loadup.el.
1965 "(and (boundp 'emacs-version)\n"
1966 ;; If there is a name at the end of emacs-version,
1967 ;; don't try to check the version number.
1968 " (< (aref emacs-version (1- (length emacs-version))) ?A)\n"
1969 (format " (string-lessp emacs-version \"%s\")\n" minimum-version)
dd41169b
SM
1970 ;; Because the header must fit in a fixed width, we cannot
1971 ;; insert arbitrary-length file names (Bug#11585).
1972 " (error \"`%s' was compiled for "
4a5f187a 1973 (format "Emacs %s or later\" #$))\n\n" minimum-version))
775adc51
GM
1974 ;; Now compensate for any change in size, to make sure all
1975 ;; positions in the file remain valid.
1976 (setq delta (- (point-max) old-header-end))
1977 (goto-char (point-max))
1978 (widen)
1979 (delete-char delta))))
1980
1981(defun byte-compile-insert-header (filename outbuffer)
1982 "Insert a header at the start of OUTBUFFER.
1983Call from the source buffer."
1984 (let ((dynamic-docstrings byte-compile-dynamic-docstrings)
1985 (dynamic byte-compile-dynamic)
1986 (optimize byte-optimize))
1987 (with-current-buffer outbuffer
a5832373
RS
1988 (goto-char (point-min))
1989 ;; The magic number of .elc files is ";ELC", or 0x3B454C43. After
430d2ee2 1990 ;; that is the file-format version number (18, 19, 20, or 23) as a
a5832373
RS
1991 ;; byte, followed by some nulls. The primary motivation for doing
1992 ;; this is to get some binary characters up in the first line of
1993 ;; the file so that `diff' will simply say "Binary files differ"
1994 ;; instead of actually doing a diff of two .elc files. An extra
1995 ;; benefit is that you can add this to /etc/magic:
a5832373
RS
1996 ;; 0 string ;ELC GNU Emacs Lisp compiled file,
1997 ;; >4 byte x version %d
775adc51
GM
1998 (insert
1999 ";ELC" 23 "\000\000\000\n"
2000 ";;; Compiled by "
2001 (or (and (boundp 'user-mail-address) user-mail-address)
2002 (concat (user-login-name) "@" (system-name)))
2003 " on " (current-time-string) "\n"
2004 ";;; from file " filename "\n"
5fa9d1ec
GM
2005 ";;; in Emacs version " emacs-version "\n"
2006 ";;; with"
775adc51
GM
2007 (cond
2008 ((eq optimize 'source) " source-level optimization only")
2009 ((eq optimize 'byte) " byte-level optimization only")
2010 (optimize " all optimizations")
2011 (t "out optimization"))
2012 ".\n"
2013 (if dynamic ";;; Function definitions are lazy-loaded.\n"
2014 "")
2015 "\n;;; This file uses "
2016 (if dynamic-docstrings
2017 "dynamic docstrings, first added in Emacs 19.29"
2018 "opcodes that do not exist in Emacs 18")
2019 ".\n\n"
2020 ;; Note that byte-compile-fix-header may change this.
2021 ";;; This file does not contain utf-8 non-ASCII characters,\n"
2022 ";;; and so can be loaded in Emacs versions earlier than 23.\n\n"
2023 ;; Insert semicolons as ballast, so that byte-compile-fix-header
2024 ;; can delete them so as to keep the buffer positions
2025 ;; constant for the actual compiled code.
2026 ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n"
2027 ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n\n"))))
1c393159
JB
2028
2029(defun byte-compile-output-file-form (form)
61b108cc 2030 ;; Write the given form to the output buffer, being careful of docstrings
4a5f187a 2031 ;; in defvar, defvaralias, defconst, autoload and
36b7e523 2032 ;; custom-declare-variable because make-docfile is so amazingly stupid.
c36881cf
ER
2033 ;; defalias calls are output directly by byte-compile-file-form-defmumble;
2034 ;; it does not pay to first build the defalias in defmumble and then parse
2035 ;; it here.
61b108cc
SM
2036 (let ((print-escape-newlines t)
2037 (print-length nil)
2038 (print-level nil)
2039 (print-quoted t)
2040 (print-gensym t)
2041 (print-circle ; Handle circular data structures.
2042 (not byte-compile-disable-print-circle)))
4a5f187a 2043 (if (and (memq (car-safe form) '(defvar defvaralias defconst
61b108cc
SM
2044 autoload custom-declare-variable))
2045 (stringp (nth 3 form)))
2046 (byte-compile-output-docform nil nil '("\n(" 3 ")") form nil
2047 (memq (car form)
2048 '(defvaralias autoload
2049 custom-declare-variable)))
7200d79c
SM
2050 (princ "\n" byte-compile--outbuffer)
2051 (prin1 form byte-compile--outbuffer)
1c393159
JB
2052 nil)))
2053
2ec42da9 2054(defvar byte-compile--for-effect)
6c2161c4 2055
d82e848c 2056(defun byte-compile-output-docform (preface name info form specindex quoted)
dac6f673
RS
2057 "Print a form with a doc string. INFO is (prefix doc-index postfix).
2058If PREFACE and NAME are non-nil, print them too,
2059before INFO and the FORM but after the doc string itself.
2060If SPECINDEX is non-nil, it is the index in FORM
2061of the function bytecode string. In that case,
2b9c3b12
JB
2062we output that argument and the following argument
2063\(the constants vector) together, for lazy loading.
dac6f673
RS
2064QUOTED says that we have to put a quote before the
2065list that represents a doc string reference.
1e857121 2066`defvaralias', `autoload' and `custom-declare-variable' need that."
dac6f673
RS
2067 ;; We need to examine byte-compile-dynamic-docstrings
2068 ;; in the input buffer (now current), not in the output buffer.
2069 (let ((dynamic-docstrings byte-compile-dynamic-docstrings))
7200d79c 2070 (with-current-buffer byte-compile--outbuffer
9ec5dfe6
SM
2071 (let (position)
2072
2073 ;; Insert the doc string, and make it a comment with #@LENGTH.
2074 (and (>= (nth 1 info) 0)
2075 dynamic-docstrings
9ec5dfe6
SM
2076 (progn
2077 ;; Make the doc string start at beginning of line
2078 ;; for make-docfile's sake.
2079 (insert "\n")
2080 (setq position
2081 (byte-compile-output-as-comment
2082 (nth (nth 1 info) form) nil))
9ec5dfe6
SM
2083 ;; If the doc string starts with * (a user variable),
2084 ;; negate POSITION.
2085 (if (and (stringp (nth (nth 1 info) form))
2086 (> (length (nth (nth 1 info) form)) 0)
2087 (eq (aref (nth (nth 1 info) form) 0) ?*))
2088 (setq position (- position)))))
2089
2090 (if preface
2091 (progn
2092 (insert preface)
7200d79c 2093 (prin1 name byte-compile--outbuffer)))
9ec5dfe6 2094 (insert (car info))
61b108cc 2095 (let ((print-continuous-numbering t)
9ec5dfe6 2096 print-number-table
4a5f187a
SM
2097 (index 0)
2098 ;; FIXME: The bindings below are only needed for when we're
2099 ;; called from ...-defmumble.
2100 (print-escape-newlines t)
2101 (print-length nil)
2102 (print-level nil)
2103 (print-quoted t)
2104 (print-gensym t)
2105 (print-circle ; Handle circular data structures.
2106 (not byte-compile-disable-print-circle)))
7200d79c 2107 (prin1 (car form) byte-compile--outbuffer)
9ec5dfe6
SM
2108 (while (setq form (cdr form))
2109 (setq index (1+ index))
2110 (insert " ")
2111 (cond ((and (numberp specindex) (= index specindex)
2112 ;; Don't handle the definition dynamically
2113 ;; if it refers (or might refer)
2114 ;; to objects already output
2115 ;; (for instance, gensyms in the arg list).
2116 (let (non-nil)
17870c01 2117 (when (hash-table-p print-number-table)
9ace101c 2118 (maphash (lambda (_k v) (if v (setq non-nil t)))
17870c01 2119 print-number-table))
9ec5dfe6
SM
2120 (not non-nil)))
2121 ;; Output the byte code and constants specially
2122 ;; for lazy dynamic loading.
2123 (let ((position
2124 (byte-compile-output-as-comment
2125 (cons (car form) (nth 1 form))
2126 t)))
2ec42da9 2127 (princ (format "(#$ . %d) nil" position)
7200d79c 2128 byte-compile--outbuffer)
9ec5dfe6
SM
2129 (setq form (cdr form))
2130 (setq index (1+ index))))
2131 ((= index (nth 1 info))
2132 (if position
2133 (princ (format (if quoted "'(#$ . %d)" "(#$ . %d)")
2134 position)
7200d79c 2135 byte-compile--outbuffer)
9ec5dfe6
SM
2136 (let ((print-escape-newlines nil))
2137 (goto-char (prog1 (1+ (point))
7200d79c
SM
2138 (prin1 (car form)
2139 byte-compile--outbuffer)))
9ec5dfe6
SM
2140 (insert "\\\n")
2141 (goto-char (point-max)))))
2142 (t
7200d79c 2143 (prin1 (car form) byte-compile--outbuffer)))))
9ec5dfe6 2144 (insert (nth 2 info)))))
1c393159
JB
2145 nil)
2146
ca105506 2147(defun byte-compile-keep-pending (form &optional handler)
1c393159
JB
2148 (if (memq byte-optimize '(t source))
2149 (setq form (byte-optimize-form form t)))
ca105506 2150 (if handler
2ec42da9 2151 (let ((byte-compile--for-effect t))
1c393159
JB
2152 ;; To avoid consing up monstrously large forms at load time, we split
2153 ;; the output regularly.
b4ff4a23
RS
2154 (and (memq (car-safe form) '(fset defalias))
2155 (nthcdr 300 byte-compile-output)
1c393159 2156 (byte-compile-flush-pending))
ca105506 2157 (funcall handler form)
2ec42da9 2158 (if byte-compile--for-effect
1c393159
JB
2159 (byte-compile-discard)))
2160 (byte-compile-form form t))
2161 nil)
2162
2163(defun byte-compile-flush-pending ()
2164 (if byte-compile-output
2165 (let ((form (byte-compile-out-toplevel t 'file)))
2166 (cond ((eq (car-safe form) 'progn)
ed62683d 2167 (mapc 'byte-compile-output-file-form (cdr form)))
1c393159
JB
2168 (form
2169 (byte-compile-output-file-form form)))
2170 (setq byte-compile-constants nil
2171 byte-compile-variables nil
2172 byte-compile-depth 0
2173 byte-compile-maxdepth 0
2174 byte-compile-output nil))))
2175
2ec42da9
SM
2176(defun byte-compile-preprocess (form &optional _for-effect)
2177 (setq form (macroexpand-all form byte-compile-macro-environment))
2178 ;; FIXME: We should run byte-optimize-form here, but it currently does not
2179 ;; recurse through all the code, so we'd have to fix this first.
2180 ;; Maybe a good fix would be to merge byte-optimize-form into
2181 ;; macroexpand-all.
2182 ;; (if (memq byte-optimize '(t source))
2183 ;; (setq form (byte-optimize-form form for-effect)))
2184 (if lexical-binding
2185 (cconv-closure-convert form)
2186 form))
2187
876c194c
SM
2188;; byte-hunk-handlers cannot call this!
2189(defun byte-compile-toplevel-file-form (form)
2190 (let ((byte-compile-current-form nil)) ; close over this for warnings.
2ec42da9 2191 (byte-compile-file-form (byte-compile-preprocess form t))))
876c194c
SM
2192
2193;; byte-hunk-handlers can call this.
1c393159 2194(defun byte-compile-file-form (form)
ca105506 2195 (let (handler)
a9de04fa
SM
2196 (cond ((and (consp form)
2197 (symbolp (car form))
ca105506
SM
2198 (setq handler (get (car form) 'byte-hunk-handler)))
2199 (cond ((setq form (funcall handler form))
b9598260
SM
2200 (byte-compile-flush-pending)
2201 (byte-compile-output-file-form form))))
2202 (t
2203 (byte-compile-keep-pending form)))))
1c393159
JB
2204
2205;; Functions and variables with doc strings must be output separately,
e1dbe924 2206;; so make-docfile can recognize them. Most other things can be output
1c393159
JB
2207;; as byte-code.
2208
1c393159
JB
2209(put 'autoload 'byte-hunk-handler 'byte-compile-file-form-autoload)
2210(defun byte-compile-file-form-autoload (form)
2211 (and (let ((form form))
4dd1c416 2212 (while (if (setq form (cdr form)) (macroexp-const-p (car form))))
1c393159
JB
2213 (null form)) ;Constants only
2214 (eval (nth 5 form)) ;Macro
2215 (eval form)) ;Define the autoload.
c5091f25 2216 ;; Avoid undefined function warnings for the autoload.
cb4fb1d0 2217 (when (and (consp (nth 1 form))
c5091f25
DL
2218 (eq (car (nth 1 form)) 'quote)
2219 (consp (cdr (nth 1 form)))
e703069f
SM
2220 (symbolp (nth 1 (nth 1 form)))
2221 ;; Don't add it if it's already defined. Otherwise, it might
2222 ;; hide the actual definition.
2223 (not (fboundp (nth 1 (nth 1 form)))))
cb4fb1d0
GM
2224 (push (cons (nth 1 (nth 1 form))
2225 (cons 'autoload (cdr (cdr form))))
2226 byte-compile-function-environment)
2227 ;; If an autoload occurs _before_ the first call to a function,
2228 ;; byte-compile-callargs-warn does not add an entry to
2229 ;; byte-compile-unresolved-functions. Here we mimic the logic
2230 ;; of byte-compile-callargs-warn so as not to warn if the
2231 ;; autoload comes _after_ the function call.
2232 ;; Alternatively, similar logic could go in
2233 ;; byte-compile-warn-about-unresolved-functions.
2234 (or (memq (nth 1 (nth 1 form)) byte-compile-noruntime-functions)
2235 (setq byte-compile-unresolved-functions
2236 (delq (assq (nth 1 (nth 1 form))
2237 byte-compile-unresolved-functions)
2238 byte-compile-unresolved-functions))))
1c393159
JB
2239 (if (stringp (nth 3 form))
2240 form
2241 ;; No doc string, so we can compile this as a normal form.
2242 (byte-compile-keep-pending form 'byte-compile-normal-call)))
2243
2244(put 'defvar 'byte-hunk-handler 'byte-compile-file-form-defvar)
2245(put 'defconst 'byte-hunk-handler 'byte-compile-file-form-defvar)
2246(defun byte-compile-file-form-defvar (form)
f3934f6f
SM
2247 (when (and (symbolp (nth 1 form))
2248 (not (string-match "[-*/:$]" (symbol-name (nth 1 form))))
2249 (byte-compile-warning-enabled-p 'lexical))
2250 (byte-compile-warn "global/dynamic var `%s' lacks a prefix"
2251 (nth 1 form)))
2252 (push (nth 1 form) byte-compile-bound-variables)
2253 (if (eq (car form) 'defconst)
2254 (push (nth 1 form) byte-compile-const-variables))
2255 (if (and (null (cddr form)) ;No `value' provided.
2256 (eq (car form) 'defvar)) ;Just a declaration.
2257 nil
1c393159 2258 (cond ((consp (nth 2 form))
f3934f6f
SM
2259 (setq form (copy-sequence form))
2260 (setcar (cdr (cdr form))
2261 (byte-compile-top-level (nth 2 form) nil 'file))))
1c393159
JB
2262 form))
2263
2ec42da9
SM
2264(put 'define-abbrev-table 'byte-hunk-handler
2265 'byte-compile-file-form-define-abbrev-table)
b7c76a30 2266(defun byte-compile-file-form-define-abbrev-table (form)
2aea6521
GM
2267 (if (eq 'quote (car-safe (car-safe (cdr form))))
2268 (push (car-safe (cdr (cadr form))) byte-compile-bound-variables))
b7c76a30
SM
2269 (byte-compile-keep-pending form))
2270
8c731d3d
RS
2271(put 'custom-declare-variable 'byte-hunk-handler
2272 'byte-compile-file-form-custom-declare-variable)
2273(defun byte-compile-file-form-custom-declare-variable (form)
cf637a34 2274 (when (byte-compile-warning-enabled-p 'callargs)
fe33e7c8 2275 (byte-compile-nogroup-warn form))
2aea6521 2276 (push (nth 1 (nth 1 form)) byte-compile-bound-variables)
2bd785a2 2277 (byte-compile-keep-pending form))
8c731d3d 2278
997011eb
RS
2279(put 'require 'byte-hunk-handler 'byte-compile-file-form-require)
2280(defun byte-compile-file-form-require (form)
3f12e5bd
GM
2281 (let ((args (mapcar 'eval (cdr form)))
2282 (hist-orig load-history)
2283 hist-new)
997011eb 2284 (apply 'require args)
3f12e5bd
GM
2285 (when (byte-compile-warning-enabled-p 'cl-functions)
2286 ;; Detect (require 'cl) in a way that works even if cl is already loaded.
2287 (if (member (car args) '("cl" cl))
2288 (progn
2289 (byte-compile-warn "cl package required at runtime")
2290 (byte-compile-disable-warning 'cl-functions))
2291 ;; We may have required something that causes cl to be loaded, eg
2292 ;; the uncompiled version of a file that requires cl when compiling.
2293 (setq hist-new load-history)
2294 (while (and (not byte-compile-cl-functions)
2295 hist-new (not (eq hist-new hist-orig)))
2296 (and (byte-compile-cl-file-p (car (pop hist-new)))
2297 (byte-compile-find-cl-functions))))))
1c393159
JB
2298 (byte-compile-keep-pending form 'byte-compile-normal-call))
2299
2300(put 'progn 'byte-hunk-handler 'byte-compile-file-form-progn)
2301(put 'prog1 'byte-hunk-handler 'byte-compile-file-form-progn)
2302(put 'prog2 'byte-hunk-handler 'byte-compile-file-form-progn)
2303(defun byte-compile-file-form-progn (form)
ed62683d 2304 (mapc 'byte-compile-file-form (cdr form))
1c393159
JB
2305 ;; Return nil so the forms are not output twice.
2306 nil)
2307
cb4fb1d0
GM
2308(put 'with-no-warnings 'byte-hunk-handler
2309 'byte-compile-file-form-with-no-warnings)
2310(defun byte-compile-file-form-with-no-warnings (form)
2311 ;; cf byte-compile-file-form-progn.
2312 (let (byte-compile-warnings)
2313 (mapc 'byte-compile-file-form (cdr form))
2314 nil))
2315
1c393159
JB
2316;; This handler is not necessary, but it makes the output from dont-compile
2317;; and similar macros cleaner.
2318(put 'eval 'byte-hunk-handler 'byte-compile-file-form-eval)
2319(defun byte-compile-file-form-eval (form)
2320 (if (eq (car-safe (nth 1 form)) 'quote)
2321 (nth 1 (nth 1 form))
2322 (byte-compile-keep-pending form)))
2323
61b108cc
SM
2324(defun byte-compile-file-form-defmumble (name macro arglist body rest)
2325 "Process a `defalias' for NAME.
2326If MACRO is non-nil, the definition is known to be a macro.
2327ARGLIST is the list of arguments, if it was recognized or t otherwise.
2328BODY of the definition, or t if not recognized.
2329Return non-nil if everything went as planned, or nil to imply that it decided
2330not to take responsibility for the actual compilation of the code."
2331 (let* ((this-kind (if macro 'byte-compile-macro-environment
2332 'byte-compile-function-environment))
2333 (that-kind (if macro 'byte-compile-function-environment
2334 'byte-compile-macro-environment))
2335 (this-one (assq name (symbol-value this-kind)))
2336 (that-one (assq name (symbol-value that-kind)))
2337 (byte-compile-current-form name)) ; For warnings.
2338
ca105506 2339 (byte-compile-set-symbol-position name)
1c393159
JB
2340 ;; When a function or macro is defined, add it to the call tree so that
2341 ;; we can tell when functions are not used.
2342 (if byte-compile-generate-call-tree
61b108cc
SM
2343 (or (assq name byte-compile-call-tree)
2344 (setq byte-compile-call-tree
2345 (cons (list name nil nil) byte-compile-call-tree))))
1c393159 2346
cf637a34 2347 (if (byte-compile-warning-enabled-p 'redefine)
61b108cc
SM
2348 (byte-compile-arglist-warn name arglist macro))
2349
1c393159 2350 (if byte-compile-verbose
61b108cc
SM
2351 (message "Compiling %s... (%s)"
2352 (or byte-compile-current-file "") name))
2353 (cond ((not (or macro (listp body)))
2354 ;; We do not know positively if the definition is a macro
2355 ;; or a function, so we shouldn't emit warnings.
2356 ;; This also silences "multiple definition" warnings for defmethods.
2357 nil)
2358 (that-one
2359 (if (and (byte-compile-warning-enabled-p 'redefine)
2360 ;; Don't warn when compiling the stubs in byte-run...
2361 (not (assq name byte-compile-initial-macro-environment)))
2362 (byte-compile-warn
29a4dcb0 2363 "`%s' defined multiple times, as both function and macro"
61b108cc
SM
2364 name))
2365 (setcdr that-one nil))
2366 (this-one
2367 (when (and (byte-compile-warning-enabled-p 'redefine)
2368 ;; Hack: Don't warn when compiling the magic internal
29a4dcb0 2369 ;; byte-compiler macros in byte-run.el...
61b108cc
SM
2370 (not (assq name byte-compile-initial-macro-environment)))
2371 (byte-compile-warn "%s `%s' defined multiple times in this file"
2372 (if macro "macro" "function")
2373 name)))
2374 ((and (fboundp name)
2375 (eq (car-safe (symbol-function name))
2376 (if macro 'lambda 'macro)))
2377 (when (byte-compile-warning-enabled-p 'redefine)
2378 (byte-compile-warn "%s `%s' being redefined as a %s"
2379 (if macro "function" "macro")
2380 name
2381 (if macro "macro" "function")))
2382 ;; Shadow existing definition.
2383 (set this-kind
2384 (cons (cons name nil)
2385 (symbol-value this-kind))))
2386 )
2387
2388 (when (and (listp body)
2389 (stringp (car body))
2390 (symbolp (car-safe (cdr-safe body)))
2391 (car-safe (cdr-safe body))
2392 (stringp (car-safe (cdr-safe (cdr-safe body)))))
2393 ;; FIXME: We've done that already just above, so this looks wrong!
2394 ;;(byte-compile-set-symbol-position name)
2395 (byte-compile-warn "probable `\"' without `\\' in doc string of %s"
2396 name))
e3ac1281 2397
61b108cc
SM
2398 (if (not (listp body))
2399 ;; The precise definition requires evaluation to find out, so it
2400 ;; will only be known at runtime.
2401 ;; For a macro, that means we can't use that macro in the same file.
2402 (progn
2403 (unless macro
2404 (push (cons name (if (listp arglist) `(declared ,arglist) t))
2405 byte-compile-function-environment))
2406 ;; Tell the caller that we didn't compile it yet.
2407 nil)
2408
2409 (let* ((code (byte-compile-lambda (cons arglist body) t)))
2410 (if this-one
2411 ;; A definition in b-c-initial-m-e should always take precedence
2412 ;; during compilation, so don't let it be redefined. (Bug#8647)
2413 (or (and macro
2414 (assq name byte-compile-initial-macro-environment))
2415 (setcdr this-one code))
2416 (set this-kind
2417 (cons (cons name code)
2418 (symbol-value this-kind))))
2419
2420 (if rest
2421 ;; There are additional args to `defalias' (like maybe a docstring)
2422 ;; that the code below can't handle: punt!
2423 nil
2424 ;; Otherwise, we have a bona-fide defun/defmacro definition, and use
2425 ;; special code to allow dynamic docstrings and byte-code.
2426 (byte-compile-flush-pending)
2427 (let ((index
2428 ;; If there's no doc string, provide -1 as the "doc string
2429 ;; index" so that no element will be treated as a doc string.
2430 (if (not (stringp (car body))) -1 4)))
2431 ;; Output the form by hand, that's much simpler than having
2432 ;; b-c-output-file-form analyze the defalias.
2433 (byte-compile-output-docform
2434 "\n(defalias '"
2435 name
2436 (if macro `(" '(macro . #[" ,index "])") `(" #[" ,index "]"))
2437 (append code nil) ; Turn byte-code-function-p into list.
2438 (and (atom code) byte-compile-dynamic
2439 1)
2440 nil))
2441 (princ ")" byte-compile--outbuffer)
2442 t)))))
d82e848c 2443
d82e848c 2444(defun byte-compile-output-as-comment (exp quoted)
61b108cc
SM
2445 "Print Lisp object EXP in the output file, inside a comment,
2446and return the file (byte) position it will have.
2447If QUOTED is non-nil, print with quoting; otherwise, print without quoting."
2448 (with-current-buffer byte-compile--outbuffer
2449 (let ((position (point)))
9ec5dfe6
SM
2450
2451 ;; Insert EXP, and make it a comment with #@LENGTH.
2452 (insert " ")
2453 (if quoted
7200d79c
SM
2454 (prin1 exp byte-compile--outbuffer)
2455 (princ exp byte-compile--outbuffer))
9ec5dfe6
SM
2456 (goto-char position)
2457 ;; Quote certain special characters as needed.
2458 ;; get_doc_string in doc.c does the unquoting.
2459 (while (search-forward "\^A" nil t)
2460 (replace-match "\^A\^A" t t))
2461 (goto-char position)
2462 (while (search-forward "\000" nil t)
2463 (replace-match "\^A0" t t))
2464 (goto-char position)
2465 (while (search-forward "\037" nil t)
2466 (replace-match "\^A_" t t))
2467 (goto-char (point-max))
2468 (insert "\037")
2469 (goto-char position)
2470 (insert "#@" (format "%d" (- (position-bytes (point-max))
2471 (position-bytes position))))
2472
2473 ;; Save the file position of the object.
61b108cc
SM
2474 ;; Note we add 1 to skip the space that we inserted before the actual doc
2475 ;; string, and subtract point-min to convert from an 1-origin Emacs
2476 ;; position to a file position.
2477 (prog1
2478 (- (position-bytes (point)) (point-min) -1)
2479 (goto-char (point-max))))))
d82e848c 2480
e3ac1281 2481(defun byte-compile--reify-function (fun)
c207708c
SM
2482 "Return an expression which will evaluate to a function value FUN.
2483FUN should be either a `lambda' value or a `closure' value."
2484 (pcase-let* (((or (and `(lambda ,args . ,body) (let env nil))
2485 `(closure ,env ,args . ,body)) fun)
2486 (renv ()))
2487 ;; Turn the function's closed vars (if any) into local let bindings.
2488 (dolist (binding env)
2489 (cond
2490 ((consp binding)
2491 ;; We check shadowing by the args, so that the `let' can be moved
2492 ;; within the lambda, which can then be unfolded. FIXME: Some of those
2493 ;; bindings might be unused in `body'.
2494 (unless (memq (car binding) args) ;Shadowed.
2495 (push `(,(car binding) ',(cdr binding)) renv)))
2496 ((eq binding t))
2497 (t (push `(defvar ,binding) body))))
2498 (if (null renv)
2499 `(lambda ,args ,@body)
2500 `(lambda ,args (let ,(nreverse renv) ,@body)))))
1c393159 2501\f
fd5285f3 2502;;;###autoload
1c393159
JB
2503(defun byte-compile (form)
2504 "If FORM is a symbol, byte-compile its function definition.
2505If FORM is a lambda or a macro, byte-compile it as a function."
2506 (displaying-byte-compile-warnings
2507 (byte-compile-close-variables
c207708c
SM
2508 (let* ((lexical-binding lexical-binding)
2509 (fun (if (symbolp form)
1c393159
JB
2510 (and (fboundp form) (symbol-function form))
2511 form))
2512 (macro (eq (car-safe fun) 'macro)))
2513 (if macro
2514 (setq fun (cdr fun)))
b5771c0d
SM
2515 (cond
2516 ;; Up until Emacs-24.1, byte-compile silently did nothing when asked to
2517 ;; compile something invalid. So let's tune down the complaint from an
2518 ;; error to a simple message for the known case where signaling an error
2519 ;; causes problems.
2520 ((byte-code-function-p fun)
2521 (message "Function %s is already compiled"
2522 (if (symbolp form) form "provided"))
2523 fun)
2524 (t
2525 (when (symbolp form)
2526 (unless (memq (car-safe fun) '(closure lambda))
2527 (error "Don't know how to compile %S" fun))
5d0ccd95
SM
2528 (setq lexical-binding (eq (car fun) 'closure))
2529 (setq fun (byte-compile--reify-function fun)))
b5771c0d 2530 (unless (eq (car-safe fun) 'lambda)
c207708c 2531 (error "Don't know how to compile %S" fun))
b5771c0d
SM
2532 ;; Expand macros.
2533 (setq fun (byte-compile-preprocess fun))
2534 ;; Get rid of the `function' quote added by the `lambda' macro.
2535 (if (eq (car-safe fun) 'function) (setq fun (cadr fun)))
2536 (setq fun (byte-compile-lambda fun))
2537 (if macro (push 'macro fun))
2538 (if (symbolp form)
2539 (fset form fun)
2540 fun)))))))
1c393159
JB
2541
2542(defun byte-compile-sexp (sexp)
2543 "Compile and return SEXP."
2544 (displaying-byte-compile-warnings
2545 (byte-compile-close-variables
2ec42da9 2546 (byte-compile-top-level (byte-compile-preprocess sexp)))))
1c393159 2547
eadd6444
GM
2548(defun byte-compile-check-lambda-list (list)
2549 "Check lambda-list LIST for errors."
2550 (let (vars)
2551 (while list
2552 (let ((arg (car list)))
ccb3c8de
CW
2553 (when (symbolp arg)
2554 (byte-compile-set-symbol-position arg))
1f006824 2555 (cond ((or (not (symbolp arg))
4dd1c416 2556 (macroexp--const-symbol-p arg t))
eadd6444
GM
2557 (error "Invalid lambda variable %s" arg))
2558 ((eq arg '&rest)
2559 (unless (cdr list)
2560 (error "&rest without variable name"))
2561 (when (cddr list)
2562 (error "Garbage following &rest VAR in lambda-list")))
2563 ((eq arg '&optional)
2564 (unless (cdr list)
2565 (error "Variable name missing after &optional")))
2566 ((memq arg vars)
e34fd2f2 2567 (byte-compile-warn "repeated variable %s in lambda-list" arg))
1f006824 2568 (t
eadd6444
GM
2569 (push arg vars))))
2570 (setq list (cdr list)))))
2571
2572
ce5b520a
SM
2573(defun byte-compile-arglist-vars (arglist)
2574 "Return a list of the variables in the lambda argument list ARGLIST."
2575 (remq '&rest (remq '&optional arglist)))
2576
2577(defun byte-compile-make-lambda-lexenv (form)
2578 "Return a new lexical environment for a lambda expression FORM."
2579 ;; See if this is a closure or not
2580 (let ((args (byte-compile-arglist-vars (cadr form))))
2581 (let ((lexenv nil))
2582 ;; Fill in the initial stack contents
2583 (let ((stackpos 0))
2584 ;; Add entries for each argument
2585 (dolist (arg args)
2586 (push (cons arg stackpos) lexenv)
2587 (setq stackpos (1+ stackpos)))
2588 ;; Return the new lexical environment
2589 lexenv))))
b9598260 2590
e2abe5a1
SM
2591(defun byte-compile-make-args-desc (arglist)
2592 (let ((mandatory 0)
2593 nonrest (rest 0))
2594 (while (and arglist (not (memq (car arglist) '(&optional &rest))))
2595 (setq mandatory (1+ mandatory))
2596 (setq arglist (cdr arglist)))
2597 (setq nonrest mandatory)
2598 (when (eq (car arglist) '&optional)
2599 (setq arglist (cdr arglist))
2600 (while (and arglist (not (eq (car arglist) '&rest)))
2601 (setq nonrest (1+ nonrest))
2602 (setq arglist (cdr arglist))))
2603 (when arglist
2604 (setq rest 1))
2605 (if (> mandatory 127)
2606 (byte-compile-report-error "Too many (>127) mandatory arguments")
2607 (logior mandatory
2608 (lsh nonrest 8)
2609 (lsh rest 7)))))
2610
61b108cc 2611
ca105506 2612(defun byte-compile-lambda (fun &optional add-lambda reserved-csts)
61b108cc
SM
2613 "Byte-compile a lambda-expression and return a valid function.
2614The value is usually a compiled function but may be the original
2615lambda-expression.
2616When ADD-LAMBDA is non-nil, the symbol `lambda' is added as head
2617of the list FUN and `byte-compile-set-symbol-position' is not called.
2618Use this feature to avoid calling `byte-compile-set-symbol-position'
2619for symbols generated by the byte compiler itself."
4ec5239c 2620 (if add-lambda
ca105506
SM
2621 (setq fun (cons 'lambda fun))
2622 (unless (eq 'lambda (car-safe fun))
2623 (error "Not a lambda list: %S" fun))
4ec5239c 2624 (byte-compile-set-symbol-position 'lambda))
ca105506
SM
2625 (byte-compile-check-lambda-list (nth 1 fun))
2626 (let* ((arglist (nth 1 fun))
1c393159 2627 (byte-compile-bound-variables
ce5b520a 2628 (append (and (not lexical-binding)
ca105506 2629 (byte-compile-arglist-vars arglist))
ce5b520a 2630 byte-compile-bound-variables))
ca105506
SM
2631 (body (cdr (cdr fun)))
2632 (doc (if (stringp (car body))
2633 (prog1 (car body)
2634 ;; Discard the doc string
2635 ;; unless it is the last element of the body.
2636 (if (cdr body)
2637 (setq body (cdr body))))))
2638 (int (assq 'interactive body)))
6c2161c4 2639 ;; Process the interactive spec.
ca105506 2640 (when int
6c2161c4
SM
2641 (byte-compile-set-symbol-position 'interactive)
2642 ;; Skip (interactive) if it is in front (the most usual location).
ca105506
SM
2643 (if (eq int (car body))
2644 (setq body (cdr body)))
2645 (cond ((consp (cdr int))
2646 (if (cdr (cdr int))
6c2161c4 2647 (byte-compile-warn "malformed interactive spec: %s"
ca105506 2648 (prin1-to-string int)))
6b61353c
KH
2649 ;; If the interactive spec is a call to `list', don't
2650 ;; compile it, because `call-interactively' looks at the
2651 ;; args of `list'. Actually, compile it to get warnings,
2652 ;; but don't use the result.
ca105506 2653 (let* ((form (nth 1 int))
d032d5e7 2654 (newform (byte-compile-top-level form)))
6c2161c4
SM
2655 (while (memq (car-safe form) '(let let* progn save-excursion))
2656 (while (consp (cdr form))
2657 (setq form (cdr form)))
2658 (setq form (car form)))
d032d5e7 2659 (if (and (eq (car-safe form) 'list)
99d99081 2660 ;; The spec is evalled in callint.c in dynamic-scoping
d032d5e7
SM
2661 ;; mode, so just leaving the form unchanged would mean
2662 ;; it won't be eval'd in the right mode.
2663 (not lexical-binding))
2664 nil
ca105506
SM
2665 (setq int `(interactive ,newform)))))
2666 ((cdr int)
6c2161c4 2667 (byte-compile-warn "malformed interactive spec: %s"
ca105506 2668 (prin1-to-string int)))))
6c2161c4 2669 ;; Process the body.
ba83908c 2670 (let ((compiled
ca105506 2671 (byte-compile-top-level (cons 'progn body) nil 'lambda
ba83908c
SM
2672 ;; If doing lexical binding, push a new
2673 ;; lexical environment containing just the
2674 ;; args (since lambda expressions should be
2675 ;; closed by now).
2676 (and lexical-binding
ca105506 2677 (byte-compile-make-lambda-lexenv fun))
ba83908c 2678 reserved-csts)))
6c2161c4 2679 ;; Build the actual byte-coded function.
f80efb86 2680 (cl-assert (eq 'byte-code (car-safe compiled)))
61b108cc
SM
2681 (apply #'make-byte-code
2682 (if lexical-binding
2683 (byte-compile-make-args-desc arglist)
2684 arglist)
2685 (append
2686 ;; byte-string, constants-vector, stack depth
2687 (cdr compiled)
2688 ;; optionally, the doc string.
2689 (cond (lexical-binding
2690 (require 'help-fns)
2691 (list (help-add-fundoc-usage doc arglist)))
2692 ((or doc int)
2693 (list doc)))
2694 ;; optionally, the interactive spec.
2695 (if int
2696 (list (nth 1 int))))))))
b9598260 2697
876c194c 2698(defvar byte-compile-reserved-constants 0)
1c393159
JB
2699
2700(defun byte-compile-constants-vector ()
2701 ;; Builds the constants-vector from the current variables and constants.
2702 ;; This modifies the constants from (const . nil) to (const . offset).
2703 ;; To keep the byte-codes to look up the vector as short as possible:
2704 ;; First 6 elements are vars, as there are one-byte varref codes for those.
2705 ;; Next up to byte-constant-limit are constants, still with one-byte codes.
2706 ;; Next variables again, to get 2-byte codes for variable lookup.
2707 ;; The rest of the constants and variables need 3-byte byte-codes.
876c194c 2708 (let* ((i (1- byte-compile-reserved-constants))
1c393159
JB
2709 (rest (nreverse byte-compile-variables)) ; nreverse because the first
2710 (other (nreverse byte-compile-constants)) ; vars often are used most.
2711 ret tmp
2712 (limits '(5 ; Use the 1-byte varref codes,
2713 63 ; 1-constlim ; 1-byte byte-constant codes,
2714 255 ; 2-byte varref codes,
f9210e18
SM
2715 65535 ; 3-byte codes for the rest.
2716 65535)) ; twice since we step when we swap.
1c393159
JB
2717 limit)
2718 (while (or rest other)
2719 (setq limit (car limits))
876c194c
SM
2720 (while (and rest (< i limit))
2721 (cond
2722 ((numberp (car rest))
f80efb86 2723 (cl-assert (< (car rest) byte-compile-reserved-constants)))
876c194c
SM
2724 ((setq tmp (assq (car (car rest)) ret))
2725 (setcdr (car rest) (cdr tmp)))
2726 (t
1c393159 2727 (setcdr (car rest) (setq i (1+ i)))
876c194c 2728 (setq ret (cons (car rest) ret))))
1c393159 2729 (setq rest (cdr rest)))
f9210e18
SM
2730 (setq limits (cdr limits) ;Step
2731 rest (prog1 other ;&Swap.
1c393159
JB
2732 (setq other rest))))
2733 (apply 'vector (nreverse (mapcar 'car ret)))))
2734
2735;; Given an expression FORM, compile it and return an equivalent byte-code
2736;; expression (a call to the function byte-code).
2ec42da9 2737(defun byte-compile-top-level (form &optional for-effect output-type
876c194c 2738 lexenv reserved-csts)
1c393159
JB
2739 ;; OUTPUT-TYPE advises about how form is expected to be used:
2740 ;; 'eval or nil -> a single form,
2741 ;; 'progn or t -> a list of forms,
2742 ;; 'lambda -> body of a lambda,
2743 ;; 'file -> used at file-level.
2ec42da9 2744 (let ((byte-compile--for-effect for-effect)
9ace101c 2745 (byte-compile-constants nil)
285cdf4e
RS
2746 (byte-compile-variables nil)
2747 (byte-compile-tag-number 0)
2748 (byte-compile-depth 0)
2749 (byte-compile-maxdepth 0)
7200d79c 2750 (byte-compile--lexical-environment lexenv)
876c194c 2751 (byte-compile-reserved-constants (or reserved-csts 0))
285cdf4e 2752 (byte-compile-output nil))
b9598260 2753 (if (memq byte-optimize '(t source))
2ec42da9 2754 (setq form (byte-optimize-form form byte-compile--for-effect)))
b9598260
SM
2755 (while (and (eq (car-safe form) 'progn) (null (cdr (cdr form))))
2756 (setq form (nth 1 form)))
29a4dcb0
SM
2757 ;; Set up things for a lexically-bound function.
2758 (when (and lexical-binding (eq output-type 'lambda))
2759 ;; See how many arguments there are, and set the current stack depth
2760 ;; accordingly.
7200d79c 2761 (setq byte-compile-depth (length byte-compile--lexical-environment))
29a4dcb0
SM
2762 ;; If there are args, output a tag to record the initial
2763 ;; stack-depth for the optimizer.
2764 (when (> byte-compile-depth 0)
2765 (byte-compile-out-tag (byte-compile-make-tag))))
2766 ;; Now compile FORM
2767 (byte-compile-form form byte-compile--for-effect)
2768 (byte-compile-out-toplevel byte-compile--for-effect output-type)))
1c393159
JB
2769
2770(defun byte-compile-out-toplevel (&optional for-effect output-type)
2771 (if for-effect
2772 ;; The stack is empty. Push a value to be returned from (byte-code ..).
2773 (if (eq (car (car byte-compile-output)) 'byte-discard)
2774 (setq byte-compile-output (cdr byte-compile-output))
2775 (byte-compile-push-constant
2776 ;; Push any constant - preferably one which already is used, and
2777 ;; a number or symbol - ie not some big sequence. The return value
2778 ;; isn't returned, but it would be a shame if some textually large
2779 ;; constant was not optimized away because we chose to return it.
2780 (and (not (assq nil byte-compile-constants)) ; Nil is often there.
2781 (let ((tmp (reverse byte-compile-constants)))
ba76e7fa
SM
2782 (while (and tmp (not (or (symbolp (caar tmp))
2783 (numberp (caar tmp)))))
1c393159 2784 (setq tmp (cdr tmp)))
ba76e7fa 2785 (caar tmp))))))
1c393159
JB
2786 (byte-compile-out 'byte-return 0)
2787 (setq byte-compile-output (nreverse byte-compile-output))
2788 (if (memq byte-optimize '(t byte))
2789 (setq byte-compile-output
9ace101c 2790 (byte-optimize-lapcode byte-compile-output)))
1f006824 2791
1c393159
JB
2792 ;; Decompile trivial functions:
2793 ;; only constants and variables, or a single funcall except in lambdas.
2794 ;; Except for Lisp_Compiled objects, forms like (foo "hi")
2795 ;; are still quicker than (byte-code "..." [foo "hi"] 2).
2796 ;; Note that even (quote foo) must be parsed just as any subr by the
2797 ;; interpreter, so quote should be compiled into byte-code in some contexts.
2798 ;; What to leave uncompiled:
69dc83fd
KH
2799 ;; lambda -> never. we used to leave it uncompiled if the body was
2800 ;; a single atom, but that causes confusion if the docstring
2801 ;; uses the (file . pos) syntax. Besides, now that we have
2802 ;; the Lisp_Compiled type, the compiled form is faster.
1c393159
JB
2803 ;; eval -> atom, quote or (function atom atom atom)
2804 ;; progn -> as <<same-as-eval>> or (progn <<same-as-eval>> atom)
2805 ;; file -> as progn, but takes both quotes and atoms, and longer forms.
2806 (let (rest
2807 (maycall (not (eq output-type 'lambda))) ; t if we may make a funcall.
2808 tmp body)
2809 (cond
2810 ;; #### This should be split out into byte-compile-nontrivial-function-p.
69dc83fd
KH
2811 ((or (eq output-type 'lambda)
2812 (nthcdr (if (eq output-type 'file) 50 8) byte-compile-output)
1c393159
JB
2813 (assq 'TAG byte-compile-output) ; Not necessary, but speeds up a bit.
2814 (not (setq tmp (assq 'byte-return byte-compile-output)))
2815 (progn
2816 (setq rest (nreverse
2817 (cdr (memq tmp (reverse byte-compile-output)))))
2ec42da9
SM
2818 (while
2819 (cond
2820 ((memq (car (car rest)) '(byte-varref byte-constant))
2821 (setq tmp (car (cdr (car rest))))
2822 (if (if (eq (car (car rest)) 'byte-constant)
2823 (or (consp tmp)
2824 (and (symbolp tmp)
4dd1c416 2825 (not (macroexp--const-symbol-p tmp)))))
2ec42da9
SM
2826 (if maycall
2827 (setq body (cons (list 'quote tmp) body)))
2828 (setq body (cons tmp body))))
2829 ((and maycall
2830 ;; Allow a funcall if at most one atom follows it.
2831 (null (nthcdr 3 rest))
2832 (setq tmp (get (car (car rest)) 'byte-opcode-invert))
2833 (or (null (cdr rest))
2834 (and (memq output-type '(file progn t))
2835 (cdr (cdr rest))
2836 (eq (car (nth 1 rest)) 'byte-discard)
2837 (progn (setq rest (cdr rest)) t))))
2838 (setq maycall nil) ; Only allow one real function call.
2839 (setq body (nreverse body))
2840 (setq body (list
2841 (if (and (eq tmp 'funcall)
f5925cd9
SM
2842 (eq (car-safe (car body)) 'quote)
2843 (symbolp (nth 1 (car body))))
2ec42da9
SM
2844 (cons (nth 1 (car body)) (cdr body))
2845 (cons tmp body))))
2846 (or (eq output-type 'file)
2847 (not (delq nil (mapcar 'consp (cdr (car body))))))))
1c393159 2848 (setq rest (cdr rest)))
69dc83fd 2849 rest))
1c393159
JB
2850 (let ((byte-compile-vector (byte-compile-constants-vector)))
2851 (list 'byte-code (byte-compile-lapcode byte-compile-output)
2852 byte-compile-vector byte-compile-maxdepth)))
2853 ;; it's a trivial function
2854 ((cdr body) (cons 'progn (nreverse body)))
2855 ((car body)))))
2856
ca105506
SM
2857;; Given BODY, compile it and return a new body.
2858(defun byte-compile-top-level-body (body &optional for-effect)
2859 (setq body
2860 (byte-compile-top-level (cons 'progn body) for-effect t))
2861 (cond ((eq (car-safe body) 'progn)
2862 (cdr body))
2863 (body
2864 (list body))))
d97362d7 2865
a9de04fa
SM
2866;; Special macro-expander used during byte-compilation.
2867(defun byte-compile-macroexpand-declare-function (fn file &rest args)
2868 (push (cons fn
2869 (if (and (consp args) (listp (car args)))
2870 (list 'declared (car args))
4a5f187a 2871 t)) ; Arglist not specified.
d97362d7 2872 byte-compile-function-environment)
a342aca4
GM
2873 ;; We are stating that it _will_ be defined at runtime.
2874 (setq byte-compile-noruntime-functions
a9de04fa
SM
2875 (delq fn byte-compile-noruntime-functions))
2876 ;; Delegate the rest to the normal macro definition.
2877 (macroexpand `(declare-function ,fn ,file ,@args)))
d97362d7 2878
1c393159 2879\f
c5091f25 2880;; This is the recursive entry point for compiling each subform of an
1c393159
JB
2881;; expression.
2882;; If for-effect is non-nil, byte-compile-form will output a byte-discard
2883;; before terminating (ie no value will be left on the stack).
2ec42da9
SM
2884;; A byte-compile handler may, when byte-compile--for-effect is non-nil, choose
2885;; output code which does not leave a value on the stack, and then set
2886;; byte-compile--for-effect to nil (to prevent byte-compile-form from
2887;; outputting the byte-discard).
1c393159 2888;; If a handler wants to call another handler, it should do so via
2ec42da9
SM
2889;; byte-compile-form, or take extreme care to handle byte-compile--for-effect
2890;; correctly. (Use byte-compile-form-do-effect to reset the
2891;; byte-compile--for-effect flag too.)
1c393159
JB
2892;;
2893(defun byte-compile-form (form &optional for-effect)
2ec42da9 2894 (let ((byte-compile--for-effect for-effect))
9ace101c
SM
2895 (cond
2896 ((not (consp form))
4dd1c416 2897 (cond ((or (not (symbolp form)) (macroexp--const-symbol-p form))
9ace101c
SM
2898 (when (symbolp form)
2899 (byte-compile-set-symbol-position form))
2900 (byte-compile-constant form))
2ec42da9 2901 ((and byte-compile--for-effect byte-compile-delete-errors)
9ace101c
SM
2902 (when (symbolp form)
2903 (byte-compile-set-symbol-position form))
2ec42da9 2904 (setq byte-compile--for-effect nil))
9ace101c
SM
2905 (t
2906 (byte-compile-variable-ref form))))
2907 ((symbolp (car form))
ca105506
SM
2908 (let* ((fn (car form))
2909 (handler (get fn 'byte-compile)))
4dd1c416 2910 (when (macroexp--const-symbol-p fn)
ca105506 2911 (byte-compile-warn "`%s' called as a function" fn))
9ace101c 2912 (and (byte-compile-warning-enabled-p 'interactive-only)
ca105506 2913 (memq fn byte-compile-interactive-only-functions)
9ace101c 2914 (byte-compile-warn "`%s' used from Lisp code\n\
ca105506 2915That command is designed for interactive use only" fn))
9ace101c
SM
2916 (if (and (fboundp (car form))
2917 (eq (car-safe (symbol-function (car form))) 'macro))
9c848d8a
SM
2918 (byte-compile-log-warning
2919 (format "Forgot to expand macro %s" (car form)) nil :error))
ca105506 2920 (if (and handler
57a7d507
SM
2921 ;; Make sure that function exists.
2922 (and (functionp handler)
2923 ;; Ignore obsolete byte-compile function used by former
2924 ;; CL code to handle compiler macros (we do it
2925 ;; differently now).
2926 (not (eq handler 'cl-byte-compile-compiler-macro))))
ca105506 2927 (funcall handler form)
9ace101c
SM
2928 (byte-compile-normal-call form))
2929 (if (byte-compile-warning-enabled-p 'cl-functions)
2930 (byte-compile-cl-warn form))))
29a4dcb0
SM
2931 ((and (byte-code-function-p (car form))
2932 (memq byte-optimize '(t lap)))
2933 (byte-compile-unfold-bcf form))
2934 ((and (eq (car-safe (car form)) 'lambda)
9ace101c
SM
2935 ;; if the form comes out the same way it went in, that's
2936 ;; because it was malformed, and we couldn't unfold it.
2937 (not (eq form (setq form (byte-compile-unfold-lambda form)))))
2ec42da9
SM
2938 (byte-compile-form form byte-compile--for-effect)
2939 (setq byte-compile--for-effect nil))
9ace101c 2940 ((byte-compile-normal-call form)))
2ec42da9 2941 (if byte-compile--for-effect
9ace101c 2942 (byte-compile-discard))))
1c393159
JB
2943
2944(defun byte-compile-normal-call (form)
876c194c
SM
2945 (when (and (byte-compile-warning-enabled-p 'callargs)
2946 (symbolp (car form)))
2947 (if (memq (car form)
03408648
SM
2948 '(custom-declare-group custom-declare-variable
2949 custom-declare-face))
876c194c 2950 (byte-compile-nogroup-warn form))
a9de04fa
SM
2951 (when (get (car form) 'byte-obsolete-info)
2952 (byte-compile-warn-obsolete (car form)))
876c194c 2953 (byte-compile-callargs-warn form))
1c393159
JB
2954 (if byte-compile-generate-call-tree
2955 (byte-compile-annotate-call-tree form))
2ec42da9 2956 (when (and byte-compile--for-effect (eq (car form) 'mapcar)
cf637a34 2957 (byte-compile-warning-enabled-p 'mapcar))
89c91fdb
GM
2958 (byte-compile-set-symbol-position 'mapcar)
2959 (byte-compile-warn
2960 "`mapcar' called for effect; use `mapc' or `dolist' instead"))
1c393159 2961 (byte-compile-push-constant (car form))
ed62683d 2962 (mapc 'byte-compile-form (cdr form)) ; wasteful, but faster.
1c393159
JB
2963 (byte-compile-out 'byte-call (length (cdr form))))
2964
29a4dcb0
SM
2965
2966;; Splice the given lap code into the current instruction stream.
2967;; If it has any labels in it, you're responsible for making sure there
2968;; are no collisions, and that byte-compile-tag-number is reasonable
2969;; after this is spliced in. The provided list is destroyed.
2970(defun byte-compile-inline-lapcode (lap end-depth)
2971 ;; "Replay" the operations: we used to just do
2972 ;; (setq byte-compile-output (nconc (nreverse lap) byte-compile-output))
2973 ;; but that fails to update byte-compile-depth, so we had to assume
2974 ;; that `lap' ends up adding exactly 1 element to the stack. This
2975 ;; happens to be true for byte-code generated by bytecomp.el without
2976 ;; lexical-binding, but it's not true in general, and it's not true for
2977 ;; code output by bytecomp.el with lexical-binding.
2978 (let ((endtag (byte-compile-make-tag)))
2979 (dolist (op lap)
2980 (cond
2981 ((eq (car op) 'TAG) (byte-compile-out-tag op))
2982 ((memq (car op) byte-goto-ops) (byte-compile-goto (car op) (cdr op)))
2983 ((eq (car op) 'byte-return)
2984 (byte-compile-discard (- byte-compile-depth end-depth) t)
2985 (byte-compile-goto 'byte-goto endtag))
2986 (t (byte-compile-out (car op) (cdr op)))))
2987 (byte-compile-out-tag endtag)))
2988
2989(defun byte-compile-unfold-bcf (form)
7200d79c 2990 "Inline call to byte-code-functions."
29a4dcb0
SM
2991 (let* ((byte-compile-bound-variables byte-compile-bound-variables)
2992 (fun (car form))
2993 (fargs (aref fun 0))
2994 (start-depth byte-compile-depth)
2995 (fmax2 (if (numberp fargs) (lsh fargs -7))) ;2*max+rest.
2996 ;; (fmin (if (numberp fargs) (logand fargs 127)))
2997 (alen (length (cdr form)))
2998 (dynbinds ()))
2999 (fetch-bytecode fun)
3000 (mapc 'byte-compile-form (cdr form))
3001 (unless fmax2
3002 ;; Old-style byte-code.
f80efb86 3003 (cl-assert (listp fargs))
29a4dcb0 3004 (while fargs
f58e0fd5
SM
3005 (pcase (car fargs)
3006 (`&optional (setq fargs (cdr fargs)))
3007 (`&rest (setq fmax2 (+ (* 2 (length dynbinds)) 1))
29a4dcb0
SM
3008 (push (cadr fargs) dynbinds)
3009 (setq fargs nil))
f58e0fd5 3010 (_ (push (pop fargs) dynbinds))))
29a4dcb0
SM
3011 (unless fmax2 (setq fmax2 (* 2 (length dynbinds)))))
3012 (cond
3013 ((<= (+ alen alen) fmax2)
3014 ;; Add missing &optional (or &rest) arguments.
2462470b 3015 (dotimes (_ (- (/ (1+ fmax2) 2) alen))
29a4dcb0
SM
3016 (byte-compile-push-constant nil)))
3017 ((zerop (logand fmax2 1))
3018 (byte-compile-log-warning "Too many arguments for inlined function"
3019 nil :error)
3020 (byte-compile-discard (- alen (/ fmax2 2))))
3021 (t
3022 ;; Turn &rest args into a list.
3023 (let ((n (- alen (/ (1- fmax2) 2))))
f80efb86 3024 (cl-assert (> n 0) nil "problem: fmax2=%S alen=%S n=%S" fmax2 alen n)
29a4dcb0
SM
3025 (if (< n 5)
3026 (byte-compile-out
3027 (aref [byte-list1 byte-list2 byte-list3 byte-list4] (1- n))
3028 0)
3029 (byte-compile-out 'byte-listN n)))))
3030 (mapc #'byte-compile-dynamic-variable-bind dynbinds)
3031 (byte-compile-inline-lapcode
3032 (byte-decompile-bytecode-1 (aref fun 1) (aref fun 2) t)
3033 (1+ start-depth))
3034 ;; Unbind dynamic variables.
3035 (when dynbinds
3036 (byte-compile-out 'byte-unbind (length dynbinds)))
f80efb86 3037 (cl-assert (eq byte-compile-depth (1+ start-depth))
29a4dcb0
SM
3038 nil "Wrong depth start=%s end=%s" start-depth byte-compile-depth)))
3039
2403c841
SM
3040(defun byte-compile-check-variable (var access-type)
3041 "Do various error checks before a use of the variable VAR."
b9598260
SM
3042 (when (symbolp var)
3043 (byte-compile-set-symbol-position var))
4dd1c416 3044 (cond ((or (not (symbolp var)) (macroexp--const-symbol-p var))
b9598260 3045 (when (byte-compile-warning-enabled-p 'constants)
2403c841 3046 (byte-compile-warn (if (eq access-type 'let-bind)
b9598260
SM
3047 "attempt to let-bind %s `%s`"
3048 "variable reference to %s `%s'")
3049 (if (symbolp var) "constant" "nonvariable")
3050 (prin1-to-string var))))
2403c841
SM
3051 ((let ((od (get var 'byte-obsolete-variable)))
3052 (and od
3053 (not (memq var byte-compile-not-obsolete-vars))
bac7ff22 3054 (not (memq var byte-compile-global-not-obsolete-vars))
f58e0fd5
SM
3055 (or (pcase (nth 1 od)
3056 (`set (not (eq access-type 'reference)))
3057 (`get (eq access-type 'reference))
3058 (_ t)))))
b9598260
SM
3059 (byte-compile-warn-obsolete var))))
3060
3061(defsubst byte-compile-dynamic-variable-op (base-op var)
3062 (let ((tmp (assq var byte-compile-variables)))
6c2161c4 3063 (unless tmp
b9598260 3064 (setq tmp (list var))
6c2161c4 3065 (push tmp byte-compile-variables))
1c393159
JB
3066 (byte-compile-out base-op tmp)))
3067
b9598260
SM
3068(defun byte-compile-dynamic-variable-bind (var)
3069 "Generate code to bind the lexical variable VAR to the top-of-stack value."
2403c841 3070 (byte-compile-check-variable var 'let-bind)
ce5b520a 3071 (push var byte-compile-bound-variables)
b9598260
SM
3072 (byte-compile-dynamic-variable-op 'byte-varbind var))
3073
b9598260
SM
3074(defun byte-compile-variable-ref (var)
3075 "Generate code to push the value of the variable VAR on the stack."
2403c841 3076 (byte-compile-check-variable var 'reference)
7200d79c 3077 (let ((lex-binding (assq var byte-compile--lexical-environment)))
b9598260
SM
3078 (if lex-binding
3079 ;; VAR is lexically bound
ce5b520a 3080 (byte-compile-stack-ref (cdr lex-binding))
b9598260
SM
3081 ;; VAR is dynamically bound
3082 (unless (or (not (byte-compile-warning-enabled-p 'free-vars))
3083 (boundp var)
3084 (memq var byte-compile-bound-variables)
3085 (memq var byte-compile-free-references))
876c194c 3086 (byte-compile-warn "reference to free variable `%S'" var)
b9598260
SM
3087 (push var byte-compile-free-references))
3088 (byte-compile-dynamic-variable-op 'byte-varref var))))
3089
3090(defun byte-compile-variable-set (var)
3091 "Generate code to set the variable VAR from the top-of-stack value."
2403c841 3092 (byte-compile-check-variable var 'assign)
7200d79c 3093 (let ((lex-binding (assq var byte-compile--lexical-environment)))
b9598260 3094 (if lex-binding
61b108cc 3095 ;; VAR is lexically bound.
ce5b520a 3096 (byte-compile-stack-set (cdr lex-binding))
61b108cc 3097 ;; VAR is dynamically bound.
b9598260
SM
3098 (unless (or (not (byte-compile-warning-enabled-p 'free-vars))
3099 (boundp var)
3100 (memq var byte-compile-bound-variables)
3101 (memq var byte-compile-free-assignments))
3102 (byte-compile-warn "assignment to free variable `%s'" var)
3103 (push var byte-compile-free-assignments))
3104 (byte-compile-dynamic-variable-op 'byte-varset var))))
3105
1c393159 3106(defmacro byte-compile-get-constant (const)
1639b803 3107 `(or (if (stringp ,const)
7fb4fa10
RS
3108 ;; In a string constant, treat properties as significant.
3109 (let (result)
3110 (dolist (elt byte-compile-constants)
3111 (if (equal-including-properties (car elt) ,const)
3112 (setq result elt)))
3113 result)
1639b803
DL
3114 (assq ,const byte-compile-constants))
3115 (car (setq byte-compile-constants
3116 (cons (list ,const) byte-compile-constants)))))
1c393159 3117
2ec42da9
SM
3118;; Use this when the value of a form is a constant.
3119;; This obeys byte-compile--for-effect.
1c393159 3120(defun byte-compile-constant (const)
2ec42da9
SM
3121 (if byte-compile--for-effect
3122 (setq byte-compile--for-effect nil)
ccb3c8de
CW
3123 (when (symbolp const)
3124 (byte-compile-set-symbol-position const))
1c393159
JB
3125 (byte-compile-out 'byte-constant (byte-compile-get-constant const))))
3126
3127;; Use this for a constant that is not the value of its containing form.
2ec42da9 3128;; This ignores byte-compile--for-effect.
1c393159 3129(defun byte-compile-push-constant (const)
2ec42da9 3130 (let ((byte-compile--for-effect nil))
1c393159 3131 (inline (byte-compile-constant const))))
1c393159
JB
3132\f
3133;; Compile those primitive ordinary functions
3134;; which have special byte codes just for speed.
3135
3136(defmacro byte-defop-compiler (function &optional compile-handler)
9d28c33e
SM
3137 "Add a compiler-form for FUNCTION.
3138If function is a symbol, then the variable \"byte-SYMBOL\" must name
3139the opcode to be used. If function is a list, the first element
3140is the function and the second element is the bytecode-symbol.
3141The second element may be nil, meaning there is no opcode.
3142COMPILE-HANDLER is the function to use to compile this byte-op, or
3143may be the abbreviations 0, 1, 2, 3, 0-1, or 1-2.
3144If it is nil, then the handler is \"byte-compile-SYMBOL.\""
1c393159
JB
3145 (let (opcode)
3146 (if (symbolp function)
3147 (setq opcode (intern (concat "byte-" (symbol-name function))))
3148 (setq opcode (car (cdr function))
3149 function (car function)))
3150 (let ((fnform
3151 (list 'put (list 'quote function) ''byte-compile
3152 (list 'quote
3153 (or (cdr (assq compile-handler
3154 '((0 . byte-compile-no-args)
3155 (1 . byte-compile-one-arg)
3156 (2 . byte-compile-two-args)
3157 (3 . byte-compile-three-args)
3158 (0-1 . byte-compile-zero-or-one-arg)
3159 (1-2 . byte-compile-one-or-two-args)
3160 (2-3 . byte-compile-two-or-three-args)
3161 )))
3162 compile-handler
3163 (intern (concat "byte-compile-"
3164 (symbol-name function))))))))
3165 (if opcode
3166 (list 'progn fnform
3167 (list 'put (list 'quote function)
3168 ''byte-opcode (list 'quote opcode))
3169 (list 'put (list 'quote opcode)
3170 ''byte-opcode-invert (list 'quote function)))
3171 fnform))))
3172
1c393159
JB
3173(defmacro byte-defop-compiler-1 (function &optional compile-handler)
3174 (list 'byte-defop-compiler (list function nil) compile-handler))
3175
3176\f
3177(put 'byte-call 'byte-opcode-invert 'funcall)
3178(put 'byte-list1 'byte-opcode-invert 'list)
3179(put 'byte-list2 'byte-opcode-invert 'list)
3180(put 'byte-list3 'byte-opcode-invert 'list)
3181(put 'byte-list4 'byte-opcode-invert 'list)
3182(put 'byte-listN 'byte-opcode-invert 'list)
3183(put 'byte-concat2 'byte-opcode-invert 'concat)
3184(put 'byte-concat3 'byte-opcode-invert 'concat)
3185(put 'byte-concat4 'byte-opcode-invert 'concat)
3186(put 'byte-concatN 'byte-opcode-invert 'concat)
3187(put 'byte-insertN 'byte-opcode-invert 'insert)
3188
1c393159
JB
3189(byte-defop-compiler point 0)
3190;;(byte-defop-compiler mark 0) ;; obsolete
3191(byte-defop-compiler point-max 0)
3192(byte-defop-compiler point-min 0)
3193(byte-defop-compiler following-char 0)
3194(byte-defop-compiler preceding-char 0)
3195(byte-defop-compiler current-column 0)
3196(byte-defop-compiler eolp 0)
3197(byte-defop-compiler eobp 0)
3198(byte-defop-compiler bolp 0)
3199(byte-defop-compiler bobp 0)
3200(byte-defop-compiler current-buffer 0)
3201;;(byte-defop-compiler read-char 0) ;; obsolete
7200d79c 3202;; (byte-defop-compiler interactive-p 0) ;; Obsolete.
eef899a9
GM
3203(byte-defop-compiler widen 0)
3204(byte-defop-compiler end-of-line 0-1)
3205(byte-defop-compiler forward-char 0-1)
3206(byte-defop-compiler forward-line 0-1)
1c393159
JB
3207(byte-defop-compiler symbolp 1)
3208(byte-defop-compiler consp 1)
3209(byte-defop-compiler stringp 1)
3210(byte-defop-compiler listp 1)
3211(byte-defop-compiler not 1)
3212(byte-defop-compiler (null byte-not) 1)
3213(byte-defop-compiler car 1)
3214(byte-defop-compiler cdr 1)
3215(byte-defop-compiler length 1)
3216(byte-defop-compiler symbol-value 1)
3217(byte-defop-compiler symbol-function 1)
3218(byte-defop-compiler (1+ byte-add1) 1)
3219(byte-defop-compiler (1- byte-sub1) 1)
3220(byte-defop-compiler goto-char 1)
b8ae93ad 3221(byte-defop-compiler char-after 0-1)
1c393159
JB
3222(byte-defop-compiler set-buffer 1)
3223;;(byte-defop-compiler set-mark 1) ;; obsolete
eef899a9
GM
3224(byte-defop-compiler forward-word 0-1)
3225(byte-defop-compiler char-syntax 1)
3226(byte-defop-compiler nreverse 1)
3227(byte-defop-compiler car-safe 1)
3228(byte-defop-compiler cdr-safe 1)
3229(byte-defop-compiler numberp 1)
3230(byte-defop-compiler integerp 1)
3231(byte-defop-compiler skip-chars-forward 1-2)
3232(byte-defop-compiler skip-chars-backward 1-2)
1c393159
JB
3233(byte-defop-compiler eq 2)
3234(byte-defop-compiler memq 2)
3235(byte-defop-compiler cons 2)
3236(byte-defop-compiler aref 2)
3237(byte-defop-compiler set 2)
3238(byte-defop-compiler (= byte-eqlsign) 2)
3239(byte-defop-compiler (< byte-lss) 2)
3240(byte-defop-compiler (> byte-gtr) 2)
3241(byte-defop-compiler (<= byte-leq) 2)
3242(byte-defop-compiler (>= byte-geq) 2)
3243(byte-defop-compiler get 2)
3244(byte-defop-compiler nth 2)
3245(byte-defop-compiler substring 2-3)
eef899a9
GM
3246(byte-defop-compiler (move-marker byte-set-marker) 2-3)
3247(byte-defop-compiler set-marker 2-3)
3248(byte-defop-compiler match-beginning 1)
3249(byte-defop-compiler match-end 1)
3250(byte-defop-compiler upcase 1)
3251(byte-defop-compiler downcase 1)
3252(byte-defop-compiler string= 2)
3253(byte-defop-compiler string< 2)
3254(byte-defop-compiler (string-equal byte-string=) 2)
3255(byte-defop-compiler (string-lessp byte-string<) 2)
3256(byte-defop-compiler equal 2)
3257(byte-defop-compiler nthcdr 2)
3258(byte-defop-compiler elt 2)
3259(byte-defop-compiler member 2)
3260(byte-defop-compiler assq 2)
3261(byte-defop-compiler (rplaca byte-setcar) 2)
3262(byte-defop-compiler (rplacd byte-setcdr) 2)
3263(byte-defop-compiler setcar 2)
3264(byte-defop-compiler setcdr 2)
3265(byte-defop-compiler buffer-substring 2)
3266(byte-defop-compiler delete-region 2)
3267(byte-defop-compiler narrow-to-region 2)
3268(byte-defop-compiler (% byte-rem) 2)
1c393159
JB
3269(byte-defop-compiler aset 3)
3270
3271(byte-defop-compiler max byte-compile-associative)
3272(byte-defop-compiler min byte-compile-associative)
3273(byte-defop-compiler (+ byte-plus) byte-compile-associative)
eef899a9 3274(byte-defop-compiler (* byte-mult) byte-compile-associative)
1c393159 3275
eef899a9 3276;;####(byte-defop-compiler move-to-column 1)
1c393159
JB
3277(byte-defop-compiler-1 interactive byte-compile-noop)
3278
3279\f
3280(defun byte-compile-subr-wrong-args (form n)
ccb3c8de 3281 (byte-compile-set-symbol-position (car form))
1d5c17c0 3282 (byte-compile-warn "`%s' called with %d arg%s, but requires %s"
1c393159
JB
3283 (car form) (length (cdr form))
3284 (if (= 1 (length (cdr form))) "" "s") n)
29a4dcb0 3285 ;; Get run-time wrong-number-of-args error.
1c393159
JB
3286 (byte-compile-normal-call form))
3287
3288(defun byte-compile-no-args (form)
3289 (if (not (= (length form) 1))
3290 (byte-compile-subr-wrong-args form "none")
3291 (byte-compile-out (get (car form) 'byte-opcode) 0)))
3292
3293(defun byte-compile-one-arg (form)
3294 (if (not (= (length form) 2))
3295 (byte-compile-subr-wrong-args form 1)
3296 (byte-compile-form (car (cdr form))) ;; Push the argument
3297 (byte-compile-out (get (car form) 'byte-opcode) 0)))
3298
3299(defun byte-compile-two-args (form)
3300 (if (not (= (length form) 3))
3301 (byte-compile-subr-wrong-args form 2)
3302 (byte-compile-form (car (cdr form))) ;; Push the arguments
3303 (byte-compile-form (nth 2 form))
3304 (byte-compile-out (get (car form) 'byte-opcode) 0)))
3305
3306(defun byte-compile-three-args (form)
3307 (if (not (= (length form) 4))
3308 (byte-compile-subr-wrong-args form 3)
3309 (byte-compile-form (car (cdr form))) ;; Push the arguments
3310 (byte-compile-form (nth 2 form))
3311 (byte-compile-form (nth 3 form))
3312 (byte-compile-out (get (car form) 'byte-opcode) 0)))
3313
3314(defun byte-compile-zero-or-one-arg (form)
3315 (let ((len (length form)))
3316 (cond ((= len 1) (byte-compile-one-arg (append form '(nil))))
3317 ((= len 2) (byte-compile-one-arg form))
3318 (t (byte-compile-subr-wrong-args form "0-1")))))
3319
3320(defun byte-compile-one-or-two-args (form)
3321 (let ((len (length form)))
3322 (cond ((= len 2) (byte-compile-two-args (append form '(nil))))
3323 ((= len 3) (byte-compile-two-args form))
3324 (t (byte-compile-subr-wrong-args form "1-2")))))
3325
3326(defun byte-compile-two-or-three-args (form)
3327 (let ((len (length form)))
3328 (cond ((= len 3) (byte-compile-three-args (append form '(nil))))
3329 ((= len 4) (byte-compile-three-args form))
3330 (t (byte-compile-subr-wrong-args form "2-3")))))
3331
9ace101c 3332(defun byte-compile-noop (_form)
1c393159
JB
3333 (byte-compile-constant nil))
3334
b9598260 3335(defun byte-compile-discard (&optional num preserve-tos)
2ec42da9
SM
3336 "Output byte codes to discard the NUM entries at the top of the stack.
3337NUM defaults to 1.
b9598260
SM
3338If PRESERVE-TOS is non-nil, preserve the top-of-stack value, as if it were
3339popped before discarding the num values, and then pushed back again after
3340discarding."
3341 (if (and (null num) (not preserve-tos))
3342 ;; common case
3343 (byte-compile-out 'byte-discard)
3344 ;; general case
3345 (unless num
3346 (setq num 1))
3347 (when (and preserve-tos (> num 0))
3348 ;; Preserve the top-of-stack value by writing it directly to the stack
3349 ;; location which will be at the top-of-stack after popping.
3350 (byte-compile-stack-set (1- (- byte-compile-depth num)))
3351 ;; Now we actually discard one less value, since we want to keep
3352 ;; the eventual TOS
3353 (setq num (1- num)))
3354 (while (> num 0)
3355 (byte-compile-out 'byte-discard)
3356 (setq num (1- num)))))
3357
3358(defun byte-compile-stack-ref (stack-pos)
2ec42da9 3359 "Output byte codes to push the value at stack position STACK-POS."
3e21b6a7
SM
3360 (let ((dist (- byte-compile-depth (1+ stack-pos))))
3361 (if (zerop dist)
3362 ;; A simple optimization
3363 (byte-compile-out 'byte-dup)
3364 ;; normal case
3365 (byte-compile-out 'byte-stack-ref dist))))
b9598260
SM
3366
3367(defun byte-compile-stack-set (stack-pos)
2ec42da9 3368 "Output byte codes to store the TOS value at stack position STACK-POS."
3e21b6a7 3369 (byte-compile-out 'byte-stack-set (- byte-compile-depth (1+ stack-pos))))
1c393159 3370
cb9336bd
SM
3371(byte-defop-compiler-1 internal-make-closure byte-compile-make-closure)
3372(byte-defop-compiler-1 internal-get-closed-var byte-compile-get-closed-var)
3373
cb9336bd 3374(defun byte-compile-make-closure (form)
7200d79c 3375 "Byte-compile the special `internal-make-closure' form."
2ec42da9 3376 (if byte-compile--for-effect (setq byte-compile--for-effect nil)
876c194c
SM
3377 (let* ((vars (nth 1 form))
3378 (env (nth 2 form))
3379 (body (nthcdr 3 form))
3380 (fun
3381 (byte-compile-lambda `(lambda ,vars . ,body) nil (length env))))
f80efb86
SM
3382 (cl-assert (> (length env) 0)) ;Otherwise, we don't need a closure.
3383 (cl-assert (byte-code-function-p fun))
876c194c
SM
3384 (byte-compile-form `(make-byte-code
3385 ',(aref fun 0) ',(aref fun 1)
3386 (vconcat (vector . ,env) ',(aref fun 2))
3387 ,@(nthcdr 3 (mapcar (lambda (x) `',x) fun)))))))
cb9336bd
SM
3388
3389(defun byte-compile-get-closed-var (form)
7200d79c 3390 "Byte-compile the special `internal-get-closed-var' form."
2ec42da9 3391 (if byte-compile--for-effect (setq byte-compile--for-effect nil)
7200d79c 3392 (byte-compile-out 'byte-constant (nth 1 form))))
1c393159
JB
3393
3394;; Compile a function that accepts one or more args and is right-associative.
c0f43df5
RS
3395;; We do it by left-associativity so that the operations
3396;; are done in the same order as in interpreted code.
10809e0f
RS
3397;; We treat the one-arg case, as in (+ x), like (+ x 0).
3398;; in order to convert markers to numbers, and trigger expected errors.
1c393159
JB
3399(defun byte-compile-associative (form)
3400 (if (cdr form)
c0f43df5 3401 (let ((opcode (get (car form) 'byte-opcode))
24ae8da4
CY
3402 args)
3403 (if (and (< 3 (length form))
3404 (memq opcode (list (get '+ 'byte-opcode)
3405 (get '* 'byte-opcode))))
3406 ;; Don't use binary operations for > 2 operands, as that
3407 ;; may cause overflow/truncation in float operations.
3408 (byte-compile-normal-call form)
3409 (setq args (copy-sequence (cdr form)))
3410 (byte-compile-form (car args))
3411 (setq args (cdr args))
3412 (or args (setq args '(0)
3413 opcode (get '+ 'byte-opcode)))
3414 (dolist (arg args)
3415 (byte-compile-form arg)
3416 (byte-compile-out opcode 0))))
1c393159
JB
3417 (byte-compile-constant (eval form))))
3418
3419\f
3420;; more complicated compiler macros
3421
ec448ae2 3422(byte-defop-compiler char-before)
a746fb65
GM
3423(byte-defop-compiler backward-char)
3424(byte-defop-compiler backward-word)
1c393159
JB
3425(byte-defop-compiler list)
3426(byte-defop-compiler concat)
3427(byte-defop-compiler fset)
3428(byte-defop-compiler (indent-to-column byte-indent-to) byte-compile-indent-to)
3429(byte-defop-compiler indent-to)
3430(byte-defop-compiler insert)
3431(byte-defop-compiler-1 function byte-compile-function-form)
3432(byte-defop-compiler-1 - byte-compile-minus)
eef899a9
GM
3433(byte-defop-compiler (/ byte-quo) byte-compile-quo)
3434(byte-defop-compiler nconc)
1c393159 3435
ec448ae2
GM
3436(defun byte-compile-char-before (form)
3437 (cond ((= 2 (length form))
a746fb65
GM
3438 (byte-compile-form (list 'char-after (if (numberp (nth 1 form))
3439 (1- (nth 1 form))
3440 `(1- ,(nth 1 form))))))
3441 ((= 1 (length form))
3442 (byte-compile-form '(char-after (1- (point)))))
3443 (t (byte-compile-subr-wrong-args form "0-1"))))
3444
3445;; backward-... ==> forward-... with negated argument.
3446(defun byte-compile-backward-char (form)
3447 (cond ((= 2 (length form))
3448 (byte-compile-form (list 'forward-char (if (numberp (nth 1 form))
3449 (- (nth 1 form))
3450 `(- ,(nth 1 form))))))
3451 ((= 1 (length form))
3452 (byte-compile-form '(forward-char -1)))
3453 (t (byte-compile-subr-wrong-args form "0-1"))))
3454
3455(defun byte-compile-backward-word (form)
3456 (cond ((= 2 (length form))
3457 (byte-compile-form (list 'forward-word (if (numberp (nth 1 form))
3458 (- (nth 1 form))
3459 `(- ,(nth 1 form))))))
3460 ((= 1 (length form))
3461 (byte-compile-form '(forward-word -1)))
3462 (t (byte-compile-subr-wrong-args form "0-1"))))
ec448ae2 3463
1c393159
JB
3464(defun byte-compile-list (form)
3465 (let ((count (length (cdr form))))
3466 (cond ((= count 0)
3467 (byte-compile-constant nil))
3468 ((< count 5)
ed62683d 3469 (mapc 'byte-compile-form (cdr form))
1c393159
JB
3470 (byte-compile-out
3471 (aref [byte-list1 byte-list2 byte-list3 byte-list4] (1- count)) 0))
e5c89ce9 3472 ((< count 256)
ed62683d 3473 (mapc 'byte-compile-form (cdr form))
1c393159
JB
3474 (byte-compile-out 'byte-listN count))
3475 (t (byte-compile-normal-call form)))))
3476
3477(defun byte-compile-concat (form)
3478 (let ((count (length (cdr form))))
3479 (cond ((and (< 1 count) (< count 5))
ed62683d 3480 (mapc 'byte-compile-form (cdr form))
1c393159
JB
3481 (byte-compile-out
3482 (aref [byte-concat2 byte-concat3 byte-concat4] (- count 2))
3483 0))
3484 ;; Concat of one arg is not a no-op if arg is not a string.
3485 ((= count 0)
3486 (byte-compile-form ""))
e5c89ce9 3487 ((< count 256)
ed62683d 3488 (mapc 'byte-compile-form (cdr form))
1c393159
JB
3489 (byte-compile-out 'byte-concatN count))
3490 ((byte-compile-normal-call form)))))
3491
3492(defun byte-compile-minus (form)
24ae8da4
CY
3493 (let ((len (length form)))
3494 (cond
3495 ((= 1 len) (byte-compile-constant 0))
3496 ((= 2 len)
3497 (byte-compile-form (cadr form))
3498 (byte-compile-out 'byte-negate 0))
2b9c3b12 3499 ((= 3 len)
24ae8da4
CY
3500 (byte-compile-form (nth 1 form))
3501 (byte-compile-form (nth 2 form))
3502 (byte-compile-out 'byte-diff 0))
3503 ;; Don't use binary operations for > 2 operands, as that may
3504 ;; cause overflow/truncation in float operations.
3505 (t (byte-compile-normal-call form)))))
1c393159
JB
3506
3507(defun byte-compile-quo (form)
3508 (let ((len (length form)))
3509 (cond ((<= len 2)
3510 (byte-compile-subr-wrong-args form "2 or more"))
24ae8da4
CY
3511 ((= len 3)
3512 (byte-compile-two-args form))
1c393159 3513 (t
24ae8da4
CY
3514 ;; Don't use binary operations for > 2 operands, as that
3515 ;; may cause overflow/truncation in float operations.
3516 (byte-compile-normal-call form)))))
1c393159
JB
3517
3518(defun byte-compile-nconc (form)
3519 (let ((len (length form)))
3520 (cond ((= len 1)
3521 (byte-compile-constant nil))
3522 ((= len 2)
3523 ;; nconc of one arg is a noop, even if that arg isn't a list.
3524 (byte-compile-form (nth 1 form)))
3525 (t
3526 (byte-compile-form (car (setq form (cdr form))))
3527 (while (setq form (cdr form))
3528 (byte-compile-form (car form))
3529 (byte-compile-out 'byte-nconc 0))))))
3530
3531(defun byte-compile-fset (form)
3532 ;; warn about forms like (fset 'foo '(lambda () ...))
3533 ;; (where the lambda expression is non-trivial...)
3534 (let ((fn (nth 2 form))
3535 body)
3536 (if (and (eq (car-safe fn) 'quote)
3537 (eq (car-safe (setq fn (nth 1 fn))) 'lambda))
3538 (progn
3539 (setq body (cdr (cdr fn)))
3540 (if (stringp (car body)) (setq body (cdr body)))
3541 (if (eq 'interactive (car-safe (car body))) (setq body (cdr body)))
3542 (if (and (consp (car body))
3543 (not (eq 'byte-code (car (car body)))))
3544 (byte-compile-warn
1d5c17c0 3545 "A quoted lambda form is the second argument of `fset'. This is probably
1c393159 3546 not what you want, as that lambda cannot be compiled. Consider using
29a4dcb0 3547 the syntax #'(lambda (...) ...) instead.")))))
1c393159
JB
3548 (byte-compile-two-args form))
3549
1c393159
JB
3550;; (function foo) must compile like 'foo, not like (symbol-function 'foo).
3551;; Otherwise it will be incompatible with the interpreter,
3552;; and (funcall (function foo)) will lose with autoloads.
3553
3554(defun byte-compile-function-form (form)
bbca48fe
SM
3555 (byte-compile-constant (if (eq 'lambda (car-safe (nth 1 form)))
3556 (byte-compile-lambda (nth 1 form))
3557 (nth 1 form))))
1c393159
JB
3558
3559(defun byte-compile-indent-to (form)
3560 (let ((len (length form)))
3561 (cond ((= len 2)
3562 (byte-compile-form (car (cdr form)))
3563 (byte-compile-out 'byte-indent-to 0))
3564 ((= len 3)
3565 ;; no opcode for 2-arg case.
3566 (byte-compile-normal-call form))
3567 (t
3568 (byte-compile-subr-wrong-args form "1-2")))))
3569
3570(defun byte-compile-insert (form)
3571 (cond ((null (cdr form))
3572 (byte-compile-constant nil))
e5c89ce9 3573 ((<= (length form) 256)
ed62683d 3574 (mapc 'byte-compile-form (cdr form))
1c393159
JB
3575 (if (cdr (cdr form))
3576 (byte-compile-out 'byte-insertN (length (cdr form)))
3577 (byte-compile-out 'byte-insert 0)))
3578 ((memq t (mapcar 'consp (cdr (cdr form))))
3579 (byte-compile-normal-call form))
3580 ;; We can split it; there is no function call after inserting 1st arg.
3581 (t
3582 (while (setq form (cdr form))
3583 (byte-compile-form (car form))
3584 (byte-compile-out 'byte-insert 0)
3585 (if (cdr form)
3586 (byte-compile-discard))))))
3587
1c393159
JB
3588\f
3589(byte-defop-compiler-1 setq)
3590(byte-defop-compiler-1 setq-default)
3591(byte-defop-compiler-1 quote)
1c393159
JB
3592
3593(defun byte-compile-setq (form)
ca105506
SM
3594 (let ((args (cdr form)))
3595 (if args
3596 (while args
3597 (byte-compile-form (car (cdr args)))
3598 (or byte-compile--for-effect (cdr (cdr args))
1c393159 3599 (byte-compile-out 'byte-dup 0))
ca105506
SM
3600 (byte-compile-variable-set (car args))
3601 (setq args (cdr (cdr args))))
1c393159 3602 ;; (setq), with no arguments.
2ec42da9
SM
3603 (byte-compile-form nil byte-compile--for-effect))
3604 (setq byte-compile--for-effect nil)))
1c393159
JB
3605
3606(defun byte-compile-setq-default (form)
9ae0c310 3607 (setq form (cdr form))
c548f821
GM
3608 (if (null form) ; (setq-default), with no arguments
3609 (byte-compile-form nil byte-compile--for-effect)
3610 (if (> (length form) 2)
3611 (let ((setters ()))
3612 (while (consp form)
3613 (push `(setq-default ,(pop form) ,(pop form)) setters))
3614 (byte-compile-form (cons 'progn (nreverse setters))))
3615 (let ((var (car form)))
3616 (and (or (not (symbolp var))
3617 (macroexp--const-symbol-p var t))
3618 (byte-compile-warning-enabled-p 'constants)
3619 (byte-compile-warn
3620 "variable assignment to %s `%s'"
3621 (if (symbolp var) "constant" "nonvariable")
3622 (prin1-to-string var)))
3623 (byte-compile-normal-call `(set-default ',var ,@(cdr form)))))))
9ae0c310
SM
3624
3625(byte-defop-compiler-1 set-default)
3626(defun byte-compile-set-default (form)
3627 (let ((varexp (car-safe (cdr-safe form))))
3628 (if (eq (car-safe varexp) 'quote)
3629 ;; If the varexp is constant, compile it as a setq-default
3630 ;; so we get more warnings.
3631 (byte-compile-setq-default `(setq-default ,(car-safe (cdr varexp))
3632 ,@(cddr form)))
3633 (byte-compile-normal-call form))))
1c393159
JB
3634
3635(defun byte-compile-quote (form)
3636 (byte-compile-constant (car (cdr form))))
1c393159
JB
3637\f
3638;;; control structures
3639
ca105506
SM
3640(defun byte-compile-body (body &optional for-effect)
3641 (while (cdr body)
3642 (byte-compile-form (car body) t)
3643 (setq body (cdr body)))
3644 (byte-compile-form (car body) for-effect))
1c393159 3645
ca105506
SM
3646(defsubst byte-compile-body-do-effect (body)
3647 (byte-compile-body body byte-compile--for-effect)
2ec42da9 3648 (setq byte-compile--for-effect nil))
1c393159 3649
52799cb8 3650(defsubst byte-compile-form-do-effect (form)
2ec42da9
SM
3651 (byte-compile-form form byte-compile--for-effect)
3652 (setq byte-compile--for-effect nil))
1c393159
JB
3653
3654(byte-defop-compiler-1 inline byte-compile-progn)
3655(byte-defop-compiler-1 progn)
3656(byte-defop-compiler-1 prog1)
3657(byte-defop-compiler-1 prog2)
3658(byte-defop-compiler-1 if)
3659(byte-defop-compiler-1 cond)
3660(byte-defop-compiler-1 and)
3661(byte-defop-compiler-1 or)
3662(byte-defop-compiler-1 while)
3663(byte-defop-compiler-1 funcall)
1c393159 3664(byte-defop-compiler-1 let)
b38b1ec0 3665(byte-defop-compiler-1 let* byte-compile-let)
1c393159
JB
3666
3667(defun byte-compile-progn (form)
3668 (byte-compile-body-do-effect (cdr form)))
3669
3670(defun byte-compile-prog1 (form)
3671 (byte-compile-form-do-effect (car (cdr form)))
3672 (byte-compile-body (cdr (cdr form)) t))
3673
3674(defun byte-compile-prog2 (form)
3675 (byte-compile-form (nth 1 form) t)
3676 (byte-compile-form-do-effect (nth 2 form))
3677 (byte-compile-body (cdr (cdr (cdr form))) t))
3678
3679(defmacro byte-compile-goto-if (cond discard tag)
1639b803
DL
3680 `(byte-compile-goto
3681 (if ,cond
3682 (if ,discard 'byte-goto-if-not-nil 'byte-goto-if-not-nil-else-pop)
3683 (if ,discard 'byte-goto-if-nil 'byte-goto-if-nil-else-pop))
3684 ,tag))
1c393159 3685
70f41945
DN
3686;; Return the list of items in CONDITION-PARAM that match PRED-LIST.
3687;; Only return items that are not in ONLY-IF-NOT-PRESENT.
516b3653
JB
3688(defun byte-compile-find-bound-condition (condition-param
3689 pred-list
70f41945
DN
3690 &optional only-if-not-present)
3691 (let ((result nil)
3692 (nth-one nil)
516b3653 3693 (cond-list
70f41945
DN
3694 (if (memq (car-safe condition-param) pred-list)
3695 ;; The condition appears by itself.
3696 (list condition-param)
3697 ;; If the condition is an `and', look for matches among the
3698 ;; `and' arguments.
3699 (when (eq 'and (car-safe condition-param))
3700 (cdr condition-param)))))
516b3653 3701
70f41945
DN
3702 (dolist (crt cond-list)
3703 (when (and (memq (car-safe crt) pred-list)
3704 (eq 'quote (car-safe (setq nth-one (nth 1 crt))))
3705 ;; Ignore if the symbol is already on the unresolved
3706 ;; list.
3707 (not (assq (nth 1 nth-one) ; the relevant symbol
3708 only-if-not-present)))
3709 (push (nth 1 (nth 1 crt)) result)))
3710 result))
3711
6b61353c
KH
3712(defmacro byte-compile-maybe-guarded (condition &rest body)
3713 "Execute forms in BODY, potentially guarded by CONDITION.
82a726b4 3714CONDITION is a variable whose value is a test in an `if' or `cond'.
d6dc41d5
GM
3715BODY is the code to compile in the first arm of the if or the body of
3716the cond clause. If CONDITION's value is of the form (fboundp 'foo)
ad50a502 3717or (boundp 'foo), the relevant warnings from BODY about foo's
8480fc7c 3718being undefined (or obsolete) will be suppressed.
82a726b4 3719
b2e948ee 3720If CONDITION's value is (not (featurep 'emacs)) or (featurep 'xemacs),
ad50a502 3721that suppresses all warnings during execution of BODY."
6b61353c 3722 (declare (indent 1) (debug t))
516b3653 3723 `(let* ((fbound-list (byte-compile-find-bound-condition
e703069f 3724 ,condition '(fboundp functionp)
70f41945 3725 byte-compile-unresolved-functions))
516b3653 3726 (bound-list (byte-compile-find-bound-condition
e703069f 3727 ,condition '(boundp default-boundp)))
6b61353c
KH
3728 ;; Maybe add to the bound list.
3729 (byte-compile-bound-variables
ce5b520a 3730 (append bound-list byte-compile-bound-variables)))
82a726b4 3731 (unwind-protect
2ec42da9
SM
3732 ;; If things not being bound at all is ok, so must them being
3733 ;; obsolete. Note that we add to the existing lists since Tramp
3734 ;; (ab)uses this feature.
8480fc7c
GM
3735 (let ((byte-compile-not-obsolete-vars
3736 (append byte-compile-not-obsolete-vars bound-list))
3737 (byte-compile-not-obsolete-funcs
3738 (append byte-compile-not-obsolete-funcs fbound-list)))
3739 ,@body)
82a726b4 3740 ;; Maybe remove the function symbol from the unresolved list.
70f41945
DN
3741 (dolist (fbound fbound-list)
3742 (when fbound
82a726b4
RS
3743 (setq byte-compile-unresolved-functions
3744 (delq (assq fbound byte-compile-unresolved-functions)
70f41945 3745 byte-compile-unresolved-functions)))))))
6b61353c 3746
1c393159
JB
3747(defun byte-compile-if (form)
3748 (byte-compile-form (car (cdr form)))
b8234c84 3749 ;; Check whether we have `(if (fboundp ...' or `(if (boundp ...'
9858f6c3 3750 ;; and avoid warnings about the relevant symbols in the consequent.
6b61353c
KH
3751 (let ((clause (nth 1 form))
3752 (donetag (byte-compile-make-tag)))
b8234c84
DL
3753 (if (null (nthcdr 3 form))
3754 ;; No else-forms
3755 (progn
2ec42da9 3756 (byte-compile-goto-if nil byte-compile--for-effect donetag)
6b61353c 3757 (byte-compile-maybe-guarded clause
2ec42da9 3758 (byte-compile-form (nth 2 form) byte-compile--for-effect))
b8234c84
DL
3759 (byte-compile-out-tag donetag))
3760 (let ((elsetag (byte-compile-make-tag)))
3761 (byte-compile-goto 'byte-goto-if-nil elsetag)
6b61353c 3762 (byte-compile-maybe-guarded clause
2ec42da9 3763 (byte-compile-form (nth 2 form) byte-compile--for-effect))
b8234c84
DL
3764 (byte-compile-goto 'byte-goto donetag)
3765 (byte-compile-out-tag elsetag)
300f994a 3766 (byte-compile-maybe-guarded (list 'not clause)
2ec42da9 3767 (byte-compile-body (cdr (cdr (cdr form))) byte-compile--for-effect))
b8234c84 3768 (byte-compile-out-tag donetag))))
2ec42da9 3769 (setq byte-compile--for-effect nil))
1c393159
JB
3770
3771(defun byte-compile-cond (clauses)
3772 (let ((donetag (byte-compile-make-tag))
3773 nexttag clause)
3774 (while (setq clauses (cdr clauses))
3775 (setq clause (car clauses))
3776 (cond ((or (eq (car clause) t)
3777 (and (eq (car-safe (car clause)) 'quote)
3778 (car-safe (cdr-safe (car clause)))))
3779 ;; Unconditional clause
3780 (setq clause (cons t clause)
3781 clauses nil))
3782 ((cdr clauses)
3783 (byte-compile-form (car clause))
3784 (if (null (cdr clause))
3785 ;; First clause is a singleton.
2ec42da9 3786 (byte-compile-goto-if t byte-compile--for-effect donetag)
82a726b4
RS
3787 (setq nexttag (byte-compile-make-tag))
3788 (byte-compile-goto 'byte-goto-if-nil nexttag)
3789 (byte-compile-maybe-guarded (car clause)
2ec42da9 3790 (byte-compile-body (cdr clause) byte-compile--for-effect))
82a726b4
RS
3791 (byte-compile-goto 'byte-goto donetag)
3792 (byte-compile-out-tag nexttag)))))
1c393159 3793 ;; Last clause
6b61353c
KH
3794 (let ((guard (car clause)))
3795 (and (cdr clause) (not (eq guard t))
3796 (progn (byte-compile-form guard)
2ec42da9 3797 (byte-compile-goto-if nil byte-compile--for-effect donetag)
6b61353c
KH
3798 (setq clause (cdr clause))))
3799 (byte-compile-maybe-guarded guard
3800 (byte-compile-body-do-effect clause)))
1c393159
JB
3801 (byte-compile-out-tag donetag)))
3802
3803(defun byte-compile-and (form)
3804 (let ((failtag (byte-compile-make-tag))
ca105506
SM
3805 (args (cdr form)))
3806 (if (null args)
1c393159 3807 (byte-compile-form-do-effect t)
ca105506 3808 (byte-compile-and-recursion args failtag))))
8877fa6f 3809
83b0af6e 3810;; Handle compilation of a nontrivial `and' call.
8877fa6f
RS
3811;; We use tail recursion so we can use byte-compile-maybe-guarded.
3812(defun byte-compile-and-recursion (rest failtag)
3813 (if (cdr rest)
3814 (progn
3815 (byte-compile-form (car rest))
2ec42da9 3816 (byte-compile-goto-if nil byte-compile--for-effect failtag)
8877fa6f
RS
3817 (byte-compile-maybe-guarded (car rest)
3818 (byte-compile-and-recursion (cdr rest) failtag)))
3819 (byte-compile-form-do-effect (car rest))
3820 (byte-compile-out-tag failtag)))
1c393159
JB
3821
3822(defun byte-compile-or (form)
3823 (let ((wintag (byte-compile-make-tag))
ca105506
SM
3824 (args (cdr form)))
3825 (if (null args)
1c393159 3826 (byte-compile-form-do-effect nil)
ca105506 3827 (byte-compile-or-recursion args wintag))))
83b0af6e
RS
3828
3829;; Handle compilation of a nontrivial `or' call.
3830;; We use tail recursion so we can use byte-compile-maybe-guarded.
3831(defun byte-compile-or-recursion (rest wintag)
3832 (if (cdr rest)
3833 (progn
3834 (byte-compile-form (car rest))
2ec42da9 3835 (byte-compile-goto-if t byte-compile--for-effect wintag)
83b0af6e
RS
3836 (byte-compile-maybe-guarded (list 'not (car rest))
3837 (byte-compile-or-recursion (cdr rest) wintag)))
3838 (byte-compile-form-do-effect (car rest))
3839 (byte-compile-out-tag wintag)))
1c393159
JB
3840
3841(defun byte-compile-while (form)
3842 (let ((endtag (byte-compile-make-tag))
3843 (looptag (byte-compile-make-tag)))
3844 (byte-compile-out-tag looptag)
3845 (byte-compile-form (car (cdr form)))
2ec42da9 3846 (byte-compile-goto-if nil byte-compile--for-effect endtag)
1c393159
JB
3847 (byte-compile-body (cdr (cdr form)) t)
3848 (byte-compile-goto 'byte-goto looptag)
3849 (byte-compile-out-tag endtag)
2ec42da9 3850 (setq byte-compile--for-effect nil)))
1c393159
JB
3851
3852(defun byte-compile-funcall (form)
ed62683d 3853 (mapc 'byte-compile-form (cdr form))
1c393159
JB
3854 (byte-compile-out 'byte-call (length (cdr (cdr form)))))
3855
b9598260
SM
3856\f
3857;; let binding
3858
ce5b520a 3859(defun byte-compile-push-binding-init (clause)
b9598260 3860 "Emit byte-codes to push the initialization value for CLAUSE on the stack.
ce5b520a
SM
3861Return the offset in the form (VAR . OFFSET)."
3862 (let* ((var (if (consp clause) (car clause) clause)))
3863 ;; We record the stack position even of dynamic bindings and
3864 ;; variables in non-stack lexical environments; we'll put
3865 ;; them in the proper place below.
3866 (prog1 (cons var byte-compile-depth)
b9598260 3867 (if (consp clause)
ce5b520a
SM
3868 (byte-compile-form (cadr clause))
3869 (byte-compile-push-constant nil)))))
3870
3871(defun byte-compile-not-lexical-var-p (var)
b38b1ec0
SM
3872 (or (not (symbolp var))
3873 (special-variable-p var)
ce5b520a
SM
3874 (memq var byte-compile-bound-variables)
3875 (memq var '(nil t))
3876 (keywordp var)))
3877
3878(defun byte-compile-bind (var init-lexenv)
7200d79c 3879 "Emit byte-codes to bind VAR and update `byte-compile--lexical-environment'.
ce5b520a
SM
3880INIT-LEXENV should be a lexical-environment alist describing the
3881positions of the init value that have been pushed on the stack.
3882Return non-nil if the TOS value was popped."
3883 ;; The presence of lexical bindings mean that we may have to
b38b1ec0
SM
3884 ;; juggle things on the stack, to move them to TOS for
3885 ;; dynamic binding.
ce5b520a
SM
3886 (cond ((not (byte-compile-not-lexical-var-p var))
3887 ;; VAR is a simple stack-allocated lexical variable
3888 (push (assq var init-lexenv)
7200d79c 3889 byte-compile--lexical-environment)
ce5b520a
SM
3890 nil)
3891 ((eq var (caar init-lexenv))
3892 ;; VAR is dynamic and is on the top of the
3893 ;; stack, so we can just bind it like usual
3894 (byte-compile-dynamic-variable-bind var)
3895 t)
3896 (t
3897 ;; VAR is dynamic, but we have to get its
3898 ;; value out of the middle of the stack
3899 (let ((stack-pos (cdr (assq var init-lexenv))))
3900 (byte-compile-stack-ref stack-pos)
3901 (byte-compile-dynamic-variable-bind var)
3902 ;; Now we have to store nil into its temporary
3903 ;; stack position to avoid problems with GC
3904 (byte-compile-push-constant nil)
3905 (byte-compile-stack-set stack-pos))
3906 nil)))
3907
3908(defun byte-compile-unbind (clauses init-lexenv
3909 &optional preserve-body-value)
3910 "Emit byte-codes to unbind the variables bound by CLAUSES.
3911CLAUSES is a `let'-style variable binding list. INIT-LEXENV should be a
3912lexical-environment alist describing the positions of the init value that
3913have been pushed on the stack. If PRESERVE-BODY-VALUE is true,
3914then an additional value on the top of the stack, above any lexical binding
3915slots, is preserved, so it will be on the top of the stack after all
3916binding slots have been popped."
3917 ;; Unbind dynamic variables
3918 (let ((num-dynamic-bindings 0))
3919 (dolist (clause clauses)
3920 (unless (assq (if (consp clause) (car clause) clause)
7200d79c 3921 byte-compile--lexical-environment)
ce5b520a
SM
3922 (setq num-dynamic-bindings (1+ num-dynamic-bindings))))
3923 (unless (zerop num-dynamic-bindings)
3924 (byte-compile-out 'byte-unbind num-dynamic-bindings)))
3925 ;; Pop lexical variables off the stack, possibly preserving the
3926 ;; return value of the body.
3927 (when init-lexenv
3928 ;; INIT-LEXENV contains all init values left on the stack
3929 (byte-compile-discard (length init-lexenv) preserve-body-value)))
1c393159
JB
3930
3931(defun byte-compile-let (form)
b9598260 3932 "Generate code for the `let' form FORM."
b38b1ec0
SM
3933 (let ((clauses (cadr form))
3934 (init-lexenv nil))
3935 (when (eq (car form) 'let)
3936 ;; First compute the binding values in the old scope.
3937 (dolist (var clauses)
3938 (push (byte-compile-push-binding-init var) init-lexenv)))
3939 ;; New scope.
3940 (let ((byte-compile-bound-variables byte-compile-bound-variables)
7200d79c
SM
3941 (byte-compile--lexical-environment
3942 byte-compile--lexical-environment))
b38b1ec0
SM
3943 ;; Bind the variables.
3944 ;; For `let', do it in reverse order, because it makes no
3945 ;; semantic difference, but it is a lot more efficient since the
3946 ;; values are now in reverse order on the stack.
3947 (dolist (var (if (eq (car form) 'let) (reverse clauses) clauses))
3948 (unless (eq (car form) 'let)
3949 (push (byte-compile-push-binding-init var) init-lexenv))
3950 (let ((var (if (consp var) (car var) var)))
3951 (cond ((null lexical-binding)
3952 ;; If there are no lexical bindings, we can do things simply.
3953 (byte-compile-dynamic-variable-bind var))
3954 ((byte-compile-bind var init-lexenv)
3955 (pop init-lexenv)))))
ce5b520a 3956 ;; Emit the body.
b38b1ec0
SM
3957 (let ((init-stack-depth byte-compile-depth))
3958 (byte-compile-body-do-effect (cdr (cdr form)))
3959 ;; Unbind the variables.
3960 (if lexical-binding
3961 ;; Unbind both lexical and dynamic variables.
3962 (progn
f80efb86
SM
3963 (cl-assert (or (eq byte-compile-depth init-stack-depth)
3964 (eq byte-compile-depth (1+ init-stack-depth))))
b38b1ec0
SM
3965 (byte-compile-unbind clauses init-lexenv (> byte-compile-depth
3966 init-stack-depth)))
3967 ;; Unbind dynamic variables.
3968 (byte-compile-out 'byte-unbind (length clauses)))))))
1c393159 3969
b9598260 3970\f
1c393159
JB
3971
3972(byte-defop-compiler-1 /= byte-compile-negated)
3973(byte-defop-compiler-1 atom byte-compile-negated)
3974(byte-defop-compiler-1 nlistp byte-compile-negated)
3975
3976(put '/= 'byte-compile-negated-op '=)
3977(put 'atom 'byte-compile-negated-op 'consp)
3978(put 'nlistp 'byte-compile-negated-op 'listp)
3979
3980(defun byte-compile-negated (form)
3981 (byte-compile-form-do-effect (byte-compile-negation-optimizer form)))
3982
3983;; Even when optimization is off, /= is optimized to (not (= ...)).
3984(defun byte-compile-negation-optimizer (form)
3985 ;; an optimizer for forms where <form1> is less efficient than (not <form2>)
ccb3c8de 3986 (byte-compile-set-symbol-position (car form))
1c393159
JB
3987 (list 'not
3988 (cons (or (get (car form) 'byte-compile-negated-op)
3989 (error
52799cb8 3990 "Compiler error: `%s' has no `byte-compile-negated-op' property"
1c393159
JB
3991 (car form)))
3992 (cdr form))))
3993\f
3994;;; other tricky macro-like special-forms
3995
3996(byte-defop-compiler-1 catch)
3997(byte-defop-compiler-1 unwind-protect)
3998(byte-defop-compiler-1 condition-case)
3999(byte-defop-compiler-1 save-excursion)
f3e472b0 4000(byte-defop-compiler-1 save-current-buffer)
1c393159 4001(byte-defop-compiler-1 save-restriction)
7200d79c
SM
4002;; (byte-defop-compiler-1 save-window-excursion) ;Obsolete: now a macro.
4003;; (byte-defop-compiler-1 with-output-to-temp-buffer) ;Obsolete: now a macro.
6e8d0db7 4004(byte-defop-compiler-1 track-mouse)
1c393159
JB
4005
4006(defun byte-compile-catch (form)
4007 (byte-compile-form (car (cdr form)))
d779e73c
SM
4008 (pcase (cddr form)
4009 (`(:fun-body ,f)
4010 (byte-compile-form `(list 'funcall ,f)))
4011 (body
4012 (byte-compile-push-constant
2ec42da9 4013 (byte-compile-top-level (cons 'progn body) byte-compile--for-effect))))
1c393159
JB
4014 (byte-compile-out 'byte-catch 0))
4015
4016(defun byte-compile-unwind-protect (form)
d779e73c
SM
4017 (pcase (cddr form)
4018 (`(:fun-body ,f)
4019 (byte-compile-form `(list (list 'funcall ,f))))
4020 (handlers
4021 (byte-compile-push-constant
4022 (byte-compile-top-level-body handlers t))))
1c393159
JB
4023 (byte-compile-out 'byte-unwind-protect 0)
4024 (byte-compile-form-do-effect (car (cdr form)))
4025 (byte-compile-out 'byte-unbind 1))
4026
6e8d0db7 4027(defun byte-compile-track-mouse (form)
d7846e08 4028 (byte-compile-form
d779e73c
SM
4029 (pcase form
4030 (`(,_ :fun-body ,f) `(eval (list 'track-mouse (list 'funcall ,f))))
4031 (_ `(eval '(track-mouse ,@(byte-compile-top-level-body (cdr form))))))))
6e8d0db7 4032
1c393159
JB
4033(defun byte-compile-condition-case (form)
4034 (let* ((var (nth 1 form))
ce5b520a
SM
4035 (fun-bodies (eq var :fun-body))
4036 (byte-compile-bound-variables
4037 (if (and var (not fun-bodies))
4038 (cons var byte-compile-bound-variables)
1c393159 4039 byte-compile-bound-variables)))
ccb3c8de
CW
4040 (byte-compile-set-symbol-position 'condition-case)
4041 (unless (symbolp var)
4042 (byte-compile-warn
1d5c17c0 4043 "`%s' is not a variable-name or nil (in condition-case)" var))
d779e73c 4044 (if fun-bodies (setq var (make-symbol "err")))
1c393159 4045 (byte-compile-push-constant var)
d779e73c
SM
4046 (if fun-bodies
4047 (byte-compile-form `(list 'funcall ,(nth 2 form)))
4048 (byte-compile-push-constant
2ec42da9 4049 (byte-compile-top-level (nth 2 form) byte-compile--for-effect)))
d779e73c
SM
4050 (let ((compiled-clauses
4051 (mapcar
4052 (lambda (clause)
4053 (let ((condition (car clause)))
4054 (cond ((not (or (symbolp condition)
4055 (and (listp condition)
4056 (let ((ok t))
4057 (dolist (sym condition)
4058 (if (not (symbolp sym))
4059 (setq ok nil)))
4060 ok))))
4061 (byte-compile-warn
4062 "`%S' is not a condition name or list of such (in condition-case)"
4063 condition))
4064 ;; (not (or (eq condition 't)
4065 ;; (and (stringp (get condition 'error-message))
4066 ;; (consp (get condition
4067 ;; 'error-conditions)))))
4068 ;; (byte-compile-warn
4069 ;; "`%s' is not a known condition name
4070 ;; (in condition-case)"
4071 ;; condition))
4072 )
4073 (if fun-bodies
4074 `(list ',condition (list 'funcall ,(cadr clause) ',var))
4075 (cons condition
4076 (byte-compile-top-level-body
2ec42da9 4077 (cdr clause) byte-compile--for-effect)))))
d779e73c
SM
4078 (cdr (cdr (cdr form))))))
4079 (if fun-bodies
4080 (byte-compile-form `(list ,@compiled-clauses))
4081 (byte-compile-push-constant compiled-clauses)))
1c393159
JB
4082 (byte-compile-out 'byte-condition-case 0)))
4083
4084
4085(defun byte-compile-save-excursion (form)
62a258a7
SM
4086 (if (and (eq 'set-buffer (car-safe (car-safe (cdr form))))
4087 (byte-compile-warning-enabled-p 'suspicious))
0a57d256
SM
4088 (byte-compile-warn
4089 "Use `with-current-buffer' rather than save-excursion+set-buffer"))
1c393159
JB
4090 (byte-compile-out 'byte-save-excursion 0)
4091 (byte-compile-body-do-effect (cdr form))
4092 (byte-compile-out 'byte-unbind 1))
4093
4094(defun byte-compile-save-restriction (form)
4095 (byte-compile-out 'byte-save-restriction 0)
4096 (byte-compile-body-do-effect (cdr form))
4097 (byte-compile-out 'byte-unbind 1))
4098
f3e472b0
RS
4099(defun byte-compile-save-current-buffer (form)
4100 (byte-compile-out 'byte-save-current-buffer 0)
4101 (byte-compile-body-do-effect (cdr form))
4102 (byte-compile-out 'byte-unbind 1))
1c393159
JB
4103\f
4104;;; top-level forms elsewhere
4105
1c393159
JB
4106(byte-defop-compiler-1 defvar)
4107(byte-defop-compiler-1 defconst byte-compile-defvar)
4108(byte-defop-compiler-1 autoload)
4109(byte-defop-compiler-1 lambda byte-compile-lambda-form)
4110
3aaaa6f1
SM
4111;; If foo.el declares `toto' as obsolete, it is likely that foo.el will
4112;; actually use `toto' in order for this obsolete variable to still work
4113;; correctly, so paradoxically, while byte-compiling foo.el, the presence
4114;; of a make-obsolete-variable call for `toto' is an indication that `toto'
4115;; should not trigger obsolete-warnings in foo.el.
4116(byte-defop-compiler-1 make-obsolete-variable)
4117(defun byte-compile-make-obsolete-variable (form)
4118 (when (eq 'quote (car-safe (nth 1 form)))
bac7ff22 4119 (push (nth 1 (nth 1 form)) byte-compile-global-not-obsolete-vars))
3aaaa6f1
SM
4120 (byte-compile-normal-call form))
4121
f3934f6f
SM
4122(defconst byte-compile-tmp-var (make-symbol "def-tmp-var"))
4123
1c393159 4124(defun byte-compile-defvar (form)
f3934f6f 4125 ;; This is not used for file-level defvar/consts.
4f1e9960 4126 (when (and (symbolp (nth 1 form))
3fe6ef4e 4127 (not (string-match "[-*/:$]" (symbol-name (nth 1 form))))
4f1e9960 4128 (byte-compile-warning-enabled-p 'lexical))
7a16788b 4129 (byte-compile-warn "global/dynamic var `%s' lacks a prefix"
4f1e9960 4130 (nth 1 form)))
1bc20d83
GM
4131 (let ((fun (nth 0 form))
4132 (var (nth 1 form))
1c393159
JB
4133 (value (nth 2 form))
4134 (string (nth 3 form)))
ccb3c8de 4135 (byte-compile-set-symbol-position fun)
6c2161c4
SM
4136 (when (or (> (length form) 4)
4137 (and (eq fun 'defconst) (null (cddr form))))
d0e07261
SM
4138 (let ((ncall (length (cdr form))))
4139 (byte-compile-warn
1d5c17c0 4140 "`%s' called with %d argument%s, but %s %s"
d0e07261
SM
4141 fun ncall
4142 (if (= 1 ncall) "" "s")
4143 (if (< ncall 2) "requires" "accepts only")
4144 "2-3")))
2aea6521
GM
4145 (push var byte-compile-bound-variables)
4146 (if (eq fun 'defconst)
4147 (push var byte-compile-const-variables))
f3934f6f
SM
4148 (when (and string (not (stringp string)))
4149 (byte-compile-warn "third arg to `%s %s' is not a string: %s"
4150 fun var string))
4151 (byte-compile-form-do-effect
4152 (if (cddr form) ; `value' provided
4153 ;; Quote with `quote' to prevent byte-compiling the body,
4154 ;; which would lead to an inf-loop.
4155 `(funcall '(lambda (,byte-compile-tmp-var)
4156 (,fun ,var ,byte-compile-tmp-var ,@(nthcdr 3 form)))
4157 ,value)
4158 (if (eq fun 'defconst)
4159 ;; This will signal an appropriate error at runtime.
4160 `(eval ',form)
4161 ;; A simple (defvar foo) just returns foo.
4162 `',var)))))
1c393159
JB
4163
4164(defun byte-compile-autoload (form)
ccb3c8de 4165 (byte-compile-set-symbol-position 'autoload)
4dd1c416
SM
4166 (and (macroexp-const-p (nth 1 form))
4167 (macroexp-const-p (nth 5 form))
1c393159
JB
4168 (eval (nth 5 form)) ; macro-p
4169 (not (fboundp (eval (nth 1 form))))
4170 (byte-compile-warn
c5091f25 4171 "The compiler ignores `autoload' except at top level. You should
1c393159
JB
4172 probably put the autoload of the macro `%s' at top-level."
4173 (eval (nth 1 form))))
4174 (byte-compile-normal-call form))
4175
c5091f25 4176;; Lambdas in valid places are handled as special cases by various code.
1c393159 4177;; The ones that remain are errors.
9ace101c 4178(defun byte-compile-lambda-form (_form)
ccb3c8de 4179 (byte-compile-set-symbol-position 'lambda)
1c393159
JB
4180 (error "`lambda' used as function name is invalid"))
4181
5286a842 4182;; Compile normally, but deal with warnings for the function being defined.
977b50fb 4183(put 'defalias 'byte-hunk-handler 'byte-compile-file-form-defalias)
d1dc2cc2 4184;; Used for eieio--defalias as well.
977b50fb 4185(defun byte-compile-file-form-defalias (form)
61b108cc
SM
4186 ;; For the compilation itself, we could largely get rid of this hunk-handler,
4187 ;; if it weren't for the fact that we need to figure out when a defalias
4188 ;; defines a macro, so as to add it to byte-compile-macro-environment.
4189 ;;
4190 ;; FIXME: we also use this hunk-handler to implement the function's dynamic
4191 ;; docstring feature. We could actually implement it more elegantly in
4192 ;; byte-compile-lambda so it applies to all lambdas, but the problem is that
4193 ;; the resulting .elc format will not be recognized by make-docfile, so
4194 ;; either we stop using DOC for the docstrings of preloaded elc files (at the
4195 ;; cost of around 24KB on 32bit hosts, double on 64bit hosts) or we need to
4196 ;; build DOC in a more clever way (e.g. handle anonymous elements).
4197 (let ((byte-compile-free-references nil)
4198 (byte-compile-free-assignments nil))
4199 (pcase form
4200 ;; Decompose `form' into:
4201 ;; - `name' is the name of the defined function.
4202 ;; - `arg' is the expression to which it is defined.
4203 ;; - `rest' is the rest of the arguments.
4204 (`(,_ ',name ,arg . ,rest)
4205 (pcase-let*
4206 ;; `macro' is non-nil if it defines a macro.
4207 ;; `fun' is the function part of `arg' (defaults to `arg').
4208 (((or (and (or `(cons 'macro ,fun) `'(macro . ,fun)) (let macro t))
4209 (and (let fun arg) (let macro nil)))
4210 arg)
4211 ;; `lam' is the lambda expression in `fun' (or nil if not
4212 ;; recognized).
4213 ((or `(,(or `quote `function) ,lam) (let lam nil))
4214 fun)
4215 ;; `arglist' is the list of arguments (or t if not recognized).
4216 ;; `body' is the body of `lam' (or t if not recognized).
4217 ((or `(lambda ,arglist . ,body)
4218 ;; `(closure ,_ ,arglist . ,body)
4219 (and `(internal-make-closure ,arglist . ,_) (let body t))
4220 (and (let arglist t) (let body t)))
4221 lam))
4222 (unless (byte-compile-file-form-defmumble
4223 name macro arglist body rest)
4224 (byte-compile-keep-pending form))))
4225
4226 ;; We used to just do: (byte-compile-normal-call form)
4227 ;; But it turns out that this fails to optimize the code.
4228 ;; So instead we now do the same as what other byte-hunk-handlers do,
4229 ;; which is to call back byte-compile-file-form and then return nil.
4230 ;; Except that we can't just call byte-compile-file-form since it would
4231 ;; call us right back.
4232 (t (byte-compile-keep-pending form)))))
3c9dc1cf
RS
4233
4234(byte-defop-compiler-1 with-no-warnings byte-compile-no-warnings)
4235(defun byte-compile-no-warnings (form)
4236 (let (byte-compile-warnings)
a4f66531 4237 (byte-compile-form (cons 'progn (cdr form)))))
01e4a4fa
SM
4238
4239;; Warn about misuses of make-variable-buffer-local.
49fec531
SM
4240(byte-defop-compiler-1 make-variable-buffer-local
4241 byte-compile-make-variable-buffer-local)
01e4a4fa 4242(defun byte-compile-make-variable-buffer-local (form)
15ce9dcf 4243 (if (and (eq (car-safe (car-safe (cdr-safe form))) 'quote)
cf637a34 4244 (byte-compile-warning-enabled-p 'make-local))
01e4a4fa
SM
4245 (byte-compile-warn
4246 "`make-variable-buffer-local' should be called at toplevel"))
4247 (byte-compile-normal-call form))
4248(put 'make-variable-buffer-local
4249 'byte-hunk-handler 'byte-compile-form-make-variable-buffer-local)
4250(defun byte-compile-form-make-variable-buffer-local (form)
4251 (byte-compile-keep-pending form 'byte-compile-normal-call))
4252
478d6f95
SM
4253(byte-defop-compiler-1 add-to-list byte-compile-add-to-list)
4254(defun byte-compile-add-to-list (form)
4255 ;; FIXME: This could be used for `set' as well, except that it's got
4256 ;; its own opcode, so the final `byte-compile-normal-call' needs to
4257 ;; be replaced with something else.
4258 (pcase form
4259 (`(,fun ',var . ,_)
4260 (byte-compile-check-variable var 'assign)
4261 (if (assq var byte-compile--lexical-environment)
4262 (byte-compile-log-warning
4263 (format "%s cannot use lexical var `%s'" fun var)
4264 nil :error)
4265 (unless (or (not (byte-compile-warning-enabled-p 'free-vars))
4266 (boundp var)
4267 (memq var byte-compile-bound-variables)
4268 (memq var byte-compile-free-references))
4269 (byte-compile-warn "assignment to free variable `%S'" var)
4270 (push var byte-compile-free-references)))))
4271 (byte-compile-normal-call form))
1c393159
JB
4272\f
4273;;; tags
4274
4275;; Note: Most operations will strip off the 'TAG, but it speeds up
4276;; optimization to have the 'TAG as a part of the tag.
4277;; Tags will be (TAG . (tag-number . stack-depth)).
4278(defun byte-compile-make-tag ()
4279 (list 'TAG (setq byte-compile-tag-number (1+ byte-compile-tag-number))))
4280
4281
4282(defun byte-compile-out-tag (tag)
4283 (setq byte-compile-output (cons tag byte-compile-output))
4284 (if (cdr (cdr tag))
4285 (progn
4286 ;; ## remove this someday
4287 (and byte-compile-depth
b38b1ec0
SM
4288 (not (= (cdr (cdr tag)) byte-compile-depth))
4289 (error "Compiler bug: depth conflict at tag %d" (car (cdr tag))))
1c393159
JB
4290 (setq byte-compile-depth (cdr (cdr tag))))
4291 (setcdr (cdr tag) byte-compile-depth)))
4292
4293(defun byte-compile-goto (opcode tag)
6c2161c4 4294 (push (cons opcode tag) byte-compile-output)
1c393159
JB
4295 (setcdr (cdr tag) (if (memq opcode byte-goto-always-pop-ops)
4296 (1- byte-compile-depth)
4297 byte-compile-depth))
4298 (setq byte-compile-depth (and (not (eq opcode 'byte-goto))
4299 (1- byte-compile-depth))))
4300
b9598260
SM
4301(defun byte-compile-stack-adjustment (op operand)
4302 "Return the amount by which an operation adjusts the stack.
4303OP and OPERAND are as passed to `byte-compile-out'."
4304 (if (memq op '(byte-call byte-discardN byte-discardN-preserve-tos))
4305 ;; For calls, OPERAND is the number of args, so we pop OPERAND + 1
4306 ;; elements, and the push the result, for a total of -OPERAND.
4307 ;; For discardN*, of course, we just pop OPERAND elements.
4308 (- operand)
4309 (or (aref byte-stack+-info (symbol-value op))
4310 ;; Ops with a nil entry in `byte-stack+-info' are byte-codes
4311 ;; that take OPERAND values off the stack and push a result, for
4312 ;; a total of 1 - OPERAND
4313 (- 1 operand))))
7200d79c 4314
b9598260
SM
4315(defun byte-compile-out (op &optional operand)
4316 (push (cons op operand) byte-compile-output)
4317 (if (eq op 'byte-return)
4318 ;; This is actually an unnecessary case, because there should be no
4319 ;; more ops behind byte-return.
4320 (setq byte-compile-depth nil)
4321 (setq byte-compile-depth
4322 (+ byte-compile-depth (byte-compile-stack-adjustment op operand)))
4323 (setq byte-compile-maxdepth (max byte-compile-depth byte-compile-maxdepth))
4324 ;;(if (< byte-compile-depth 0) (error "Compiler error: stack underflow"))
4325 ))
1c393159
JB
4326\f
4327;;; call tree stuff
4328
4329(defun byte-compile-annotate-call-tree (form)
4330 (let (entry)
4331 ;; annotate the current call
4332 (if (setq entry (assq (car form) byte-compile-call-tree))
4333 (or (memq byte-compile-current-form (nth 1 entry)) ;callers
4334 (setcar (cdr entry)
4335 (cons byte-compile-current-form (nth 1 entry))))
4336 (setq byte-compile-call-tree
4337 (cons (list (car form) (list byte-compile-current-form) nil)
4338 byte-compile-call-tree)))
4339 ;; annotate the current function
4340 (if (setq entry (assq byte-compile-current-form byte-compile-call-tree))
4341 (or (memq (car form) (nth 2 entry)) ;called
4342 (setcar (cdr (cdr entry))
4343 (cons (car form) (nth 2 entry))))
4344 (setq byte-compile-call-tree
4345 (cons (list byte-compile-current-form nil (list (car form)))
4346 byte-compile-call-tree)))
4347 ))
4348
52799cb8
RS
4349;; Renamed from byte-compile-report-call-tree
4350;; to avoid interfering with completion of byte-compile-file.
fd5285f3 4351;;;###autoload
52799cb8
RS
4352(defun display-call-tree (&optional filename)
4353 "Display a call graph of a specified file.
4354This lists which functions have been called, what functions called
4355them, and what functions they call. The list includes all functions
4356whose definitions have been compiled in this Emacs session, as well as
4357all functions called by those functions.
1c393159 4358
52799cb8
RS
4359The call graph does not include macros, inline functions, or
4360primitives that the byte-code interpreter knows about directly \(eq,
4361cons, etc.\).
1c393159
JB
4362
4363The call tree also lists those functions which are not known to be called
52799cb8
RS
4364\(that is, to which no calls have been compiled\), and which cannot be
4365invoked interactively."
1c393159
JB
4366 (interactive)
4367 (message "Generating call tree...")
4368 (with-output-to-temp-buffer "*Call-Tree*"
4369 (set-buffer "*Call-Tree*")
4370 (erase-buffer)
47cf9d3a 4371 (message "Generating call tree... (sorting on %s)"
1c393159
JB
4372 byte-compile-call-tree-sort)
4373 (insert "Call tree for "
4374 (cond ((null byte-compile-current-file) (or filename "???"))
4375 ((stringp byte-compile-current-file)
4376 byte-compile-current-file)
4377 (t (buffer-name byte-compile-current-file)))
4378 " sorted on "
4379 (prin1-to-string byte-compile-call-tree-sort)
4380 ":\n\n")
4381 (if byte-compile-call-tree-sort
4382 (setq byte-compile-call-tree
4383 (sort byte-compile-call-tree
f58e0fd5
SM
4384 (pcase byte-compile-call-tree-sort
4385 (`callers
2ec42da9
SM
4386 (lambda (x y) (< (length (nth 1 x))
4387 (length (nth 1 y)))))
f58e0fd5 4388 (`calls
2ec42da9
SM
4389 (lambda (x y) (< (length (nth 2 x))
4390 (length (nth 2 y)))))
f58e0fd5 4391 (`calls+callers
2ec42da9
SM
4392 (lambda (x y) (< (+ (length (nth 1 x))
4393 (length (nth 2 x)))
4394 (+ (length (nth 1 y))
4395 (length (nth 2 y))))))
f58e0fd5 4396 (`name
2ec42da9 4397 (lambda (x y) (string< (car x) (car y))))
f58e0fd5 4398 (_ (error "`byte-compile-call-tree-sort': `%s' - unknown sort mode"
2ec42da9 4399 byte-compile-call-tree-sort))))))
1c393159
JB
4400 (message "Generating call tree...")
4401 (let ((rest byte-compile-call-tree)
4402 (b (current-buffer))
4403 f p
4404 callers calls)
4405 (while rest
4406 (prin1 (car (car rest)) b)
4407 (setq callers (nth 1 (car rest))
4408 calls (nth 2 (car rest)))
4409 (insert "\t"
4410 (cond ((not (fboundp (setq f (car (car rest)))))
4411 (if (null f)
4412 " <top level>";; shouldn't insert nil then, actually -sk
4413 " <not defined>"))
4414 ((subrp (setq f (symbol-function f)))
4415 " <subr>")
4416 ((symbolp f)
4417 (format " ==> %s" f))
ed015bdd 4418 ((byte-code-function-p f)
1c393159
JB
4419 "<compiled function>")
4420 ((not (consp f))
4421 "<malformed function>")
4422 ((eq 'macro (car f))
ed015bdd 4423 (if (or (byte-code-function-p (cdr f))
1c393159
JB
4424 (assq 'byte-code (cdr (cdr (cdr f)))))
4425 " <compiled macro>"
4426 " <macro>"))
4427 ((assq 'byte-code (cdr (cdr f)))
4428 "<compiled lambda>")
4429 ((eq 'lambda (car f))
4430 "<function>")
4431 (t "???"))
4432 (format " (%d callers + %d calls = %d)"
4433 ;; Does the optimizer eliminate common subexpressions?-sk
4434 (length callers)
4435 (length calls)
4436 (+ (length callers) (length calls)))
4437 "\n")
4438 (if callers
4439 (progn
4440 (insert " called by:\n")
4441 (setq p (point))
4442 (insert " " (if (car callers)
4443 (mapconcat 'symbol-name callers ", ")
4444 "<top level>"))
4445 (let ((fill-prefix " "))
78bba1c8
TTN
4446 (fill-region-as-paragraph p (point)))
4447 (unless (= 0 (current-column))
4448 (insert "\n"))))
1c393159
JB
4449 (if calls
4450 (progn
4451 (insert " calls:\n")
4452 (setq p (point))
4453 (insert " " (mapconcat 'symbol-name calls ", "))
4454 (let ((fill-prefix " "))
78bba1c8
TTN
4455 (fill-region-as-paragraph p (point)))
4456 (unless (= 0 (current-column))
4457 (insert "\n"))))
1c393159
JB
4458 (setq rest (cdr rest)))
4459
4460 (message "Generating call tree...(finding uncalled functions...)")
4461 (setq rest byte-compile-call-tree)
416d3588 4462 (let (uncalled def)
1c393159
JB
4463 (while rest
4464 (or (nth 1 (car rest))
416d3588
GM
4465 (null (setq f (caar rest)))
4466 (progn
4467 (setq def (byte-compile-fdefinition f t))
4468 (and (eq (car-safe def) 'macro)
4469 (eq (car-safe (cdr-safe def)) 'lambda)
4470 (setq def (cdr def)))
4471 (functionp def))
4472 (progn
4473 (setq def (byte-compile-fdefinition f nil))
4474 (and (eq (car-safe def) 'macro)
4475 (eq (car-safe (cdr-safe def)) 'lambda)
4476 (setq def (cdr def)))
4477 (commandp def))
1c393159
JB
4478 (setq uncalled (cons f uncalled)))
4479 (setq rest (cdr rest)))
4480 (if uncalled
4481 (let ((fill-prefix " "))
4482 (insert "Noninteractive functions not known to be called:\n ")
4483 (setq p (point))
4484 (insert (mapconcat 'symbol-name (nreverse uncalled) ", "))
416d3588
GM
4485 (fill-region-as-paragraph p (point))))))
4486 (message "Generating call tree...done.")))
1c393159
JB
4487
4488\f
814c447f 4489;;;###autoload
7e7d0f8b
RS
4490(defun batch-byte-compile-if-not-done ()
4491 "Like `byte-compile-file' but doesn't recompile if already up to date.
4492Use this from the command line, with `-batch';
4493it won't work in an interactive Emacs."
4494 (batch-byte-compile t))
4495
1c393159
JB
4496;;; by crl@newton.purdue.edu
4497;;; Only works noninteractively.
fd5285f3 4498;;;###autoload
7e7d0f8b 4499(defun batch-byte-compile (&optional noforce)
52799cb8
RS
4500 "Run `byte-compile-file' on the files remaining on the command line.
4501Use this from the command line, with `-batch';
4502it won't work in an interactive Emacs.
4503Each file is processed even if an error occurred previously.
7e7d0f8b
RS
4504For example, invoke \"emacs -batch -f batch-byte-compile $emacs/ ~/*.el\".
4505If NOFORCE is non-nil, don't recompile a file that seems to be
4506already up-to-date."
2ec42da9
SM
4507 ;; command-line-args-left is what is left of the command line, from
4508 ;; startup.el.
1c393159
JB
4509 (defvar command-line-args-left) ;Avoid 'free variable' warning
4510 (if (not noninteractive)
52799cb8 4511 (error "`batch-byte-compile' is to be used only with -batch"))
ca105506 4512 (let ((error nil))
1c393159
JB
4513 (while command-line-args-left
4514 (if (file-directory-p (expand-file-name (car command-line-args-left)))
7e7d0f8b 4515 ;; Directory as argument.
ca105506
SM
4516 (let (source dest)
4517 (dolist (file (directory-files (car command-line-args-left)))
4518 (if (and (string-match emacs-lisp-file-regexp file)
4519 (not (auto-save-file-name-p file))
4520 (setq source
4521 (expand-file-name file
1c3b663f 4522 (car command-line-args-left)))
ca105506
SM
4523 (setq dest (byte-compile-dest-file source))
4524 (file-exists-p dest)
4525 (file-newer-than-file-p source dest))
4526 (if (null (batch-byte-compile-file source))
4527 (setq error t)))))
7e7d0f8b
RS
4528 ;; Specific file argument
4529 (if (or (not noforce)
ca105506
SM
4530 (let* ((source (car command-line-args-left))
4531 (dest (byte-compile-dest-file source)))
4532 (or (not (file-exists-p dest))
4533 (file-newer-than-file-p source dest))))
7e7d0f8b 4534 (if (null (batch-byte-compile-file (car command-line-args-left)))
ca105506 4535 (setq error t))))
1c393159 4536 (setq command-line-args-left (cdr command-line-args-left)))
ca105506 4537 (kill-emacs (if error 1 0))))
1c393159 4538
ca105506 4539(defun batch-byte-compile-file (file)
3858bfe7
SM
4540 (let ((byte-compile-root-dir (or byte-compile-root-dir default-directory)))
4541 (if debug-on-error
4542 (byte-compile-file file)
4543 (condition-case err
4544 (byte-compile-file file)
4545 (file-error
4546 (message (if (cdr err)
4547 ">>Error occurred processing %s: %s (%s)"
4548 ">>Error occurred processing %s: %s")
4549 file
4550 (get (car err) 'error-message)
4551 (prin1-to-string (cdr err)))
4552 (let ((destfile (byte-compile-dest-file file)))
4553 (if (file-exists-p destfile)
4554 (delete-file destfile)))
4555 nil)
4556 (error
4557 (message (if (cdr err)
4558 ">>Error occurred processing %s: %s (%s)"
4559 ">>Error occurred processing %s: %s")
4560 file
4561 (get (car err) 'error-message)
4562 (prin1-to-string (cdr err)))
4563 nil)))))
1c393159 4564
49fec531
SM
4565(defun byte-compile-refresh-preloaded ()
4566 "Reload any Lisp file that was changed since Emacs was dumped.
4567Use with caution."
4568 (let* ((argv0 (car command-line-args))
4569 (emacs-file (executable-find argv0)))
4570 (if (not (and emacs-file (file-executable-p emacs-file)))
4571 (message "Can't find %s to refresh preloaded Lisp files" argv0)
4572 (dolist (f (reverse load-history))
4573 (setq f (car f))
4574 (if (string-match "elc\\'" f) (setq f (substring f 0 -1)))
4575 (when (and (file-readable-p f)
d032d5e7
SM
4576 (file-newer-than-file-p f emacs-file)
4577 ;; Don't reload the source version of the files below
4578 ;; because that causes subsequent byte-compilation to
4579 ;; be a lot slower and need a higher max-lisp-eval-depth,
4580 ;; so it can cause recompilation to fail.
4581 (not (member (file-name-nondirectory f)
4582 '("pcase.el" "bytecomp.el" "macroexp.el"
4583 "cconv.el" "byte-opt.el"))))
49fec531
SM
4584 (message "Reloading stale %s" (file-name-nondirectory f))
4585 (condition-case nil
4586 (load f 'noerror nil 'nosuffix)
4587 ;; Probably shouldn't happen, but in case of an error, it seems
4588 ;; at least as useful to ignore it as it is to stop compilation.
4589 (error nil)))))))
4590
e9681c45 4591;;;###autoload
6f8e3590 4592(defun batch-byte-recompile-directory (&optional arg)
4f6d5bf0 4593 "Run `byte-recompile-directory' on the dirs remaining on the command line.
79c6071d 4594Must be used only with `-batch', and kills Emacs on completion.
defe3b41
EZ
4595For example, invoke `emacs -batch -f batch-byte-recompile-directory .'.
4596
4597Optional argument ARG is passed as second argument ARG to
516b3653 4598`byte-recompile-directory'; see there for its possible values
defe3b41 4599and corresponding effects."
e27c3564
JB
4600 ;; command-line-args-left is what is left of the command line (startup.el)
4601 (defvar command-line-args-left) ;Avoid 'free variable' warning
4602 (if (not noninteractive)
4603 (error "batch-byte-recompile-directory is to be used only with -batch"))
4604 (or command-line-args-left
4605 (setq command-line-args-left '(".")))
4606 (while command-line-args-left
6f8e3590 4607 (byte-recompile-directory (car command-line-args-left) arg)
e27c3564
JB
4608 (setq command-line-args-left (cdr command-line-args-left)))
4609 (kill-emacs 0))
4610
c4c8444a
SM
4611;;; Core compiler macros.
4612
4613(put 'featurep 'compiler-macro
4614 (lambda (form feature &rest _ignore)
4615 ;; Emacs-21's byte-code doesn't run under XEmacs or SXEmacs anyway, so
4616 ;; we can safely optimize away this test.
4617 (if (member feature '('xemacs 'sxemacs 'emacs))
4618 (eval form)
4619 form)))
4620
1c393159 4621(provide 'byte-compile)
200503bb 4622(provide 'bytecomp)
1c393159
JB
4623
4624\f
4625;;; report metering (see the hacks in bytecode.c)
4626
08d21785 4627(defvar byte-code-meter)
52799cb8 4628(defun byte-compile-report-ops ()
5a972c36
GM
4629 (or (boundp 'byte-metering-on)
4630 (error "You must build Emacs with -DBYTE_CODE_METER to use this"))
52799cb8
RS
4631 (with-output-to-temp-buffer "*Meter*"
4632 (set-buffer "*Meter*")
4633 (let ((i 0) n op off)
4634 (while (< i 256)
4635 (setq n (aref (aref byte-code-meter 0) i)
4636 off nil)
4637 (if t ;(not (zerop n))
4638 (progn
4639 (setq op i)
4640 (setq off nil)
4641 (cond ((< op byte-nth)
4642 (setq off (logand op 7))
4643 (setq op (logand op 248)))
4644 ((>= op byte-constant)
4645 (setq off (- op byte-constant)
4646 op byte-constant)))
4647 (setq op (aref byte-code-vector op))
4648 (insert (format "%-4d" i))
4649 (insert (symbol-name op))
4650 (if off (insert " [" (int-to-string off) "]"))
4651 (indent-to 40)
4652 (insert (int-to-string n) "\n")))
4653 (setq i (1+ i))))))
1c393159
JB
4654\f
4655;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when bytecomp compiles
4656;; itself, compile some of its most used recursive functions (at load time).
4657;;
4658(eval-when-compile
591655c7
SM
4659 (or (byte-code-function-p (symbol-function 'byte-compile-form))
4660 (assq 'byte-code (symbol-function 'byte-compile-form))
4661 (let ((byte-optimize nil) ; do it fast
4662 (byte-compile-warnings nil))
86da2828
GM
4663 (mapc (lambda (x)
4664 (or noninteractive (message "compiling %s..." x))
4665 (byte-compile x)
4666 (or noninteractive (message "compiling %s...done" x)))
4667 '(byte-compile-normal-call
4668 byte-compile-form
4669 byte-compile-body
4670 ;; Inserted some more than necessary, to speed it up.
4671 byte-compile-top-level
4672 byte-compile-out-toplevel
4673 byte-compile-constant
4674 byte-compile-variable-ref))))
591655c7 4675 nil)
fd5285f3 4676
3433c43f
DL
4677(run-hooks 'bytecomp-load-hook)
4678
fd5285f3 4679;;; bytecomp.el ends here