1 ;;; bytecomp.el --- compilation of Lisp code into byte code -*- lexical-binding: t -*-
3 ;; Copyright (C) 1985-1987, 1992, 1994, 1998, 2000-2017 Free Software
6 ;; Author: Jamie Zawinski <jwz@lucid.com>
7 ;; Hallvard Furuseth <hbf@ulrik.uio.no>
8 ;; Maintainer: emacs-devel@gnu.org
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
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.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; The Emacs Lisp byte compiler. This crunches lisp source into a sort
30 ;; of p-code (`lapcode') which takes up less space and can be interpreted
31 ;; faster. [`LAP' == `Lisp Assembly Program'.]
32 ;; The user entry points are byte-compile-file and byte-recompile-directory.
36 ;; - Turn "not bound at runtime" functions into autoloads.
40 ;; ========================================================================
42 ;; byte-recompile-directory, byte-compile-file,
43 ;; byte-recompile-file,
44 ;; batch-byte-compile, batch-byte-recompile-directory,
45 ;; byte-compile, compile-defun,
47 ;; (byte-compile-buffer and byte-compile-and-load-file were turned off
48 ;; because they are not terribly useful and get in the way of completion.)
50 ;; This version of the byte compiler has the following improvements:
51 ;; + optimization of compiled code:
52 ;; - removal of unreachable code;
53 ;; - removal of calls to side-effectless functions whose return-value
55 ;; - compile-time evaluation of safe constant forms, such as (consp nil)
57 ;; - open-coding of literal lambdas;
58 ;; - peephole optimization of emitted code;
59 ;; - trivial functions are left uncompiled for speed.
60 ;; + support for inline functions;
61 ;; + compile-time evaluation of arbitrary expressions;
62 ;; + compile-time warning messages for:
63 ;; - functions being redefined with incompatible arglists;
64 ;; - functions being redefined as macros, or vice-versa;
65 ;; - functions or macros defined multiple times in the same file;
66 ;; - functions being called with the incorrect number of arguments;
67 ;; - functions being called which are not defined globally, in the
68 ;; file, or as autoloads;
69 ;; - assignment and reference of undeclared free variables;
70 ;; - various syntax errors;
71 ;; + correct compilation of nested defuns, defmacros, defvars and defsubsts;
72 ;; + correct compilation of top-level uses of macros;
73 ;; + the ability to generate a histogram of functions called.
75 ;; User customization variables: M-x customize-group bytecomp
79 ;; o The form `defsubst' is just like `defun', except that the function
80 ;; generated will be open-coded in compiled code which uses it. This
81 ;; means that no function call will be generated, it will simply be
82 ;; spliced in. Lisp functions calls are very slow, so this can be a
85 ;; You can generally accomplish the same thing with `defmacro', but in
86 ;; that case, the defined procedure can't be used as an argument to
89 ;; o You can also open-code one particular call to a function without
90 ;; open-coding all calls. Use the 'inline' form to do this, like so:
92 ;; (inline (foo 1 2 3)) ;; `foo' will be open-coded
94 ;; (inline ;; `foo' and `baz' will be
95 ;; (foo 1 2 3 (bar 5)) ;; open-coded, but `bar' will not.
98 ;; o It is possible to open-code a function in the same file it is defined
99 ;; in without having to load that file before compiling it. The
100 ;; byte-compiler has been modified to remember function definitions in
101 ;; the compilation environment in the same way that it remembers macro
104 ;; o Forms like ((lambda ...) ...) are open-coded.
106 ;; o The form `eval-when-compile' is like progn, except that the body
107 ;; is evaluated at compile-time. When it appears at top-level, this
108 ;; is analogous to the Common Lisp idiom (eval-when (compile) ...).
109 ;; When it does not appear at top-level, it is similar to the
110 ;; Common Lisp #. reader macro (but not in interpreted code).
112 ;; o The form `eval-and-compile' is similar to eval-when-compile, but
113 ;; the whole form is evalled both at compile-time and at run-time.
115 ;; o The command compile-defun is analogous to eval-defun.
117 ;; o If you run byte-compile-file on a filename which is visited in a
118 ;; buffer, and that buffer is modified, you are asked whether you want
119 ;; to save the buffer before compiling.
121 ;; o byte-compiled files now start with the string `;ELC'.
122 ;; Some versions of `file' can be customized to recognize that.
128 ;; During bootstrap, cl-loaddefs.el is not created yet, so loading cl-lib
129 ;; doesn't setup autoloads for things like cl-every, which is why we have to
130 ;; require cl-extra instead (bug#18804).
133 (or (fboundp 'defsubst
)
134 ;; This really ought to be loaded already!
137 ;; The feature of compiling in a specific target Emacs version
138 ;; has been turned off because compile time options are a bad idea.
139 (defgroup bytecomp nil
140 "Emacs Lisp byte-compiler."
143 (defcustom emacs-lisp-file-regexp
"\\.el\\'"
144 "Regexp which matches Emacs Lisp source files.
145 If you change this, you might want to set `byte-compile-dest-file-function'."
149 (defcustom byte-compile-dest-file-function nil
150 "Function for the function `byte-compile-dest-file' to call.
151 It should take one argument, the name of an Emacs Lisp source
152 file name, and return the name of the compiled file."
154 :type
'(choice (const nil
) function
)
157 ;; This enables file name handlers such as jka-compr
158 ;; to remove parts of the file name that should not be copied
159 ;; through to the output file name.
160 (defun byte-compiler-base-file-name (filename)
161 (let ((handler (find-file-name-handler filename
162 'byte-compiler-base-file-name
)))
164 (funcall handler
'byte-compiler-base-file-name filename
)
167 (or (fboundp 'byte-compile-dest-file
)
168 ;; The user may want to redefine this along with emacs-lisp-file-regexp,
169 ;; so only define it if it is undefined.
170 ;; Note - redefining this function is obsolete as of 23.2.
171 ;; Customize byte-compile-dest-file-function instead.
172 (defun byte-compile-dest-file (filename)
173 "Convert an Emacs Lisp source file name to a compiled file name.
174 If `byte-compile-dest-file-function' is non-nil, uses that
175 function to do the work. Otherwise, if FILENAME matches
176 `emacs-lisp-file-regexp' (by default, files with the extension `.el'),
177 adds `c' to it; otherwise adds `.elc'."
178 (if byte-compile-dest-file-function
179 (funcall byte-compile-dest-file-function filename
)
180 (setq filename
(file-name-sans-versions
181 (byte-compiler-base-file-name filename
)))
182 (cond ((string-match emacs-lisp-file-regexp filename
)
183 (concat (substring filename
0 (match-beginning 0)) ".elc"))
184 (t (concat filename
".elc"))))))
186 ;; This can be the 'byte-compile property of any symbol.
187 (autoload 'byte-compile-inline-expand
"byte-opt")
189 ;; This is the entry point to the lapcode optimizer pass1.
190 (autoload 'byte-optimize-form
"byte-opt")
191 ;; This is the entry point to the lapcode optimizer pass2.
192 (autoload 'byte-optimize-lapcode
"byte-opt")
193 (autoload 'byte-compile-unfold-lambda
"byte-opt")
195 ;; This is the entry point to the decompiler, which is used by the
196 ;; disassembler. The disassembler just requires 'byte-compile, but
197 ;; that doesn't define this function, so this seems to be a reasonable
199 (autoload 'byte-decompile-bytecode
"byte-opt")
201 (defcustom byte-compile-verbose
202 (and (not noninteractive
) (> baud-rate search-slow-speed
))
203 "Non-nil means print messages describing progress of byte-compiler."
207 (defcustom byte-optimize t
208 "Enable optimization in the byte compiler.
210 nil - no optimization
211 t - all optimizations
212 `source' - source-level optimizations only
213 `byte' - code-level optimizations only"
215 :type
'(choice (const :tag
"none" nil
)
217 (const :tag
"source-level" source
)
218 (const :tag
"byte-level" byte
)))
220 (defcustom byte-compile-delete-errors nil
221 "If non-nil, the optimizer may delete forms that may signal an error.
222 This includes variable references and calls to functions such as `car'."
226 (defcustom byte-compile-cond-use-jump-table t
227 "Compile `cond' clauses to a jump table implementation (using a hash-table)."
231 (defvar byte-compile-dynamic nil
232 "If non-nil, compile function bodies so they load lazily.
233 They are hidden in comments in the compiled file,
234 and each one is brought into core when the
237 To enable this option, make it a file-local variable
238 in the source file you want it to apply to.
239 For example, add -*-byte-compile-dynamic: t;-*- on the first line.
241 When this option is true, if you load the compiled file and then move it,
242 the functions you loaded will not be able to run.")
243 ;;;###autoload(put 'byte-compile-dynamic 'safe-local-variable 'booleanp)
245 (defvar byte-compile-disable-print-circle nil
246 "If non-nil, disable `print-circle' on printing a byte-compiled code.")
247 (make-obsolete-variable 'byte-compile-disable-print-circle nil
"24.1")
248 ;;;###autoload(put 'byte-compile-disable-print-circle 'safe-local-variable 'booleanp)
250 (defcustom byte-compile-dynamic-docstrings t
251 "If non-nil, compile doc strings for lazy access.
252 We bury the doc strings of functions and variables inside comments in
253 the file, and bring them into core only when they are actually needed.
255 When this option is true, if you load the compiled file and then move it,
256 you won't be able to find the documentation of anything in that file.
258 To disable this option for a certain file, make it a file-local variable
259 in the source file. For example, add this to the first line:
260 -*-byte-compile-dynamic-docstrings:nil;-*-
261 You can also set the variable globally.
263 This option is enabled by default because it reduces Emacs memory usage."
266 ;;;###autoload(put 'byte-compile-dynamic-docstrings 'safe-local-variable 'booleanp)
268 (defconst byte-compile-log-buffer
"*Compile-Log*"
269 "Name of the byte-compiler's log buffer.")
271 (defcustom byte-optimize-log nil
272 "If non-nil, the byte-compiler will log its optimizations.
273 If this is `source', then only source-level optimizations will be logged.
274 If it is `byte', then only byte-level optimizations will be logged.
275 The information is logged to `byte-compile-log-buffer'."
277 :type
'(choice (const :tag
"none" nil
)
279 (const :tag
"source-level" source
)
280 (const :tag
"byte-level" byte
)))
282 (defcustom byte-compile-error-on-warn nil
283 "If true, the byte-compiler reports warnings with `error'."
287 (defconst byte-compile-warning-types
288 '(redefine callargs free-vars unresolved
289 obsolete noruntime cl-functions interactive-only
290 make-local mapcar constants suspicious lexical
)
291 "The list of warning types used when `byte-compile-warnings' is t.")
292 (defcustom byte-compile-warnings t
293 "List of warnings that the byte-compiler should issue (t for all).
295 Elements of the list may be:
297 free-vars references to variables not in the current lexical scope.
298 unresolved calls to unknown functions.
299 callargs function calls with args that don't match the definition.
300 redefine function name redefined from a macro to ordinary function or vice
301 versa, or redefined to take a different number of arguments.
302 obsolete obsolete variables and functions.
303 noruntime functions that may not be defined at runtime (typically
304 defined only under `eval-when-compile').
305 cl-functions calls to runtime functions (as distinguished from macros and
306 aliases) from the old CL package (not the newer cl-lib).
308 commands that normally shouldn't be called from Lisp code.
309 lexical global/dynamic variables lacking a prefix.
310 make-local calls to make-variable-buffer-local that may be incorrect.
311 mapcar mapcar called for effect.
312 constants let-binding of, or assignment to, constants/nonvariables.
313 suspicious constructs that usually don't do what the coder wanted.
315 If the list begins with `not', then the remaining elements specify warnings to
316 suppress. For example, (not mapcar) will suppress warnings about mapcar."
318 :type
`(choice (const :tag
"All" t
)
319 (set :menu-tag
"Some"
320 ,@(mapcar (lambda (x) `(const ,x
))
321 byte-compile-warning-types
))))
324 (put 'byte-compile-warnings
'safe-local-variable
327 (null (delq nil
(mapcar (lambda (x) (not (symbolp x
))) v
))))))
329 (defun byte-compile-warning-enabled-p (warning)
330 "Return non-nil if WARNING is enabled, according to `byte-compile-warnings'."
331 (or (eq byte-compile-warnings t
)
332 (if (eq (car byte-compile-warnings
) 'not
)
333 (not (memq warning byte-compile-warnings
))
334 (memq warning byte-compile-warnings
))))
337 (defun byte-compile-disable-warning (warning)
338 "Change `byte-compile-warnings' to disable WARNING.
339 If `byte-compile-warnings' is t, set it to `(not WARNING)'.
340 Otherwise, if the first element is `not', add WARNING, else remove it.
341 Normally you should let-bind `byte-compile-warnings' before calling this,
342 else the global value will be modified."
343 (setq byte-compile-warnings
344 (cond ((eq byte-compile-warnings t
)
346 ((eq (car byte-compile-warnings
) 'not
)
347 (if (memq warning byte-compile-warnings
)
348 byte-compile-warnings
349 (append byte-compile-warnings
(list warning
))))
351 (delq warning byte-compile-warnings
)))))
354 (defun byte-compile-enable-warning (warning)
355 "Change `byte-compile-warnings' to enable WARNING.
356 If `byte-compile-warnings' is t, do nothing. Otherwise, if the
357 first element is `not', remove WARNING, else add it.
358 Normally you should let-bind `byte-compile-warnings' before calling this,
359 else the global value will be modified."
360 (or (eq byte-compile-warnings t
)
361 (setq byte-compile-warnings
362 (cond ((eq (car byte-compile-warnings
) 'not
)
363 (delq warning byte-compile-warnings
))
364 ((memq warning byte-compile-warnings
)
365 byte-compile-warnings
)
367 (append byte-compile-warnings
(list warning
)))))))
369 (defvar byte-compile-interactive-only-functions nil
370 "List of commands that are not meant to be called from Lisp.")
371 (make-obsolete-variable 'byte-compile-interactive-only-functions
372 "use the `interactive-only' symbol property instead."
375 (defvar byte-compile-not-obsolete-vars nil
376 "List of variables that shouldn't be reported as obsolete.")
377 (defvar byte-compile-global-not-obsolete-vars nil
378 "Global list of variables that shouldn't be reported as obsolete.")
380 (defvar byte-compile-not-obsolete-funcs nil
381 "List of functions that shouldn't be reported as obsolete.")
383 (defcustom byte-compile-generate-call-tree nil
384 "Non-nil means collect call-graph information when compiling.
385 This records which functions were called and from where.
386 If the value is t, compilation displays the call graph when it finishes.
387 If the value is neither t nor nil, compilation asks you whether to display
390 The call tree only lists functions called, not macros used. Those functions
391 which the byte-code interpreter knows about directly (eq, cons, etc.) are
394 The call tree also lists those functions which are not known to be called
395 \(that is, to which no calls have been compiled). Functions which can be
396 invoked interactively are excluded from this list."
398 :type
'(choice (const :tag
"Yes" t
) (const :tag
"No" nil
)
399 (other :tag
"Ask" lambda
)))
401 (defvar byte-compile-call-tree nil
402 "Alist of functions and their call tree.
403 Each element looks like
405 (FUNCTION CALLERS CALLS)
407 where CALLERS is a list of functions that call FUNCTION, and CALLS
408 is a list of functions for which calls were generated while compiling
411 (defcustom byte-compile-call-tree-sort
'name
412 "If non-nil, sort the call tree.
413 The values `name', `callers', `calls', `calls+callers'
414 specify different fields to sort on."
416 :type
'(choice (const name
) (const callers
) (const calls
)
417 (const calls
+callers
) (const nil
)))
419 (defvar byte-compile-debug nil
)
420 (defvar byte-compile-jump-tables nil
421 "List of all jump tables used during compilation of this form.")
422 (defvar byte-compile-constants nil
423 "List of all constants encountered during compilation of this form.")
424 (defvar byte-compile-variables nil
425 "List of all variables encountered during compilation of this form.")
426 (defvar byte-compile-bound-variables nil
427 "List of dynamic variables bound in the context of the current form.
428 This list lives partly on the stack.")
429 (defvar byte-compile-lexical-variables nil
430 "List of variables that have been treated as lexical.
431 Filled in `cconv-analyze-form' but initialized and consulted here.")
432 (defvar byte-compile-const-variables nil
433 "List of variables declared as constants during compilation of this file.")
434 (defvar byte-compile-free-references
)
435 (defvar byte-compile-free-assignments
)
437 (defvar byte-compiler-error-flag
)
439 (defun byte-compile-recurse-toplevel (form non-toplevel-case
)
440 "Implement `eval-when-compile' and `eval-and-compile'.
441 Return the compile-time value of FORM."
442 ;; Macroexpand (not macroexpand-all!) form at toplevel in case it
443 ;; expands into a toplevel-equivalent `progn'. See CLHS section
444 ;; 3.2.3.1, "Processing of Top Level Forms". The semantics are very
445 ;; subtle: see test/automated/bytecomp-tests.el for interesting
447 (setf form
(macroexp-macroexpand form byte-compile-macro-environment
))
448 (if (eq (car-safe form
) 'progn
)
450 (mapcar (lambda (subform)
451 (byte-compile-recurse-toplevel
452 subform non-toplevel-case
))
454 (funcall non-toplevel-case form
)))
456 (defconst byte-compile-initial-macro-environment
458 ;; (byte-compiler-options . (lambda (&rest forms)
459 ;; (apply 'byte-compiler-options-handler forms)))
460 (declare-function . byte-compile-macroexpand-declare-function
)
461 (eval-when-compile .
,(lambda (&rest body
)
463 (byte-compile-recurse-toplevel
464 (macroexp-progn body
)
466 ;; Insulate the following variables
467 ;; against changes made in the
468 ;; subsidiary compilation. This
469 ;; prevents spurious warning
470 ;; messages: "not defined at runtime"
472 (let ((byte-compile-unresolved-functions
473 byte-compile-unresolved-functions
)
474 (byte-compile-new-defuns
475 byte-compile-new-defuns
))
478 (byte-compile-top-level
479 (byte-compile-preprocess form
)))))))
480 (list 'quote result
))))
481 (eval-and-compile .
,(lambda (&rest body
)
482 (byte-compile-recurse-toplevel
483 (macroexp-progn body
)
485 ;; Don't compile here, since we don't know
486 ;; whether to compile as byte-compile-form
487 ;; or byte-compile-file-form.
491 macroexpand-all-environment
)))
492 (eval expanded lexical-binding
)
494 "The default macro-environment passed to macroexpand by the compiler.
495 Placing a macro here will cause a macro to have different semantics when
496 expanded by the compiler as when expanded by the interpreter.")
498 (defvar byte-compile-macro-environment byte-compile-initial-macro-environment
499 "Alist of macros defined in the file being compiled.
500 Each element looks like (MACRONAME . DEFINITION). It is
501 \(MACRONAME . nil) when a macro is redefined as a function.")
503 (defvar byte-compile-function-environment nil
504 "Alist of functions defined in the file being compiled.
505 This is so we can inline them when necessary.
506 Each element looks like (FUNCTIONNAME . DEFINITION). It is
507 \(FUNCTIONNAME . nil) when a function is redefined as a macro.
508 It is \(FUNCTIONNAME . t) when all we know is that it was defined,
509 and we don't know the definition. For an autoloaded function, DEFINITION
510 has the form (autoload . FILENAME).")
512 (defvar byte-compile-unresolved-functions nil
513 "Alist of undefined functions to which calls have been compiled.
514 This variable is only significant whilst compiling an entire buffer.
515 Used for warnings when a function is not known to be defined or is later
516 defined with incorrect args.")
518 (defvar byte-compile-noruntime-functions nil
519 "Alist of functions called that may not be defined when the compiled code is run.
520 Used for warnings about calling a function that is defined during compilation
521 but won't necessarily be defined when the compiled file is loaded.")
523 (defvar byte-compile-new-defuns nil
524 "List of (runtime) functions defined in this compilation run.
525 This variable is used to qualify `byte-compile-noruntime-functions' when
526 outputting warnings about functions not being defined at runtime.")
528 ;; Variables for lexical binding
529 (defvar byte-compile--lexical-environment nil
530 "The current lexical environment.")
532 (defvar byte-compile-tag-number
0)
533 (defvar byte-compile-output nil
534 "Alist describing contents to put in byte code string.
535 Each element is (INDEX . VALUE)")
536 (defvar byte-compile-depth
0 "Current depth of execution stack.")
537 (defvar byte-compile-maxdepth
0 "Maximum depth of execution stack.")
540 ;;; The byte codes; this information is duplicated in bytecomp.c
542 (defvar byte-code-vector nil
543 "An array containing byte-code names indexed by byte-code values.")
545 (defvar byte-stack
+-info nil
546 "An array with the stack adjustment for each byte-code.")
548 (defmacro byte-defop
(opcode stack-adjust opname
&optional docstring
)
549 ;; This is a speed-hack for building the byte-code-vector at compile-time.
550 ;; We fill in the vector at macroexpand-time, and then after the last call
551 ;; to byte-defop, we write the vector out as a constant instead of writing
552 ;; out a bunch of calls to aset.
553 ;; Actually, we don't fill in the vector itself, because that could make
554 ;; it problematic to compile big changes to this compiler; we store the
555 ;; values on its plist, and remove them later in -extrude.
556 (let ((v1 (or (get 'byte-code-vector
'tmp-compile-time-value
)
557 (put 'byte-code-vector
'tmp-compile-time-value
558 (make-vector 256 nil
))))
559 (v2 (or (get 'byte-stack
+-info
'tmp-compile-time-value
)
560 (put 'byte-stack
+-info
'tmp-compile-time-value
561 (make-vector 256 nil
)))))
562 (aset v1 opcode opname
)
563 (aset v2 opcode stack-adjust
))
565 (list 'defconst opname opcode
(concat "Byte code opcode " docstring
"."))
566 (list 'defconst opname opcode
)))
568 (defmacro byte-extrude-byte-code-vectors
()
569 (prog1 (list 'setq
'byte-code-vector
570 (get 'byte-code-vector
'tmp-compile-time-value
)
572 (get 'byte-stack
+-info
'tmp-compile-time-value
))
573 (put 'byte-code-vector
'tmp-compile-time-value nil
)
574 (put 'byte-stack
+-info
'tmp-compile-time-value nil
)))
577 ;; These opcodes are special in that they pack their argument into the
580 (byte-defop 0 1 byte-stack-ref
"for stack reference")
581 (byte-defop 8 1 byte-varref
"for variable reference")
582 (byte-defop 16 -
1 byte-varset
"for setting a variable")
583 (byte-defop 24 -
1 byte-varbind
"for binding a variable")
584 (byte-defop 32 0 byte-call
"for calling a function")
585 (byte-defop 40 0 byte-unbind
"for unbinding special bindings")
586 ;; codes 8-47 are consumed by the preceding opcodes
588 ;; New (in Emacs-24.4) bytecodes for more efficient handling of non-local exits
589 ;; (especially useful in lexical-binding code).
590 (byte-defop 48 0 byte-pophandler
)
591 (byte-defop 50 -
1 byte-pushcatch
)
592 (byte-defop 49 -
1 byte-pushconditioncase
)
596 (byte-defop 56 -
1 byte-nth
)
597 (byte-defop 57 0 byte-symbolp
)
598 (byte-defop 58 0 byte-consp
)
599 (byte-defop 59 0 byte-stringp
)
600 (byte-defop 60 0 byte-listp
)
601 (byte-defop 61 -
1 byte-eq
)
602 (byte-defop 62 -
1 byte-memq
)
603 (byte-defop 63 0 byte-not
)
604 (byte-defop 64 0 byte-car
)
605 (byte-defop 65 0 byte-cdr
)
606 (byte-defop 66 -
1 byte-cons
)
607 (byte-defop 67 0 byte-list1
)
608 (byte-defop 68 -
1 byte-list2
)
609 (byte-defop 69 -
2 byte-list3
)
610 (byte-defop 70 -
3 byte-list4
)
611 (byte-defop 71 0 byte-length
)
612 (byte-defop 72 -
1 byte-aref
)
613 (byte-defop 73 -
2 byte-aset
)
614 (byte-defop 74 0 byte-symbol-value
)
615 (byte-defop 75 0 byte-symbol-function
) ; this was commented out
616 (byte-defop 76 -
1 byte-set
)
617 (byte-defop 77 -
1 byte-fset
) ; this was commented out
618 (byte-defop 78 -
1 byte-get
)
619 (byte-defop 79 -
2 byte-substring
)
620 (byte-defop 80 -
1 byte-concat2
)
621 (byte-defop 81 -
2 byte-concat3
)
622 (byte-defop 82 -
3 byte-concat4
)
623 (byte-defop 83 0 byte-sub1
)
624 (byte-defop 84 0 byte-add1
)
625 (byte-defop 85 -
1 byte-eqlsign
)
626 (byte-defop 86 -
1 byte-gtr
)
627 (byte-defop 87 -
1 byte-lss
)
628 (byte-defop 88 -
1 byte-leq
)
629 (byte-defop 89 -
1 byte-geq
)
630 (byte-defop 90 -
1 byte-diff
)
631 (byte-defop 91 0 byte-negate
)
632 (byte-defop 92 -
1 byte-plus
)
633 (byte-defop 93 -
1 byte-max
)
634 (byte-defop 94 -
1 byte-min
)
635 (byte-defop 95 -
1 byte-mult
) ; v19 only
636 (byte-defop 96 1 byte-point
)
637 (byte-defop 98 0 byte-goto-char
)
638 (byte-defop 99 0 byte-insert
)
639 (byte-defop 100 1 byte-point-max
)
640 (byte-defop 101 1 byte-point-min
)
641 (byte-defop 102 0 byte-char-after
)
642 (byte-defop 103 1 byte-following-char
)
643 (byte-defop 104 1 byte-preceding-char
)
644 (byte-defop 105 1 byte-current-column
)
645 (byte-defop 106 0 byte-indent-to
)
646 (byte-defop 107 0 byte-scan-buffer-OBSOLETE
) ; no longer generated as of v18
647 (byte-defop 108 1 byte-eolp
)
648 (byte-defop 109 1 byte-eobp
)
649 (byte-defop 110 1 byte-bolp
)
650 (byte-defop 111 1 byte-bobp
)
651 (byte-defop 112 1 byte-current-buffer
)
652 (byte-defop 113 0 byte-set-buffer
)
653 (byte-defop 114 0 byte-save-current-buffer
654 "To make a binding to record the current buffer")
655 (byte-defop 115 0 byte-set-mark-OBSOLETE
)
656 (byte-defop 116 1 byte-interactive-p-OBSOLETE
)
658 ;; These ops are new to v19
659 (byte-defop 117 0 byte-forward-char
)
660 (byte-defop 118 0 byte-forward-word
)
661 (byte-defop 119 -
1 byte-skip-chars-forward
)
662 (byte-defop 120 -
1 byte-skip-chars-backward
)
663 (byte-defop 121 0 byte-forward-line
)
664 (byte-defop 122 0 byte-char-syntax
)
665 (byte-defop 123 -
1 byte-buffer-substring
)
666 (byte-defop 124 -
1 byte-delete-region
)
667 (byte-defop 125 -
1 byte-narrow-to-region
)
668 (byte-defop 126 1 byte-widen
)
669 (byte-defop 127 0 byte-end-of-line
)
673 ;; These store their argument in the next two bytes
674 (byte-defop 129 1 byte-constant2
675 "for reference to a constant with vector index >= byte-constant-limit")
676 (byte-defop 130 0 byte-goto
"for unconditional jump")
677 (byte-defop 131 -
1 byte-goto-if-nil
"to pop value and jump if it's nil")
678 (byte-defop 132 -
1 byte-goto-if-not-nil
"to pop value and jump if it's not nil")
679 (byte-defop 133 -
1 byte-goto-if-nil-else-pop
680 "to examine top-of-stack, jump and don't pop it if it's nil,
682 (byte-defop 134 -
1 byte-goto-if-not-nil-else-pop
683 "to examine top-of-stack, jump and don't pop it if it's non nil,
686 (byte-defop 135 -
1 byte-return
"to pop a value and return it from `byte-code'")
687 (byte-defop 136 -
1 byte-discard
"to discard one value from stack")
688 (byte-defop 137 1 byte-dup
"to duplicate the top of the stack")
690 (byte-defop 138 0 byte-save-excursion
691 "to make a binding to record the buffer, point and mark")
692 (byte-defop 139 0 byte-save-window-excursion-OBSOLETE
693 "to make a binding to record entire window configuration")
694 (byte-defop 140 0 byte-save-restriction
695 "to make a binding to record the current buffer clipping restrictions")
696 (byte-defop 141 -
1 byte-catch
697 "for catch. Takes, on stack, the tag and an expression for the body")
698 (byte-defop 142 -
1 byte-unwind-protect
699 "for unwind-protect. Takes, on stack, an expression for the unwind-action")
701 ;; For condition-case. Takes, on stack, the variable to bind,
702 ;; an expression for the body, and a list of clauses.
703 (byte-defop 143 -
2 byte-condition-case
)
705 (byte-defop 144 0 byte-temp-output-buffer-setup-OBSOLETE
)
706 (byte-defop 145 -
1 byte-temp-output-buffer-show-OBSOLETE
)
708 ;; these ops are new to v19
710 ;; To unbind back to the beginning of this frame.
711 ;; Not used yet, but will be needed for tail-recursion elimination.
712 (byte-defop 146 0 byte-unbind-all
)
714 ;; these ops are new to v19
715 (byte-defop 147 -
2 byte-set-marker
)
716 (byte-defop 148 0 byte-match-beginning
)
717 (byte-defop 149 0 byte-match-end
)
718 (byte-defop 150 0 byte-upcase
)
719 (byte-defop 151 0 byte-downcase
)
720 (byte-defop 152 -
1 byte-string
=)
721 (byte-defop 153 -
1 byte-string
<)
722 (byte-defop 154 -
1 byte-equal
)
723 (byte-defop 155 -
1 byte-nthcdr
)
724 (byte-defop 156 -
1 byte-elt
)
725 (byte-defop 157 -
1 byte-member
)
726 (byte-defop 158 -
1 byte-assq
)
727 (byte-defop 159 0 byte-nreverse
)
728 (byte-defop 160 -
1 byte-setcar
)
729 (byte-defop 161 -
1 byte-setcdr
)
730 (byte-defop 162 0 byte-car-safe
)
731 (byte-defop 163 0 byte-cdr-safe
)
732 (byte-defop 164 -
1 byte-nconc
)
733 (byte-defop 165 -
1 byte-quo
)
734 (byte-defop 166 -
1 byte-rem
)
735 (byte-defop 167 0 byte-numberp
)
736 (byte-defop 168 0 byte-integerp
)
739 (byte-defop 175 nil byte-listN
)
740 (byte-defop 176 nil byte-concatN
)
741 (byte-defop 177 nil byte-insertN
)
743 (byte-defop 178 -
1 byte-stack-set
) ; Stack offset in following one byte.
744 (byte-defop 179 -
1 byte-stack-set2
) ; Stack offset in following two bytes.
746 ;; If (following one byte & 0x80) == 0
747 ;; discard (following one byte & 0x7F) stack entries
749 ;; discard (following one byte & 0x7F) stack entries _underneath_ TOS
750 ;; (that is, if the operand = 0x83, ... X Y Z T => ... T)
751 (byte-defop 182 nil byte-discardN
)
752 ;; `byte-discardN-preserve-tos' is a pseudo-op that gets turned into
753 ;; `byte-discardN' with the high bit in the operand set (by
754 ;; `byte-compile-lapcode').
755 (defconst byte-discardN-preserve-tos byte-discardN
)
757 (byte-defop 183 -
2 byte-switch
758 "to take a hash table and a value from the stack, and jump to the address
759 the value maps to, if any.")
763 (byte-defop 192 1 byte-constant
"for reference to a constant")
764 ;; codes 193-255 are consumed by byte-constant.
765 (defconst byte-constant-limit
64
766 "Exclusive maximum index usable in the `byte-constant' opcode.")
768 (defconst byte-goto-ops
'(byte-goto byte-goto-if-nil byte-goto-if-not-nil
769 byte-goto-if-nil-else-pop
770 byte-goto-if-not-nil-else-pop
771 byte-pushcatch byte-pushconditioncase
)
772 "List of byte-codes whose offset is a pc.")
774 (defconst byte-goto-always-pop-ops
'(byte-goto-if-nil byte-goto-if-not-nil
))
776 (byte-extrude-byte-code-vectors)
778 ;;; lapcode generator
780 ;; the byte-compiler now does source -> lapcode -> bytecode instead of
781 ;; source -> bytecode, because it's a lot easier to make optimizations
782 ;; on lapcode than on bytecode.
784 ;; Elements of the lapcode list are of the form (<instruction> . <parameter>)
785 ;; where instruction is a symbol naming a byte-code instruction,
786 ;; and parameter is an argument to that instruction, if any.
788 ;; The instruction can be the pseudo-op TAG, which means that this position
789 ;; in the instruction stream is a target of a goto. (car PARAMETER) will be
790 ;; the PC for this location, and the whole instruction "(TAG pc)" will be the
791 ;; parameter for some goto op.
793 ;; If the operation is varbind, varref, varset or push-constant, then the
794 ;; parameter is (variable/constant . index_in_constant_vector).
796 ;; First, the source code is macroexpanded and optimized in various ways.
797 ;; Then the resultant code is compiled into lapcode. Another set of
798 ;; optimizations are then run over the lapcode. Then the variables and
799 ;; constants referenced by the lapcode are collected and placed in the
800 ;; constants-vector. (This happens now so that variables referenced by dead
801 ;; code don't consume space.) And finally, the lapcode is transformed into
802 ;; compacted byte-code.
804 ;; A distinction is made between variables and constants because the variable-
805 ;; referencing instructions are more sensitive to the variables being near the
806 ;; front of the constants-vector than the constant-referencing instructions.
807 ;; Also, this lets us notice references to free variables.
809 (defmacro byte-compile-push-bytecodes
(&rest args
)
810 "Push bytes onto BVAR, and increment CVAR by the number of bytes pushed.
811 BVAR and CVAR are variables which are updated after evaluating
814 \(fn BYTE1 BYTE2 ... BYTEn BVAR CVAR)"
815 (let ((byte-exprs (butlast args
2))
816 (bytes-var (car (last args
2)))
817 (pc-var (car (last args
))))
818 `(setq ,bytes-var
,(if (null (cdr byte-exprs
))
819 `(progn (cl-assert (<= 0 ,(car byte-exprs
)))
820 (cons ,@byte-exprs
,bytes-var
))
821 `(nconc (list ,@(reverse byte-exprs
)) ,bytes-var
))
822 ,pc-var
(+ ,(length byte-exprs
) ,pc-var
))))
824 (defmacro byte-compile-push-bytecode-const2
(opcode const2 bytes pc
)
825 "Push OPCODE and the two-byte constant CONST2 onto BYTES, and add 3 to PC.
826 CONST2 may be evaluated multiple times."
827 `(byte-compile-push-bytecodes ,opcode
(logand ,const2
255) (lsh ,const2 -
8)
830 (defun byte-compile-lapcode (lap)
831 "Turns lapcode into bytecode. The lapcode is destroyed."
832 ;; Lapcode modifications: changes the ID of a tag to be the tag's PC.
833 (let ((pc 0) ; Program counter
834 op off
; Operation & offset
835 opcode
; numeric value of OP
836 (bytes '()) ; Put the output bytes here
837 (patchlist nil
)) ; List of gotos to patch
838 (dolist (lap-entry lap
)
839 (setq op
(car lap-entry
)
843 (error "Non-symbolic opcode `%s'" op
))
848 (if (eq op
'byte-discardN-preserve-tos
)
849 ;; byte-discardN-preserve-tos is a pseudo op, which
850 ;; is actually the same as byte-discardN
851 ;; with a modified argument.
854 (cond ((memq op byte-goto-ops
)
856 (byte-compile-push-bytecodes opcode nil
(cdr off
) bytes pc
)
857 (push bytes patchlist
))
858 ((or (and (consp off
)
859 ;; Variable or constant reference
862 (eq op
'byte-constant
)))
863 (and (eq op
'byte-constant
)
866 (if (< off byte-constant-limit
)
867 (byte-compile-push-bytecodes (+ byte-constant off
)
869 (byte-compile-push-bytecode-const2 byte-constant2 off
871 ((and (= opcode byte-stack-set
)
873 ;; Use the two-byte version of byte-stack-set if the
874 ;; offset is too large for the normal version.
875 (byte-compile-push-bytecode-const2 byte-stack-set2 off
877 ((and (>= opcode byte-listN
)
878 (< opcode byte-discardN
))
879 ;; These insns all put their operand into one extra byte.
880 (byte-compile-push-bytecodes opcode off bytes pc
))
881 ((= opcode byte-discardN
)
882 ;; byte-discardN is weird in that it encodes a flag in the
883 ;; top bit of its one-byte argument. If the argument is
884 ;; too large to fit in 7 bits, the opcode can be repeated.
885 (let ((flag (if (eq op
'byte-discardN-preserve-tos
) #x80
0)))
887 (byte-compile-push-bytecodes opcode
(logior #x7f flag
)
889 (setq off
(- off
#x7f
)))
890 (byte-compile-push-bytecodes opcode
(logior off flag
)
893 ;; opcode that doesn't use OFF
894 (byte-compile-push-bytecodes opcode bytes pc
))
895 ((and (eq opcode byte-stack-ref
) (eq off
0))
896 ;; (stack-ref 0) is really just another name for `dup'.
897 (debug) ;FIXME: When would this happen?
898 (byte-compile-push-bytecodes byte-dup bytes pc
))
899 ;; The following three cases are for the special
900 ;; insns that encode their operand into 0, 1, or 2
901 ;; extra bytes depending on its magnitude.
903 (byte-compile-push-bytecodes (+ opcode off
) bytes pc
))
905 (byte-compile-push-bytecodes (+ opcode
6) off bytes pc
))
907 (byte-compile-push-bytecode-const2 (+ opcode
7) off
909 ;;(if (not (= pc (length bytes)))
910 ;; (error "Compiler error: pc mismatch - %s %s" pc (length bytes)))
911 ;; Patch tag PCs into absolute jumps.
912 (dolist (bytes-tail patchlist
)
913 (setq pc
(caar bytes-tail
)) ; Pick PC from goto's tag.
914 ;; Splits PC's value into 2 bytes. The jump address is
915 ;; "reconstructed" by the `FETCH2' macro in `bytecode.c'.
916 (setcar (cdr bytes-tail
) (logand pc
255))
917 (setcar bytes-tail
(lsh pc -
8))
918 ;; FIXME: Replace this by some workaround.
919 (if (> (car bytes-tail
) 255) (error "Bytecode overflow")))
921 ;; Similarly, replace TAGs in all jump tables with the correct PC index.
922 (dolist (hash-table byte-compile-jump-tables
)
923 (maphash #'(lambda (value tag
)
925 ;; We don't need to split PC here, as it is stored as a lisp
926 ;; object in the hash table (whereas other goto-* ops store
927 ;; it within 2 bytes in the byte string).
928 (puthash value pc hash-table
))
930 (apply 'unibyte-string
(nreverse bytes
))))
933 ;;; compile-time evaluation
935 (defun byte-compile-cl-file-p (file)
936 "Return non-nil if FILE is one of the CL files."
938 (string-match "^cl\\.el" (file-name-nondirectory file
))))
940 (defun byte-compile-eval (form)
941 "Eval FORM and mark the functions defined therein.
942 Each function's symbol gets added to `byte-compile-noruntime-functions'."
943 (let ((hist-orig load-history
)
944 (hist-nil-orig current-load-list
))
945 (prog1 (eval form lexical-binding
)
946 (when (byte-compile-warning-enabled-p 'noruntime
)
947 (let ((hist-new load-history
)
948 (hist-nil-new current-load-list
))
949 ;; Go through load-history, look for newly loaded files
950 ;; and mark all the functions defined therein.
951 (while (and hist-new
(not (eq hist-new hist-orig
)))
952 (let ((xs (pop hist-new
))
954 ;; Make sure the file was not already loaded before.
955 (unless (assoc (car xs
) hist-orig
)
958 ((and (consp s
) (eq t
(car s
)))
959 (push (cdr s
) old-autoloads
))
960 ((and (consp s
) (memq (car s
) '(autoload defun
)))
961 (unless (memq (cdr s
) old-autoloads
)
962 (push (cdr s
) byte-compile-noruntime-functions
))))))))
963 ;; Go through current-load-list for the locally defined funs.
965 (while (and hist-nil-new
(not (eq hist-nil-new hist-nil-orig
)))
966 (let ((s (pop hist-nil-new
)))
967 (when (and (symbolp s
) (not (memq s old-autoloads
)))
968 (push s byte-compile-noruntime-functions
))
969 (when (and (consp s
) (eq t
(car s
)))
970 (push (cdr s
) old-autoloads
)))))))
971 (when (byte-compile-warning-enabled-p 'cl-functions
)
972 (let ((hist-new load-history
))
973 ;; Go through load-history, looking for the cl files.
974 ;; Since new files are added at the start of load-history,
975 ;; we scan the new history until the tail matches the old.
976 (while (and (not byte-compile-cl-functions
)
977 hist-new
(not (eq hist-new hist-orig
)))
978 ;; We used to check if the file had already been loaded,
979 ;; but it is better to check non-nil byte-compile-cl-functions.
980 (and (byte-compile-cl-file-p (car (pop hist-new
)))
981 (byte-compile-find-cl-functions))))))))
983 (defun byte-compile-eval-before-compile (form)
984 "Evaluate FORM for `eval-and-compile'."
985 (let ((hist-nil-orig current-load-list
))
986 (prog1 (eval form lexical-binding
)
987 ;; (eval-and-compile (require 'cl) turns off warnings for cl functions.
988 ;; FIXME Why does it do that - just as a hack?
989 ;; There are other ways to do this nowadays.
990 (let ((tem current-load-list
))
991 (while (not (eq tem hist-nil-orig
))
992 (when (equal (car tem
) '(require . cl
))
993 (byte-compile-disable-warning 'cl-functions
))
994 (setq tem
(cdr tem
)))))))
996 ;;; byte compiler messages
998 (defvar byte-compile-current-form nil
)
999 (defvar byte-compile-dest-file nil
)
1000 (defvar byte-compile-current-file nil
)
1001 (defvar byte-compile-current-group nil
)
1002 (defvar byte-compile-current-buffer nil
)
1004 ;; Log something that isn't a warning.
1005 (defmacro byte-compile-log
(format-string &rest args
)
1008 (memq byte-optimize-log
'(t source
))
1009 (let ((print-escape-newlines t
)
1016 (lambda (x) (if (symbolp x
) (list 'prin1-to-string x
) x
))
1019 ;; Log something that isn't a warning.
1020 (defun byte-compile-log-1 (string)
1021 (with-current-buffer byte-compile-log-buffer
1022 (let ((inhibit-read-only t
))
1023 (goto-char (point-max))
1024 (byte-compile-warning-prefix nil nil
)
1025 (cond (noninteractive
1026 (message " %s" string
))
1028 (insert (format "%s\n" string
)))))))
1030 (defvar byte-compile-read-position nil
1031 "Character position we began the last `read' from.")
1032 (defvar byte-compile-last-position nil
1033 "Last known character position in the input.")
1035 ;; copied from gnus-util.el
1036 (defsubst byte-compile-delete-first
(elt list
)
1037 (if (eq (car list
) elt
)
1040 (while (and (cdr list
)
1041 (not (eq (cadr list
) elt
)))
1042 (setq list
(cdr list
)))
1044 (setcdr list
(cddr list
)))
1047 ;; The purpose of `byte-compile-set-symbol-position' is to attempt to
1048 ;; set `byte-compile-last-position' to the "current position" in the
1049 ;; raw source code. This is used for warning and error messages.
1051 ;; The function should be called for most occurrences of symbols in
1052 ;; the forms being compiled, strictly in the order they occur in the
1053 ;; source code. It should never be called twice for any single
1054 ;; occurrence, and should not be called for symbols generated by the
1055 ;; byte compiler itself.
1057 ;; The function works by scanning the elements in the alist
1058 ;; `read-symbol-positions-list' for the next match for the symbol
1059 ;; after the current value of `byte-compile-last-position', setting
1060 ;; that variable to the match's character position, then deleting the
1061 ;; matching element from the list. Thus the new value for
1062 ;; `byte-compile-last-position' is later than the old value unless,
1063 ;; perhaps, ALLOW-PREVIOUS is non-nil.
1065 ;; So your're probably asking yourself: Isn't this function a gross
1066 ;; hack? And the answer, of course, would be yes.
1067 (defun byte-compile-set-symbol-position (sym &optional allow-previous
)
1068 (when byte-compile-read-position
1069 (let ((last byte-compile-last-position
)
1072 (setq entry
(assq sym read-symbol-positions-list
))
1074 (setq byte-compile-last-position
1075 (+ byte-compile-read-position
(cdr entry
))
1076 read-symbol-positions-list
1077 (byte-compile-delete-first
1078 entry read-symbol-positions-list
)))
1080 (or (and allow-previous
1081 (not (= last byte-compile-last-position
)))
1082 (> last byte-compile-last-position
))))))))
1084 (defvar byte-compile-last-warned-form nil
)
1085 (defvar byte-compile-last-logged-file nil
)
1086 (defvar byte-compile-root-dir nil
1087 "Directory relative to which file names in error messages are written.")
1089 ;; FIXME: We should maybe extend abbreviate-file-name with an optional DIR
1090 ;; argument to try and use a relative file-name.
1091 (defun byte-compile-abbreviate-file (file &optional dir
)
1092 (let ((f1 (abbreviate-file-name file
))
1093 (f2 (file-relative-name file dir
)))
1094 (if (< (length f2
) (length f1
)) f2 f1
)))
1096 ;; This is used as warning-prefix for the compiler.
1097 ;; It is always called with the warnings buffer current.
1098 (defun byte-compile-warning-prefix (level entry
)
1099 (let* ((inhibit-read-only t
)
1100 (dir (or byte-compile-root-dir default-directory
))
1101 (file (cond ((stringp byte-compile-current-file
)
1102 (format "%s:" (byte-compile-abbreviate-file
1103 byte-compile-current-file dir
)))
1104 ((bufferp byte-compile-current-file
)
1105 (format "Buffer %s:"
1106 (buffer-name byte-compile-current-file
)))
1107 ;; We might be simply loading a file that
1108 ;; contains explicit calls to byte-compile functions.
1109 ((stringp load-file-name
)
1110 (format "%s:" (byte-compile-abbreviate-file
1111 load-file-name dir
)))
1113 (pos (if (and byte-compile-current-file
1114 (integerp byte-compile-read-position
))
1115 (with-current-buffer byte-compile-current-buffer
1118 (goto-char byte-compile-last-position
)
1119 (1+ (count-lines (point-min) (point-at-bol))))
1121 (goto-char byte-compile-last-position
)
1122 (1+ (current-column)))))
1124 (form (if (eq byte-compile-current-form
:end
) "end of data"
1125 (or byte-compile-current-form
"toplevel form"))))
1126 (when (or (and byte-compile-current-file
1127 (not (equal byte-compile-current-file
1128 byte-compile-last-logged-file
)))
1129 (and byte-compile-current-form
1130 (not (eq byte-compile-current-form
1131 byte-compile-last-warned-form
))))
1132 (insert (format "\nIn %s:\n" form
)))
1134 (insert (format "%s%s" file pos
))))
1135 (setq byte-compile-last-logged-file byte-compile-current-file
1136 byte-compile-last-warned-form byte-compile-current-form
)
1139 ;; This no-op function is used as the value of warning-series
1140 ;; to tell inner calls to displaying-byte-compile-warnings
1141 ;; not to bind warning-series.
1142 (defun byte-compile-warning-series (&rest _ignore
)
1145 ;; (compile-mode) will cause this to be loaded.
1146 (declare-function compilation-forget-errors
"compile" ())
1148 ;; Log the start of a file in `byte-compile-log-buffer', and mark it as done.
1149 ;; Return the position of the start of the page in the log buffer.
1150 ;; But do nothing in batch mode.
1151 (defun byte-compile-log-file ()
1152 (and (not (equal byte-compile-current-file byte-compile-last-logged-file
))
1153 (not noninteractive
)
1154 (with-current-buffer (get-buffer-create byte-compile-log-buffer
)
1155 (goto-char (point-max))
1156 (let* ((inhibit-read-only t
)
1157 (dir (and byte-compile-current-file
1158 (file-name-directory byte-compile-current-file
)))
1159 (was-same (equal default-directory dir
))
1163 (insert (format-message "Leaving directory `%s'\n"
1164 default-directory
))))
1167 (setq pt
(point-marker))
1168 (if byte-compile-current-file
1169 (insert "\f\nCompiling "
1170 (if (stringp byte-compile-current-file
)
1171 (concat "file " byte-compile-current-file
)
1173 (buffer-name byte-compile-current-file
)))
1174 " at " (current-time-string) "\n")
1175 (insert "\f\nCompiling no file at " (current-time-string) "\n"))
1177 (setq default-directory dir
)
1179 (insert (format-message "Entering directory `%s'\n"
1180 default-directory
))))
1181 (setq byte-compile-last-logged-file byte-compile-current-file
1182 byte-compile-last-warned-form nil
)
1183 ;; Do this after setting default-directory.
1184 (unless (derived-mode-p 'compilation-mode
) (compilation-mode))
1185 (compilation-forget-errors)
1188 (defun byte-compile-log-warning (string &optional fill level
)
1189 "Log a message STRING in `byte-compile-log-buffer'.
1190 Also log the current function and file if not already done. If
1191 FILL is non-nil, set `warning-fill-prefix' to four spaces. LEVEL
1192 is the warning level (`:warning' or `:error'). Do not call this
1193 function directly; use `byte-compile-warn' or
1194 `byte-compile-report-error' instead."
1195 (let ((warning-prefix-function 'byte-compile-warning-prefix
)
1196 (warning-type-format "")
1197 (warning-fill-prefix (if fill
" ")))
1198 (display-warning 'bytecomp string level byte-compile-log-buffer
)))
1200 (defun byte-compile-warn (format &rest args
)
1201 "Issue a byte compiler warning; use (format-message FORMAT ARGS...) for message."
1202 (setq format
(apply #'format-message format args
))
1203 (if byte-compile-error-on-warn
1204 (error "%s" format
) ; byte-compile-file catches and logs it
1205 (byte-compile-log-warning format t
:warning
)))
1207 (defun byte-compile-warn-obsolete (symbol)
1208 "Warn that SYMBOL (a variable or function) is obsolete."
1209 (when (byte-compile-warning-enabled-p 'obsolete
)
1210 (let* ((funcp (get symbol
'byte-obsolete-info
))
1211 (msg (macroexp--obsolete-warning
1213 (or funcp
(get symbol
'byte-obsolete-variable
))
1214 (if funcp
"function" "variable"))))
1215 (unless (and funcp
(memq symbol byte-compile-not-obsolete-funcs
))
1216 (byte-compile-warn "%s" msg
)))))
1218 (defun byte-compile-report-error (error-info &optional fill
)
1219 "Report Lisp error in compilation.
1220 ERROR-INFO is the error data, in the form of either (ERROR-SYMBOL . DATA)
1221 or STRING. If FILL is non-nil, set ‘warning-fill-prefix’ to four spaces
1222 when printing the error message."
1223 (setq byte-compiler-error-flag t
)
1224 (byte-compile-log-warning
1225 (if (stringp error-info
) error-info
1226 (error-message-string error-info
))
1229 ;;; sanity-checking arglists
1231 (defun byte-compile-fdefinition (name macro-p
)
1232 ;; If a function has an entry saying (FUNCTION . t).
1233 ;; that means we know it is defined but we don't know how.
1234 ;; If a function has an entry saying (FUNCTION . nil),
1235 ;; that means treat it as not defined.
1236 (let* ((list (if macro-p
1237 byte-compile-macro-environment
1238 byte-compile-function-environment
))
1239 (env (cdr (assq name list
))))
1242 (while (and (symbolp fn
)
1244 (or (symbolp (symbol-function fn
))
1245 (consp (symbol-function fn
))
1247 (byte-code-function-p (symbol-function fn
)))))
1248 (setq fn
(symbol-function fn
)))
1249 (let ((advertised (gethash (if (and (symbolp fn
) (fboundp fn
))
1251 (symbol-function fn
)
1253 advertised-signature-table t
)))
1257 `(macro lambda
,advertised
)
1258 `(lambda ,advertised
)))
1259 ((and (not macro-p
) (byte-code-function-p fn
)) fn
)
1260 ((not (consp fn
)) nil
)
1261 ((eq 'macro
(car fn
)) (cdr fn
))
1263 ((eq 'autoload
(car fn
)) nil
)
1266 (defun byte-compile-arglist-signature (arglist)
1268 ;; New style byte-code arglist.
1270 (cons (logand arglist
127) ;Mandatory.
1271 (if (zerop (logand arglist
128)) ;No &rest.
1272 (lsh arglist -
8)))) ;Nonrest.
1273 ;; Old style byte-code, or interpreted function.
1279 (cond ((eq (car arglist
) '&optional
)
1280 (or opts
(setq opts
0)))
1281 ((eq (car arglist
) '&rest
)
1287 (setq opts
(1+ opts
))
1288 (setq args
(1+ args
)))))
1289 (setq arglist
(cdr arglist
)))
1290 (cons args
(if restp nil
(if opts
(+ args opts
) args
)))))
1295 (defun byte-compile-arglist-signatures-congruent-p (old new
)
1297 (> (car new
) (car old
)) ; requires more args now
1298 (and (null (cdr old
)) ; took rest-args, doesn't any more
1300 (and (cdr new
) (cdr old
) ; can't take as many args now
1301 (< (cdr new
) (cdr old
)))
1304 (defun byte-compile-arglist-signature-string (signature)
1305 (cond ((null (cdr signature
))
1306 (format "%d+" (car signature
)))
1307 ((= (car signature
) (cdr signature
))
1308 (format "%d" (car signature
)))
1309 (t (format "%d-%d" (car signature
) (cdr signature
)))))
1311 (defun byte-compile-function-warn (f nargs def
)
1312 (byte-compile-set-symbol-position f
)
1313 (when (get f
'byte-obsolete-info
)
1314 (byte-compile-warn-obsolete f
))
1316 ;; Check to see if the function will be available at runtime
1317 ;; and/or remember its arity if it's unknown.
1318 (or (and (or def
(fboundp f
)) ; might be a subr or autoload.
1319 (not (memq f byte-compile-noruntime-functions
)))
1320 (eq f byte-compile-current-form
) ; ## This doesn't work
1322 ;; It's a currently-undefined function.
1323 ;; Remember number of args in call.
1324 (let ((cons (assq f byte-compile-unresolved-functions
)))
1326 (or (memq nargs
(cdr cons
))
1327 (push nargs
(cdr cons
)))
1328 (push (list f nargs
)
1329 byte-compile-unresolved-functions
)))))
1331 ;; Warn if the form is calling a function with the wrong number of arguments.
1332 (defun byte-compile-callargs-warn (form)
1333 (let* ((def (or (byte-compile-fdefinition (car form
) nil
)
1334 (byte-compile-fdefinition (car form
) t
)))
1335 (sig (if (and def
(not (eq def t
)))
1337 (and (eq (car-safe def
) 'macro
)
1338 (eq (car-safe (cdr-safe def
)) 'lambda
)
1339 (setq def
(cdr def
)))
1340 (byte-compile-arglist-signature
1341 (if (memq (car-safe def
) '(declared lambda
))
1343 (if (byte-code-function-p def
)
1346 (if (subrp (symbol-function (car form
)))
1347 (subr-arity (symbol-function (car form
))))))
1348 (ncall (length (cdr form
))))
1349 ;; Check many or unevalled from subr-arity.
1350 (if (and (cdr-safe sig
)
1351 (not (numberp (cdr sig
))))
1354 (when (or (< ncall
(car sig
))
1355 (and (cdr sig
) (> ncall
(cdr sig
))))
1356 (byte-compile-set-symbol-position (car form
))
1358 "%s called with %d argument%s, but %s %s"
1360 (if (= 1 ncall
) "" "s")
1361 (if (< ncall
(car sig
))
1364 (byte-compile-arglist-signature-string sig
))))
1365 (byte-compile-format-warn form
)
1366 (byte-compile-function-warn (car form
) (length (cdr form
)) def
)))
1368 (defun byte-compile-format-warn (form)
1369 "Warn if FORM is `format'-like with inconsistent args.
1370 Applies if head of FORM is a symbol with non-nil property
1371 `byte-compile-format-like' and first arg is a constant string.
1372 Then check the number of format fields matches the number of
1374 (when (and (symbolp (car form
))
1375 (stringp (nth 1 form
))
1376 (get (car form
) 'byte-compile-format-like
))
1377 (let ((nfields (with-temp-buffer
1378 (insert (nth 1 form
))
1379 (goto-char (point-min))
1381 (while (re-search-forward "%." nil t
)
1382 (unless (eq ?%
(char-after (1+ (match-beginning 0))))
1385 (nargs (- (length form
) 2)))
1386 (unless (= nargs nfields
)
1388 "`%s' called with %d args to fill %d format field(s)" (car form
)
1391 (dolist (elt '(format message error
))
1392 (put elt
'byte-compile-format-like t
))
1394 ;; Warn if a custom definition fails to specify :group, or :type.
1395 (defun byte-compile-nogroup-warn (form)
1396 (let ((keyword-args (cdr (cdr (cdr (cdr form
)))))
1398 (when (eq (car-safe name
) 'quote
)
1399 (or (not (eq (car form
) 'custom-declare-variable
))
1400 (plist-get keyword-args
:type
)
1402 "defcustom for `%s' fails to specify type" (cadr name
)))
1403 (if (and (memq (car form
) '(custom-declare-face custom-declare-variable
))
1404 byte-compile-current-group
)
1405 ;; The group will be provided implicitly.
1407 (or (and (eq (car form
) 'custom-declare-group
)
1408 (equal name
''emacs
))
1409 (plist-get keyword-args
:group
)
1411 "%s for `%s' fails to specify containing group"
1412 (cdr (assq (car form
)
1413 '((custom-declare-group . defgroup
)
1414 (custom-declare-face . defface
)
1415 (custom-declare-variable . defcustom
))))
1417 ;; Update the current group, if needed.
1418 (if (and byte-compile-current-file
;Only when compiling a whole file.
1419 (eq (car form
) 'custom-declare-group
))
1420 (setq byte-compile-current-group
(cadr name
)))))))
1422 ;; Warn if the function or macro is being redefined with a different
1423 ;; number of arguments.
1424 (defun byte-compile-arglist-warn (name arglist macrop
)
1425 ;; This is the first definition. See if previous calls are compatible.
1426 (let ((calls (assq name byte-compile-unresolved-functions
))
1428 (when (and calls macrop
)
1429 (byte-compile-warn "macro `%s' defined too late" name
))
1430 (setq byte-compile-unresolved-functions
1431 (delq calls byte-compile-unresolved-functions
))
1432 (setq calls
(delq t calls
)) ;Ignore higher-order uses of the function.
1434 (when (and (symbolp name
)
1435 (eq (function-get name
'byte-optimizer
)
1436 'byte-compile-inline-expand
))
1437 (byte-compile-warn "defsubst `%s' was used before it was defined"
1439 (setq sig
(byte-compile-arglist-signature arglist
)
1440 nums
(sort (copy-sequence (cdr calls
)) (function <))
1442 max
(car (nreverse nums
)))
1443 (when (or (< min
(car sig
))
1444 (and (cdr sig
) (> max
(cdr sig
))))
1445 (byte-compile-set-symbol-position name
)
1447 "%s being defined to take %s%s, but was previously called with %s"
1449 (byte-compile-arglist-signature-string sig
)
1450 (if (equal sig
'(1 .
1)) " arg" " args")
1451 (byte-compile-arglist-signature-string (cons min max
))))))
1452 (let* ((old (byte-compile-fdefinition name macrop
))
1453 (initial (and macrop
1455 byte-compile-initial-macro-environment
)))))
1456 ;; Assumes an element of b-c-i-macro-env that is a symbol points
1457 ;; to a defined function. (Bug#8646)
1458 (and initial
(symbolp initial
)
1459 (setq old
(byte-compile-fdefinition initial nil
)))
1460 (when (and old
(not (eq old t
)))
1461 (and (eq 'macro
(car-safe old
))
1462 (eq 'lambda
(car-safe (cdr-safe old
)))
1463 (setq old
(cdr old
)))
1464 (let ((sig1 (byte-compile-arglist-signature
1466 (`(lambda ,args .
,_
) args
)
1467 (`(closure ,_
,args .
,_
) args
)
1468 ((pred byte-code-function-p
) (aref old
0))
1470 (sig2 (byte-compile-arglist-signature arglist
)))
1471 (unless (byte-compile-arglist-signatures-congruent-p sig1 sig2
)
1472 (byte-compile-set-symbol-position name
)
1474 "%s %s used to take %s %s, now takes %s"
1475 (if macrop
"macro" "function")
1477 (byte-compile-arglist-signature-string sig1
)
1478 (if (equal sig1
'(1 .
1)) "argument" "arguments")
1479 (byte-compile-arglist-signature-string sig2
)))))))
1481 (defvar byte-compile-cl-functions nil
1482 "List of functions defined in CL.")
1484 ;; Can't just add this to cl-load-hook, because that runs just before
1485 ;; the forms from cl.el get added to load-history.
1486 (defun byte-compile-find-cl-functions ()
1487 (unless byte-compile-cl-functions
1488 (dolist (elt load-history
)
1489 (and (byte-compile-cl-file-p (car elt
))
1490 (dolist (e (cdr elt
))
1491 ;; Includes the cl-foo functions that cl autoloads.
1492 (when (memq (car-safe e
) '(autoload defun
))
1493 (push (cdr e
) byte-compile-cl-functions
)))))))
1495 (defun byte-compile-cl-warn (form)
1496 "Warn if FORM is a call of a function from the CL package."
1497 (let ((func (car-safe form
)))
1498 (if (and byte-compile-cl-functions
1499 (memq func byte-compile-cl-functions
)
1500 ;; Aliases which won't have been expanded at this point.
1501 ;; These aren't all aliases of subrs, so not trivial to
1502 ;; avoid hardwiring the list.
1504 '(cl--block-wrapper cl--block-throw
1505 multiple-value-call nth-value
1506 copy-seq first second rest endp cl-member
1507 ;; These are included in generated code
1508 ;; that can't be called except at compile time
1509 ;; or unless cl is loaded anyway.
1510 cl--defsubst-expand cl-struct-setf-expander
1511 ;; These would sometimes be warned about
1512 ;; but such warnings are never useful,
1513 ;; so don't warn about them.
1515 cl--compiling-file
))))
1516 (byte-compile-warn "function `%s' from cl package called at runtime"
1520 (defun byte-compile-print-syms (str1 strn syms
)
1522 (byte-compile-set-symbol-position (car syms
) t
))
1523 (cond ((and (cdr syms
) (not noninteractive
))
1528 (setq s
(symbol-name (pop syms
))
1529 L
(+ L
(length s
) 2))
1530 (if (< L
(1- fill-column
))
1531 (setq str
(concat str
" " s
(and syms
",")))
1532 (setq str
(concat str
"\n " s
(and syms
","))
1533 L
(+ (length s
) 4))))
1534 (byte-compile-warn "%s" str
)))
1536 (byte-compile-warn "%s %s"
1538 (mapconcat #'symbol-name syms
", ")))
1541 (byte-compile-warn str1
(car syms
)))))
1543 ;; If we have compiled any calls to functions which are not known to be
1544 ;; defined, issue a warning enumerating them.
1545 ;; `unresolved' in the list `byte-compile-warnings' disables this.
1546 (defun byte-compile-warn-about-unresolved-functions ()
1547 (when (byte-compile-warning-enabled-p 'unresolved
)
1548 (let ((byte-compile-current-form :end
)
1551 ;; Separate the functions that will not be available at runtime
1552 ;; from the truly unresolved ones.
1553 (dolist (f byte-compile-unresolved-functions
)
1555 (when (not (memq f byte-compile-new-defuns
))
1556 (if (fboundp f
) (push f noruntime
) (push f unresolved
))))
1557 ;; Complain about the no-run-time functions
1558 (byte-compile-print-syms
1559 "the function `%s' might not be defined at runtime."
1560 "the following functions might not be defined at runtime:"
1562 ;; Complain about the unresolved functions
1563 (byte-compile-print-syms
1564 "the function `%s' is not known to be defined."
1565 "the following functions are not known to be defined:"
1570 ;; Dynamically bound in byte-compile-from-buffer.
1571 ;; NB also used in cl.el and cl-macs.el.
1572 (defvar byte-compile--outbuffer
)
1574 (defmacro byte-compile-close-variables
(&rest body
)
1577 ;; Close over these variables to encapsulate the
1578 ;; compilation state
1580 (byte-compile-macro-environment
1581 ;; Copy it because the compiler may patch into the
1582 ;; macroenvironment.
1583 (copy-alist byte-compile-initial-macro-environment
))
1584 (byte-compile--outbuffer nil
)
1585 (byte-compile-function-environment nil
)
1586 (byte-compile-bound-variables nil
)
1587 (byte-compile-lexical-variables nil
)
1588 (byte-compile-const-variables nil
)
1589 (byte-compile-free-references nil
)
1590 (byte-compile-free-assignments nil
)
1592 ;; Close over these variables so that `byte-compiler-options'
1593 ;; can change them on a per-file basis.
1595 (byte-compile-verbose byte-compile-verbose
)
1596 (byte-optimize byte-optimize
)
1597 (byte-compile-dynamic byte-compile-dynamic
)
1598 (byte-compile-dynamic-docstrings
1599 byte-compile-dynamic-docstrings
)
1600 ;; (byte-compile-generate-emacs19-bytecodes
1601 ;; byte-compile-generate-emacs19-bytecodes)
1602 (byte-compile-warnings byte-compile-warnings
)
1606 (defmacro displaying-byte-compile-warnings
(&rest body
)
1608 `(let* ((--displaying-byte-compile-warnings-fn (lambda () ,@body
))
1609 (warning-series-started
1610 (and (markerp warning-series
)
1611 (eq (marker-buffer warning-series
)
1612 (get-buffer byte-compile-log-buffer
)))))
1613 (byte-compile-find-cl-functions)
1614 (if (or (eq warning-series
'byte-compile-warning-series
)
1615 warning-series-started
)
1616 ;; warning-series does come from compilation,
1617 ;; so don't bind it, but maybe do set it.
1619 ;; Log the file name. Record position of that text.
1620 (setq tem
(byte-compile-log-file))
1621 (unless warning-series-started
1622 (setq warning-series
(or tem
'byte-compile-warning-series
)))
1623 (if byte-compile-debug
1624 (funcall --displaying-byte-compile-warnings-fn
)
1625 (condition-case error-info
1626 (funcall --displaying-byte-compile-warnings-fn
)
1627 (error (byte-compile-report-error error-info
)))))
1628 ;; warning-series does not come from compilation, so bind it.
1629 (let ((warning-series
1630 ;; Log the file name. Record position of that text.
1631 (or (byte-compile-log-file) 'byte-compile-warning-series
)))
1632 (if byte-compile-debug
1633 (funcall --displaying-byte-compile-warnings-fn
)
1634 (condition-case error-info
1635 (funcall --displaying-byte-compile-warnings-fn
)
1636 (error (byte-compile-report-error error-info
))))))))
1639 (defun byte-force-recompile (directory)
1640 "Recompile every `.el' file in DIRECTORY that already has a `.elc' file.
1641 Files in subdirectories of DIRECTORY are processed also."
1642 (interactive "DByte force recompile (directory): ")
1643 (byte-recompile-directory directory nil t
))
1646 (defun byte-recompile-directory (directory &optional arg force
)
1647 "Recompile every `.el' file in DIRECTORY that needs recompilation.
1648 This happens when a `.elc' file exists but is older than the `.el' file.
1649 Files in subdirectories of DIRECTORY are processed also.
1651 If the `.elc' file does not exist, normally this function *does not*
1652 compile the corresponding `.el' file. However, if the prefix argument
1653 ARG is 0, that means do compile all those files. A nonzero
1654 ARG means ask the user, for each such `.el' file, whether to
1655 compile it. A nonzero ARG also means ask about each subdirectory
1658 If the third argument FORCE is non-nil, recompile every `.el' file
1659 that already has a `.elc' file."
1660 (interactive "DByte recompile directory: \nP")
1661 (if arg
(setq arg
(prefix-numeric-value arg
)))
1665 (force-mode-line-update))
1666 (with-current-buffer (get-buffer-create byte-compile-log-buffer
)
1667 (setq default-directory
(expand-file-name directory
))
1668 ;; compilation-mode copies value of default-directory.
1669 (unless (eq major-mode
'compilation-mode
)
1671 (let ((directories (list default-directory
))
1672 (default-directory default-directory
)
1678 (displaying-byte-compile-warnings
1680 (setq directory
(car directories
))
1681 (message "Checking %s..." directory
)
1682 (dolist (file (directory-files directory
))
1683 (let ((source (expand-file-name file directory
)))
1684 (if (file-directory-p source
)
1685 (and (not (member file
'("RCS" "CVS")))
1686 (not (eq ?\.
(aref file
0)))
1687 (not (file-symlink-p source
))
1688 ;; This file is a subdirectory. Handle them differently.
1689 (or (null arg
) (eq 0 arg
)
1690 (y-or-n-p (concat "Check " source
"? ")))
1691 (setq directories
(nconc directories
(list source
))))
1692 ;; It is an ordinary file. Decide whether to compile it.
1693 (if (and (string-match emacs-lisp-file-regexp source
)
1694 ;; The next 2 tests avoid compiling lock files
1695 (file-readable-p source
)
1696 (not (string-match "\\`\\.#" file
))
1697 (not (auto-save-file-name-p source
))
1698 (not (string-equal dir-locals-file
1699 (file-name-nondirectory source
))))
1701 (pcase (byte-recompile-file source force arg
)
1702 (`no-byte-compile skip-count
)
1706 (message "Checking %s..." directory
))
1707 (if (not (eq last-dir directory
))
1708 (setq last-dir directory
1709 dir-count
(1+ dir-count
)))
1711 (setq directories
(cdr directories
))))
1712 (message "Done (Total of %d file%s compiled%s%s%s)"
1713 file-count
(if (= file-count
1) "" "s")
1714 (if (> fail-count
0) (format ", %d failed" fail-count
) "")
1715 (if (> skip-count
0) (format ", %d skipped" skip-count
) "")
1717 (format " in %d directories" dir-count
) "")))))
1719 (defvar no-byte-compile nil
1720 "Non-nil to prevent byte-compiling of Emacs Lisp code.
1721 This is normally set in local file variables at the end of the elisp file:
1723 \;; Local Variables:\n;; no-byte-compile: t\n;; End: ") ;Backslash for compile-main.
1724 ;;;###autoload(put 'no-byte-compile 'safe-local-variable 'booleanp)
1726 (defun byte-recompile-file (filename &optional force arg load
)
1727 "Recompile FILENAME file if it needs recompilation.
1728 This happens when its `.elc' file is older than itself.
1730 If the `.elc' file exists and is up-to-date, normally this function
1731 *does not* compile FILENAME. If the prefix argument FORCE is non-nil,
1732 however, it compiles FILENAME even if the destination already
1733 exists and is up-to-date.
1735 If the `.elc' file does not exist, normally this function *does not*
1736 compile FILENAME. If optional argument ARG is 0, it compiles
1737 the input file even if the `.elc' file does not exist.
1738 Any other non-nil value of ARG means to ask the user.
1740 If optional argument LOAD is non-nil, loads the file after compiling.
1742 If compilation is needed, this functions returns the result of
1743 `byte-compile-file'; otherwise it returns `no-byte-compile'."
1745 (let ((file buffer-file-name
)
1749 (derived-mode-p 'emacs-lisp-mode
)
1750 (setq file-name
(file-name-nondirectory file
)
1751 file-dir
(file-name-directory file
)))
1752 (list (read-file-name (if current-prefix-arg
1753 "Byte compile file: "
1754 "Byte recompile file: ")
1755 file-dir file-name nil
)
1756 current-prefix-arg
)))
1757 (let ((dest (byte-compile-dest-file filename
))
1758 ;; Expand now so we get the current buffer's defaults
1759 (filename (expand-file-name filename
)))
1760 (if (if (file-exists-p dest
)
1761 ;; File was already compiled
1762 ;; Compile if forced to, or filename newer
1764 (file-newer-than-file-p filename dest
))
1767 (y-or-n-p (concat "Compile "
1770 (if (and noninteractive
(not byte-compile-verbose
))
1771 (message "Compiling %s..." filename
))
1772 (byte-compile-file filename load
))
1774 (load (if (file-exists-p dest
) dest filename
)))
1777 (defvar byte-compile-level
0 ; bug#13787
1778 "Depth of a recursive byte compilation.")
1781 (defun byte-compile-file (filename &optional load
)
1782 "Compile a file of Lisp code named FILENAME into a file of byte code.
1783 The output file's name is generated by passing FILENAME to the
1784 function `byte-compile-dest-file' (which see).
1785 With prefix arg (noninteractively: 2nd arg), LOAD the file after compiling.
1786 The value is non-nil if there were no errors, nil if errors."
1787 ;; (interactive "fByte compile file: \nP")
1789 (let ((file buffer-file-name
)
1792 (derived-mode-p 'emacs-lisp-mode
)
1793 (setq file-dir
(file-name-directory file
)))
1794 (list (read-file-name (if current-prefix-arg
1795 "Byte compile and load file: "
1796 "Byte compile file: ")
1797 file-dir buffer-file-name nil
)
1798 current-prefix-arg
)))
1799 ;; Expand now so we get the current buffer's defaults
1800 (setq filename
(expand-file-name filename
))
1802 ;; If we're compiling a file that's in a buffer and is modified, offer
1803 ;; to save it first.
1805 (let ((b (get-file-buffer (expand-file-name filename
))))
1806 (if (and b
(buffer-modified-p b
)
1807 (y-or-n-p (format "Save buffer %s first? " (buffer-name b
))))
1808 (with-current-buffer b
(save-buffer)))))
1810 ;; Force logging of the file name for each file compiled.
1811 (setq byte-compile-last-logged-file nil
)
1812 (let ((byte-compile-current-file filename
)
1813 (byte-compile-current-group nil
)
1814 (set-auto-coding-for-load t
)
1815 target-file input-buffer output-buffer
1816 byte-compile-dest-file
)
1817 (setq target-file
(byte-compile-dest-file filename
))
1818 (setq byte-compile-dest-file target-file
)
1819 (with-current-buffer
1820 ;; It would be cleaner to use a temp buffer, but if there was
1821 ;; an error, we leave this buffer around for diagnostics.
1822 ;; Its name is documented in the lispref.
1823 (setq input-buffer
(get-buffer-create
1824 (concat " *Compiler Input*"
1825 (if (zerop byte-compile-level
) ""
1826 (format "-%s" byte-compile-level
)))))
1828 (setq buffer-file-coding-system nil
)
1829 ;; Always compile an Emacs Lisp file as multibyte
1830 ;; unless the file itself forces unibyte with -*-coding: raw-text;-*-
1831 (set-buffer-multibyte t
)
1832 (insert-file-contents filename
)
1833 ;; Mimic the way after-insert-file-set-coding can make the
1834 ;; buffer unibyte when visiting this file.
1835 (when (or (eq last-coding-system-used
'no-conversion
)
1836 (eq (coding-system-type last-coding-system-used
) 5))
1837 ;; For coding systems no-conversion and raw-text...,
1838 ;; edit the buffer as unibyte.
1839 (set-buffer-multibyte nil
))
1840 ;; Run hooks including the uncompression hook.
1841 ;; If they change the file name, then change it for the output also.
1842 (let ((buffer-file-name filename
)
1843 (dmm (default-value 'major-mode
))
1844 ;; Ignore unsafe local variables.
1845 ;; We only care about a few of them for our purposes.
1846 (enable-local-variables :safe
)
1847 (enable-local-eval nil
))
1850 (setq-default major-mode
'emacs-lisp-mode
)
1851 ;; Arg of t means don't alter enable-local-variables.
1852 (delay-mode-hooks (normal-mode t
)))
1853 (setq-default major-mode dmm
))
1854 ;; There may be a file local variable setting (bug#10419).
1855 (setq buffer-read-only nil
1856 filename buffer-file-name
))
1857 ;; Don't inherit lexical-binding from caller (bug#12938).
1858 (unless (local-variable-p 'lexical-binding
)
1859 (setq-local lexical-binding nil
))
1860 ;; Set the default directory, in case an eval-when-compile uses it.
1861 (setq default-directory
(file-name-directory filename
)))
1862 ;; Check if the file's local variables explicitly specify not to
1863 ;; compile this file.
1864 (if (with-current-buffer input-buffer no-byte-compile
)
1866 ;; (message "%s not compiled because of `no-byte-compile: %s'"
1867 ;; (byte-compile-abbreviate-file filename)
1868 ;; (with-current-buffer input-buffer no-byte-compile))
1869 (when (file-exists-p target-file
)
1870 (message "%s deleted because of `no-byte-compile: %s'"
1871 (byte-compile-abbreviate-file target-file
)
1872 (buffer-local-value 'no-byte-compile input-buffer
))
1873 (condition-case nil
(delete-file target-file
) (error nil
)))
1874 ;; We successfully didn't compile this file.
1876 (when byte-compile-verbose
1877 (message "Compiling %s..." filename
))
1878 (setq byte-compiler-error-flag nil
)
1879 ;; It is important that input-buffer not be current at this call,
1880 ;; so that the value of point set in input-buffer
1881 ;; within byte-compile-from-buffer lingers in that buffer.
1883 (save-current-buffer
1884 (let ((byte-compile-level (1+ byte-compile-level
)))
1885 (byte-compile-from-buffer input-buffer
))))
1886 (if byte-compiler-error-flag
1888 (when byte-compile-verbose
1889 (message "Compiling %s...done" filename
))
1890 (kill-buffer input-buffer
)
1891 (with-current-buffer output-buffer
1892 (goto-char (point-max))
1893 (insert "\n") ; aaah, unix.
1894 (if (file-writable-p target-file
)
1895 ;; We must disable any code conversion here.
1896 (let* ((coding-system-for-write 'no-conversion
)
1897 ;; Write to a tempfile so that if another Emacs
1898 ;; process is trying to load target-file (eg in a
1899 ;; parallel bootstrap), it does not risk getting a
1900 ;; half-finished file. (Bug#4196)
1901 (tempfile (make-temp-name target-file
))
1903 (cons (lambda () (ignore-errors (delete-file tempfile
)))
1905 (write-region (point-min) (point-max) tempfile nil
1)
1906 ;; This has the intentional side effect that any
1907 ;; hard-links to target-file continue to
1908 ;; point to the old file (this makes it possible
1909 ;; for installed files to share disk space with
1910 ;; the build tree, without causing problems when
1911 ;; emacs-lisp files in the build tree are
1912 ;; recompiled). Previously this was accomplished by
1913 ;; deleting target-file before writing it.
1914 (rename-file tempfile target-file t
)
1915 (or noninteractive
(message "Wrote %s" target-file
)))
1916 ;; This is just to give a better error message than write-region
1917 (let ((exists (file-exists-p target-file
)))
1918 (signal (if exists
'file-error
'file-missing
)
1919 (list "Opening output file"
1921 "Cannot overwrite file"
1922 "Directory not writable or nonexistent")
1924 (kill-buffer (current-buffer)))
1925 (if (and byte-compile-generate-call-tree
1926 (or (eq t byte-compile-generate-call-tree
)
1927 (y-or-n-p (format "Report call tree for %s? "
1930 (display-call-tree filename
)))
1935 ;;; compiling a single function
1937 (defun compile-defun (&optional arg
)
1938 "Compile and evaluate the current top-level form.
1939 Print the result in the echo area.
1940 With argument ARG, insert value in current buffer after the form."
1944 (beginning-of-defun)
1945 (let* ((byte-compile-current-file nil
)
1946 (byte-compile-current-buffer (current-buffer))
1947 (byte-compile-read-position (point))
1948 (byte-compile-last-position byte-compile-read-position
)
1949 (byte-compile-last-warned-form 'nothing
)
1951 (let ((read-with-symbol-positions (current-buffer))
1952 (read-symbol-positions-list nil
))
1953 (displaying-byte-compile-warnings
1955 (eval-sexp-add-defvars
1956 (read (current-buffer))
1957 byte-compile-read-position
))))
1960 (message "Compiling from buffer... done.")
1961 (prin1 value
(current-buffer))
1963 ((message "%s" (prin1-to-string value
)))))))
1965 (defun byte-compile-from-buffer (inbuffer)
1966 (let ((byte-compile-current-buffer inbuffer
)
1967 (byte-compile-read-position nil
)
1968 (byte-compile-last-position nil
)
1969 ;; Prevent truncation of flonums and lists as we read and print them
1970 (float-output-format nil
)
1971 (case-fold-search nil
)
1974 ;; Prevent edebug from interfering when we compile
1975 ;; and put the output into a file.
1976 ;; (edebug-all-defs nil)
1977 ;; (edebug-all-forms nil)
1978 ;; Simulate entry to byte-compile-top-level
1979 (byte-compile-jump-tables nil
)
1980 (byte-compile-constants nil
)
1981 (byte-compile-variables nil
)
1982 (byte-compile-tag-number 0)
1983 (byte-compile-depth 0)
1984 (byte-compile-maxdepth 0)
1985 (byte-compile-output nil
)
1986 ;; This allows us to get the positions of symbols read; it's
1987 ;; new in Emacs 22.1.
1988 (read-with-symbol-positions inbuffer
)
1989 (read-symbol-positions-list nil
)
1990 ;; #### This is bound in b-c-close-variables.
1991 ;; (byte-compile-warnings byte-compile-warnings)
1993 (byte-compile-close-variables
1994 (with-current-buffer
1995 (setq byte-compile--outbuffer
1997 (concat " *Compiler Output*"
1998 (if (<= byte-compile-level
1) ""
1999 (format "-%s" (1- byte-compile-level
))))))
2000 (set-buffer-multibyte t
)
2002 ;; (emacs-lisp-mode)
2003 (setq case-fold-search nil
))
2004 (displaying-byte-compile-warnings
2005 (with-current-buffer inbuffer
2006 (and byte-compile-current-file
2007 (byte-compile-insert-header byte-compile-current-file
2008 byte-compile--outbuffer
))
2009 (goto-char (point-min))
2010 ;; Should we always do this? When calling multiple files, it
2011 ;; would be useful to delay this warning until all have been
2012 ;; compiled. A: Yes! b-c-u-f might contain dross from a
2013 ;; previous byte-compile.
2014 (setq byte-compile-unresolved-functions nil
)
2015 (setq byte-compile-noruntime-functions nil
)
2016 (setq byte-compile-new-defuns nil
)
2018 ;; Compile the forms from the input buffer.
2020 (while (progn (skip-chars-forward " \t\n\^l")
2021 (= (following-char) ?\
;))
2024 (setq byte-compile-read-position
(point)
2025 byte-compile-last-position byte-compile-read-position
)
2026 (let* ((old-style-backquotes nil
)
2027 (form (read inbuffer
)))
2028 ;; Warn about the use of old-style backquotes.
2029 (when old-style-backquotes
2030 (byte-compile-warn "!! The file uses old-style backquotes !!
2031 This functionality has been obsolete for more than 10 years already
2032 and will be removed soon. See (elisp)Backquote in the manual."))
2033 (byte-compile-toplevel-file-form form
)))
2034 ;; Compile pending forms at end of file.
2035 (byte-compile-flush-pending)
2036 ;; Make warnings about unresolved functions
2037 ;; give the end of the file as their position.
2038 (setq byte-compile-last-position
(point-max))
2039 (byte-compile-warn-about-unresolved-functions))
2040 ;; Fix up the header at the front of the output
2041 ;; if the buffer contains multibyte characters.
2042 (and byte-compile-current-file
2043 (with-current-buffer byte-compile--outbuffer
2044 (byte-compile-fix-header byte-compile-current-file
))))
2045 byte-compile--outbuffer
)))
2047 (defun byte-compile-fix-header (_filename)
2048 "If the current buffer has any multibyte characters, insert a version test."
2049 (when (< (point-max) (position-bytes (point-max)))
2050 (goto-char (point-min))
2051 ;; Find the comment that describes the version condition.
2052 (search-forward "\n;;; This file uses")
2053 (narrow-to-region (line-beginning-position) (point-max))
2054 ;; Find the first line of ballast semicolons.
2055 (search-forward ";;;;;;;;;;")
2057 (narrow-to-region (point-min) (point))
2058 (let ((old-header-end (point))
2059 (minimum-version "23")
2061 (delete-region (point-min) (point-max))
2063 ";;; This file contains utf-8 non-ASCII characters,\n"
2064 ";;; and so cannot be loaded into Emacs 22 or earlier.\n"
2065 ;; Have to check if emacs-version is bound so that this works
2066 ;; in files loaded early in loadup.el.
2067 "(and (boundp 'emacs-version)\n"
2068 ;; If there is a name at the end of emacs-version,
2069 ;; don't try to check the version number.
2070 " (< (aref emacs-version (1- (length emacs-version))) ?A)\n"
2071 (format " (string-lessp emacs-version \"%s\")\n" minimum-version
)
2072 ;; Because the header must fit in a fixed width, we cannot
2073 ;; insert arbitrary-length file names (Bug#11585).
2074 " (error \"`%s' was compiled for "
2075 (format "Emacs %s or later\" #$))\n\n" minimum-version
))
2076 ;; Now compensate for any change in size, to make sure all
2077 ;; positions in the file remain valid.
2078 (setq delta
(- (point-max) old-header-end
))
2079 (goto-char (point-max))
2081 (delete-char delta
))))
2083 (defun byte-compile-insert-header (_filename outbuffer
)
2084 "Insert a header at the start of OUTBUFFER.
2085 Call from the source buffer."
2086 (let ((dynamic-docstrings byte-compile-dynamic-docstrings
)
2087 (dynamic byte-compile-dynamic
)
2088 (optimize byte-optimize
))
2089 (with-current-buffer outbuffer
2090 (goto-char (point-min))
2091 ;; The magic number of .elc files is ";ELC", or 0x3B454C43. After
2092 ;; that is the file-format version number (18, 19, 20, or 23) as a
2093 ;; byte, followed by some nulls. The primary motivation for doing
2094 ;; this is to get some binary characters up in the first line of
2095 ;; the file so that `diff' will simply say "Binary files differ"
2096 ;; instead of actually doing a diff of two .elc files. An extra
2097 ;; benefit is that you can add this to /etc/magic:
2098 ;; 0 string ;ELC GNU Emacs Lisp compiled file,
2099 ;; >4 byte x version %d
2101 ";ELC" 23 "\000\000\000\n"
2103 ";;; in Emacs version " emacs-version
"\n"
2106 ((eq optimize
'source
) " source-level optimization only")
2107 ((eq optimize
'byte
) " byte-level optimization only")
2108 (optimize " all optimizations")
2109 (t "out optimization"))
2111 (if dynamic
";;; Function definitions are lazy-loaded.\n"
2113 "\n;;; This file uses "
2114 (if dynamic-docstrings
2115 "dynamic docstrings, first added in Emacs 19.29"
2116 "opcodes that do not exist in Emacs 18")
2118 ;; Note that byte-compile-fix-header may change this.
2119 ";;; This file does not contain utf-8 non-ASCII characters,\n"
2120 ";;; and so can be loaded in Emacs versions earlier than 23.\n\n"
2121 ;; Insert semicolons as ballast, so that byte-compile-fix-header
2122 ;; can delete them so as to keep the buffer positions
2123 ;; constant for the actual compiled code.
2124 ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n"
2125 ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n\n"))))
2127 (defun byte-compile-output-file-form (form)
2128 ;; Write the given form to the output buffer, being careful of docstrings
2129 ;; in defvar, defvaralias, defconst, autoload and
2130 ;; custom-declare-variable because make-docfile is so amazingly stupid.
2131 ;; defalias calls are output directly by byte-compile-file-form-defmumble;
2132 ;; it does not pay to first build the defalias in defmumble and then parse
2134 (let ((print-escape-newlines t
)
2139 (print-circle ; Handle circular data structures.
2140 (not byte-compile-disable-print-circle
)))
2141 (if (and (memq (car-safe form
) '(defvar defvaralias defconst
2142 autoload custom-declare-variable
))
2143 (stringp (nth 3 form
)))
2144 (byte-compile-output-docform nil nil
'("\n(" 3 ")") form nil
2146 '(defvaralias autoload
2147 custom-declare-variable
)))
2148 (princ "\n" byte-compile--outbuffer
)
2149 (prin1 form byte-compile--outbuffer
)
2152 (defvar byte-compile--for-effect
)
2154 (defun byte-compile-output-docform (preface name info form specindex quoted
)
2155 "Print a form with a doc string. INFO is (prefix doc-index postfix).
2156 If PREFACE and NAME are non-nil, print them too,
2157 before INFO and the FORM but after the doc string itself.
2158 If SPECINDEX is non-nil, it is the index in FORM
2159 of the function bytecode string. In that case,
2160 we output that argument and the following argument
2161 \(the constants vector) together, for lazy loading.
2162 QUOTED says that we have to put a quote before the
2163 list that represents a doc string reference.
2164 `defvaralias', `autoload' and `custom-declare-variable' need that."
2165 ;; We need to examine byte-compile-dynamic-docstrings
2166 ;; in the input buffer (now current), not in the output buffer.
2167 (let ((dynamic-docstrings byte-compile-dynamic-docstrings
))
2168 (with-current-buffer byte-compile--outbuffer
2171 ;; Insert the doc string, and make it a comment with #@LENGTH.
2172 (and (>= (nth 1 info
) 0)
2175 ;; Make the doc string start at beginning of line
2176 ;; for make-docfile's sake.
2179 (byte-compile-output-as-comment
2180 (nth (nth 1 info
) form
) nil
))
2181 ;; If the doc string starts with * (a user variable),
2183 (if (and (stringp (nth (nth 1 info
) form
))
2184 (> (length (nth (nth 1 info
) form
)) 0)
2185 (eq (aref (nth (nth 1 info
) form
) 0) ?
*))
2186 (setq position
(- position
)))))
2188 (let ((print-continuous-numbering t
)
2191 ;; FIXME: The bindings below are only needed for when we're
2192 ;; called from ...-defmumble.
2193 (print-escape-newlines t
)
2198 (print-circle ; Handle circular data structures.
2199 (not byte-compile-disable-print-circle
)))
2202 ;; FIXME: We don't handle uninterned names correctly.
2203 ;; E.g. if cl-define-compiler-macro uses uninterned name we get:
2204 ;; (defalias '#1=#:foo--cmacro #[514 ...])
2205 ;; (put 'foo 'compiler-macro '#:foo--cmacro)
2207 (prin1 name byte-compile--outbuffer
)))
2209 (prin1 (car form
) byte-compile--outbuffer
)
2210 (while (setq form
(cdr form
))
2211 (setq index
(1+ index
))
2213 (cond ((and (numberp specindex
) (= index specindex
)
2214 ;; Don't handle the definition dynamically
2215 ;; if it refers (or might refer)
2216 ;; to objects already output
2217 ;; (for instance, gensyms in the arg list).
2219 (when (hash-table-p print-number-table
)
2220 (maphash (lambda (_k v
) (if v
(setq non-nil t
)))
2221 print-number-table
))
2223 ;; Output the byte code and constants specially
2224 ;; for lazy dynamic loading.
2226 (byte-compile-output-as-comment
2227 (cons (car form
) (nth 1 form
))
2229 (princ (format "(#$ . %d) nil" position
)
2230 byte-compile--outbuffer
)
2231 (setq form
(cdr form
))
2232 (setq index
(1+ index
))))
2233 ((= index
(nth 1 info
))
2235 (princ (format (if quoted
"'(#$ . %d)" "(#$ . %d)")
2237 byte-compile--outbuffer
)
2238 (let ((print-escape-newlines nil
))
2239 (goto-char (prog1 (1+ (point))
2241 byte-compile--outbuffer
)))
2243 (goto-char (point-max)))))
2245 (prin1 (car form
) byte-compile--outbuffer
)))))
2246 (insert (nth 2 info
)))))
2249 (defun byte-compile-keep-pending (form &optional handler
)
2250 (if (memq byte-optimize
'(t source
))
2251 (setq form
(byte-optimize-form form t
)))
2253 (let ((byte-compile--for-effect t
))
2254 ;; To avoid consing up monstrously large forms at load time, we split
2255 ;; the output regularly.
2256 (and (memq (car-safe form
) '(fset defalias
))
2257 (nthcdr 300 byte-compile-output
)
2258 (byte-compile-flush-pending))
2259 (funcall handler form
)
2260 (if byte-compile--for-effect
2261 (byte-compile-discard)))
2262 (byte-compile-form form t
))
2265 (defun byte-compile-flush-pending ()
2266 (if byte-compile-output
2267 (let ((form (byte-compile-out-toplevel t
'file
)))
2268 (cond ((eq (car-safe form
) 'progn
)
2269 (mapc 'byte-compile-output-file-form
(cdr form
)))
2271 (byte-compile-output-file-form form
)))
2272 (setq byte-compile-constants nil
2273 byte-compile-variables nil
2274 byte-compile-depth
0
2275 byte-compile-maxdepth
0
2276 byte-compile-output nil
2277 byte-compile-jump-tables nil
))))
2279 (defvar byte-compile-force-lexical-warnings nil
)
2281 (defun byte-compile-preprocess (form &optional _for-effect
)
2282 (setq form
(macroexpand-all form byte-compile-macro-environment
))
2283 ;; FIXME: We should run byte-optimize-form here, but it currently does not
2284 ;; recurse through all the code, so we'd have to fix this first.
2285 ;; Maybe a good fix would be to merge byte-optimize-form into
2287 ;; (if (memq byte-optimize '(t source))
2288 ;; (setq form (byte-optimize-form form for-effect)))
2290 (lexical-binding (cconv-closure-convert form
))
2291 (byte-compile-force-lexical-warnings (cconv-warnings-only form
))
2294 ;; byte-hunk-handlers cannot call this!
2295 (defun byte-compile-toplevel-file-form (top-level-form)
2296 (byte-compile-recurse-toplevel
2299 (let ((byte-compile-current-form nil
)) ; close over this for warnings.
2300 (byte-compile-file-form (byte-compile-preprocess form t
))))))
2302 ;; byte-hunk-handlers can call this.
2303 (defun byte-compile-file-form (form)
2305 (cond ((and (consp form
)
2306 (symbolp (car form
))
2307 (setq handler
(get (car form
) 'byte-hunk-handler
)))
2308 (cond ((setq form
(funcall handler form
))
2309 (byte-compile-flush-pending)
2310 (byte-compile-output-file-form form
))))
2312 (byte-compile-keep-pending form
)))))
2314 ;; Functions and variables with doc strings must be output separately,
2315 ;; so make-docfile can recognize them. Most other things can be output
2318 (put 'autoload
'byte-hunk-handler
'byte-compile-file-form-autoload
)
2319 (defun byte-compile-file-form-autoload (form)
2320 (and (let ((form form
))
2321 (while (if (setq form
(cdr form
)) (macroexp-const-p (car form
))))
2322 (null form
)) ;Constants only
2323 (memq (eval (nth 5 form
)) '(t macro
)) ;Macro
2324 (eval form
)) ;Define the autoload.
2325 ;; Avoid undefined function warnings for the autoload.
2327 (`',(and (pred symbolp
) funsym
)
2328 ;; Don't add it if it's already defined. Otherwise, it might
2329 ;; hide the actual definition. However, do remove any entry from
2330 ;; byte-compile-noruntime-functions, in case we have an autoload
2331 ;; of foo-func following an (eval-when-compile (require 'foo)).
2332 (unless (fboundp funsym
)
2333 (push (cons funsym
(cons 'autoload
(cdr (cdr form
))))
2334 byte-compile-function-environment
))
2335 ;; If an autoload occurs _before_ the first call to a function,
2336 ;; byte-compile-callargs-warn does not add an entry to
2337 ;; byte-compile-unresolved-functions. Here we mimic the logic
2338 ;; of byte-compile-callargs-warn so as not to warn if the
2339 ;; autoload comes _after_ the function call.
2340 ;; Alternatively, similar logic could go in
2341 ;; byte-compile-warn-about-unresolved-functions.
2342 (if (memq funsym byte-compile-noruntime-functions
)
2343 (setq byte-compile-noruntime-functions
2344 (delq funsym byte-compile-noruntime-functions
))
2345 (setq byte-compile-unresolved-functions
2346 (delq (assq funsym byte-compile-unresolved-functions
)
2347 byte-compile-unresolved-functions
)))))
2348 (if (stringp (nth 3 form
))
2350 ;; No doc string, so we can compile this as a normal form.
2351 (byte-compile-keep-pending form
'byte-compile-normal-call
)))
2353 (put 'defvar
'byte-hunk-handler
'byte-compile-file-form-defvar
)
2354 (put 'defconst
'byte-hunk-handler
'byte-compile-file-form-defvar
)
2356 (defun byte-compile--declare-var (sym)
2357 (when (and (symbolp sym
)
2358 (not (string-match "[-*/:$]" (symbol-name sym
)))
2359 (byte-compile-warning-enabled-p 'lexical
))
2360 (byte-compile-warn "global/dynamic var `%s' lacks a prefix"
2362 (when (memq sym byte-compile-lexical-variables
)
2363 (setq byte-compile-lexical-variables
2364 (delq sym byte-compile-lexical-variables
))
2365 (byte-compile-warn "Variable `%S' declared after its first use" sym
))
2366 (push sym byte-compile-bound-variables
))
2368 (defun byte-compile-file-form-defvar (form)
2369 (let ((sym (nth 1 form
)))
2370 (byte-compile--declare-var sym
)
2371 (if (eq (car form
) 'defconst
)
2372 (push sym byte-compile-const-variables
)))
2373 (if (and (null (cddr form
)) ;No `value' provided.
2374 (eq (car form
) 'defvar
)) ;Just a declaration.
2376 (cond ((consp (nth 2 form
))
2377 (setq form
(copy-sequence form
))
2378 (setcar (cdr (cdr form
))
2379 (byte-compile-top-level (nth 2 form
) nil
'file
))))
2382 (put 'define-abbrev-table
'byte-hunk-handler
2383 'byte-compile-file-form-defvar-function
)
2384 (put 'defvaralias
'byte-hunk-handler
'byte-compile-file-form-defvar-function
)
2386 (defun byte-compile-file-form-defvar-function (form)
2387 (pcase-let (((or `',name
(let name nil
)) (nth 1 form
)))
2388 (if name
(byte-compile--declare-var name
)))
2389 (byte-compile-keep-pending form
))
2391 (put 'custom-declare-variable
'byte-hunk-handler
2392 'byte-compile-file-form-custom-declare-variable
)
2393 (defun byte-compile-file-form-custom-declare-variable (form)
2394 (when (byte-compile-warning-enabled-p 'callargs
)
2395 (byte-compile-nogroup-warn form
))
2396 (byte-compile-file-form-defvar-function form
))
2398 (put 'require
'byte-hunk-handler
'byte-compile-file-form-require
)
2399 (defun byte-compile-file-form-require (form)
2400 (let ((args (mapcar 'eval
(cdr form
)))
2401 (hist-orig load-history
)
2403 (apply 'require args
)
2405 ;; Record the functions defined by the require in `byte-compile-new-defuns'.
2406 (setq hist-new load-history
)
2407 (setq prov-cons
(cons 'provide
(car args
)))
2408 (while (and hist-new
2409 (not (member prov-cons
(car hist-new
))))
2410 (setq hist-new
(cdr hist-new
)))
2412 (dolist (x (car hist-new
))
2413 (when (and (consp x
)
2414 (memq (car x
) '(defun t)))
2415 (push (cdr x
) byte-compile-new-defuns
))))
2417 (when (byte-compile-warning-enabled-p 'cl-functions
)
2418 ;; Detect (require 'cl) in a way that works even if cl is already loaded.
2419 (if (member (car args
) '("cl" cl
))
2421 (byte-compile-warn "cl package required at runtime")
2422 (byte-compile-disable-warning 'cl-functions
))
2423 ;; We may have required something that causes cl to be loaded, eg
2424 ;; the uncompiled version of a file that requires cl when compiling.
2425 (setq hist-new load-history
)
2426 (while (and (not byte-compile-cl-functions
)
2427 hist-new
(not (eq hist-new hist-orig
)))
2428 (and (byte-compile-cl-file-p (car (pop hist-new
)))
2429 (byte-compile-find-cl-functions))))))
2430 (byte-compile-keep-pending form
'byte-compile-normal-call
))
2432 (put 'progn
'byte-hunk-handler
'byte-compile-file-form-progn
)
2433 (put 'prog1
'byte-hunk-handler
'byte-compile-file-form-progn
)
2434 (put 'prog2
'byte-hunk-handler
'byte-compile-file-form-progn
)
2435 (defun byte-compile-file-form-progn (form)
2436 (mapc 'byte-compile-file-form
(cdr form
))
2437 ;; Return nil so the forms are not output twice.
2440 (put 'with-no-warnings
'byte-hunk-handler
2441 'byte-compile-file-form-with-no-warnings
)
2442 (defun byte-compile-file-form-with-no-warnings (form)
2443 ;; cf byte-compile-file-form-progn.
2444 (let (byte-compile-warnings)
2445 (mapc 'byte-compile-file-form
(cdr form
))
2448 ;; This handler is not necessary, but it makes the output from dont-compile
2449 ;; and similar macros cleaner.
2450 (put 'eval
'byte-hunk-handler
'byte-compile-file-form-eval
)
2451 (defun byte-compile-file-form-eval (form)
2452 (if (eq (car-safe (nth 1 form
)) 'quote
)
2453 (nth 1 (nth 1 form
))
2454 (byte-compile-keep-pending form
)))
2456 (defun byte-compile-file-form-defmumble (name macro arglist body rest
)
2457 "Process a `defalias' for NAME.
2458 If MACRO is non-nil, the definition is known to be a macro.
2459 ARGLIST is the list of arguments, if it was recognized or t otherwise.
2460 BODY of the definition, or t if not recognized.
2461 Return non-nil if everything went as planned, or nil to imply that it decided
2462 not to take responsibility for the actual compilation of the code."
2463 (let* ((this-kind (if macro
'byte-compile-macro-environment
2464 'byte-compile-function-environment
))
2465 (that-kind (if macro
'byte-compile-function-environment
2466 'byte-compile-macro-environment
))
2467 (this-one (assq name
(symbol-value this-kind
)))
2468 (that-one (assq name
(symbol-value that-kind
)))
2469 (byte-compile-current-form name
)) ; For warnings.
2471 (byte-compile-set-symbol-position name
)
2472 (push name byte-compile-new-defuns
)
2473 ;; When a function or macro is defined, add it to the call tree so that
2474 ;; we can tell when functions are not used.
2475 (if byte-compile-generate-call-tree
2476 (or (assq name byte-compile-call-tree
)
2477 (setq byte-compile-call-tree
2478 (cons (list name nil nil
) byte-compile-call-tree
))))
2480 (if (byte-compile-warning-enabled-p 'redefine
)
2481 (byte-compile-arglist-warn name arglist macro
))
2483 (if byte-compile-verbose
2484 (message "Compiling %s... (%s)"
2485 (or byte-compile-current-file
"") name
))
2486 (cond ((not (or macro
(listp body
)))
2487 ;; We do not know positively if the definition is a macro
2488 ;; or a function, so we shouldn't emit warnings.
2489 ;; This also silences "multiple definition" warnings for defmethods.
2492 (if (and (byte-compile-warning-enabled-p 'redefine
)
2493 ;; Don't warn when compiling the stubs in byte-run...
2494 (not (assq name byte-compile-initial-macro-environment
)))
2496 "`%s' defined multiple times, as both function and macro"
2498 (setcdr that-one nil
))
2500 (when (and (byte-compile-warning-enabled-p 'redefine
)
2501 ;; Hack: Don't warn when compiling the magic internal
2502 ;; byte-compiler macros in byte-run.el...
2503 (not (assq name byte-compile-initial-macro-environment
)))
2504 (byte-compile-warn "%s `%s' defined multiple times in this file"
2505 (if macro
"macro" "function")
2507 ((eq (car-safe (symbol-function name
))
2508 (if macro
'lambda
'macro
))
2509 (when (byte-compile-warning-enabled-p 'redefine
)
2510 (byte-compile-warn "%s `%s' being redefined as a %s"
2511 (if macro
"function" "macro")
2513 (if macro
"macro" "function")))
2514 ;; Shadow existing definition.
2516 (cons (cons name nil
)
2517 (symbol-value this-kind
))))
2520 (when (and (listp body
)
2521 (stringp (car body
))
2522 (symbolp (car-safe (cdr-safe body
)))
2523 (car-safe (cdr-safe body
))
2524 (stringp (car-safe (cdr-safe (cdr-safe body
)))))
2525 ;; FIXME: We've done that already just above, so this looks wrong!
2526 ;;(byte-compile-set-symbol-position name)
2527 (byte-compile-warn "probable `\"' without `\\' in doc string of %s"
2530 (if (not (listp body
))
2531 ;; The precise definition requires evaluation to find out, so it
2532 ;; will only be known at runtime.
2533 ;; For a macro, that means we can't use that macro in the same file.
2536 (push (cons name
(if (listp arglist
) `(declared ,arglist
) t
))
2537 byte-compile-function-environment
))
2538 ;; Tell the caller that we didn't compile it yet.
2541 (let* ((code (byte-compile-lambda (cons arglist body
) t
)))
2543 ;; A definition in b-c-initial-m-e should always take precedence
2544 ;; during compilation, so don't let it be redefined. (Bug#8647)
2546 (assq name byte-compile-initial-macro-environment
))
2547 (setcdr this-one code
))
2549 (cons (cons name code
)
2550 (symbol-value this-kind
))))
2553 ;; There are additional args to `defalias' (like maybe a docstring)
2554 ;; that the code below can't handle: punt!
2556 ;; Otherwise, we have a bona-fide defun/defmacro definition, and use
2557 ;; special code to allow dynamic docstrings and byte-code.
2558 (byte-compile-flush-pending)
2560 ;; If there's no doc string, provide -1 as the "doc string
2561 ;; index" so that no element will be treated as a doc string.
2562 (if (not (stringp (car body
))) -
1 4)))
2563 ;; Output the form by hand, that's much simpler than having
2564 ;; b-c-output-file-form analyze the defalias.
2565 (byte-compile-output-docform
2568 (if macro
`(" '(macro . #[" ,index
"])") `(" #[" ,index
"]"))
2569 (append code nil
) ; Turn byte-code-function-p into list.
2570 (and (atom code
) byte-compile-dynamic
2573 (princ ")" byte-compile--outbuffer
)
2576 (defun byte-compile-output-as-comment (exp quoted
)
2577 "Print Lisp object EXP in the output file, inside a comment,
2578 and return the file (byte) position it will have.
2579 If QUOTED is non-nil, print with quoting; otherwise, print without quoting."
2580 (with-current-buffer byte-compile--outbuffer
2581 (let ((position (point)))
2583 ;; Insert EXP, and make it a comment with #@LENGTH.
2586 (prin1 exp byte-compile--outbuffer
)
2587 (princ exp byte-compile--outbuffer
))
2588 (goto-char position
)
2589 ;; Quote certain special characters as needed.
2590 ;; get_doc_string in doc.c does the unquoting.
2591 (while (search-forward "\^A" nil t
)
2592 (replace-match "\^A\^A" t t
))
2593 (goto-char position
)
2594 (while (search-forward "\000" nil t
)
2595 (replace-match "\^A0" t t
))
2596 (goto-char position
)
2597 (while (search-forward "\037" nil t
)
2598 (replace-match "\^A_" t t
))
2599 (goto-char (point-max))
2601 (goto-char position
)
2602 (insert "#@" (format "%d" (- (position-bytes (point-max))
2603 (position-bytes position
))))
2605 ;; Save the file position of the object.
2606 ;; Note we add 1 to skip the space that we inserted before the actual doc
2607 ;; string, and subtract point-min to convert from an 1-origin Emacs
2608 ;; position to a file position.
2610 (- (position-bytes (point)) (point-min) -
1)
2611 (goto-char (point-max))))))
2613 (defun byte-compile--reify-function (fun)
2614 "Return an expression which will evaluate to a function value FUN.
2615 FUN should be either a `lambda' value or a `closure' value."
2616 (pcase-let* (((or (and `(lambda ,args .
,body
) (let env nil
))
2617 `(closure ,env
,args .
,body
))
2621 ;; Split docstring and `interactive' form from body.
2622 (when (stringp (car body
))
2623 (push (pop body
) preamble
))
2624 (when (eq (car-safe (car body
)) 'interactive
)
2625 (push (pop body
) preamble
))
2626 ;; Turn the function's closed vars (if any) into local let bindings.
2627 (dolist (binding env
)
2630 ;; We check shadowing by the args, so that the `let' can be moved
2631 ;; within the lambda, which can then be unfolded. FIXME: Some of those
2632 ;; bindings might be unused in `body'.
2633 (unless (memq (car binding
) args
) ;Shadowed.
2634 (push `(,(car binding
) ',(cdr binding
)) renv
)))
2636 (t (push `(defvar ,binding
) body
))))
2638 `(lambda ,args
,@preamble
,@body
)
2639 `(lambda ,args
,@preamble
(let ,(nreverse renv
) ,@body
)))))
2642 (defun byte-compile (form)
2643 "If FORM is a symbol, byte-compile its function definition.
2644 If FORM is a lambda or a macro, byte-compile it as a function."
2645 (displaying-byte-compile-warnings
2646 (byte-compile-close-variables
2647 (let* ((lexical-binding lexical-binding
)
2648 (fun (if (symbolp form
)
2649 (symbol-function form
)
2651 (macro (eq (car-safe fun
) 'macro
)))
2653 (setq fun
(cdr fun
)))
2655 ;; Up until Emacs-24.1, byte-compile silently did nothing when asked to
2656 ;; compile something invalid. So let's tune down the complaint from an
2657 ;; error to a simple message for the known case where signaling an error
2659 ((byte-code-function-p fun
)
2660 (message "Function %s is already compiled"
2661 (if (symbolp form
) form
"provided"))
2664 (when (or (symbolp form
) (eq (car-safe fun
) 'closure
))
2665 ;; `fun' is a function *value*, so try to recover its corresponding
2667 (setq lexical-binding
(eq (car fun
) 'closure
))
2668 (setq fun
(byte-compile--reify-function fun
)))
2670 (setq fun
(byte-compile-preprocess fun
))
2671 (setq fun
(byte-compile-top-level fun nil
'eval
))
2672 (if macro
(push 'macro fun
))
2677 (defun byte-compile-sexp (sexp)
2678 "Compile and return SEXP."
2679 (displaying-byte-compile-warnings
2680 (byte-compile-close-variables
2681 (byte-compile-top-level (byte-compile-preprocess sexp
)))))
2683 (defun byte-compile-check-lambda-list (list)
2684 "Check lambda-list LIST for errors."
2687 (let ((arg (car list
)))
2689 (byte-compile-set-symbol-position arg
))
2690 (cond ((or (not (symbolp arg
))
2691 (macroexp--const-symbol-p arg t
))
2692 (error "Invalid lambda variable %s" arg
))
2695 (error "&rest without variable name"))
2697 (error "Garbage following &rest VAR in lambda-list")))
2698 ((eq arg
'&optional
)
2699 (when (or (null (cdr list
))
2700 (memq (cadr list
) '(&optional
&rest
)))
2701 (error "Variable name missing after &optional"))
2702 (when (memq '&optional
(cddr list
))
2703 (error "Duplicate &optional")))
2705 (byte-compile-warn "repeated variable %s in lambda-list" arg
))
2708 (setq list
(cdr list
)))))
2711 (defun byte-compile-arglist-vars (arglist)
2712 "Return a list of the variables in the lambda argument list ARGLIST."
2713 (remq '&rest
(remq '&optional arglist
)))
2715 (defun byte-compile-make-lambda-lexenv (args)
2716 "Return a new lexical environment for a lambda expression FORM."
2719 ;; Add entries for each argument.
2721 (push (cons arg stackpos
) lexenv
)
2722 (setq stackpos
(1+ stackpos
)))
2723 ;; Return the new lexical environment.
2726 (defun byte-compile-make-args-desc (arglist)
2729 (while (and arglist
(not (memq (car arglist
) '(&optional
&rest
))))
2730 (setq mandatory
(1+ mandatory
))
2731 (setq arglist
(cdr arglist
)))
2732 (setq nonrest mandatory
)
2733 (when (eq (car arglist
) '&optional
)
2734 (setq arglist
(cdr arglist
))
2735 (while (and arglist
(not (eq (car arglist
) '&rest
)))
2736 (setq nonrest
(1+ nonrest
))
2737 (setq arglist
(cdr arglist
))))
2740 (if (> mandatory
127)
2741 (byte-compile-report-error "Too many (>127) mandatory arguments")
2747 (defun byte-compile-lambda (fun &optional add-lambda reserved-csts
)
2748 "Byte-compile a lambda-expression and return a valid function.
2749 The value is usually a compiled function but may be the original
2751 When ADD-LAMBDA is non-nil, the symbol `lambda' is added as head
2752 of the list FUN and `byte-compile-set-symbol-position' is not called.
2753 Use this feature to avoid calling `byte-compile-set-symbol-position'
2754 for symbols generated by the byte compiler itself."
2756 (setq fun
(cons 'lambda fun
))
2757 (unless (eq 'lambda
(car-safe fun
))
2758 (error "Not a lambda list: %S" fun
))
2759 (byte-compile-set-symbol-position 'lambda
))
2760 (byte-compile-check-lambda-list (nth 1 fun
))
2761 (let* ((arglist (nth 1 fun
))
2762 (arglistvars (byte-compile-arglist-vars arglist
))
2763 (byte-compile-bound-variables
2764 (append (if (not lexical-binding
) arglistvars
)
2765 byte-compile-bound-variables
))
2766 (body (cdr (cdr fun
)))
2767 (doc (if (stringp (car body
))
2769 ;; Discard the doc string
2770 ;; unless it is the last element of the body.
2772 (setq body
(cdr body
))))))
2773 (int (assq 'interactive body
)))
2774 ;; Process the interactive spec.
2776 (byte-compile-set-symbol-position 'interactive
)
2777 ;; Skip (interactive) if it is in front (the most usual location).
2778 (if (eq int
(car body
))
2779 (setq body
(cdr body
)))
2780 (cond ((consp (cdr int
))
2782 (byte-compile-warn "malformed interactive spec: %s"
2783 (prin1-to-string int
)))
2784 ;; If the interactive spec is a call to `list', don't
2785 ;; compile it, because `call-interactively' looks at the
2786 ;; args of `list'. Actually, compile it to get warnings,
2787 ;; but don't use the result.
2788 (let* ((form (nth 1 int
))
2789 (newform (byte-compile-top-level form
)))
2790 (while (memq (car-safe form
) '(let let
* progn save-excursion
))
2791 (while (consp (cdr form
))
2792 (setq form
(cdr form
)))
2793 (setq form
(car form
)))
2794 (if (and (eq (car-safe form
) 'list
)
2795 ;; The spec is evalled in callint.c in dynamic-scoping
2796 ;; mode, so just leaving the form unchanged would mean
2797 ;; it won't be eval'd in the right mode.
2798 (not lexical-binding
))
2800 (setq int
`(interactive ,newform
)))))
2802 (byte-compile-warn "malformed interactive spec: %s"
2803 (prin1-to-string int
)))))
2804 ;; Process the body.
2806 (byte-compile-top-level (cons 'progn body
) nil
'lambda
2807 ;; If doing lexical binding, push a new
2808 ;; lexical environment containing just the
2809 ;; args (since lambda expressions should be
2811 (and lexical-binding
2812 (byte-compile-make-lambda-lexenv
2815 ;; Build the actual byte-coded function.
2816 (cl-assert (eq 'byte-code
(car-safe compiled
)))
2817 (apply #'make-byte-code
2819 (byte-compile-make-args-desc arglist
)
2822 ;; byte-string, constants-vector, stack depth
2824 ;; optionally, the doc string.
2825 (cond ((and lexical-binding arglist
)
2826 ;; byte-compile-make-args-desc lost the args's names,
2827 ;; so preserve them in the docstring.
2828 (list (help-add-fundoc-usage doc arglist
)))
2831 ;; optionally, the interactive spec.
2833 (list (nth 1 int
))))))))
2835 (defvar byte-compile-reserved-constants
0)
2837 (defun byte-compile-constants-vector ()
2838 ;; Builds the constants-vector from the current variables and constants.
2839 ;; This modifies the constants from (const . nil) to (const . offset).
2840 ;; To keep the byte-codes to look up the vector as short as possible:
2841 ;; First 6 elements are vars, as there are one-byte varref codes for those.
2842 ;; Next up to byte-constant-limit are constants, still with one-byte codes.
2843 ;; Next variables again, to get 2-byte codes for variable lookup.
2844 ;; The rest of the constants and variables need 3-byte byte-codes.
2845 (let* ((i (1- byte-compile-reserved-constants
))
2846 (rest (nreverse byte-compile-variables
)) ; nreverse because the first
2847 (other (nreverse byte-compile-constants
)) ; vars often are used most.
2849 (limits '(5 ; Use the 1-byte varref codes,
2850 63 ; 1-constlim ; 1-byte byte-constant codes,
2851 255 ; 2-byte varref codes,
2852 65535 ; 3-byte codes for the rest.
2853 65535)) ; twice since we step when we swap.
2855 (while (or rest other
)
2856 (setq limit
(car limits
))
2857 (while (and rest
(< i limit
))
2859 ((numberp (car rest
))
2860 (cl-assert (< (car rest
) byte-compile-reserved-constants
)))
2861 ((setq tmp
(assq (car (car rest
)) ret
))
2862 (setcdr (car rest
) (cdr tmp
)))
2864 (setcdr (car rest
) (setq i
(1+ i
)))
2865 (setq ret
(cons (car rest
) ret
))))
2866 (setq rest
(cdr rest
)))
2867 (setq limits
(cdr limits
) ;Step
2868 rest
(prog1 other
;&Swap.
2869 (setq other rest
))))
2870 (apply 'vector
(nreverse (mapcar 'car ret
)))))
2872 ;; Given an expression FORM, compile it and return an equivalent byte-code
2873 ;; expression (a call to the function byte-code).
2874 (defun byte-compile-top-level (form &optional for-effect output-type
2875 lexenv reserved-csts
)
2876 ;; OUTPUT-TYPE advises about how form is expected to be used:
2877 ;; 'eval or nil -> a single form,
2878 ;; 'progn or t -> a list of forms,
2879 ;; 'lambda -> body of a lambda,
2880 ;; 'file -> used at file-level.
2881 (let ((byte-compile--for-effect for-effect
)
2882 (byte-compile-constants nil
)
2883 (byte-compile-variables nil
)
2884 (byte-compile-tag-number 0)
2885 (byte-compile-depth 0)
2886 (byte-compile-maxdepth 0)
2887 (byte-compile--lexical-environment lexenv
)
2888 (byte-compile-reserved-constants (or reserved-csts
0))
2889 (byte-compile-output nil
)
2890 (byte-compile-jump-tables nil
))
2891 (if (memq byte-optimize
'(t source
))
2892 (setq form
(byte-optimize-form form byte-compile--for-effect
)))
2893 (while (and (eq (car-safe form
) 'progn
) (null (cdr (cdr form
))))
2894 (setq form
(nth 1 form
)))
2895 ;; Set up things for a lexically-bound function.
2896 (when (and lexical-binding
(eq output-type
'lambda
))
2897 ;; See how many arguments there are, and set the current stack depth
2899 (setq byte-compile-depth
(length byte-compile--lexical-environment
))
2900 ;; If there are args, output a tag to record the initial
2901 ;; stack-depth for the optimizer.
2902 (when (> byte-compile-depth
0)
2903 (byte-compile-out-tag (byte-compile-make-tag))))
2905 (byte-compile-form form byte-compile--for-effect
)
2906 (byte-compile-out-toplevel byte-compile--for-effect output-type
)))
2908 (defun byte-compile-out-toplevel (&optional for-effect output-type
)
2910 ;; The stack is empty. Push a value to be returned from (byte-code ..).
2911 (if (eq (car (car byte-compile-output
)) 'byte-discard
)
2912 (setq byte-compile-output
(cdr byte-compile-output
))
2913 (byte-compile-push-constant
2914 ;; Push any constant - preferably one which already is used, and
2915 ;; a number or symbol - ie not some big sequence. The return value
2916 ;; isn't returned, but it would be a shame if some textually large
2917 ;; constant was not optimized away because we chose to return it.
2918 (and (not (assq nil byte-compile-constants
)) ; Nil is often there.
2919 (let ((tmp (reverse byte-compile-constants
)))
2920 (while (and tmp
(not (or (symbolp (caar tmp
))
2921 (numberp (caar tmp
)))))
2922 (setq tmp
(cdr tmp
)))
2924 (byte-compile-out 'byte-return
0)
2925 (setq byte-compile-output
(nreverse byte-compile-output
))
2926 (if (memq byte-optimize
'(t byte
))
2927 (setq byte-compile-output
2928 (byte-optimize-lapcode byte-compile-output
)))
2930 ;; Decompile trivial functions:
2931 ;; only constants and variables, or a single funcall except in lambdas.
2932 ;; Except for Lisp_Compiled objects, forms like (foo "hi")
2933 ;; are still quicker than (byte-code "..." [foo "hi"] 2).
2934 ;; Note that even (quote foo) must be parsed just as any subr by the
2935 ;; interpreter, so quote should be compiled into byte-code in some contexts.
2936 ;; What to leave uncompiled:
2937 ;; lambda -> never. we used to leave it uncompiled if the body was
2938 ;; a single atom, but that causes confusion if the docstring
2939 ;; uses the (file . pos) syntax. Besides, now that we have
2940 ;; the Lisp_Compiled type, the compiled form is faster.
2941 ;; eval -> atom, quote or (function atom atom atom)
2942 ;; progn -> as <<same-as-eval>> or (progn <<same-as-eval>> atom)
2943 ;; file -> as progn, but takes both quotes and atoms, and longer forms.
2945 (maycall (not (eq output-type
'lambda
))) ; t if we may make a funcall.
2948 ;; #### This should be split out into byte-compile-nontrivial-function-p.
2949 ((or (eq output-type
'lambda
)
2950 (nthcdr (if (eq output-type
'file
) 50 8) byte-compile-output
)
2951 (assq 'TAG byte-compile-output
) ; Not necessary, but speeds up a bit.
2952 (not (setq tmp
(assq 'byte-return byte-compile-output
)))
2954 (setq rest
(nreverse
2955 (cdr (memq tmp
(reverse byte-compile-output
)))))
2958 ((memq (car (car rest
)) '(byte-varref byte-constant
))
2959 (setq tmp
(car (cdr (car rest
))))
2960 (if (if (eq (car (car rest
)) 'byte-constant
)
2963 (not (macroexp--const-symbol-p tmp
)))))
2965 (setq body
(cons (list 'quote tmp
) body
)))
2966 (setq body
(cons tmp body
))))
2968 ;; Allow a funcall if at most one atom follows it.
2969 (null (nthcdr 3 rest
))
2970 (setq tmp
(get (car (car rest
)) 'byte-opcode-invert
))
2971 (or (null (cdr rest
))
2972 (and (memq output-type
'(file progn t
))
2974 (eq (car (nth 1 rest
)) 'byte-discard
)
2975 (progn (setq rest
(cdr rest
)) t
))))
2976 (setq maycall nil
) ; Only allow one real function call.
2977 (setq body
(nreverse body
))
2979 (if (and (eq tmp
'funcall
)
2980 (eq (car-safe (car body
)) 'quote
)
2981 (symbolp (nth 1 (car body
))))
2982 (cons (nth 1 (car body
)) (cdr body
))
2984 (or (eq output-type
'file
)
2985 (not (delq nil
(mapcar 'consp
(cdr (car body
))))))))
2986 (setq rest
(cdr rest
)))
2988 (let ((byte-compile-vector (byte-compile-constants-vector)))
2989 (list 'byte-code
(byte-compile-lapcode byte-compile-output
)
2990 byte-compile-vector byte-compile-maxdepth
)))
2991 ;; it's a trivial function
2992 ((cdr body
) (cons 'progn
(nreverse body
)))
2995 ;; Given BODY, compile it and return a new body.
2996 (defun byte-compile-top-level-body (body &optional for-effect
)
2998 (byte-compile-top-level (cons 'progn body
) for-effect t
))
2999 (cond ((eq (car-safe body
) 'progn
)
3004 ;; Special macro-expander used during byte-compilation.
3005 (defun byte-compile-macroexpand-declare-function (fn file
&rest args
)
3006 (declare (advertised-calling-convention
3007 (fn file
&optional arglist fileonly
) nil
))
3008 (let ((gotargs (and (consp args
) (listp (car args
))))
3009 (unresolved (assq fn byte-compile-unresolved-functions
)))
3010 (when unresolved
; function was called before declaration
3011 (if (and gotargs
(byte-compile-warning-enabled-p 'callargs
))
3012 (byte-compile-arglist-warn fn
(car args
) nil
)
3013 (setq byte-compile-unresolved-functions
3014 (delq unresolved byte-compile-unresolved-functions
))))
3015 (push (cons fn
(if gotargs
3016 (list 'declared
(car args
))
3017 t
)) ; Arglist not specified.
3018 byte-compile-function-environment
))
3019 ;; We are stating that it _will_ be defined at runtime.
3020 (setq byte-compile-noruntime-functions
3021 (delq fn byte-compile-noruntime-functions
))
3022 ;; Delegate the rest to the normal macro definition.
3023 (macroexpand `(declare-function ,fn
,file
,@args
)))
3026 ;; This is the recursive entry point for compiling each subform of an
3028 ;; If for-effect is non-nil, byte-compile-form will output a byte-discard
3029 ;; before terminating (ie no value will be left on the stack).
3030 ;; A byte-compile handler may, when byte-compile--for-effect is non-nil, choose
3031 ;; output code which does not leave a value on the stack, and then set
3032 ;; byte-compile--for-effect to nil (to prevent byte-compile-form from
3033 ;; outputting the byte-discard).
3034 ;; If a handler wants to call another handler, it should do so via
3035 ;; byte-compile-form, or take extreme care to handle byte-compile--for-effect
3036 ;; correctly. (Use byte-compile-form-do-effect to reset the
3037 ;; byte-compile--for-effect flag too.)
3039 (defun byte-compile-form (form &optional for-effect
)
3040 (let ((byte-compile--for-effect for-effect
))
3043 (cond ((or (not (symbolp form
)) (macroexp--const-symbol-p form
))
3044 (when (symbolp form
)
3045 (byte-compile-set-symbol-position form
))
3046 (byte-compile-constant form
))
3047 ((and byte-compile--for-effect byte-compile-delete-errors
)
3048 (when (symbolp form
)
3049 (byte-compile-set-symbol-position form
))
3050 (setq byte-compile--for-effect nil
))
3052 (byte-compile-variable-ref form
))))
3053 ((symbolp (car form
))
3054 (let* ((fn (car form
))
3055 (handler (get fn
'byte-compile
))
3057 (or (get fn
'interactive-only
)
3058 (memq fn byte-compile-interactive-only-functions
))))
3059 (when (memq fn
'(set symbol-value run-hooks
;; add-to-list
3060 add-hook remove-hook run-hook-with-args
3061 run-hook-with-args-until-success
3062 run-hook-with-args-until-failure
))
3065 (when (assq var byte-compile-lexical-variables
)
3066 (byte-compile-report-error
3067 (format-message "%s cannot use lexical var `%s'" fn var
))))))
3068 (when (macroexp--const-symbol-p fn
)
3069 (byte-compile-warn "`%s' called as a function" fn
))
3070 (when (and (byte-compile-warning-enabled-p 'interactive-only
)
3072 (byte-compile-warn "`%s' is for interactive use only%s"
3074 (cond ((stringp interactive-only
)
3076 (substitute-command-keys
3078 ((and (symbolp 'interactive-only
)
3079 (not (eq interactive-only t
)))
3080 (format-message "; use `%s' instead."
3083 (if (eq (car-safe (symbol-function (car form
))) 'macro
)
3084 (byte-compile-report-error
3085 (format "Forgot to expand macro %s in %S" (car form
) form
)))
3087 ;; Make sure that function exists.
3088 (and (functionp handler
)
3089 ;; Ignore obsolete byte-compile function used by former
3090 ;; CL code to handle compiler macros (we do it
3091 ;; differently now).
3092 (not (eq handler
'cl-byte-compile-compiler-macro
))))
3093 (funcall handler form
)
3094 (byte-compile-normal-call form
))
3095 (if (byte-compile-warning-enabled-p 'cl-functions
)
3096 (byte-compile-cl-warn form
))))
3097 ((and (byte-code-function-p (car form
))
3098 (memq byte-optimize
'(t lap
)))
3099 (byte-compile-unfold-bcf form
))
3100 ((and (eq (car-safe (car form
)) 'lambda
)
3101 ;; if the form comes out the same way it went in, that's
3102 ;; because it was malformed, and we couldn't unfold it.
3103 (not (eq form
(setq form
(byte-compile-unfold-lambda form
)))))
3104 (byte-compile-form form byte-compile--for-effect
)
3105 (setq byte-compile--for-effect nil
))
3106 ((byte-compile-normal-call form
)))
3107 (if byte-compile--for-effect
3108 (byte-compile-discard))))
3110 (defun byte-compile-normal-call (form)
3111 (when (and (byte-compile-warning-enabled-p 'callargs
)
3112 (symbolp (car form
)))
3113 (if (memq (car form
)
3114 '(custom-declare-group custom-declare-variable
3115 custom-declare-face
))
3116 (byte-compile-nogroup-warn form
))
3117 (byte-compile-callargs-warn form
))
3118 (if byte-compile-generate-call-tree
3119 (byte-compile-annotate-call-tree form
))
3120 (when (and byte-compile--for-effect
(eq (car form
) 'mapcar
)
3121 (byte-compile-warning-enabled-p 'mapcar
))
3122 (byte-compile-set-symbol-position 'mapcar
)
3124 "`mapcar' called for effect; use `mapc' or `dolist' instead"))
3125 (byte-compile-push-constant (car form
))
3126 (mapc 'byte-compile-form
(cdr form
)) ; wasteful, but faster.
3127 (byte-compile-out 'byte-call
(length (cdr form
))))
3130 ;; Splice the given lap code into the current instruction stream.
3131 ;; If it has any labels in it, you're responsible for making sure there
3132 ;; are no collisions, and that byte-compile-tag-number is reasonable
3133 ;; after this is spliced in. The provided list is destroyed.
3134 (defun byte-compile-inline-lapcode (lap end-depth
)
3135 ;; "Replay" the operations: we used to just do
3136 ;; (setq byte-compile-output (nconc (nreverse lap) byte-compile-output))
3137 ;; but that fails to update byte-compile-depth, so we had to assume
3138 ;; that `lap' ends up adding exactly 1 element to the stack. This
3139 ;; happens to be true for byte-code generated by bytecomp.el without
3140 ;; lexical-binding, but it's not true in general, and it's not true for
3141 ;; code output by bytecomp.el with lexical-binding.
3142 ;; We also restore the value of `byte-compile-depth' and remove TAG depths
3143 ;; accordingly when inlining lapcode containing lap-code, exactly as
3144 ;; documented in `byte-compile-cond-jump-table'.
3145 (let ((endtag (byte-compile-make-tag))
3146 last-jump-tag
;; last TAG we have jumped to
3147 last-depth
;; last value of `byte-compile-depth'
3148 last-constant
;; value of the last constant encountered
3149 last-switch
;; whether the last op encountered was byte-switch
3150 switch-tags
;; a list of tags that byte-switch could jump to
3151 ;; a list of tags byte-switch will jump to, if the value doesn't
3152 ;; match any entry in the hash table
3153 switch-default-tags
)
3157 (when (or (member op switch-tags
) (member op switch-default-tags
))
3158 ;; This TAG is used in a jump table, this means the last goto
3159 ;; was to a done/default TAG, and thus it's cddr should be set to nil.
3161 (setcdr (cdr last-jump-tag
) nil
))
3162 ;; Also, restore the value of `byte-compile-depth' to what it was
3163 ;; before the last goto.
3164 (setq byte-compile-depth last-depth
3166 (byte-compile-out-tag op
))
3167 ((memq (car op
) byte-goto-ops
)
3168 (setq last-depth byte-compile-depth
3169 last-jump-tag
(cdr op
))
3170 (byte-compile-goto (car op
) (cdr op
))
3172 ;; The last op was byte-switch, this goto jumps to a "default" TAG
3173 ;; (when no value in the jump table is satisfied).
3174 (push (cdr op
) switch-default-tags
)
3175 (setcdr (cdr (cdr op
)) nil
)
3176 (setq byte-compile-depth last-depth
3178 ((eq (car op
) 'byte-return
)
3179 (byte-compile-discard (- byte-compile-depth end-depth
) t
)
3180 (byte-compile-goto 'byte-goto endtag
))
3182 (when (eq (car op
) 'byte-switch
)
3183 ;; The last constant is a jump table.
3184 (push last-constant byte-compile-jump-tables
)
3185 (setq last-switch t
)
3186 ;; Push all TAGs in the jump to switch-tags.
3187 (maphash #'(lambda (_k tag
)
3188 (push tag switch-tags
))
3190 (setq last-constant
(and (eq (car op
) 'byte-constant
) (cadr op
)))
3191 (setq last-depth byte-compile-depth
)
3192 (byte-compile-out (car op
) (cdr op
)))))
3193 (byte-compile-out-tag endtag
)))
3195 (defun byte-compile-unfold-bcf (form)
3196 "Inline call to byte-code-functions."
3197 (let* ((byte-compile-bound-variables byte-compile-bound-variables
)
3199 (fargs (aref fun
0))
3200 (start-depth byte-compile-depth
)
3201 (fmax2 (if (numberp fargs
) (lsh fargs -
7))) ;2*max+rest.
3202 ;; (fmin (if (numberp fargs) (logand fargs 127)))
3203 (alen (length (cdr form
)))
3205 (fetch-bytecode fun
)
3206 (mapc 'byte-compile-form
(cdr form
))
3208 ;; Old-style byte-code.
3209 (cl-assert (listp fargs
))
3212 (`&optional
(setq fargs
(cdr fargs
)))
3213 (`&rest
(setq fmax2
(+ (* 2 (length dynbinds
)) 1))
3214 (push (cadr fargs
) dynbinds
)
3216 (_ (push (pop fargs
) dynbinds
))))
3217 (unless fmax2
(setq fmax2
(* 2 (length dynbinds
)))))
3219 ((<= (+ alen alen
) fmax2
)
3220 ;; Add missing &optional (or &rest) arguments.
3221 (dotimes (_ (- (/ (1+ fmax2
) 2) alen
))
3222 (byte-compile-push-constant nil
)))
3223 ((zerop (logand fmax2
1))
3224 (byte-compile-report-error
3225 (format "Too many arguments for inlined function %S" form
))
3226 (byte-compile-discard (- alen
(/ fmax2
2))))
3228 ;; Turn &rest args into a list.
3229 (let ((n (- alen
(/ (1- fmax2
) 2))))
3230 (cl-assert (> n
0) nil
"problem: fmax2=%S alen=%S n=%S" fmax2 alen n
)
3233 (aref [byte-list1 byte-list2 byte-list3 byte-list4
] (1- n
))
3235 (byte-compile-out 'byte-listN n
)))))
3236 (mapc #'byte-compile-dynamic-variable-bind dynbinds
)
3237 (byte-compile-inline-lapcode
3238 (byte-decompile-bytecode-1 (aref fun
1) (aref fun
2) t
)
3240 ;; Unbind dynamic variables.
3242 (byte-compile-out 'byte-unbind
(length dynbinds
)))
3243 (cl-assert (eq byte-compile-depth
(1+ start-depth
))
3244 nil
"Wrong depth start=%s end=%s" start-depth byte-compile-depth
)))
3246 (defun byte-compile-check-variable (var access-type
)
3247 "Do various error checks before a use of the variable VAR."
3249 (byte-compile-set-symbol-position var
))
3250 (cond ((or (not (symbolp var
)) (macroexp--const-symbol-p var
))
3251 (when (byte-compile-warning-enabled-p 'constants
)
3252 (byte-compile-warn (if (eq access-type
'let-bind
)
3253 "attempt to let-bind %s `%s'"
3254 "variable reference to %s `%s'")
3255 (if (symbolp var
) "constant" "nonvariable")
3256 (prin1-to-string var
))))
3257 ((let ((od (get var
'byte-obsolete-variable
)))
3259 (not (memq var byte-compile-not-obsolete-vars
))
3260 (not (memq var byte-compile-global-not-obsolete-vars
))
3261 (or (pcase (nth 1 od
)
3262 (`set
(not (eq access-type
'reference
)))
3263 (`get
(eq access-type
'reference
))
3265 (byte-compile-warn-obsolete var
))))
3267 (defsubst byte-compile-dynamic-variable-op
(base-op var
)
3268 (let ((tmp (assq var byte-compile-variables
)))
3270 (setq tmp
(list var
))
3271 (push tmp byte-compile-variables
))
3272 (byte-compile-out base-op tmp
)))
3274 (defun byte-compile-dynamic-variable-bind (var)
3275 "Generate code to bind the lexical variable VAR to the top-of-stack value."
3276 (byte-compile-check-variable var
'let-bind
)
3277 (push var byte-compile-bound-variables
)
3278 (byte-compile-dynamic-variable-op 'byte-varbind var
))
3280 (defun byte-compile-variable-ref (var)
3281 "Generate code to push the value of the variable VAR on the stack."
3282 (byte-compile-check-variable var
'reference
)
3283 (let ((lex-binding (assq var byte-compile--lexical-environment
)))
3285 ;; VAR is lexically bound
3286 (byte-compile-stack-ref (cdr lex-binding
))
3287 ;; VAR is dynamically bound
3288 (unless (or (not (byte-compile-warning-enabled-p 'free-vars
))
3290 (memq var byte-compile-bound-variables
)
3291 (memq var byte-compile-free-references
))
3292 (byte-compile-warn "reference to free variable `%S'" var
)
3293 (push var byte-compile-free-references
))
3294 (byte-compile-dynamic-variable-op 'byte-varref var
))))
3296 (defun byte-compile-variable-set (var)
3297 "Generate code to set the variable VAR from the top-of-stack value."
3298 (byte-compile-check-variable var
'assign
)
3299 (let ((lex-binding (assq var byte-compile--lexical-environment
)))
3301 ;; VAR is lexically bound.
3302 (byte-compile-stack-set (cdr lex-binding
))
3303 ;; VAR is dynamically bound.
3304 (unless (or (not (byte-compile-warning-enabled-p 'free-vars
))
3306 (memq var byte-compile-bound-variables
)
3307 (memq var byte-compile-free-assignments
))
3308 (byte-compile-warn "assignment to free variable `%s'" var
)
3309 (push var byte-compile-free-assignments
))
3310 (byte-compile-dynamic-variable-op 'byte-varset var
))))
3312 (defmacro byte-compile-get-constant
(const)
3313 `(or (if (stringp ,const
)
3314 ;; In a string constant, treat properties as significant.
3316 (dolist (elt byte-compile-constants
)
3317 (if (equal-including-properties (car elt
) ,const
)
3320 (assq ,const byte-compile-constants
))
3321 (car (setq byte-compile-constants
3322 (cons (list ,const
) byte-compile-constants
)))))
3324 ;; Use this when the value of a form is a constant.
3325 ;; This obeys byte-compile--for-effect.
3326 (defun byte-compile-constant (const)
3327 (if byte-compile--for-effect
3328 (setq byte-compile--for-effect nil
)
3329 (when (symbolp const
)
3330 (byte-compile-set-symbol-position const
))
3331 (byte-compile-out 'byte-constant
(byte-compile-get-constant const
))))
3333 ;; Use this for a constant that is not the value of its containing form.
3334 ;; This ignores byte-compile--for-effect.
3335 (defun byte-compile-push-constant (const)
3336 (let ((byte-compile--for-effect nil
))
3337 (inline (byte-compile-constant const
))))
3339 ;; Compile those primitive ordinary functions
3340 ;; which have special byte codes just for speed.
3342 (defmacro byte-defop-compiler
(function &optional compile-handler
)
3343 "Add a compiler-form for FUNCTION.
3344 If function is a symbol, then the variable \"byte-SYMBOL\" must name
3345 the opcode to be used. If function is a list, the first element
3346 is the function and the second element is the bytecode-symbol.
3347 The second element may be nil, meaning there is no opcode.
3348 COMPILE-HANDLER is the function to use to compile this byte-op, or
3349 may be the abbreviations 0, 1, 2, 3, 0-1, or 1-2.
3350 If it is nil, then the handler is \"byte-compile-SYMBOL.\""
3352 (if (symbolp function
)
3353 (setq opcode
(intern (concat "byte-" (symbol-name function
))))
3354 (setq opcode
(car (cdr function
))
3355 function
(car function
)))
3357 (list 'put
(list 'quote function
) ''byte-compile
3359 (or (cdr (assq compile-handler
3360 '((0 . byte-compile-no-args
)
3361 (1 . byte-compile-one-arg
)
3362 (2 . byte-compile-two-args
)
3363 (2-and . byte-compile-and-folded
)
3364 (3 . byte-compile-three-args
)
3365 (0-1 . byte-compile-zero-or-one-arg
)
3366 (1-2 . byte-compile-one-or-two-args
)
3367 (2-3 . byte-compile-two-or-three-args
)
3370 (intern (concat "byte-compile-"
3371 (symbol-name function
))))))))
3374 (list 'put
(list 'quote function
)
3375 ''byte-opcode
(list 'quote opcode
))
3376 (list 'put
(list 'quote opcode
)
3377 ''byte-opcode-invert
(list 'quote function
)))
3380 (defmacro byte-defop-compiler-1
(function &optional compile-handler
)
3381 (list 'byte-defop-compiler
(list function nil
) compile-handler
))
3384 (put 'byte-call
'byte-opcode-invert
'funcall
)
3385 (put 'byte-list1
'byte-opcode-invert
'list
)
3386 (put 'byte-list2
'byte-opcode-invert
'list
)
3387 (put 'byte-list3
'byte-opcode-invert
'list
)
3388 (put 'byte-list4
'byte-opcode-invert
'list
)
3389 (put 'byte-listN
'byte-opcode-invert
'list
)
3390 (put 'byte-concat2
'byte-opcode-invert
'concat
)
3391 (put 'byte-concat3
'byte-opcode-invert
'concat
)
3392 (put 'byte-concat4
'byte-opcode-invert
'concat
)
3393 (put 'byte-concatN
'byte-opcode-invert
'concat
)
3394 (put 'byte-insertN
'byte-opcode-invert
'insert
)
3396 (byte-defop-compiler point
0)
3397 ;;(byte-defop-compiler mark 0) ;; obsolete
3398 (byte-defop-compiler point-max
0)
3399 (byte-defop-compiler point-min
0)
3400 (byte-defop-compiler following-char
0)
3401 (byte-defop-compiler preceding-char
0)
3402 (byte-defop-compiler current-column
0)
3403 (byte-defop-compiler eolp
0)
3404 (byte-defop-compiler eobp
0)
3405 (byte-defop-compiler bolp
0)
3406 (byte-defop-compiler bobp
0)
3407 (byte-defop-compiler current-buffer
0)
3408 ;;(byte-defop-compiler read-char 0) ;; obsolete
3409 ;; (byte-defop-compiler interactive-p 0) ;; Obsolete.
3410 (byte-defop-compiler widen
0)
3411 (byte-defop-compiler end-of-line
0-
1)
3412 (byte-defop-compiler forward-char
0-
1)
3413 (byte-defop-compiler forward-line
0-
1)
3414 (byte-defop-compiler symbolp
1)
3415 (byte-defop-compiler consp
1)
3416 (byte-defop-compiler stringp
1)
3417 (byte-defop-compiler listp
1)
3418 (byte-defop-compiler not
1)
3419 (byte-defop-compiler (null byte-not
) 1)
3420 (byte-defop-compiler car
1)
3421 (byte-defop-compiler cdr
1)
3422 (byte-defop-compiler length
1)
3423 (byte-defop-compiler symbol-value
1)
3424 (byte-defop-compiler symbol-function
1)
3425 (byte-defop-compiler (1+ byte-add1
) 1)
3426 (byte-defop-compiler (1- byte-sub1
) 1)
3427 (byte-defop-compiler goto-char
1)
3428 (byte-defop-compiler char-after
0-
1)
3429 (byte-defop-compiler set-buffer
1)
3430 ;;(byte-defop-compiler set-mark 1) ;; obsolete
3431 (byte-defop-compiler forward-word
0-
1)
3432 (byte-defop-compiler char-syntax
1)
3433 (byte-defop-compiler nreverse
1)
3434 (byte-defop-compiler car-safe
1)
3435 (byte-defop-compiler cdr-safe
1)
3436 (byte-defop-compiler numberp
1)
3437 (byte-defop-compiler integerp
1)
3438 (byte-defop-compiler skip-chars-forward
1-
2)
3439 (byte-defop-compiler skip-chars-backward
1-
2)
3440 (byte-defop-compiler eq
2)
3441 (byte-defop-compiler memq
2)
3442 (byte-defop-compiler cons
2)
3443 (byte-defop-compiler aref
2)
3444 (byte-defop-compiler set
2)
3445 (byte-defop-compiler (= byte-eqlsign
) 2-and
)
3446 (byte-defop-compiler (< byte-lss
) 2-and
)
3447 (byte-defop-compiler (> byte-gtr
) 2-and
)
3448 (byte-defop-compiler (<= byte-leq
) 2-and
)
3449 (byte-defop-compiler (>= byte-geq
) 2-and
)
3450 (byte-defop-compiler get
2)
3451 (byte-defop-compiler nth
2)
3452 (byte-defop-compiler substring
2-
3)
3453 (byte-defop-compiler (move-marker byte-set-marker
) 2-
3)
3454 (byte-defop-compiler set-marker
2-
3)
3455 (byte-defop-compiler match-beginning
1)
3456 (byte-defop-compiler match-end
1)
3457 (byte-defop-compiler upcase
1)
3458 (byte-defop-compiler downcase
1)
3459 (byte-defop-compiler string
= 2)
3460 (byte-defop-compiler string
< 2)
3461 (byte-defop-compiler (string-equal byte-string
=) 2)
3462 (byte-defop-compiler (string-lessp byte-string
<) 2)
3463 (byte-defop-compiler equal
2)
3464 (byte-defop-compiler nthcdr
2)
3465 (byte-defop-compiler elt
2)
3466 (byte-defop-compiler member
2)
3467 (byte-defop-compiler assq
2)
3468 (byte-defop-compiler (rplaca byte-setcar
) 2)
3469 (byte-defop-compiler (rplacd byte-setcdr
) 2)
3470 (byte-defop-compiler setcar
2)
3471 (byte-defop-compiler setcdr
2)
3472 (byte-defop-compiler buffer-substring
2)
3473 (byte-defop-compiler delete-region
2)
3474 (byte-defop-compiler narrow-to-region
2)
3475 (byte-defop-compiler (% byte-rem
) 2)
3476 (byte-defop-compiler aset
3)
3478 (byte-defop-compiler max byte-compile-associative
)
3479 (byte-defop-compiler min byte-compile-associative
)
3480 (byte-defop-compiler (+ byte-plus
) byte-compile-associative
)
3481 (byte-defop-compiler (* byte-mult
) byte-compile-associative
)
3483 ;;####(byte-defop-compiler move-to-column 1)
3484 (byte-defop-compiler-1 interactive byte-compile-noop
)
3487 (defun byte-compile-subr-wrong-args (form n
)
3488 (byte-compile-set-symbol-position (car form
))
3489 (byte-compile-warn "`%s' called with %d arg%s, but requires %s"
3490 (car form
) (length (cdr form
))
3491 (if (= 1 (length (cdr form
))) "" "s") n
)
3492 ;; Get run-time wrong-number-of-args error.
3493 (byte-compile-normal-call form
))
3495 (defun byte-compile-no-args (form)
3496 (if (not (= (length form
) 1))
3497 (byte-compile-subr-wrong-args form
"none")
3498 (byte-compile-out (get (car form
) 'byte-opcode
) 0)))
3500 (defun byte-compile-one-arg (form)
3501 (if (not (= (length form
) 2))
3502 (byte-compile-subr-wrong-args form
1)
3503 (byte-compile-form (car (cdr form
))) ;; Push the argument
3504 (byte-compile-out (get (car form
) 'byte-opcode
) 0)))
3506 (defun byte-compile-two-args (form)
3507 (if (not (= (length form
) 3))
3508 (byte-compile-subr-wrong-args form
2)
3509 (byte-compile-form (car (cdr form
))) ;; Push the arguments
3510 (byte-compile-form (nth 2 form
))
3511 (byte-compile-out (get (car form
) 'byte-opcode
) 0)))
3513 (defun byte-compile-and-folded (form)
3514 "Compile calls to functions like `<='.
3515 These implicitly `and' together a bunch of two-arg bytecodes."
3516 (let ((l (length form
)))
3518 ((< l
3) (byte-compile-form `(progn ,(nth 1 form
) t
)))
3519 ((= l
3) (byte-compile-two-args form
))
3520 ((cl-every #'macroexp-copyable-p
(nthcdr 2 form
))
3521 (byte-compile-form `(and (,(car form
) ,(nth 1 form
) ,(nth 2 form
))
3522 (,(car form
) ,@(nthcdr 2 form
)))))
3523 (t (byte-compile-normal-call form
)))))
3525 (defun byte-compile-three-args (form)
3526 (if (not (= (length form
) 4))
3527 (byte-compile-subr-wrong-args form
3)
3528 (byte-compile-form (car (cdr form
))) ;; Push the arguments
3529 (byte-compile-form (nth 2 form
))
3530 (byte-compile-form (nth 3 form
))
3531 (byte-compile-out (get (car form
) 'byte-opcode
) 0)))
3533 (defun byte-compile-zero-or-one-arg (form)
3534 (let ((len (length form
)))
3535 (cond ((= len
1) (byte-compile-one-arg (append form
'(nil))))
3536 ((= len
2) (byte-compile-one-arg form
))
3537 (t (byte-compile-subr-wrong-args form
"0-1")))))
3539 (defun byte-compile-one-or-two-args (form)
3540 (let ((len (length form
)))
3541 (cond ((= len
2) (byte-compile-two-args (append form
'(nil))))
3542 ((= len
3) (byte-compile-two-args form
))
3543 (t (byte-compile-subr-wrong-args form
"1-2")))))
3545 (defun byte-compile-two-or-three-args (form)
3546 (let ((len (length form
)))
3547 (cond ((= len
3) (byte-compile-three-args (append form
'(nil))))
3548 ((= len
4) (byte-compile-three-args form
))
3549 (t (byte-compile-subr-wrong-args form
"2-3")))))
3551 (defun byte-compile-noop (_form)
3552 (byte-compile-constant nil
))
3554 (defun byte-compile-discard (&optional num preserve-tos
)
3555 "Output byte codes to discard the NUM entries at the top of the stack.
3557 If PRESERVE-TOS is non-nil, preserve the top-of-stack value, as if it were
3558 popped before discarding the num values, and then pushed back again after
3560 (if (and (null num
) (not preserve-tos
))
3562 (byte-compile-out 'byte-discard
)
3566 (when (and preserve-tos
(> num
0))
3567 ;; Preserve the top-of-stack value by writing it directly to the stack
3568 ;; location which will be at the top-of-stack after popping.
3569 (byte-compile-stack-set (1- (- byte-compile-depth num
)))
3570 ;; Now we actually discard one less value, since we want to keep
3572 (setq num
(1- num
)))
3574 (byte-compile-out 'byte-discard
)
3575 (setq num
(1- num
)))))
3577 (defun byte-compile-stack-ref (stack-pos)
3578 "Output byte codes to push the value at stack position STACK-POS."
3579 (let ((dist (- byte-compile-depth
(1+ stack-pos
))))
3581 ;; A simple optimization
3582 (byte-compile-out 'byte-dup
)
3584 (byte-compile-out 'byte-stack-ref dist
))))
3586 (defun byte-compile-stack-set (stack-pos)
3587 "Output byte codes to store the TOS value at stack position STACK-POS."
3588 (byte-compile-out 'byte-stack-set
(- byte-compile-depth
(1+ stack-pos
))))
3590 (byte-defop-compiler-1 internal-make-closure byte-compile-make-closure
)
3591 (byte-defop-compiler-1 internal-get-closed-var byte-compile-get-closed-var
)
3593 (defun byte-compile-make-closure (form)
3594 "Byte-compile the special `internal-make-closure' form."
3595 (if byte-compile--for-effect
(setq byte-compile--for-effect nil
)
3596 (let* ((vars (nth 1 form
))
3598 (docstring-exp (nth 3 form
))
3599 (body (nthcdr 4 form
))
3601 (byte-compile-lambda `(lambda ,vars .
,body
) nil
(length env
))))
3602 (cl-assert (or (> (length env
) 0)
3603 docstring-exp
)) ;Otherwise, we don't need a closure.
3604 (cl-assert (byte-code-function-p fun
))
3605 (byte-compile-form `(make-byte-code
3606 ',(aref fun
0) ',(aref fun
1)
3607 (vconcat (vector .
,env
) ',(aref fun
2))
3608 ,@(let ((rest (nthcdr 3 (mapcar (lambda (x) `',x
) fun
))))
3615 (defun byte-compile-get-closed-var (form)
3616 "Byte-compile the special `internal-get-closed-var' form."
3617 (if byte-compile--for-effect
(setq byte-compile--for-effect nil
)
3618 (byte-compile-out 'byte-constant
(nth 1 form
))))
3620 ;; Compile a function that accepts one or more args and is right-associative.
3621 ;; We do it by left-associativity so that the operations
3622 ;; are done in the same order as in interpreted code.
3623 ;; We treat the one-arg case, as in (+ x), like (+ x 0).
3624 ;; in order to convert markers to numbers, and trigger expected errors.
3625 (defun byte-compile-associative (form)
3627 (let ((opcode (get (car form
) 'byte-opcode
))
3629 (if (and (< 3 (length form
))
3630 (memq opcode
(list (get '+ 'byte-opcode
)
3631 (get '* 'byte-opcode
))))
3632 ;; Don't use binary operations for > 2 operands, as that
3633 ;; may cause overflow/truncation in float operations.
3634 (byte-compile-normal-call form
)
3635 (setq args
(copy-sequence (cdr form
)))
3636 (byte-compile-form (car args
))
3637 (setq args
(cdr args
))
3638 (or args
(setq args
'(0)
3639 opcode
(get '+ 'byte-opcode
)))
3641 (byte-compile-form arg
)
3642 (byte-compile-out opcode
0))))
3643 (byte-compile-constant (eval form
))))
3646 ;; more complicated compiler macros
3648 (byte-defop-compiler char-before
)
3649 (byte-defop-compiler backward-char
)
3650 (byte-defop-compiler backward-word
)
3651 (byte-defop-compiler list
)
3652 (byte-defop-compiler concat
)
3653 (byte-defop-compiler fset
)
3654 (byte-defop-compiler (indent-to-column byte-indent-to
) byte-compile-indent-to
)
3655 (byte-defop-compiler indent-to
)
3656 (byte-defop-compiler insert
)
3657 (byte-defop-compiler-1 function byte-compile-function-form
)
3658 (byte-defop-compiler-1 - byte-compile-minus
)
3659 (byte-defop-compiler (/ byte-quo
) byte-compile-quo
)
3660 (byte-defop-compiler nconc
)
3662 ;; Is this worth it? Both -before and -after are written in C.
3663 (defun byte-compile-char-before (form)
3664 (cond ((or (= 1 (length form
))
3665 (and (= 2 (length form
)) (not (nth 1 form
))))
3666 (byte-compile-form '(char-after (1- (point)))))
3667 ((= 2 (length form
))
3668 (byte-compile-form (list 'char-after
(if (numberp (nth 1 form
))
3670 `(1- (or ,(nth 1 form
)
3672 (t (byte-compile-subr-wrong-args form
"0-1"))))
3674 ;; backward-... ==> forward-... with negated argument.
3675 ;; Is this worth it? Both -backward and -forward are written in C.
3676 (defun byte-compile-backward-char (form)
3677 (cond ((or (= 1 (length form
))
3678 (and (= 2 (length form
)) (not (nth 1 form
))))
3679 (byte-compile-form '(forward-char -
1)))
3680 ((= 2 (length form
))
3681 (byte-compile-form (list 'forward-char
(if (numberp (nth 1 form
))
3683 `(- (or ,(nth 1 form
) 1))))))
3684 (t (byte-compile-subr-wrong-args form
"0-1"))))
3686 (defun byte-compile-backward-word (form)
3687 (cond ((or (= 1 (length form
))
3688 (and (= 2 (length form
)) (not (nth 1 form
))))
3689 (byte-compile-form '(forward-word -
1)))
3690 ((= 2 (length form
))
3691 (byte-compile-form (list 'forward-word
(if (numberp (nth 1 form
))
3693 `(- (or ,(nth 1 form
) 1))))))
3694 (t (byte-compile-subr-wrong-args form
"0-1"))))
3696 (defun byte-compile-list (form)
3697 (let ((count (length (cdr form
))))
3699 (byte-compile-constant nil
))
3701 (mapc 'byte-compile-form
(cdr form
))
3703 (aref [byte-list1 byte-list2 byte-list3 byte-list4
] (1- count
)) 0))
3705 (mapc 'byte-compile-form
(cdr form
))
3706 (byte-compile-out 'byte-listN count
))
3707 (t (byte-compile-normal-call form
)))))
3709 (defun byte-compile-concat (form)
3710 (let ((count (length (cdr form
))))
3711 (cond ((and (< 1 count
) (< count
5))
3712 (mapc 'byte-compile-form
(cdr form
))
3714 (aref [byte-concat2 byte-concat3 byte-concat4
] (- count
2))
3716 ;; Concat of one arg is not a no-op if arg is not a string.
3718 (byte-compile-form ""))
3720 (mapc 'byte-compile-form
(cdr form
))
3721 (byte-compile-out 'byte-concatN count
))
3722 ((byte-compile-normal-call form
)))))
3724 (defun byte-compile-minus (form)
3725 (let ((len (length form
)))
3727 ((= 1 len
) (byte-compile-constant 0))
3729 (byte-compile-form (cadr form
))
3730 (byte-compile-out 'byte-negate
0))
3732 (byte-compile-form (nth 1 form
))
3733 (byte-compile-form (nth 2 form
))
3734 (byte-compile-out 'byte-diff
0))
3735 ;; Don't use binary operations for > 2 operands, as that may
3736 ;; cause overflow/truncation in float operations.
3737 (t (byte-compile-normal-call form
)))))
3739 (defun byte-compile-quo (form)
3740 (let ((len (length form
)))
3742 (byte-compile-subr-wrong-args form
"1 or more"))
3744 (byte-compile-two-args form
))
3746 ;; Don't use binary operations for > 2 operands, as that
3747 ;; may cause overflow/truncation in float operations.
3748 (byte-compile-normal-call form
)))))
3750 (defun byte-compile-nconc (form)
3751 (let ((len (length form
)))
3753 (byte-compile-constant nil
))
3755 ;; nconc of one arg is a noop, even if that arg isn't a list.
3756 (byte-compile-form (nth 1 form
)))
3758 (byte-compile-form (car (setq form
(cdr form
))))
3759 (while (setq form
(cdr form
))
3760 (byte-compile-form (car form
))
3761 (byte-compile-out 'byte-nconc
0))))))
3763 (defun byte-compile-fset (form)
3764 ;; warn about forms like (fset 'foo '(lambda () ...))
3765 ;; (where the lambda expression is non-trivial...)
3766 (let ((fn (nth 2 form
))
3768 (if (and (eq (car-safe fn
) 'quote
)
3769 (eq (car-safe (setq fn
(nth 1 fn
))) 'lambda
))
3771 (setq body
(cdr (cdr fn
)))
3772 (if (stringp (car body
)) (setq body
(cdr body
)))
3773 (if (eq 'interactive
(car-safe (car body
))) (setq body
(cdr body
)))
3774 (if (and (consp (car body
))
3775 (not (eq 'byte-code
(car (car body
)))))
3777 "A quoted lambda form is the second argument of `fset'. This is probably
3778 not what you want, as that lambda cannot be compiled. Consider using
3779 the syntax #'(lambda (...) ...) instead.")))))
3780 (byte-compile-two-args form
))
3782 ;; (function foo) must compile like 'foo, not like (symbol-function 'foo).
3783 ;; Otherwise it will be incompatible with the interpreter,
3784 ;; and (funcall (function foo)) will lose with autoloads.
3786 (defun byte-compile-function-form (form)
3787 (let ((f (nth 1 form
)))
3788 (when (and (symbolp f
)
3789 (byte-compile-warning-enabled-p 'callargs
))
3790 (byte-compile-function-warn f t
(byte-compile-fdefinition f nil
)))
3792 (byte-compile-constant (if (eq 'lambda
(car-safe f
))
3793 (byte-compile-lambda f
)
3796 (defun byte-compile-indent-to (form)
3797 (let ((len (length form
)))
3799 (byte-compile-form (car (cdr form
)))
3800 (byte-compile-out 'byte-indent-to
0))
3802 ;; no opcode for 2-arg case.
3803 (byte-compile-normal-call form
))
3805 (byte-compile-subr-wrong-args form
"1-2")))))
3807 (defun byte-compile-insert (form)
3808 (cond ((null (cdr form
))
3809 (byte-compile-constant nil
))
3810 ((<= (length form
) 256)
3811 (mapc 'byte-compile-form
(cdr form
))
3812 (if (cdr (cdr form
))
3813 (byte-compile-out 'byte-insertN
(length (cdr form
)))
3814 (byte-compile-out 'byte-insert
0)))
3815 ((memq t
(mapcar 'consp
(cdr (cdr form
))))
3816 (byte-compile-normal-call form
))
3817 ;; We can split it; there is no function call after inserting 1st arg.
3819 (while (setq form
(cdr form
))
3820 (byte-compile-form (car form
))
3821 (byte-compile-out 'byte-insert
0)
3823 (byte-compile-discard))))))
3826 (byte-defop-compiler-1 setq
)
3827 (byte-defop-compiler-1 setq-default
)
3828 (byte-defop-compiler-1 quote
)
3830 (defun byte-compile-setq (form)
3831 (let* ((args (cdr form
))
3832 (len (length args
)))
3833 (if (= (logand len
1) 1)
3835 (byte-compile-report-error
3837 "missing value for `%S' at end of setq" (car (last args
))))
3839 `(signal 'wrong-number-of-arguments
'(setq ,len
))
3840 byte-compile--for-effect
))
3843 (byte-compile-form (car (cdr args
)))
3844 (or byte-compile--for-effect
(cdr (cdr args
))
3845 (byte-compile-out 'byte-dup
0))
3846 (byte-compile-variable-set (car args
))
3847 (setq args
(cdr (cdr args
))))
3848 ;; (setq), with no arguments.
3849 (byte-compile-form nil byte-compile--for-effect
)))
3850 (setq byte-compile--for-effect nil
)))
3852 (defun byte-compile-setq-default (form)
3853 (setq form
(cdr form
))
3854 (if (null form
) ; (setq-default), with no arguments
3855 (byte-compile-form nil byte-compile--for-effect
)
3856 (if (> (length form
) 2)
3859 (push `(setq-default ,(pop form
) ,(pop form
)) setters
))
3860 (byte-compile-form (cons 'progn
(nreverse setters
))))
3861 (let ((var (car form
)))
3862 (and (or (not (symbolp var
))
3863 (macroexp--const-symbol-p var t
))
3864 (byte-compile-warning-enabled-p 'constants
)
3866 "variable assignment to %s `%s'"
3867 (if (symbolp var
) "constant" "nonvariable")
3868 (prin1-to-string var
)))
3869 (byte-compile-normal-call `(set-default ',var
,@(cdr form
)))))))
3871 (byte-defop-compiler-1 set-default
)
3872 (defun byte-compile-set-default (form)
3873 (let ((varexp (car-safe (cdr-safe form
))))
3874 (if (eq (car-safe varexp
) 'quote
)
3875 ;; If the varexp is constant, compile it as a setq-default
3876 ;; so we get more warnings.
3877 (byte-compile-setq-default `(setq-default ,(car-safe (cdr varexp
))
3879 (byte-compile-normal-call form
))))
3881 (defun byte-compile-quote (form)
3882 (byte-compile-constant (car (cdr form
))))
3884 ;;; control structures
3886 (defun byte-compile-body (body &optional for-effect
)
3888 (byte-compile-form (car body
) t
)
3889 (setq body
(cdr body
)))
3890 (byte-compile-form (car body
) for-effect
))
3892 (defsubst byte-compile-body-do-effect
(body)
3893 (byte-compile-body body byte-compile--for-effect
)
3894 (setq byte-compile--for-effect nil
))
3896 (defsubst byte-compile-form-do-effect
(form)
3897 (byte-compile-form form byte-compile--for-effect
)
3898 (setq byte-compile--for-effect nil
))
3900 (byte-defop-compiler-1 inline byte-compile-progn
)
3901 (byte-defop-compiler-1 progn
)
3902 (byte-defop-compiler-1 prog1
)
3903 (byte-defop-compiler-1 prog2
)
3904 (byte-defop-compiler-1 if
)
3905 (byte-defop-compiler-1 cond
)
3906 (byte-defop-compiler-1 and
)
3907 (byte-defop-compiler-1 or
)
3908 (byte-defop-compiler-1 while
)
3909 (byte-defop-compiler-1 funcall
)
3910 (byte-defop-compiler-1 let
)
3911 (byte-defop-compiler-1 let
* byte-compile-let
)
3913 (defun byte-compile-progn (form)
3914 (byte-compile-body-do-effect (cdr form
)))
3916 (defun byte-compile-prog1 (form)
3917 (byte-compile-form-do-effect (car (cdr form
)))
3918 (byte-compile-body (cdr (cdr form
)) t
))
3920 (defun byte-compile-prog2 (form)
3921 (byte-compile-form (nth 1 form
) t
)
3922 (byte-compile-form-do-effect (nth 2 form
))
3923 (byte-compile-body (cdr (cdr (cdr form
))) t
))
3925 (defmacro byte-compile-goto-if
(cond discard tag
)
3928 (if ,discard
'byte-goto-if-not-nil
'byte-goto-if-not-nil-else-pop
)
3929 (if ,discard
'byte-goto-if-nil
'byte-goto-if-nil-else-pop
))
3932 ;; Return the list of items in CONDITION-PARAM that match PRED-LIST.
3933 ;; Only return items that are not in ONLY-IF-NOT-PRESENT.
3934 (defun byte-compile-find-bound-condition (condition-param
3936 &optional only-if-not-present
)
3940 (if (memq (car-safe condition-param
) pred-list
)
3941 ;; The condition appears by itself.
3942 (list condition-param
)
3943 ;; If the condition is an `and', look for matches among the
3945 (when (eq 'and
(car-safe condition-param
))
3946 (cdr condition-param
)))))
3948 (dolist (crt cond-list
)
3949 (when (and (memq (car-safe crt
) pred-list
)
3950 (eq 'quote
(car-safe (setq nth-one
(nth 1 crt
))))
3951 ;; Ignore if the symbol is already on the unresolved
3953 (not (assq (nth 1 nth-one
) ; the relevant symbol
3954 only-if-not-present
)))
3955 (push (nth 1 (nth 1 crt
)) result
)))
3958 (defmacro byte-compile-maybe-guarded
(condition &rest body
)
3959 "Execute forms in BODY, potentially guarded by CONDITION.
3960 CONDITION is a variable whose value is a test in an `if' or `cond'.
3961 BODY is the code to compile in the first arm of the if or the body of
3962 the cond clause. If CONDITION's value is of the form (fboundp \\='foo)
3963 or (boundp \\='foo), the relevant warnings from BODY about foo's
3964 being undefined (or obsolete) will be suppressed.
3966 If CONDITION's value is (not (featurep \\='emacs)) or (featurep \\='xemacs),
3967 that suppresses all warnings during execution of BODY."
3968 (declare (indent 1) (debug t
))
3969 `(let* ((fbound-list (byte-compile-find-bound-condition
3970 ,condition
'(fboundp functionp
)
3971 byte-compile-unresolved-functions
))
3972 (bound-list (byte-compile-find-bound-condition
3973 ,condition
'(boundp default-boundp
)))
3974 ;; Maybe add to the bound list.
3975 (byte-compile-bound-variables
3976 (append bound-list byte-compile-bound-variables
)))
3978 ;; If things not being bound at all is ok, so must them being
3979 ;; obsolete. Note that we add to the existing lists since Tramp
3980 ;; (ab)uses this feature.
3981 ;; FIXME: If `foo' is obsoleted by `bar', the code below
3982 ;; correctly arranges to silence the warnings after testing
3983 ;; existence of `foo', but the warning should also be
3984 ;; silenced after testing the existence of `bar'.
3985 (let ((byte-compile-not-obsolete-vars
3986 (append byte-compile-not-obsolete-vars bound-list
))
3987 (byte-compile-not-obsolete-funcs
3988 (append byte-compile-not-obsolete-funcs fbound-list
)))
3990 ;; Maybe remove the function symbol from the unresolved list.
3991 (dolist (fbound fbound-list
)
3993 (setq byte-compile-unresolved-functions
3994 (delq (assq fbound byte-compile-unresolved-functions
)
3995 byte-compile-unresolved-functions
)))))))
3997 (defun byte-compile-if (form)
3998 (byte-compile-form (car (cdr form
)))
3999 ;; Check whether we have `(if (fboundp ...' or `(if (boundp ...'
4000 ;; and avoid warnings about the relevant symbols in the consequent.
4001 (let ((clause (nth 1 form
))
4002 (donetag (byte-compile-make-tag)))
4003 (if (null (nthcdr 3 form
))
4006 (byte-compile-goto-if nil byte-compile--for-effect donetag
)
4007 (byte-compile-maybe-guarded clause
4008 (byte-compile-form (nth 2 form
) byte-compile--for-effect
))
4009 (byte-compile-out-tag donetag
))
4010 (let ((elsetag (byte-compile-make-tag)))
4011 (byte-compile-goto 'byte-goto-if-nil elsetag
)
4012 (byte-compile-maybe-guarded clause
4013 (byte-compile-form (nth 2 form
) byte-compile--for-effect
))
4014 (byte-compile-goto 'byte-goto donetag
)
4015 (byte-compile-out-tag elsetag
)
4016 (byte-compile-maybe-guarded (list 'not clause
)
4017 (byte-compile-body (cdr (cdr (cdr form
))) byte-compile--for-effect
))
4018 (byte-compile-out-tag donetag
))))
4019 (setq byte-compile--for-effect nil
))
4021 (defun byte-compile-cond-vars (obj1 obj2
)
4022 ;; We make sure that of OBJ1 and OBJ2, one of them is a symbol,
4023 ;; and the other is a constant expression whose value can be
4024 ;; compared with `eq' (with `macroexp-const-p').
4026 (and (symbolp obj1
) (macroexp-const-p obj2
) (cons obj1 obj2
))
4027 (and (symbolp obj2
) (macroexp-const-p obj1
) (cons obj2 obj1
))))
4029 (defun byte-compile-cond-jump-table-info (clauses)
4030 "If CLAUSES is a `cond' form where:
4031 The condition for each clause is of the form (TEST VAR VALUE).
4033 TEST and VAR are the same throughout all conditions.
4034 VALUE satisfies `macroexp-const-p'.
4036 Return a list of the form ((TEST . VAR) ((VALUE BODY) ...))"
4041 (dolist (clause (cdr clauses
) ok
)
4042 (let* ((condition (car clause
))
4043 (test (car-safe condition
))
4044 (vars (when (consp condition
)
4045 (byte-compile-cond-vars (cadr condition
) (cl-caddr condition
))))
4046 (obj1 (car-safe vars
))
4047 (obj2 (cdr-safe vars
))
4048 (body (cdr-safe clause
)))
4050 (setq prev-var obj1
))
4052 (setq prev-test test
))
4053 (if (and obj1
(memq test
'(eq eql equal
))
4057 ;; discard duplicate clauses
4058 (not (assq obj2 cases
)))
4059 (push (list (if (consp obj2
) (eval obj2
) obj2
) body
) cases
)
4060 (if (eq condition t
)
4061 (progn (push (list 'default body
) cases
)
4064 (throw 'break nil
))))))
4065 (list (cons prev-test prev-var
) (nreverse cases
)))))
4067 (defun byte-compile-cond-jump-table (clauses)
4068 (let* ((table-info (byte-compile-cond-jump-table-info clauses
))
4069 (test (caar table-info
))
4070 (var (cdar table-info
))
4071 (cases (cadr table-info
))
4072 jump-table test-obj body tag donetag default-tag default-case
)
4073 (when (and cases
(not (= (length cases
) 1)))
4074 ;; TODO: Once :linear-search is implemented for `make-hash-table'
4075 ;; set it to `t' for cond forms with a small number of cases.
4076 (setq jump-table
(make-hash-table :test test
4078 :size
(if (assq 'default cases
)
4081 default-tag
(byte-compile-make-tag)
4082 donetag
(byte-compile-make-tag))
4083 ;; The structure of byte-switch code:
4086 ;; constant #s(hash-table purecopy t data (val1 (TAG1) val2 (TAG2)))
4096 ;; <body for `t' clause, if any (else `constant nil')>
4099 (byte-compile-variable-ref var
)
4100 (byte-compile-push-constant jump-table
)
4101 (byte-compile-out 'byte-switch
)
4103 ;; When the opcode argument is `byte-goto', `byte-compile-goto' sets
4104 ;; `byte-compile-depth' to `nil'. However, we need `byte-compile-depth'
4105 ;; to be non-nil for generating tags for all cases. Since
4106 ;; `byte-compile-depth' will increase by at most 1 after compiling
4107 ;; all of the clause (which is further enforced by cl-assert below)
4108 ;; it should be safe to preserve it's value.
4109 (let ((byte-compile-depth byte-compile-depth
))
4110 (byte-compile-goto 'byte-goto default-tag
))
4112 (when (assq 'default cases
)
4113 (setq default-case
(cadr (assq 'default cases
))
4114 cases
(butlast cases
1)))
4116 (dolist (case cases
)
4117 (setq tag
(byte-compile-make-tag)
4118 test-obj
(nth 0 case
)
4120 (byte-compile-out-tag tag
)
4121 (puthash test-obj tag jump-table
)
4123 (let ((byte-compile-depth byte-compile-depth
)
4124 (init-depth byte-compile-depth
))
4125 ;; Since `byte-compile-body' might increase `byte-compile-depth'
4126 ;; by 1, not preserving it's value will cause it to potentially
4127 ;; increase by one for every clause body compiled, causing
4128 ;; depth/tag conflicts or violating asserts down the road.
4129 ;; To make sure `byte-compile-body' itself doesn't violate this,
4130 ;; we use `cl-assert'.
4131 (byte-compile-body body byte-compile--for-effect
)
4132 (cl-assert (or (= byte-compile-depth init-depth
)
4133 (= byte-compile-depth
(1+ init-depth
))))
4134 (byte-compile-goto 'byte-goto donetag
)
4135 (setcdr (cdr donetag
) nil
)))
4137 (byte-compile-out-tag default-tag
)
4139 (byte-compile-body-do-effect default-case
)
4140 (byte-compile-constant nil
))
4141 (byte-compile-out-tag donetag
)
4142 (push jump-table byte-compile-jump-tables
))))
4144 (defun byte-compile-cond (clauses)
4145 (or (and byte-compile-cond-use-jump-table
4146 (byte-compile-cond-jump-table clauses
))
4147 (let ((donetag (byte-compile-make-tag))
4149 (while (setq clauses
(cdr clauses
))
4150 (setq clause
(car clauses
))
4151 (cond ((or (eq (car clause
) t
)
4152 (and (eq (car-safe (car clause
)) 'quote
)
4153 (car-safe (cdr-safe (car clause
)))))
4154 ;; Unconditional clause
4155 (setq clause
(cons t clause
)
4158 (byte-compile-form (car clause
))
4159 (if (null (cdr clause
))
4160 ;; First clause is a singleton.
4161 (byte-compile-goto-if t byte-compile--for-effect donetag
)
4162 (setq nexttag
(byte-compile-make-tag))
4163 (byte-compile-goto 'byte-goto-if-nil nexttag
)
4164 (byte-compile-maybe-guarded (car clause
)
4165 (byte-compile-body (cdr clause
) byte-compile--for-effect
))
4166 (byte-compile-goto 'byte-goto donetag
)
4167 (byte-compile-out-tag nexttag
)))))
4169 (let ((guard (car clause
)))
4170 (and (cdr clause
) (not (eq guard t
))
4171 (progn (byte-compile-form guard
)
4172 (byte-compile-goto-if nil byte-compile--for-effect donetag
)
4173 (setq clause
(cdr clause
))))
4174 (byte-compile-maybe-guarded guard
4175 (byte-compile-body-do-effect clause
)))
4176 (byte-compile-out-tag donetag
))))
4178 (defun byte-compile-and (form)
4179 (let ((failtag (byte-compile-make-tag))
4182 (byte-compile-form-do-effect t
)
4183 (byte-compile-and-recursion args failtag
))))
4185 ;; Handle compilation of a nontrivial `and' call.
4186 ;; We use tail recursion so we can use byte-compile-maybe-guarded.
4187 (defun byte-compile-and-recursion (rest failtag
)
4190 (byte-compile-form (car rest
))
4191 (byte-compile-goto-if nil byte-compile--for-effect failtag
)
4192 (byte-compile-maybe-guarded (car rest
)
4193 (byte-compile-and-recursion (cdr rest
) failtag
)))
4194 (byte-compile-form-do-effect (car rest
))
4195 (byte-compile-out-tag failtag
)))
4197 (defun byte-compile-or (form)
4198 (let ((wintag (byte-compile-make-tag))
4201 (byte-compile-form-do-effect nil
)
4202 (byte-compile-or-recursion args wintag
))))
4204 ;; Handle compilation of a nontrivial `or' call.
4205 ;; We use tail recursion so we can use byte-compile-maybe-guarded.
4206 (defun byte-compile-or-recursion (rest wintag
)
4209 (byte-compile-form (car rest
))
4210 (byte-compile-goto-if t byte-compile--for-effect wintag
)
4211 (byte-compile-maybe-guarded (list 'not
(car rest
))
4212 (byte-compile-or-recursion (cdr rest
) wintag
)))
4213 (byte-compile-form-do-effect (car rest
))
4214 (byte-compile-out-tag wintag
)))
4216 (defun byte-compile-while (form)
4217 (let ((endtag (byte-compile-make-tag))
4218 (looptag (byte-compile-make-tag)))
4219 (byte-compile-out-tag looptag
)
4220 (byte-compile-form (car (cdr form
)))
4221 (byte-compile-goto-if nil byte-compile--for-effect endtag
)
4222 (byte-compile-body (cdr (cdr form
)) t
)
4223 (byte-compile-goto 'byte-goto looptag
)
4224 (byte-compile-out-tag endtag
)
4225 (setq byte-compile--for-effect nil
)))
4227 (defun byte-compile-funcall (form)
4230 (mapc 'byte-compile-form
(cdr form
))
4231 (byte-compile-out 'byte-call
(length (cdr (cdr form
)))))
4232 (byte-compile-report-error
4233 (format-message "`funcall' called with no arguments"))
4234 (byte-compile-form '(signal 'wrong-number-of-arguments
'(funcall 0))
4235 byte-compile--for-effect
)))
4240 (defun byte-compile-push-binding-init (clause)
4241 "Emit byte-codes to push the initialization value for CLAUSE on the stack.
4242 Return the offset in the form (VAR . OFFSET)."
4243 (let* ((var (if (consp clause
) (car clause
) clause
)))
4244 ;; We record the stack position even of dynamic bindings; we'll put
4245 ;; them in the proper place later.
4246 (prog1 (cons var byte-compile-depth
)
4248 (byte-compile-form (cadr clause
))
4249 (byte-compile-push-constant nil
)))))
4251 (defun byte-compile-not-lexical-var-p (var)
4252 (or (not (symbolp var
))
4253 (special-variable-p var
)
4254 (memq var byte-compile-bound-variables
)
4258 (defun byte-compile-bind (var init-lexenv
)
4259 "Emit byte-codes to bind VAR and update `byte-compile--lexical-environment'.
4260 INIT-LEXENV should be a lexical-environment alist describing the
4261 positions of the init value that have been pushed on the stack.
4262 Return non-nil if the TOS value was popped."
4263 ;; The mix of lexical and dynamic bindings mean that we may have to
4264 ;; juggle things on the stack, to move them to TOS for
4266 (if (and lexical-binding
(not (byte-compile-not-lexical-var-p var
)))
4267 ;; VAR is a simple stack-allocated lexical variable.
4268 (progn (push (assq var init-lexenv
)
4269 byte-compile--lexical-environment
)
4271 ;; VAR should be dynamically bound.
4272 (while (assq var byte-compile--lexical-environment
)
4273 ;; This dynamic binding shadows a lexical binding.
4274 (setq byte-compile--lexical-environment
4275 (remq (assq var byte-compile--lexical-environment
)
4276 byte-compile--lexical-environment
)))
4278 ((eq var
(caar init-lexenv
))
4279 ;; VAR is dynamic and is on the top of the
4280 ;; stack, so we can just bind it like usual.
4281 (byte-compile-dynamic-variable-bind var
)
4284 ;; VAR is dynamic, but we have to get its
4285 ;; value out of the middle of the stack.
4286 (let ((stack-pos (cdr (assq var init-lexenv
))))
4287 (byte-compile-stack-ref stack-pos
)
4288 (byte-compile-dynamic-variable-bind var
)
4289 ;; Now we have to store nil into its temporary
4290 ;; stack position so it doesn't prevent the value from being GC'd.
4291 ;; FIXME: Not worth the trouble.
4292 ;; (byte-compile-push-constant nil)
4293 ;; (byte-compile-stack-set stack-pos)
4297 (defun byte-compile-unbind (clauses init-lexenv preserve-body-value
)
4298 "Emit byte-codes to unbind the variables bound by CLAUSES.
4299 CLAUSES is a `let'-style variable binding list. INIT-LEXENV should be a
4300 lexical-environment alist describing the positions of the init value that
4301 have been pushed on the stack. If PRESERVE-BODY-VALUE is true,
4302 then an additional value on the top of the stack, above any lexical binding
4303 slots, is preserved, so it will be on the top of the stack after all
4304 binding slots have been popped."
4305 ;; Unbind dynamic variables.
4306 (let ((num-dynamic-bindings 0))
4307 (dolist (clause clauses
)
4308 (unless (assq (if (consp clause
) (car clause
) clause
)
4309 byte-compile--lexical-environment
)
4310 (setq num-dynamic-bindings
(1+ num-dynamic-bindings
))))
4311 (unless (zerop num-dynamic-bindings
)
4312 (byte-compile-out 'byte-unbind num-dynamic-bindings
)))
4313 ;; Pop lexical variables off the stack, possibly preserving the
4314 ;; return value of the body.
4316 ;; INIT-LEXENV contains all init values left on the stack.
4317 (byte-compile-discard (length init-lexenv
) preserve-body-value
)))
4319 (defun byte-compile-let (form)
4320 "Generate code for the `let' or `let*' form FORM."
4321 (let ((clauses (cadr form
))
4323 (is-let (eq (car form
) 'let
)))
4325 ;; First compute the binding values in the old scope.
4326 (dolist (var clauses
)
4327 (push (byte-compile-push-binding-init var
) init-lexenv
)))
4329 (let ((byte-compile-bound-variables byte-compile-bound-variables
)
4330 (byte-compile--lexical-environment
4331 byte-compile--lexical-environment
))
4332 ;; Bind the variables.
4333 ;; For `let', do it in reverse order, because it makes no
4334 ;; semantic difference, but it is a lot more efficient since the
4335 ;; values are now in reverse order on the stack.
4336 (dolist (var (if is-let
(reverse clauses
) clauses
))
4338 (push (byte-compile-push-binding-init var
) init-lexenv
))
4339 (let ((var (if (consp var
) (car var
) var
)))
4340 (if (byte-compile-bind var init-lexenv
)
4341 (pop init-lexenv
))))
4343 (let ((init-stack-depth byte-compile-depth
))
4344 (byte-compile-body-do-effect (cdr (cdr form
)))
4345 ;; Unbind both lexical and dynamic variables.
4346 (cl-assert (or (eq byte-compile-depth init-stack-depth
)
4347 (eq byte-compile-depth
(1+ init-stack-depth
))))
4348 (byte-compile-unbind clauses init-lexenv
4349 (> byte-compile-depth init-stack-depth
))))))
4353 (byte-defop-compiler-1 /= byte-compile-negated
)
4354 (byte-defop-compiler-1 atom byte-compile-negated
)
4355 (byte-defop-compiler-1 nlistp byte-compile-negated
)
4357 (put '/= 'byte-compile-negated-op
'=)
4358 (put 'atom
'byte-compile-negated-op
'consp
)
4359 (put 'nlistp
'byte-compile-negated-op
'listp
)
4361 (defun byte-compile-negated (form)
4362 (byte-compile-form-do-effect (byte-compile-negation-optimizer form
)))
4364 ;; Even when optimization is off, /= is optimized to (not (= ...)).
4365 (defun byte-compile-negation-optimizer (form)
4366 ;; an optimizer for forms where <form1> is less efficient than (not <form2>)
4367 (byte-compile-set-symbol-position (car form
))
4369 (cons (or (get (car form
) 'byte-compile-negated-op
)
4371 "Compiler error: `%s' has no `byte-compile-negated-op' property"
4375 ;;; other tricky macro-like special-forms
4377 (byte-defop-compiler-1 catch
)
4378 (byte-defop-compiler-1 unwind-protect
)
4379 (byte-defop-compiler-1 condition-case
)
4380 (byte-defop-compiler-1 save-excursion
)
4381 (byte-defop-compiler-1 save-current-buffer
)
4382 (byte-defop-compiler-1 save-restriction
)
4383 ;; (byte-defop-compiler-1 save-window-excursion) ;Obsolete: now a macro.
4384 ;; (byte-defop-compiler-1 with-output-to-temp-buffer) ;Obsolete: now a macro.
4386 (defvar byte-compile--use-old-handlers nil
4387 "If nil, use new byte codes introduced in Emacs-24.4.")
4389 (defun byte-compile-catch (form)
4390 (byte-compile-form (car (cdr form
)))
4391 (if (not byte-compile--use-old-handlers
)
4392 (let ((endtag (byte-compile-make-tag)))
4393 (byte-compile-goto 'byte-pushcatch endtag
)
4394 (byte-compile-body (cddr form
) nil
)
4395 (byte-compile-out 'byte-pophandler
)
4396 (byte-compile-out-tag endtag
))
4399 (byte-compile-form `(list 'funcall
,f
)))
4401 (byte-compile-push-constant
4402 (byte-compile-top-level (cons 'progn body
) byte-compile--for-effect
))))
4403 (byte-compile-out 'byte-catch
0)))
4405 (defun byte-compile-unwind-protect (form)
4409 (if byte-compile--use-old-handlers
`(list (list 'funcall
,f
)) f
)))
4411 (if byte-compile--use-old-handlers
4412 (byte-compile-push-constant
4413 (byte-compile-top-level-body handlers t
))
4414 (byte-compile-form `#'(lambda () ,@handlers
)))))
4415 (byte-compile-out 'byte-unwind-protect
0)
4416 (byte-compile-form-do-effect (car (cdr form
)))
4417 (byte-compile-out 'byte-unbind
1))
4419 (defun byte-compile-condition-case (form)
4420 (if byte-compile--use-old-handlers
4421 (byte-compile-condition-case--old form
)
4422 (byte-compile-condition-case--new form
)))
4424 (defun byte-compile-condition-case--old (form)
4425 (let* ((var (nth 1 form
))
4426 (fun-bodies (eq var
:fun-body
))
4427 (byte-compile-bound-variables
4428 (if (and var
(not fun-bodies
))
4429 (cons var byte-compile-bound-variables
)
4430 byte-compile-bound-variables
)))
4431 (byte-compile-set-symbol-position 'condition-case
)
4432 (unless (symbolp var
)
4434 "`%s' is not a variable-name or nil (in condition-case)" var
))
4435 (if fun-bodies
(setq var
(make-symbol "err")))
4436 (byte-compile-push-constant var
)
4438 (byte-compile-form `(list 'funcall
,(nth 2 form
)))
4439 (byte-compile-push-constant
4440 (byte-compile-top-level (nth 2 form
) byte-compile--for-effect
)))
4441 (let ((compiled-clauses
4444 (let ((condition (car clause
)))
4445 (cond ((not (or (symbolp condition
)
4446 (and (listp condition
)
4448 (dolist (sym condition
)
4449 (if (not (symbolp sym
))
4453 "`%S' is not a condition name or list of such (in condition-case)"
4455 ;; (not (or (eq condition 't)
4456 ;; (and (stringp (get condition 'error-message))
4457 ;; (consp (get condition
4458 ;; 'error-conditions)))))
4459 ;; (byte-compile-warn
4460 ;; "`%s' is not a known condition name
4461 ;; (in condition-case)"
4465 `(list ',condition
(list 'funcall
,(cadr clause
) ',var
))
4467 (byte-compile-top-level-body
4468 (cdr clause
) byte-compile--for-effect
)))))
4469 (cdr (cdr (cdr form
))))))
4471 (byte-compile-form `(list ,@compiled-clauses
))
4472 (byte-compile-push-constant compiled-clauses
)))
4473 (byte-compile-out 'byte-condition-case
0)))
4475 (defun byte-compile-condition-case--new (form)
4476 (let* ((var (nth 1 form
))
4478 (depth byte-compile-depth
)
4479 (clauses (mapcar (lambda (clause)
4480 (cons (byte-compile-make-tag) clause
))
4482 (endtag (byte-compile-make-tag)))
4483 (byte-compile-set-symbol-position 'condition-case
)
4484 (unless (symbolp var
)
4486 "`%s' is not a variable-name or nil (in condition-case)" var
))
4488 (dolist (clause (reverse clauses
))
4489 (let ((condition (nth 1 clause
)))
4490 (unless (consp condition
) (setq condition
(list condition
)))
4491 (dolist (c condition
)
4492 (unless (and c
(symbolp c
))
4494 "`%S' is not a condition name (in condition-case)" c
))
4495 ;; In reality, the `error-conditions' property is only required
4496 ;; for the argument to `signal', not to `condition-case'.
4497 ;;(unless (consp (get c 'error-conditions))
4498 ;; (byte-compile-warn
4499 ;; "`%s' is not a known condition name (in condition-case)"
4502 (byte-compile-push-constant condition
))
4503 (byte-compile-goto 'byte-pushconditioncase
(car clause
)))
4505 (byte-compile-form body
) ;; byte-compile--for-effect
4506 (dolist (_ clauses
) (byte-compile-out 'byte-pophandler
))
4507 (byte-compile-goto 'byte-goto endtag
)
4510 (let ((clause (pop clauses
))
4511 (byte-compile-bound-variables byte-compile-bound-variables
)
4512 (byte-compile--lexical-environment
4513 byte-compile--lexical-environment
))
4514 (setq byte-compile-depth
(1+ depth
))
4515 (byte-compile-out-tag (pop clause
))
4516 (dolist (_ clauses
) (byte-compile-out 'byte-pophandler
))
4518 ((null var
) (byte-compile-discard))
4520 (push (cons var
(1- byte-compile-depth
))
4521 byte-compile--lexical-environment
))
4522 (t (byte-compile-dynamic-variable-bind var
)))
4523 (byte-compile-body (cdr clause
)) ;; byte-compile--for-effect
4526 (lexical-binding (byte-compile-discard 1 'preserve-tos
))
4527 (t (byte-compile-out 'byte-unbind
1)))
4528 (byte-compile-goto 'byte-goto endtag
)))
4530 (byte-compile-out-tag endtag
)))
4532 (defun byte-compile-save-excursion (form)
4533 (if (and (eq 'set-buffer
(car-safe (car-safe (cdr form
))))
4534 (byte-compile-warning-enabled-p 'suspicious
))
4536 "Use `with-current-buffer' rather than save-excursion+set-buffer"))
4537 (byte-compile-out 'byte-save-excursion
0)
4538 (byte-compile-body-do-effect (cdr form
))
4539 (byte-compile-out 'byte-unbind
1))
4541 (defun byte-compile-save-restriction (form)
4542 (byte-compile-out 'byte-save-restriction
0)
4543 (byte-compile-body-do-effect (cdr form
))
4544 (byte-compile-out 'byte-unbind
1))
4546 (defun byte-compile-save-current-buffer (form)
4547 (byte-compile-out 'byte-save-current-buffer
0)
4548 (byte-compile-body-do-effect (cdr form
))
4549 (byte-compile-out 'byte-unbind
1))
4551 ;;; top-level forms elsewhere
4553 (byte-defop-compiler-1 defvar
)
4554 (byte-defop-compiler-1 defconst byte-compile-defvar
)
4555 (byte-defop-compiler-1 autoload
)
4556 (byte-defop-compiler-1 lambda byte-compile-lambda-form
)
4558 ;; If foo.el declares `toto' as obsolete, it is likely that foo.el will
4559 ;; actually use `toto' in order for this obsolete variable to still work
4560 ;; correctly, so paradoxically, while byte-compiling foo.el, the presence
4561 ;; of a make-obsolete-variable call for `toto' is an indication that `toto'
4562 ;; should not trigger obsolete-warnings in foo.el.
4563 (byte-defop-compiler-1 make-obsolete-variable
)
4564 (defun byte-compile-make-obsolete-variable (form)
4565 (when (eq 'quote
(car-safe (nth 1 form
)))
4566 (push (nth 1 (nth 1 form
)) byte-compile-global-not-obsolete-vars
))
4567 (byte-compile-normal-call form
))
4569 (defconst byte-compile-tmp-var
(make-symbol "def-tmp-var"))
4571 (defun byte-compile-defvar (form)
4572 ;; This is not used for file-level defvar/consts.
4573 (when (and (symbolp (nth 1 form
))
4574 (not (string-match "[-*/:$]" (symbol-name (nth 1 form
))))
4575 (byte-compile-warning-enabled-p 'lexical
))
4576 (byte-compile-warn "global/dynamic var `%s' lacks a prefix"
4578 (let ((fun (nth 0 form
))
4580 (value (nth 2 form
))
4581 (string (nth 3 form
)))
4582 (byte-compile-set-symbol-position fun
)
4583 (when (or (> (length form
) 4)
4584 (and (eq fun
'defconst
) (null (cddr form
))))
4585 (let ((ncall (length (cdr form
))))
4587 "`%s' called with %d argument%s, but %s %s"
4589 (if (= 1 ncall
) "" "s")
4590 (if (< ncall
2) "requires" "accepts only")
4592 (push var byte-compile-bound-variables
)
4593 (if (eq fun
'defconst
)
4594 (push var byte-compile-const-variables
))
4595 (when (and string
(not (stringp string
)))
4596 (byte-compile-warn "third arg to `%s %s' is not a string: %s"
4598 (byte-compile-form-do-effect
4599 (if (cddr form
) ; `value' provided
4600 ;; Quote with `quote' to prevent byte-compiling the body,
4601 ;; which would lead to an inf-loop.
4602 `(funcall '(lambda (,byte-compile-tmp-var
)
4603 (,fun
,var
,byte-compile-tmp-var
,@(nthcdr 3 form
)))
4605 (if (eq fun
'defconst
)
4606 ;; This will signal an appropriate error at runtime.
4608 ;; A simple (defvar foo) just returns foo.
4611 (defun byte-compile-autoload (form)
4612 (byte-compile-set-symbol-position 'autoload
)
4613 (and (macroexp-const-p (nth 1 form
))
4614 (macroexp-const-p (nth 5 form
))
4615 (memq (eval (nth 5 form
)) '(t macro
)) ; macro-p
4616 (not (fboundp (eval (nth 1 form
))))
4618 "The compiler ignores `autoload' except at top level. You should
4619 probably put the autoload of the macro `%s' at top-level."
4620 (eval (nth 1 form
))))
4621 (byte-compile-normal-call form
))
4623 ;; Lambdas in valid places are handled as special cases by various code.
4624 ;; The ones that remain are errors.
4625 (defun byte-compile-lambda-form (_form)
4626 (byte-compile-set-symbol-position 'lambda
)
4627 (error "`lambda' used as function name is invalid"))
4629 ;; Compile normally, but deal with warnings for the function being defined.
4630 (put 'defalias
'byte-hunk-handler
'byte-compile-file-form-defalias
)
4631 ;; Used for eieio--defalias as well.
4632 (defun byte-compile-file-form-defalias (form)
4633 ;; For the compilation itself, we could largely get rid of this hunk-handler,
4634 ;; if it weren't for the fact that we need to figure out when a defalias
4635 ;; defines a macro, so as to add it to byte-compile-macro-environment.
4637 ;; FIXME: we also use this hunk-handler to implement the function's dynamic
4638 ;; docstring feature. We could actually implement it more elegantly in
4639 ;; byte-compile-lambda so it applies to all lambdas, but the problem is that
4640 ;; the resulting .elc format will not be recognized by make-docfile, so
4641 ;; either we stop using DOC for the docstrings of preloaded elc files (at the
4642 ;; cost of around 24KB on 32bit hosts, double on 64bit hosts) or we need to
4643 ;; build DOC in a more clever way (e.g. handle anonymous elements).
4644 (let ((byte-compile-free-references nil
)
4645 (byte-compile-free-assignments nil
))
4647 ;; Decompose `form' into:
4648 ;; - `name' is the name of the defined function.
4649 ;; - `arg' is the expression to which it is defined.
4650 ;; - `rest' is the rest of the arguments.
4651 (`(,_
',name
,arg .
,rest
)
4653 ;; `macro' is non-nil if it defines a macro.
4654 ;; `fun' is the function part of `arg' (defaults to `arg').
4655 (((or (and (or `(cons 'macro
,fun
) `'(macro .
,fun
)) (let macro t
))
4656 (and (let fun arg
) (let macro nil
)))
4658 ;; `lam' is the lambda expression in `fun' (or nil if not
4660 ((or `(,(or `quote
`function
) ,lam
) (let lam nil
))
4662 ;; `arglist' is the list of arguments (or t if not recognized).
4663 ;; `body' is the body of `lam' (or t if not recognized).
4664 ((or `(lambda ,arglist .
,body
)
4665 ;; `(closure ,_ ,arglist . ,body)
4666 (and `(internal-make-closure ,arglist .
,_
) (let body t
))
4667 (and (let arglist t
) (let body t
)))
4669 (unless (byte-compile-file-form-defmumble
4670 name macro arglist body rest
)
4673 (message "Macro %s unrecognized, won't work in file" name
)
4674 (message "Macro %s partly recognized, trying our luck" name
)
4675 (push (cons name
(eval fun
))
4676 byte-compile-macro-environment
)))
4677 (byte-compile-keep-pending form
))))
4679 ;; We used to just do: (byte-compile-normal-call form)
4680 ;; But it turns out that this fails to optimize the code.
4681 ;; So instead we now do the same as what other byte-hunk-handlers do,
4682 ;; which is to call back byte-compile-file-form and then return nil.
4683 ;; Except that we can't just call byte-compile-file-form since it would
4684 ;; call us right back.
4685 (_ (byte-compile-keep-pending form
)))))
4687 (byte-defop-compiler-1 with-no-warnings byte-compile-no-warnings
)
4688 (defun byte-compile-no-warnings (form)
4689 (let (byte-compile-warnings)
4690 (byte-compile-form (cons 'progn
(cdr form
)))))
4692 ;; Warn about misuses of make-variable-buffer-local.
4693 (byte-defop-compiler-1 make-variable-buffer-local
4694 byte-compile-make-variable-buffer-local
)
4695 (defun byte-compile-make-variable-buffer-local (form)
4696 (if (and (eq (car-safe (car-safe (cdr-safe form
))) 'quote
)
4697 (byte-compile-warning-enabled-p 'make-local
))
4699 "`make-variable-buffer-local' not called at toplevel"))
4700 (byte-compile-normal-call form
))
4701 (put 'make-variable-buffer-local
4702 'byte-hunk-handler
'byte-compile-form-make-variable-buffer-local
)
4703 (defun byte-compile-form-make-variable-buffer-local (form)
4704 (byte-compile-keep-pending form
'byte-compile-normal-call
))
4708 ;; Note: Most operations will strip off the 'TAG, but it speeds up
4709 ;; optimization to have the 'TAG as a part of the tag.
4710 ;; Tags will be (TAG . (tag-number . stack-depth)).
4711 (defun byte-compile-make-tag ()
4712 (list 'TAG
(setq byte-compile-tag-number
(1+ byte-compile-tag-number
))))
4715 (defun byte-compile-out-tag (tag)
4716 (setq byte-compile-output
(cons tag byte-compile-output
))
4719 ;; ## remove this someday
4720 (and byte-compile-depth
4721 (not (= (cdr (cdr tag
)) byte-compile-depth
))
4722 (error "Compiler bug: depth conflict at tag %d" (car (cdr tag
))))
4723 (setq byte-compile-depth
(cdr (cdr tag
))))
4724 (setcdr (cdr tag
) byte-compile-depth
)))
4726 (defun byte-compile-goto (opcode tag
)
4727 (push (cons opcode tag
) byte-compile-output
)
4728 (setcdr (cdr tag
) (if (memq opcode byte-goto-always-pop-ops
)
4729 (1- byte-compile-depth
)
4730 byte-compile-depth
))
4731 (setq byte-compile-depth
(and (not (eq opcode
'byte-goto
))
4732 (1- byte-compile-depth
))))
4734 (defun byte-compile-stack-adjustment (op operand
)
4735 "Return the amount by which an operation adjusts the stack.
4736 OP and OPERAND are as passed to `byte-compile-out'."
4737 (if (memq op
'(byte-call byte-discardN byte-discardN-preserve-tos
))
4738 ;; For calls, OPERAND is the number of args, so we pop OPERAND + 1
4739 ;; elements, and the push the result, for a total of -OPERAND.
4740 ;; For discardN*, of course, we just pop OPERAND elements.
4742 (or (aref byte-stack
+-info
(symbol-value op
))
4743 ;; Ops with a nil entry in `byte-stack+-info' are byte-codes
4744 ;; that take OPERAND values off the stack and push a result, for
4745 ;; a total of 1 - OPERAND
4748 (defun byte-compile-out (op &optional operand
)
4749 (push (cons op operand
) byte-compile-output
)
4750 (if (eq op
'byte-return
)
4751 ;; This is actually an unnecessary case, because there should be no
4752 ;; more ops behind byte-return.
4753 (setq byte-compile-depth nil
)
4754 (setq byte-compile-depth
4755 (+ byte-compile-depth
(byte-compile-stack-adjustment op operand
)))
4756 (setq byte-compile-maxdepth
(max byte-compile-depth byte-compile-maxdepth
))
4757 ;;(if (< byte-compile-depth 0) (error "Compiler error: stack underflow"))
4762 (defun byte-compile-annotate-call-tree (form)
4764 ;; annotate the current call
4765 (if (setq entry
(assq (car form
) byte-compile-call-tree
))
4766 (or (memq byte-compile-current-form
(nth 1 entry
)) ;callers
4768 (cons byte-compile-current-form
(nth 1 entry
))))
4769 (setq byte-compile-call-tree
4770 (cons (list (car form
) (list byte-compile-current-form
) nil
)
4771 byte-compile-call-tree
)))
4772 ;; annotate the current function
4773 (if (setq entry
(assq byte-compile-current-form byte-compile-call-tree
))
4774 (or (memq (car form
) (nth 2 entry
)) ;called
4775 (setcar (cdr (cdr entry
))
4776 (cons (car form
) (nth 2 entry
))))
4777 (setq byte-compile-call-tree
4778 (cons (list byte-compile-current-form nil
(list (car form
)))
4779 byte-compile-call-tree
)))
4782 ;; Renamed from byte-compile-report-call-tree
4783 ;; to avoid interfering with completion of byte-compile-file.
4785 (defun display-call-tree (&optional filename
)
4786 "Display a call graph of a specified file.
4787 This lists which functions have been called, what functions called
4788 them, and what functions they call. The list includes all functions
4789 whose definitions have been compiled in this Emacs session, as well as
4790 all functions called by those functions.
4792 The call graph does not include macros, inline functions, or
4793 primitives that the byte-code interpreter knows about directly
4794 \(`eq', `cons', etc.).
4796 The call tree also lists those functions which are not known to be called
4797 \(that is, to which no calls have been compiled), and which cannot be
4798 invoked interactively."
4800 (message "Generating call tree...")
4801 (with-output-to-temp-buffer "*Call-Tree*"
4802 (set-buffer "*Call-Tree*")
4804 (message "Generating call tree... (sorting on %s)"
4805 byte-compile-call-tree-sort
)
4806 (insert "Call tree for "
4807 (cond ((null byte-compile-current-file
) (or filename
"???"))
4808 ((stringp byte-compile-current-file
)
4809 byte-compile-current-file
)
4810 (t (buffer-name byte-compile-current-file
)))
4812 (prin1-to-string byte-compile-call-tree-sort
)
4814 (if byte-compile-call-tree-sort
4815 (setq byte-compile-call-tree
4816 (sort byte-compile-call-tree
4817 (pcase byte-compile-call-tree-sort
4819 (lambda (x y
) (< (length (nth 1 x
))
4820 (length (nth 1 y
)))))
4822 (lambda (x y
) (< (length (nth 2 x
))
4823 (length (nth 2 y
)))))
4825 (lambda (x y
) (< (+ (length (nth 1 x
))
4827 (+ (length (nth 1 y
))
4828 (length (nth 2 y
))))))
4830 (lambda (x y
) (string< (car x
) (car y
))))
4831 (_ (error "`byte-compile-call-tree-sort': `%s' - unknown sort mode"
4832 byte-compile-call-tree-sort
))))))
4833 (message "Generating call tree...")
4834 (let ((rest byte-compile-call-tree
)
4835 (b (current-buffer))
4839 (prin1 (car (car rest
)) b
)
4840 (setq callers
(nth 1 (car rest
))
4841 calls
(nth 2 (car rest
)))
4843 (cond ((not (fboundp (setq f
(car (car rest
)))))
4845 " <top level>";; shouldn't insert nil then, actually -sk
4847 ((subrp (setq f
(symbol-function f
)))
4850 (format " ==> %s" f
))
4851 ((byte-code-function-p f
)
4852 "<compiled function>")
4854 "<malformed function>")
4855 ((eq 'macro
(car f
))
4856 (if (or (byte-code-function-p (cdr f
))
4857 (assq 'byte-code
(cdr (cdr (cdr f
)))))
4860 ((assq 'byte-code
(cdr (cdr f
)))
4861 "<compiled lambda>")
4862 ((eq 'lambda
(car f
))
4865 (format " (%d callers + %d calls = %d)"
4866 ;; Does the optimizer eliminate common subexpressions?-sk
4869 (+ (length callers
) (length calls
)))
4873 (insert " called by:\n")
4875 (insert " " (if (car callers
)
4876 (mapconcat 'symbol-name callers
", ")
4878 (let ((fill-prefix " "))
4879 (fill-region-as-paragraph p
(point)))
4880 (unless (= 0 (current-column))
4884 (insert " calls:\n")
4886 (insert " " (mapconcat 'symbol-name calls
", "))
4887 (let ((fill-prefix " "))
4888 (fill-region-as-paragraph p
(point)))
4889 (unless (= 0 (current-column))
4891 (setq rest
(cdr rest
)))
4893 (message "Generating call tree...(finding uncalled functions...)")
4894 (setq rest byte-compile-call-tree
)
4897 (or (nth 1 (car rest
))
4898 (null (setq f
(caar rest
)))
4900 (setq def
(byte-compile-fdefinition f t
))
4901 (and (eq (car-safe def
) 'macro
)
4902 (eq (car-safe (cdr-safe def
)) 'lambda
)
4903 (setq def
(cdr def
)))
4906 (setq def
(byte-compile-fdefinition f nil
))
4907 (and (eq (car-safe def
) 'macro
)
4908 (eq (car-safe (cdr-safe def
)) 'lambda
)
4909 (setq def
(cdr def
)))
4911 (setq uncalled
(cons f uncalled
)))
4912 (setq rest
(cdr rest
)))
4914 (let ((fill-prefix " "))
4915 (insert "Noninteractive functions not known to be called:\n ")
4917 (insert (mapconcat 'symbol-name
(nreverse uncalled
) ", "))
4918 (fill-region-as-paragraph p
(point))))))
4919 (message "Generating call tree...done.")))
4923 (defun batch-byte-compile-if-not-done ()
4924 "Like `byte-compile-file' but doesn't recompile if already up to date.
4925 Use this from the command line, with `-batch';
4926 it won't work in an interactive Emacs."
4927 (batch-byte-compile t
))
4929 ;;; by crl@newton.purdue.edu
4930 ;;; Only works noninteractively.
4932 (defun batch-byte-compile (&optional noforce
)
4933 "Run `byte-compile-file' on the files remaining on the command line.
4934 Use this from the command line, with `-batch';
4935 it won't work in an interactive Emacs.
4936 Each file is processed even if an error occurred previously.
4937 For example, invoke \"emacs -batch -f batch-byte-compile $emacs/ ~/*.el\".
4938 If NOFORCE is non-nil, don't recompile a file that seems to be
4939 already up-to-date."
4940 ;; command-line-args-left is what is left of the command line, from
4942 (defvar command-line-args-left
) ;Avoid 'free variable' warning
4943 (if (not noninteractive
)
4944 (error "`batch-byte-compile' is to be used only with -batch"))
4946 (while command-line-args-left
4947 (if (file-directory-p (expand-file-name (car command-line-args-left
)))
4948 ;; Directory as argument.
4950 (dolist (file (directory-files (car command-line-args-left
)))
4951 (if (and (string-match emacs-lisp-file-regexp file
)
4952 (not (auto-save-file-name-p file
))
4954 (expand-file-name file
4955 (car command-line-args-left
)))
4956 (setq dest
(byte-compile-dest-file source
))
4957 (file-exists-p dest
)
4958 (file-newer-than-file-p source dest
))
4959 (if (null (batch-byte-compile-file source
))
4961 ;; Specific file argument
4962 (if (or (not noforce
)
4963 (let* ((source (car command-line-args-left
))
4964 (dest (byte-compile-dest-file source
)))
4965 (or (not (file-exists-p dest
))
4966 (file-newer-than-file-p source dest
))))
4967 (if (null (batch-byte-compile-file (car command-line-args-left
)))
4969 (setq command-line-args-left
(cdr command-line-args-left
)))
4970 (kill-emacs (if error
1 0))))
4972 (defun batch-byte-compile-file (file)
4973 (let ((byte-compile-root-dir (or byte-compile-root-dir default-directory
)))
4975 (byte-compile-file file
)
4977 (byte-compile-file file
)
4979 (message (if (cdr err
)
4980 ">>Error occurred processing %s: %s (%s)"
4981 ">>Error occurred processing %s: %s")
4983 (get (car err
) 'error-message
)
4984 (prin1-to-string (cdr err
)))
4985 (let ((destfile (byte-compile-dest-file file
)))
4986 (if (file-exists-p destfile
)
4987 (delete-file destfile
)))
4990 (message (if (cdr err
)
4991 ">>Error occurred processing %s: %s (%s)"
4992 ">>Error occurred processing %s: %s")
4994 (get (car err
) 'error-message
)
4995 (prin1-to-string (cdr err
)))
4998 (defun byte-compile-refresh-preloaded ()
4999 "Reload any Lisp file that was changed since Emacs was dumped.
5001 (let* ((argv0 (car command-line-args
))
5002 (emacs-file (executable-find argv0
)))
5003 (if (not (and emacs-file
(file-executable-p emacs-file
)))
5004 (message "Can't find %s to refresh preloaded Lisp files" argv0
)
5005 (dolist (f (reverse load-history
))
5007 (if (string-match "elc\\'" f
) (setq f
(substring f
0 -
1)))
5008 (when (and (file-readable-p f
)
5009 (file-newer-than-file-p f emacs-file
)
5010 ;; Don't reload the source version of the files below
5011 ;; because that causes subsequent byte-compilation to
5012 ;; be a lot slower and need a higher max-lisp-eval-depth,
5013 ;; so it can cause recompilation to fail.
5014 (not (member (file-name-nondirectory f
)
5015 '("pcase.el" "bytecomp.el" "macroexp.el"
5016 "cconv.el" "byte-opt.el"))))
5017 (message "Reloading stale %s" (file-name-nondirectory f
))
5019 (load f
'noerror nil
'nosuffix
)
5020 ;; Probably shouldn't happen, but in case of an error, it seems
5021 ;; at least as useful to ignore it as it is to stop compilation.
5025 (defun batch-byte-recompile-directory (&optional arg
)
5026 "Run `byte-recompile-directory' on the dirs remaining on the command line.
5027 Must be used only with `-batch', and kills Emacs on completion.
5028 For example, invoke `emacs -batch -f batch-byte-recompile-directory .'.
5030 Optional argument ARG is passed as second argument ARG to
5031 `byte-recompile-directory'; see there for its possible values
5032 and corresponding effects."
5033 ;; command-line-args-left is what is left of the command line (startup.el)
5034 (defvar command-line-args-left
) ;Avoid 'free variable' warning
5035 (if (not noninteractive
)
5036 (error "batch-byte-recompile-directory is to be used only with -batch"))
5037 (or command-line-args-left
5038 (setq command-line-args-left
'(".")))
5039 (while command-line-args-left
5040 (byte-recompile-directory (car command-line-args-left
) arg
)
5041 (setq command-line-args-left
(cdr command-line-args-left
)))
5044 ;;; Core compiler macros.
5046 (put 'featurep
'compiler-macro
5047 (lambda (form feature
&rest _ignore
)
5048 ;; Emacs-21's byte-code doesn't run under XEmacs or SXEmacs anyway, so
5049 ;; we can safely optimize away this test.
5050 (if (member feature
'('xemacs
'sxemacs
'emacs
))
5054 (provide 'byte-compile
)
5058 ;;; report metering (see the hacks in bytecode.c)
5060 (defvar byte-code-meter
)
5061 (defun byte-compile-report-ops ()
5062 (or (boundp 'byte-metering-on
)
5063 (error "You must build Emacs with -DBYTE_CODE_METER to use this"))
5064 (with-output-to-temp-buffer "*Meter*"
5065 (set-buffer "*Meter*")
5066 (let ((i 0) n op off
)
5068 (setq n
(aref (aref byte-code-meter
0) i
)
5070 (if t
;(not (zerop n))
5074 (cond ((< op byte-nth
)
5075 (setq off
(logand op
7))
5076 (setq op
(logand op
248)))
5077 ((>= op byte-constant
)
5078 (setq off
(- op byte-constant
)
5080 (setq op
(aref byte-code-vector op
))
5081 (insert (format "%-4d" i
))
5082 (insert (symbol-name op
))
5083 (if off
(insert " [" (int-to-string off
) "]"))
5085 (insert (int-to-string n
) "\n")))
5088 ;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when bytecomp compiles
5089 ;; itself, compile some of its most used recursive functions (at load time).
5092 (or (byte-code-function-p (symbol-function 'byte-compile-form
))
5093 (assq 'byte-code
(symbol-function 'byte-compile-form
))
5094 (let ((byte-optimize nil
) ; do it fast
5095 (byte-compile-warnings nil
))
5097 (or noninteractive
(message "compiling %s..." x
))
5099 (or noninteractive
(message "compiling %s...done" x
)))
5100 '(byte-compile-normal-call
5103 ;; Inserted some more than necessary, to speed it up.
5104 byte-compile-top-level
5105 byte-compile-out-toplevel
5106 byte-compile-constant
5107 byte-compile-variable-ref
))))
5110 (run-hooks 'bytecomp-load-hook
)
5112 ;;; bytecomp.el ends here