1 ;;; bytecomp.el --- compilation of Lisp code into byte code.
3 ;; Copyright (C) 1985, 1986, 1987, 1992, 1994 Free Software Foundation, Inc.
5 ;; Author: Jamie Zawinski <jwz@lucid.com>
6 ;; Hallvard Furuseth <hbf@ulrik.uio.no>
10 ;;; This version incorporates changes up to version 2.10 of the
11 ;;; Zawinski-Furuseth compiler.
12 (defconst byte-compile-version "$Revision: 2.41 $")
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to the
28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 ;; Boston, MA 02111-1307, USA.
33 ;; The Emacs Lisp byte compiler. This crunches lisp source into a sort
34 ;; of p-code which takes up less space and can be interpreted faster.
35 ;; The user entry points are byte-compile-file and byte-recompile-directory.
39 ;; ========================================================================
41 ;; byte-recompile-directory, byte-compile-file,
42 ;; batch-byte-compile, batch-byte-recompile-directory,
43 ;; byte-compile, compile-defun,
45 ;; (byte-compile-buffer and byte-compile-and-load-file were turned off
46 ;; because they are not terribly useful and get in the way of completion.)
48 ;; This version of the byte compiler has the following improvements:
49 ;; + optimization of compiled code:
50 ;; - removal of unreachable code;
51 ;; - removal of calls to side-effectless functions whose return-value
53 ;; - compile-time evaluation of safe constant forms, such as (consp nil)
55 ;; - open-coding of literal lambdas;
56 ;; - peephole optimization of emitted code;
57 ;; - trivial functions are left uncompiled for speed.
58 ;; + support for inline functions;
59 ;; + compile-time evaluation of arbitrary expressions;
60 ;; + compile-time warning messages for:
61 ;; - functions being redefined with incompatible arglists;
62 ;; - functions being redefined as macros, or vice-versa;
63 ;; - functions or macros defined multiple times in the same file;
64 ;; - functions being called with the incorrect number of arguments;
65 ;; - functions being called which are not defined globally, in the
66 ;; file, or as autoloads;
67 ;; - assignment and reference of undeclared free variables;
68 ;; - various syntax errors;
69 ;; + correct compilation of nested defuns, defmacros, defvars and defsubsts;
70 ;; + correct compilation of top-level uses of macros;
71 ;; + the ability to generate a histogram of functions called.
73 ;; User customization variables:
75 ;; byte-compile-verbose Whether to report the function currently being
76 ;; compiled in the minibuffer;
77 ;; byte-optimize Whether to do optimizations; this may be
78 ;; t, nil, 'source, or 'byte;
79 ;; byte-optimize-log Whether to report (in excruciating detail)
80 ;; exactly which optimizations have been made.
81 ;; This may be t, nil, 'source, or 'byte;
82 ;; byte-compile-error-on-warn Whether to stop compilation when a warning is
84 ;; byte-compile-delete-errors Whether the optimizer may delete calls or
85 ;; variable references that are side-effect-free
86 ;; except that they may return an error.
87 ;; byte-compile-generate-call-tree Whether to generate a histogram of
88 ;; function calls. This can be useful for
89 ;; finding unused functions, as well as simple
90 ;; performance metering.
91 ;; byte-compile-warnings List of warnings to issue, or t. May contain
92 ;; 'free-vars (references to variables not in the
93 ;; current lexical scope)
94 ;; 'unresolved (calls to unknown functions)
95 ;; 'callargs (lambda calls with args that don't
96 ;; match the lambda's definition)
97 ;; 'redefine (function cell redefined from
98 ;; a macro to a lambda or vice versa,
99 ;; or redefined to take other args)
100 ;; 'obsolete (obsolete variables and functions)
101 ;; byte-compile-compatibility Whether the compiler should
102 ;; generate .elc files which can be loaded into
104 ;; emacs-lisp-file-regexp Regexp for the extension of source-files;
105 ;; see also the function byte-compile-dest-file.
109 ;; o The form `defsubst' is just like `defun', except that the function
110 ;; generated will be open-coded in compiled code which uses it. This
111 ;; means that no function call will be generated, it will simply be
112 ;; spliced in. Lisp functions calls are very slow, so this can be a
115 ;; You can generally accomplish the same thing with `defmacro', but in
116 ;; that case, the defined procedure can't be used as an argument to
119 ;; o You can also open-code one particular call to a function without
120 ;; open-coding all calls. Use the 'inline' form to do this, like so:
122 ;; (inline (foo 1 2 3)) ;; `foo' will be open-coded
124 ;; (inline ;; `foo' and `baz' will be
125 ;; (foo 1 2 3 (bar 5)) ;; open-coded, but `bar' will not.
128 ;; o It is possible to open-code a function in the same file it is defined
129 ;; in without having to load that file before compiling it. the
130 ;; byte-compiler has been modified to remember function definitions in
131 ;; the compilation environment in the same way that it remembers macro
134 ;; o Forms like ((lambda ...) ...) are open-coded.
136 ;; o The form `eval-when-compile' is like progn, except that the body
137 ;; is evaluated at compile-time. When it appears at top-level, this
138 ;; is analogous to the Common Lisp idiom (eval-when (compile) ...).
139 ;; When it does not appear at top-level, it is similar to the
140 ;; Common Lisp #. reader macro (but not in interpreted code).
142 ;; o The form `eval-and-compile' is similar to eval-when-compile, but
143 ;; the whole form is evalled both at compile-time and at run-time.
145 ;; o The command compile-defun is analogous to eval-defun.
147 ;; o If you run byte-compile-file on a filename which is visited in a
148 ;; buffer, and that buffer is modified, you are asked whether you want
149 ;; to save the buffer before compiling.
151 ;; o byte-compiled files now start with the string `;ELC'.
152 ;; Some versions of `file' can be customized to recognize that.
156 (or (fboundp 'defsubst)
157 ;; This really ought to be loaded already!
158 (load-library "byte-run"))
160 ;;; The feature of compiling in a specific target Emacs version
161 ;;; has been turned off because compile time options are a bad idea.
162 (defmacro byte-compile-single-version () nil)
163 (defmacro byte-compile-version-cond (cond) cond)
165 ;;; The crud you see scattered through this file of the form
166 ;;; (or (and (boundp 'epoch::version) epoch::version)
167 ;;; (string-lessp emacs-version "19"))
168 ;;; is because the Epoch folks couldn't be bothered to follow the
169 ;;; normal emacs version numbering convention.
171 ;; (if (byte-compile-version-cond
172 ;; (or (and (boundp 'epoch::version) epoch::version)
173 ;; (string-lessp emacs-version "19")))
175 ;; ;; emacs-18 compatibility.
176 ;; (defvar baud-rate (baud-rate)) ;Define baud-rate if it's undefined
178 ;; (if (byte-compile-single-version)
179 ;; (defmacro byte-code-function-p (x) "Emacs 18 doesn't have these." nil)
180 ;; (defun byte-code-function-p (x) "Emacs 18 doesn't have these." nil))
182 ;; (or (and (fboundp 'member)
183 ;; ;; avoid using someone else's possibly bogus definition of this.
184 ;; (subrp (symbol-function 'member)))
185 ;; (defun member (elt list)
186 ;; "like memq, but uses equal instead of eq. In v19, this is a subr."
187 ;; (while (and list (not (equal elt (car list))))
188 ;; (setq list (cdr list)))
192 (defgroup bytecomp nil
193 "Emacs Lisp byte-compiler"
196 (defcustom emacs-lisp-file-regexp (if (eq system-type 'vax-vms)
197 "\\.EL\\(;[0-9]+\\)?$"
199 "*Regexp which matches Emacs Lisp source files.
200 You may want to redefine the function `byte-compile-dest-file'
201 if you change this variable."
205 ;; This enables file name handlers such as jka-compr
206 ;; to remove parts of the file name that should not be copied
207 ;; through to the output file name.
208 (defun byte-compiler-base-file-name (filename)
209 (let ((handler (find-file-name-handler filename
210 'byte-compiler-base-file-name)))
212 (funcall handler 'byte-compiler-base-file-name filename)
215 (or (fboundp 'byte-compile-dest-file)
216 ;; The user may want to redefine this along with emacs-lisp-file-regexp,
217 ;; so only define it if it is undefined.
218 (defun byte-compile-dest-file (filename)
219 "Convert an Emacs Lisp source file name to a compiled file name."
220 (setq filename (byte-compiler-base-file-name filename))
221 (setq filename (file-name-sans-versions filename))
222 (cond ((eq system-type 'vax-vms)
223 (concat (substring filename 0 (string-match ";" filename)) "c"))
224 ((string-match emacs-lisp-file-regexp filename)
225 (concat (substring filename 0 (match-beginning 0)) ".elc"))
226 (t (concat filename ".elc")))))
228 ;; This can be the 'byte-compile property of any symbol.
229 (autoload 'byte-compile-inline-expand "byte-opt")
231 ;; This is the entrypoint to the lapcode optimizer pass1.
232 (autoload 'byte-optimize-form "byte-opt")
233 ;; This is the entrypoint to the lapcode optimizer pass2.
234 (autoload 'byte-optimize-lapcode "byte-opt")
235 (autoload 'byte-compile-unfold-lambda "byte-opt")
237 ;; This is the entry point to the decompiler, which is used by the
238 ;; disassembler. The disassembler just requires 'byte-compile, but
239 ;; that doesn't define this function, so this seems to be a reasonable
241 (autoload 'byte-decompile-bytecode "byte-opt")
243 (defcustom byte-compile-verbose
244 (and (not noninteractive) (> baud-rate search-slow-speed))
245 "*Non-nil means print messages describing progress of byte-compiler."
249 (defcustom byte-compile-compatibility nil
250 "*Non-nil means generate output that can run in Emacs 18."
254 ;; (defvar byte-compile-generate-emacs19-bytecodes
255 ;; (not (or (and (boundp 'epoch::version) epoch::version)
256 ;; (string-lessp emacs-version "19")))
257 ;; "*If this is true, then the byte-compiler will generate bytecode which
258 ;; makes use of byte-ops which are present only in Emacs 19. Code generated
259 ;; this way can never be run in Emacs 18, and may even cause it to crash.")
261 (defcustom byte-optimize t
262 "*Enables optimization in the byte compiler.
263 nil means don't do any optimization.
264 t means do all optimizations.
265 `source' means do source-level optimizations only.
266 `byte' means do code-level optimizations only."
268 :type '(choice (const :tag "none" nil)
270 (const :tag "source-level" source)
271 (const :tag "byte-level" byte)))
273 (defcustom byte-compile-delete-errors t
274 "*If non-nil, the optimizer may delete forms that may signal an error.
275 This includes variable references and calls to functions such as `car'."
279 (defvar byte-compile-dynamic nil
280 "If non-nil, compile function bodies so they load lazily.
281 They are hidden comments in the compiled file, and brought into core when the
284 To enable this option, make it a file-local variable
285 in the source file you want it to apply to.
286 For example, add -*-byte-compile-dynamic: t;-*- on the first line.
288 When this option is true, if you load the compiled file and then move it,
289 the functions you loaded will not be able to run.")
291 (defcustom byte-compile-dynamic-docstrings t
292 "*If non-nil, compile doc strings for lazy access.
293 We bury the doc strings of functions and variables
294 inside comments in the file, and bring them into core only when they
297 When this option is true, if you load the compiled file and then move it,
298 you won't be able to find the documentation of anything in that file.
300 To disable this option for a certain file, make it a file-local variable
301 in the source file. For example, add this to the first line:
302 -*-byte-compile-dynamic-docstrings:nil;-*-
303 You can also set the variable globally.
305 This option is enabled by default because it reduces Emacs memory usage."
309 (defcustom byte-optimize-log nil
310 "*If true, the byte-compiler will log its optimizations into *Compile-Log*.
311 If this is 'source, then only source-level optimizations will be logged.
312 If it is 'byte, then only byte-level optimizations will be logged."
314 :type '(choice (const :tag "none" nil)
316 (const :tag "source-level" source)
317 (const :tag "byte-level" byte)))
319 (defcustom byte-compile-error-on-warn nil
320 "*If true, the byte-compiler reports warnings with `error'."
324 (defconst byte-compile-warning-types
325 '(redefine callargs free-vars unresolved obsolete))
326 (defcustom byte-compile-warnings t
327 "*List of warnings that the byte-compiler should issue (t for all).
328 Elements of the list may be be:
330 free-vars references to variables not in the current lexical scope.
331 unresolved calls to unknown functions.
332 callargs lambda calls with args that don't match the definition.
333 redefine function cell redefined from a macro to a lambda or vice
334 versa, or redefined to take a different number of arguments.
335 obsolete obsolete variables and functions.
337 See also the macro `byte-compiler-options'."
339 :type '(choice (const :tag "All" t)
340 (set :menu-tag "Some"
341 (const free-vars) (const unresolved)
342 (const callargs) (const redefined)
345 (defcustom byte-compile-generate-call-tree nil
346 "*Non-nil means collect call-graph information when compiling.
347 This records functions were called and from where.
348 If the value is t, compilation displays the call graph when it finishes.
349 If the value is neither t nor nil, compilation asks you whether to display
352 The call tree only lists functions called, not macros used. Those functions
353 which the byte-code interpreter knows about directly (eq, cons, etc.) are
356 The call tree also lists those functions which are not known to be called
357 \(that is, to which no calls have been compiled). Functions which can be
358 invoked interactively are excluded from this list."
360 :type '(choice (const :tag "Yes" t) (const :tag "No" nil)
361 (const :tag "Ask" lambda)))
363 (defconst byte-compile-call-tree nil "Alist of functions and their call tree.
364 Each element looks like
366 \(FUNCTION CALLERS CALLS\)
368 where CALLERS is a list of functions that call FUNCTION, and CALLS
369 is a list of functions for which calls were generated while compiling
372 (defcustom byte-compile-call-tree-sort 'name
373 "*If non-nil, sort the call tree.
374 The values `name', `callers', `calls', `calls+callers'
375 specify different fields to sort on."
377 :type '(choice (const name) (const callers) (const calls)
378 (const calls+callers) (const nil)))
380 ;; (defvar byte-compile-overwrite-file t
381 ;; "If nil, old .elc files are deleted before the new is saved, and .elc
382 ;; files will have the same modes as the corresponding .el file. Otherwise,
383 ;; existing .elc files will simply be overwritten, and the existing modes
384 ;; will not be changed. If this variable is nil, then an .elc file which
385 ;; is a symbolic link will be turned into a normal file, instead of the file
386 ;; which the link points to being overwritten.")
388 (defvar byte-compile-constants nil
389 "list of all constants encountered during compilation of this form")
390 (defvar byte-compile-variables nil
391 "list of all variables encountered during compilation of this form")
392 (defvar byte-compile-bound-variables nil
393 "list of variables bound in the context of the current form; this list
394 lives partly on the stack.")
395 (defvar byte-compile-free-references)
396 (defvar byte-compile-free-assignments)
398 (defvar byte-compiler-error-flag)
400 (defconst byte-compile-initial-macro-environment
402 ;; (byte-compiler-options . (lambda (&rest forms)
403 ;; (apply 'byte-compiler-options-handler forms)))
404 (eval-when-compile . (lambda (&rest body)
405 (list 'quote (eval (byte-compile-top-level
406 (cons 'progn body))))))
407 (eval-and-compile . (lambda (&rest body)
408 (eval (cons 'progn body))
409 (cons 'progn body))))
410 "The default macro-environment passed to macroexpand by the compiler.
411 Placing a macro here will cause a macro to have different semantics when
412 expanded by the compiler as when expanded by the interpreter.")
414 (defvar byte-compile-macro-environment byte-compile-initial-macro-environment
415 "Alist of macros defined in the file being compiled.
416 Each element looks like (MACRONAME . DEFINITION). It is
417 \(MACRONAME . nil) when a macro is redefined as a function.")
419 (defvar byte-compile-function-environment nil
420 "Alist of functions defined in the file being compiled.
421 This is so we can inline them when necessary.
422 Each element looks like (FUNCTIONNAME . DEFINITION). It is
423 \(FUNCTIONNAME . nil) when a function is redefined as a macro.")
425 (defvar byte-compile-unresolved-functions nil
426 "Alist of undefined functions to which calls have been compiled (used for
427 warnings when the function is later defined with incorrect args).")
429 (defvar byte-compile-tag-number 0)
430 (defvar byte-compile-output nil
431 "Alist describing contents to put in byte code string.
432 Each element is (INDEX . VALUE)")
433 (defvar byte-compile-depth 0 "Current depth of execution stack.")
434 (defvar byte-compile-maxdepth 0 "Maximum depth of execution stack.")
437 ;;; The byte codes; this information is duplicated in bytecomp.c
439 (defconst byte-code-vector nil
440 "An array containing byte-code names indexed by byte-code values.")
442 (defconst byte-stack+-info nil
443 "An array with the stack adjustment for each byte-code.")
445 (defmacro byte-defop (opcode stack-adjust opname &optional docstring)
446 ;; This is a speed-hack for building the byte-code-vector at compile-time.
447 ;; We fill in the vector at macroexpand-time, and then after the last call
448 ;; to byte-defop, we write the vector out as a constant instead of writing
449 ;; out a bunch of calls to aset.
450 ;; Actually, we don't fill in the vector itself, because that could make
451 ;; it problematic to compile big changes to this compiler; we store the
452 ;; values on its plist, and remove them later in -extrude.
453 (let ((v1 (or (get 'byte-code-vector 'tmp-compile-time-value)
454 (put 'byte-code-vector 'tmp-compile-time-value
455 (make-vector 256 nil))))
456 (v2 (or (get 'byte-stack+-info 'tmp-compile-time-value)
457 (put 'byte-stack+-info 'tmp-compile-time-value
458 (make-vector 256 nil)))))
459 (aset v1 opcode opname)
460 (aset v2 opcode stack-adjust))
462 (list 'defconst opname opcode (concat "Byte code opcode " docstring "."))
463 (list 'defconst opname opcode)))
465 (defmacro byte-extrude-byte-code-vectors ()
466 (prog1 (list 'setq 'byte-code-vector
467 (get 'byte-code-vector 'tmp-compile-time-value)
469 (get 'byte-stack+-info 'tmp-compile-time-value))
470 ;; emacs-18 has no REMPROP.
471 (put 'byte-code-vector 'tmp-compile-time-value nil)
472 (put 'byte-stack+-info 'tmp-compile-time-value nil)))
477 ;; These opcodes are special in that they pack their argument into the
480 (byte-defop 8 1 byte-varref "for variable reference")
481 (byte-defop 16 -1 byte-varset "for setting a variable")
482 (byte-defop 24 -1 byte-varbind "for binding a variable")
483 (byte-defop 32 0 byte-call "for calling a function")
484 (byte-defop 40 0 byte-unbind "for unbinding special bindings")
485 ;; codes 8-47 are consumed by the preceding opcodes
489 (byte-defop 56 -1 byte-nth)
490 (byte-defop 57 0 byte-symbolp)
491 (byte-defop 58 0 byte-consp)
492 (byte-defop 59 0 byte-stringp)
493 (byte-defop 60 0 byte-listp)
494 (byte-defop 61 -1 byte-eq)
495 (byte-defop 62 -1 byte-memq)
496 (byte-defop 63 0 byte-not)
497 (byte-defop 64 0 byte-car)
498 (byte-defop 65 0 byte-cdr)
499 (byte-defop 66 -1 byte-cons)
500 (byte-defop 67 0 byte-list1)
501 (byte-defop 68 -1 byte-list2)
502 (byte-defop 69 -2 byte-list3)
503 (byte-defop 70 -3 byte-list4)
504 (byte-defop 71 0 byte-length)
505 (byte-defop 72 -1 byte-aref)
506 (byte-defop 73 -2 byte-aset)
507 (byte-defop 74 0 byte-symbol-value)
508 (byte-defop 75 0 byte-symbol-function) ; this was commented out
509 (byte-defop 76 -1 byte-set)
510 (byte-defop 77 -1 byte-fset) ; this was commented out
511 (byte-defop 78 -1 byte-get)
512 (byte-defop 79 -2 byte-substring)
513 (byte-defop 80 -1 byte-concat2)
514 (byte-defop 81 -2 byte-concat3)
515 (byte-defop 82 -3 byte-concat4)
516 (byte-defop 83 0 byte-sub1)
517 (byte-defop 84 0 byte-add1)
518 (byte-defop 85 -1 byte-eqlsign)
519 (byte-defop 86 -1 byte-gtr)
520 (byte-defop 87 -1 byte-lss)
521 (byte-defop 88 -1 byte-leq)
522 (byte-defop 89 -1 byte-geq)
523 (byte-defop 90 -1 byte-diff)
524 (byte-defop 91 0 byte-negate)
525 (byte-defop 92 -1 byte-plus)
526 (byte-defop 93 -1 byte-max)
527 (byte-defop 94 -1 byte-min)
528 (byte-defop 95 -1 byte-mult) ; v19 only
529 (byte-defop 96 1 byte-point)
530 (byte-defop 98 0 byte-goto-char)
531 (byte-defop 99 0 byte-insert)
532 (byte-defop 100 1 byte-point-max)
533 (byte-defop 101 1 byte-point-min)
534 (byte-defop 102 0 byte-char-after)
535 (byte-defop 103 1 byte-following-char)
536 (byte-defop 104 1 byte-preceding-char)
537 (byte-defop 105 1 byte-current-column)
538 (byte-defop 106 0 byte-indent-to)
539 (byte-defop 107 0 byte-scan-buffer-OBSOLETE) ; no longer generated as of v18
540 (byte-defop 108 1 byte-eolp)
541 (byte-defop 109 1 byte-eobp)
542 (byte-defop 110 1 byte-bolp)
543 (byte-defop 111 1 byte-bobp)
544 (byte-defop 112 1 byte-current-buffer)
545 (byte-defop 113 0 byte-set-buffer)
546 (byte-defop 114 0 byte-save-current-buffer
547 "To make a binding to record the current buffer")
548 (byte-defop 115 0 byte-set-mark-OBSOLETE)
549 (byte-defop 116 1 byte-interactive-p)
551 ;; These ops are new to v19
552 (byte-defop 117 0 byte-forward-char)
553 (byte-defop 118 0 byte-forward-word)
554 (byte-defop 119 -1 byte-skip-chars-forward)
555 (byte-defop 120 -1 byte-skip-chars-backward)
556 (byte-defop 121 0 byte-forward-line)
557 (byte-defop 122 0 byte-char-syntax)
558 (byte-defop 123 -1 byte-buffer-substring)
559 (byte-defop 124 -1 byte-delete-region)
560 (byte-defop 125 -1 byte-narrow-to-region)
561 (byte-defop 126 1 byte-widen)
562 (byte-defop 127 0 byte-end-of-line)
566 ;; These store their argument in the next two bytes
567 (byte-defop 129 1 byte-constant2
568 "for reference to a constant with vector index >= byte-constant-limit")
569 (byte-defop 130 0 byte-goto "for unconditional jump")
570 (byte-defop 131 -1 byte-goto-if-nil "to pop value and jump if it's nil")
571 (byte-defop 132 -1 byte-goto-if-not-nil "to pop value and jump if it's not nil")
572 (byte-defop 133 -1 byte-goto-if-nil-else-pop
573 "to examine top-of-stack, jump and don't pop it if it's nil,
575 (byte-defop 134 -1 byte-goto-if-not-nil-else-pop
576 "to examine top-of-stack, jump and don't pop it if it's non nil,
579 (byte-defop 135 -1 byte-return "to pop a value and return it from `byte-code'")
580 (byte-defop 136 -1 byte-discard "to discard one value from stack")
581 (byte-defop 137 1 byte-dup "to duplicate the top of the stack")
583 (byte-defop 138 0 byte-save-excursion
584 "to make a binding to record the buffer, point and mark")
585 (byte-defop 139 0 byte-save-window-excursion
586 "to make a binding to record entire window configuration")
587 (byte-defop 140 0 byte-save-restriction
588 "to make a binding to record the current buffer clipping restrictions")
589 (byte-defop 141 -1 byte-catch
590 "for catch. Takes, on stack, the tag and an expression for the body")
591 (byte-defop 142 -1 byte-unwind-protect
592 "for unwind-protect. Takes, on stack, an expression for the unwind-action")
594 ;; For condition-case. Takes, on stack, the variable to bind,
595 ;; an expression for the body, and a list of clauses.
596 (byte-defop 143 -2 byte-condition-case)
598 ;; For entry to with-output-to-temp-buffer.
599 ;; Takes, on stack, the buffer name.
600 ;; Binds standard-output and does some other things.
601 ;; Returns with temp buffer on the stack in place of buffer name.
602 (byte-defop 144 0 byte-temp-output-buffer-setup)
604 ;; For exit from with-output-to-temp-buffer.
605 ;; Expects the temp buffer on the stack underneath value to return.
606 ;; Pops them both, then pushes the value back on.
607 ;; Unbinds standard-output and makes the temp buffer visible.
608 (byte-defop 145 -1 byte-temp-output-buffer-show)
610 ;; these ops are new to v19
612 ;; To unbind back to the beginning of this frame.
613 ;; Not used yet, but will be needed for tail-recursion elimination.
614 (byte-defop 146 0 byte-unbind-all)
616 ;; these ops are new to v19
617 (byte-defop 147 -2 byte-set-marker)
618 (byte-defop 148 0 byte-match-beginning)
619 (byte-defop 149 0 byte-match-end)
620 (byte-defop 150 0 byte-upcase)
621 (byte-defop 151 0 byte-downcase)
622 (byte-defop 152 -1 byte-string=)
623 (byte-defop 153 -1 byte-string<)
624 (byte-defop 154 -1 byte-equal)
625 (byte-defop 155 -1 byte-nthcdr)
626 (byte-defop 156 -1 byte-elt)
627 (byte-defop 157 -1 byte-member)
628 (byte-defop 158 -1 byte-assq)
629 (byte-defop 159 0 byte-nreverse)
630 (byte-defop 160 -1 byte-setcar)
631 (byte-defop 161 -1 byte-setcdr)
632 (byte-defop 162 0 byte-car-safe)
633 (byte-defop 163 0 byte-cdr-safe)
634 (byte-defop 164 -1 byte-nconc)
635 (byte-defop 165 -1 byte-quo)
636 (byte-defop 166 -1 byte-rem)
637 (byte-defop 167 0 byte-numberp)
638 (byte-defop 168 0 byte-integerp)
641 (byte-defop 175 nil byte-listN)
642 (byte-defop 176 nil byte-concatN)
643 (byte-defop 177 nil byte-insertN)
647 (byte-defop 192 1 byte-constant "for reference to a constant")
648 ;; codes 193-255 are consumed by byte-constant.
649 (defconst byte-constant-limit 64
650 "Exclusive maximum index usable in the `byte-constant' opcode.")
652 (defconst byte-goto-ops '(byte-goto byte-goto-if-nil byte-goto-if-not-nil
653 byte-goto-if-nil-else-pop
654 byte-goto-if-not-nil-else-pop)
655 "List of byte-codes whose offset is a pc.")
657 (defconst byte-goto-always-pop-ops '(byte-goto-if-nil byte-goto-if-not-nil))
659 (byte-extrude-byte-code-vectors)
661 ;;; lapcode generator
663 ;;; the byte-compiler now does source -> lapcode -> bytecode instead of
664 ;;; source -> bytecode, because it's a lot easier to make optimizations
665 ;;; on lapcode than on bytecode.
667 ;;; Elements of the lapcode list are of the form (<instruction> . <parameter>)
668 ;;; where instruction is a symbol naming a byte-code instruction,
669 ;;; and parameter is an argument to that instruction, if any.
671 ;;; The instruction can be the pseudo-op TAG, which means that this position
672 ;;; in the instruction stream is a target of a goto. (car PARAMETER) will be
673 ;;; the PC for this location, and the whole instruction "(TAG pc)" will be the
674 ;;; parameter for some goto op.
676 ;;; If the operation is varbind, varref, varset or push-constant, then the
677 ;;; parameter is (variable/constant . index_in_constant_vector).
679 ;;; First, the source code is macroexpanded and optimized in various ways.
680 ;;; Then the resultant code is compiled into lapcode. Another set of
681 ;;; optimizations are then run over the lapcode. Then the variables and
682 ;;; constants referenced by the lapcode are collected and placed in the
683 ;;; constants-vector. (This happens now so that variables referenced by dead
684 ;;; code don't consume space.) And finally, the lapcode is transformed into
685 ;;; compacted byte-code.
687 ;;; A distinction is made between variables and constants because the variable-
688 ;;; referencing instructions are more sensitive to the variables being near the
689 ;;; front of the constants-vector than the constant-referencing instructions.
690 ;;; Also, this lets us notice references to free variables.
692 (defun byte-compile-lapcode (lap)
693 "Turns lapcode into bytecode. The lapcode is destroyed."
694 ;; Lapcode modifications: changes the ID of a tag to be the tag's PC.
695 (let ((pc 0) ; Program counter
696 op off ; Operation & offset
697 (bytes '()) ; Put the output bytes here
698 (patchlist nil) ; List of tags and goto's to patch
701 (setq op (car (car lap))
703 (cond ((not (symbolp op))
704 (error "Non-symbolic opcode `%s'" op))
707 (setq patchlist (cons off patchlist)))
708 ((memq op byte-goto-ops)
710 (setq bytes (cons (cons pc (cdr off))
712 (cons (symbol-value op) bytes))))
713 (setq patchlist (cons bytes patchlist)))
716 (cond ((cond ((consp off)
717 ;; Variable or constant reference
719 (eq op 'byte-constant)))
720 (cond ((< off byte-constant-limit)
722 (cons (+ byte-constant off) bytes))
726 (cons (logand off 255)
727 (cons byte-constant2 bytes))))))
728 ((<= byte-listN (symbol-value op))
730 (cons off (cons (symbol-value op) bytes)))
733 (cons (+ (symbol-value op) off) bytes))
736 (cons off (cons (+ (symbol-value op) 6) bytes)))
740 (cons (logand off 255)
741 (cons (+ (symbol-value op) 7)
743 (setq lap (cdr lap)))
744 ;;(if (not (= pc (length bytes)))
745 ;; (error "Compiler error: pc mismatch - %s %s" pc (length bytes)))
746 ;; Patch PC into jumps
749 (setq bytes (car patchlist))
750 (cond ((atom (car bytes))) ; Tag
752 (setq pc (car (cdr (car bytes)))) ; Pick PC from tag
753 (setcar (cdr bytes) (logand pc 255))
754 (setcar bytes (lsh pc -8))))
755 (setq patchlist (cdr patchlist))))
756 (concat (nreverse bytes))))
759 ;;; byte compiler messages
761 (defvar byte-compile-current-form nil)
762 (defvar byte-compile-current-file nil)
763 (defvar byte-compile-dest-file nil)
765 (defmacro byte-compile-log (format-string &rest args)
768 '(memq byte-optimize-log '(t source))
769 (list 'let '((print-escape-newlines t)
772 (list 'byte-compile-log-1
777 (if (symbolp x) (list 'prin1-to-string x) x))
780 (defconst byte-compile-last-warned-form nil)
782 ;; Log a message STRING in *Compile-Log*.
783 ;; Also log the current function and file if not already done.
784 (defun byte-compile-log-1 (string &optional fill)
785 (cond (noninteractive
786 (if (or byte-compile-current-file
787 (and byte-compile-last-warned-form
788 (not (eq byte-compile-current-form
789 byte-compile-last-warned-form))))
790 (message "While compiling %s%s:"
791 (or byte-compile-current-form "toplevel forms")
792 (if byte-compile-current-file
793 (if (stringp byte-compile-current-file)
794 (concat " in file " byte-compile-current-file)
795 (concat " in buffer "
796 (buffer-name byte-compile-current-file)))
798 (message " %s" string))
801 (set-buffer (get-buffer-create "*Compile-Log*"))
802 (goto-char (point-max))
803 (cond ((or byte-compile-current-file
804 (and byte-compile-last-warned-form
805 (not (eq byte-compile-current-form
806 byte-compile-last-warned-form))))
807 (if byte-compile-current-file
808 (insert "\n\^L\n" (current-time-string) "\n"))
809 (insert "While compiling "
810 (if byte-compile-current-form
811 (format "%s" byte-compile-current-form)
813 (if byte-compile-current-file
814 (if (stringp byte-compile-current-file)
815 (insert " in file " byte-compile-current-file)
816 (insert " in buffer "
817 (buffer-name byte-compile-current-file))))
819 (insert " " string "\n")
820 (if (and fill (not (string-match "\n" string)))
821 (let ((fill-prefix " ")
823 (fill-paragraph nil)))
825 (setq byte-compile-current-file nil
826 byte-compile-last-warned-form byte-compile-current-form))
828 ;; Log the start of a file in *Compile-Log*, and mark it as done.
829 ;; But do nothing in batch mode.
830 (defun byte-compile-log-file ()
831 (and byte-compile-current-file (not noninteractive)
833 (set-buffer (get-buffer-create "*Compile-Log*"))
834 (goto-char (point-max))
835 (insert "\n\^L\nCompiling "
836 (if (stringp byte-compile-current-file)
837 (concat "file " byte-compile-current-file)
838 (concat "buffer " (buffer-name byte-compile-current-file)))
839 " at " (current-time-string) "\n")
840 (setq byte-compile-current-file nil))))
842 (defun byte-compile-warn (format &rest args)
843 (setq format (apply 'format format args))
844 (if byte-compile-error-on-warn
845 (error "%s" format) ; byte-compile-file catches and logs it
846 (byte-compile-log-1 (concat "** " format) t)
847 ;;; It is useless to flash warnings too fast to be read.
848 ;;; Besides, they will all be shown at the end.
849 ;;; (or noninteractive ; already written on stdout.
850 ;;; (message "Warning: %s" format))
853 ;;; This function should be used to report errors that have halted
854 ;;; compilation of the current file.
855 (defun byte-compile-report-error (error-info)
856 (setq byte-compiler-error-flag t)
859 (format (if (cdr error-info) "%s (%s)" "%s")
860 (get (car error-info) 'error-message)
861 (prin1-to-string (cdr error-info))))))
863 ;;; Used by make-obsolete.
864 (defun byte-compile-obsolete (form)
865 (let ((new (get (car form) 'byte-obsolete-info)))
866 (if (memq 'obsolete byte-compile-warnings)
867 (byte-compile-warn "%s is an obsolete function; %s" (car form)
868 (if (stringp (car new))
870 (format "use %s instead." (car new)))))
871 (funcall (or (cdr new) 'byte-compile-normal-call) form)))
875 ;; (defvar byte-compiler-valid-options
876 ;; '((optimize byte-optimize (t nil source byte) val)
877 ;; (file-format byte-compile-compatibility (emacs18 emacs19)
878 ;; (eq val 'emacs18))
879 ;; ;; (new-bytecodes byte-compile-generate-emacs19-bytecodes (t nil) val)
880 ;; (delete-errors byte-compile-delete-errors (t nil) val)
881 ;; (verbose byte-compile-verbose (t nil) val)
882 ;; (warnings byte-compile-warnings ((callargs redefine free-vars unresolved))
885 ;; Inhibit v18/v19 selectors if the version is hardcoded.
886 ;; #### This should print a warning if the user tries to change something
887 ;; than can't be changed because the running compiler doesn't support it.
889 ;; ((byte-compile-single-version)
890 ;; (setcar (cdr (cdr (assq 'new-bytecodes byte-compiler-valid-options)))
891 ;; (list (byte-compile-version-cond
892 ;; byte-compile-generate-emacs19-bytecodes)))
893 ;; (setcar (cdr (cdr (assq 'file-format byte-compiler-valid-options)))
894 ;; (if (byte-compile-version-cond byte-compile-compatibility)
895 ;; '(emacs18) '(emacs19)))))
897 ;; (defun byte-compiler-options-handler (&rest args)
898 ;; (let (key val desc choices)
900 ;; (if (or (atom (car args)) (nthcdr 2 (car args)) (null (cdr (car args))))
901 ;; (error "Malformed byte-compiler option `%s'" (car args)))
902 ;; (setq key (car (car args))
903 ;; val (car (cdr (car args)))
904 ;; desc (assq key byte-compiler-valid-options))
906 ;; (error "Unknown byte-compiler option `%s'" key))
907 ;; (setq choices (nth 2 desc))
908 ;; (if (consp (car choices))
911 ;; (ret (and (memq (car val) '(+ -))
912 ;; (copy-sequence (if (eq t (symbol-value (nth 1 desc)))
914 ;; (symbol-value (nth 1 desc)))))))
915 ;; (setq choices (car choices))
917 ;; (setq this (car val))
918 ;; (cond ((memq this choices)
919 ;; (setq ret (funcall handler this ret)))
920 ;; ((eq this '+) (setq handler 'cons))
921 ;; ((eq this '-) (setq handler 'delq))
922 ;; ((error "`%s' only accepts %s" key choices)))
923 ;; (setq val (cdr val)))
924 ;; (set (nth 1 desc) ret))
925 ;; (or (memq val choices)
926 ;; (error "`%s' must be one of `%s'" key choices))
927 ;; (set (nth 1 desc) (eval (nth 3 desc))))
928 ;; (setq args (cdr args)))
931 ;;; sanity-checking arglists
933 (defun byte-compile-fdefinition (name macro-p)
934 (let* ((list (if macro-p
935 byte-compile-macro-environment
936 byte-compile-function-environment))
937 (env (cdr (assq name list))))
940 (while (and (symbolp fn)
942 (or (symbolp (symbol-function fn))
943 (consp (symbol-function fn))
945 (byte-code-function-p (symbol-function fn)))))
946 (setq fn (symbol-function fn)))
947 (if (and (not macro-p) (byte-code-function-p fn))
950 (if (eq 'macro (car fn))
954 (if (eq 'autoload (car fn))
958 (defun byte-compile-arglist-signature (arglist)
963 (cond ((eq (car arglist) '&optional)
964 (or opts (setq opts 0)))
965 ((eq (car arglist) '&rest)
971 (setq opts (1+ opts))
972 (setq args (1+ args)))))
973 (setq arglist (cdr arglist)))
974 (cons args (if restp nil (if opts (+ args opts) args)))))
977 (defun byte-compile-arglist-signatures-congruent-p (old new)
979 (> (car new) (car old)) ; requires more args now
980 (and (null (cdr old)) ; took rest-args, doesn't any more
982 (and (cdr new) (cdr old) ; can't take as many args now
983 (< (cdr new) (cdr old)))
986 (defun byte-compile-arglist-signature-string (signature)
987 (cond ((null (cdr signature))
988 (format "%d+" (car signature)))
989 ((= (car signature) (cdr signature))
990 (format "%d" (car signature)))
991 (t (format "%d-%d" (car signature) (cdr signature)))))
994 ;; Warn if the form is calling a function with the wrong number of arguments.
995 (defun byte-compile-callargs-warn (form)
996 (let* ((def (or (byte-compile-fdefinition (car form) nil)
997 (byte-compile-fdefinition (car form) t)))
998 (sig (and def (byte-compile-arglist-signature
999 (if (eq 'lambda (car-safe def))
1001 (if (byte-code-function-p def)
1004 (ncall (length (cdr form))))
1006 (if (or (< ncall (car sig))
1007 (and (cdr sig) (> ncall (cdr sig))))
1009 "%s called with %d argument%s, but %s %s"
1011 (if (= 1 ncall) "" "s")
1012 (if (< ncall (car sig))
1015 (byte-compile-arglist-signature-string sig)))
1016 (or (fboundp (car form)) ; might be a subr or autoload.
1017 (eq (car form) byte-compile-current-form) ; ## this doesn't work with recursion.
1018 ;; It's a currently-undefined function. Remember number of args in call.
1019 (let ((cons (assq (car form) byte-compile-unresolved-functions))
1020 (n (length (cdr form))))
1022 (or (memq n (cdr cons))
1023 (setcdr cons (cons n (cdr cons))))
1024 (setq byte-compile-unresolved-functions
1025 (cons (list (car form) n)
1026 byte-compile-unresolved-functions))))))))
1028 ;; Warn if the function or macro is being redefined with a different
1029 ;; number of arguments.
1030 (defun byte-compile-arglist-warn (form macrop)
1031 (let ((old (byte-compile-fdefinition (nth 1 form) macrop)))
1033 (let ((sig1 (byte-compile-arglist-signature
1034 (if (eq 'lambda (car-safe old))
1036 (if (byte-code-function-p old)
1039 (sig2 (byte-compile-arglist-signature (nth 2 form))))
1040 (or (byte-compile-arglist-signatures-congruent-p sig1 sig2)
1041 (byte-compile-warn "%s %s used to take %s %s, now takes %s"
1042 (if (eq (car form) 'defun) "function" "macro")
1044 (byte-compile-arglist-signature-string sig1)
1045 (if (equal sig1 '(1 . 1)) "argument" "arguments")
1046 (byte-compile-arglist-signature-string sig2))))
1047 ;; This is the first definition. See if previous calls are compatible.
1048 (let ((calls (assq (nth 1 form) byte-compile-unresolved-functions))
1052 (setq sig (byte-compile-arglist-signature (nth 2 form))
1053 nums (sort (copy-sequence (cdr calls)) (function <))
1055 max (car (nreverse nums)))
1056 (if (or (< min (car sig))
1057 (and (cdr sig) (> max (cdr sig))))
1059 "%s being defined to take %s%s, but was previously called with %s"
1061 (byte-compile-arglist-signature-string sig)
1062 (if (equal sig '(1 . 1)) " arg" " args")
1063 (byte-compile-arglist-signature-string (cons min max))))
1065 (setq byte-compile-unresolved-functions
1066 (delq calls byte-compile-unresolved-functions)))))
1069 ;; If we have compiled any calls to functions which are not known to be
1070 ;; defined, issue a warning enumerating them.
1071 ;; `unresolved' in the list `byte-compile-warnings' disables this.
1072 (defun byte-compile-warn-about-unresolved-functions ()
1073 (if (memq 'unresolved byte-compile-warnings)
1074 (let ((byte-compile-current-form "the end of the data"))
1075 (if (cdr byte-compile-unresolved-functions)
1076 (let* ((str "The following functions are not known to be defined:")
1078 (rest (reverse byte-compile-unresolved-functions))
1081 (setq s (symbol-name (car (car rest)))
1082 L (+ L (length s) 2)
1084 (if (< L (1- fill-column))
1085 (setq str (concat str " " s (and rest ",")))
1086 (setq str (concat str "\n " s (and rest ","))
1087 L (+ (length s) 4))))
1088 (byte-compile-warn "%s" str))
1089 (if byte-compile-unresolved-functions
1090 (byte-compile-warn "the function %s is not known to be defined."
1091 (car (car byte-compile-unresolved-functions)))))))
1095 (defmacro byte-compile-constp (form)
1096 ;; Returns non-nil if FORM is a constant.
1097 (` (cond ((consp (, form)) (eq (car (, form)) 'quote))
1098 ((not (symbolp (, form))))
1099 ((memq (, form) '(nil t))))))
1101 (defmacro byte-compile-close-variables (&rest body)
1104 ;; Close over these variables to encapsulate the
1105 ;; compilation state
1107 (byte-compile-macro-environment
1108 ;; Copy it because the compiler may patch into the
1109 ;; macroenvironment.
1110 (copy-alist byte-compile-initial-macro-environment))
1111 (byte-compile-function-environment nil)
1112 (byte-compile-bound-variables nil)
1113 (byte-compile-free-references nil)
1114 (byte-compile-free-assignments nil)
1116 ;; Close over these variables so that `byte-compiler-options'
1117 ;; can change them on a per-file basis.
1119 (byte-compile-verbose byte-compile-verbose)
1120 (byte-optimize byte-optimize)
1121 (byte-compile-compatibility byte-compile-compatibility)
1122 (byte-compile-dynamic byte-compile-dynamic)
1123 (byte-compile-dynamic-docstrings
1124 byte-compile-dynamic-docstrings)
1125 ;; (byte-compile-generate-emacs19-bytecodes
1126 ;; byte-compile-generate-emacs19-bytecodes)
1127 (byte-compile-warnings (if (eq byte-compile-warnings t)
1128 byte-compile-warning-types
1129 byte-compile-warnings))
1133 (defvar byte-compile-warnings-point-max nil)
1134 (defmacro displaying-byte-compile-warnings (&rest body)
1136 '((byte-compile-warnings-point-max byte-compile-warnings-point-max))
1137 ;; Log the file name.
1138 '(byte-compile-log-file)
1139 ;; Record how much is logged now.
1140 ;; We will display the log buffer if anything more is logged
1141 ;; before the end of BODY.
1142 '(or byte-compile-warnings-point-max
1144 (set-buffer (get-buffer-create "*Compile-Log*"))
1145 (setq byte-compile-warnings-point-max (point-max))))
1146 (list 'unwind-protect
1147 (list 'condition-case 'error-info
1150 (byte-compile-report-error error-info)))
1152 ;; If there were compilation warnings, display them.
1153 (set-buffer "*Compile-Log*")
1154 (if (= byte-compile-warnings-point-max (point-max))
1157 (prog1 (selected-window)
1158 (select-window (display-buffer (current-buffer)))
1159 (goto-char byte-compile-warnings-point-max)
1166 (defun byte-force-recompile (directory)
1167 "Recompile every `.el' file in DIRECTORY that already has a `.elc' file.
1168 Files in subdirectories of DIRECTORY are processed also."
1169 (interactive "DByte force recompile (directory): ")
1170 (byte-recompile-directory directory nil t))
1173 (defun byte-recompile-directory (directory &optional arg force)
1174 "Recompile every `.el' file in DIRECTORY that needs recompilation.
1175 This is if a `.elc' file exists but is older than the `.el' file.
1176 Files in subdirectories of DIRECTORY are processed also.
1178 If the `.elc' file does not exist, normally the `.el' file is *not* compiled.
1179 But a prefix argument (optional second arg) means ask user,
1180 for each such `.el' file, whether to compile it. Prefix argument 0 means
1181 don't ask and compile the file anyway.
1183 A nonzero prefix argument also means ask about each subdirectory.
1185 If the third argument FORCE is non-nil,
1186 recompile every `.el' file that already has a `.elc' file."
1187 (interactive "DByte recompile directory: \nP")
1189 (setq arg (prefix-numeric-value arg)))
1193 (force-mode-line-update))
1194 (let ((directories (list (expand-file-name directory)))
1198 (displaying-byte-compile-warnings
1200 (setq directory (car directories))
1201 (or noninteractive (message "Checking %s..." directory))
1202 (let ((files (directory-files directory))
1205 (setq source (expand-file-name (car files) directory))
1206 (if (and (not (member (car files) '("." ".." "RCS" "CVS")))
1207 (file-directory-p source)
1208 (not (file-symlink-p source)))
1209 ;; This file is a subdirectory. Handle them differently.
1212 (y-or-n-p (concat "Check " source "? ")))
1214 (nconc directories (list source))))
1215 ;; It is an ordinary file. Decide whether to compile it.
1216 (if (and (string-match emacs-lisp-file-regexp source)
1217 (not (auto-save-file-name-p source))
1218 (setq dest (byte-compile-dest-file source))
1219 (if (file-exists-p dest)
1220 ;; File was already compiled.
1221 (or force (file-newer-than-file-p source dest))
1222 ;; No compiled file exists yet.
1225 (y-or-n-p (concat "Compile " source "? "))))))
1226 (progn (if (and noninteractive (not byte-compile-verbose))
1227 (message "Compiling %s..." source))
1228 (byte-compile-file source)
1230 (message "Checking %s..." directory))
1231 (setq file-count (1+ file-count))
1232 (if (not (eq last-dir directory))
1233 (setq last-dir directory
1234 dir-count (1+ dir-count)))
1236 (setq files (cdr files))))
1237 (setq directories (cdr directories))))
1238 (message "Done (Total of %d file%s compiled%s)"
1239 file-count (if (= file-count 1) "" "s")
1240 (if (> dir-count 1) (format " in %d directories" dir-count) ""))))
1243 (defun byte-compile-file (filename &optional load)
1244 "Compile a file of Lisp code named FILENAME into a file of byte code.
1245 The output file's name is made by appending `c' to the end of FILENAME.
1246 With prefix arg (noninteractively: 2nd arg), load the file after compiling.
1247 The value is t if there were no errors, nil if errors."
1248 ;; (interactive "fByte compile file: \nP")
1250 (let ((file buffer-file-name)
1254 (eq (cdr (assq 'major-mode (buffer-local-variables)))
1256 (setq file-name (file-name-nondirectory file)
1257 file-dir (file-name-directory file)))
1258 (list (read-file-name (if current-prefix-arg
1259 "Byte compile and load file: "
1260 "Byte compile file: ")
1261 file-dir file-name nil)
1262 current-prefix-arg)))
1263 ;; Expand now so we get the current buffer's defaults
1264 (setq filename (expand-file-name filename))
1266 ;; If we're compiling a file that's in a buffer and is modified, offer
1267 ;; to save it first.
1269 (let ((b (get-file-buffer (expand-file-name filename))))
1270 (if (and b (buffer-modified-p b)
1271 (y-or-n-p (format "save buffer %s first? " (buffer-name b))))
1272 (save-excursion (set-buffer b) (save-buffer)))))
1274 (if byte-compile-verbose
1275 (message "Compiling %s..." filename))
1276 (let ((byte-compile-current-file filename)
1277 target-file input-buffer output-buffer
1278 byte-compile-dest-file)
1279 (setq target-file (byte-compile-dest-file filename))
1280 (setq byte-compile-dest-file target-file)
1282 (setq input-buffer (get-buffer-create " *Compiler Input*"))
1283 (set-buffer input-buffer)
1285 ;; Always compile an Emacs Lisp file as multibyte
1286 ;; unless the file itself forces unibyte with -*-coding: raw-text;-*-x
1287 (set-buffer-multibyte t)
1288 (insert-file-contents filename)
1289 ;; Run hooks including the uncompression hook.
1290 ;; If they change the file name, then change it for the output also.
1291 (let ((buffer-file-name filename)
1292 (default-major-mode 'emacs-lisp-mode)
1293 (enable-local-eval nil))
1295 (setq filename buffer-file-name))
1296 ;; Set the default directory, in case an eval-when-compile uses it.
1297 (setq default-directory (file-name-directory filename)))
1298 (setq byte-compiler-error-flag nil)
1299 ;; It is important that input-buffer not be current at this call,
1300 ;; so that the value of point set in input-buffer
1301 ;; within byte-compile-from-buffer lingers in that buffer.
1302 (setq output-buffer (byte-compile-from-buffer input-buffer filename))
1303 (if byte-compiler-error-flag
1305 (if byte-compile-verbose
1306 (message "Compiling %s...done" filename))
1307 (kill-buffer input-buffer)
1309 (set-buffer output-buffer)
1310 (goto-char (point-max))
1311 (insert "\n") ; aaah, unix.
1312 (let ((vms-stmlf-recfm t))
1313 (if (file-writable-p target-file)
1314 ;; We must disable any code conversion here.
1315 (let ((coding-system-for-write 'no-conversion))
1316 (if (or (eq system-type 'ms-dos) (eq system-type 'windows-nt))
1317 (setq buffer-file-type t))
1318 (write-region 1 (point-max) target-file))
1319 ;; This is just to give a better error message than
1322 (list "Opening output file"
1323 (if (file-exists-p target-file)
1324 "cannot overwrite file"
1325 "directory not writable or nonexistent")
1327 (kill-buffer (current-buffer)))
1328 (if (and byte-compile-generate-call-tree
1329 (or (eq t byte-compile-generate-call-tree)
1330 (y-or-n-p (format "Report call tree for %s? " filename))))
1332 (display-call-tree filename)))
1337 ;;(defun byte-compile-and-load-file (&optional filename)
1338 ;; "Compile a file of Lisp code named FILENAME into a file of byte code,
1339 ;;and then load it. The output file's name is made by appending \"c\" to
1340 ;;the end of FILENAME."
1342 ;; (if filename ; I don't get it, (interactive-p) doesn't always work
1343 ;; (byte-compile-file filename t)
1344 ;; (let ((current-prefix-arg '(4)))
1345 ;; (call-interactively 'byte-compile-file))))
1347 ;;(defun byte-compile-buffer (&optional buffer)
1348 ;; "Byte-compile and evaluate contents of BUFFER (default: the current buffer)."
1349 ;; (interactive "bByte compile buffer: ")
1350 ;; (setq buffer (if buffer (get-buffer buffer) (current-buffer)))
1351 ;; (message "Compiling %s..." (buffer-name buffer))
1352 ;; (let* ((filename (or (buffer-file-name buffer)
1353 ;; (concat "#<buffer " (buffer-name buffer) ">")))
1354 ;; (byte-compile-current-file buffer))
1355 ;; (byte-compile-from-buffer buffer nil))
1356 ;; (message "Compiling %s...done" (buffer-name buffer))
1359 ;;; compiling a single function
1361 (defun compile-defun (&optional arg)
1362 "Compile and evaluate the current top-level form.
1363 Print the result in the minibuffer.
1364 With argument, insert value in current buffer after the form."
1368 (beginning-of-defun)
1369 (let* ((byte-compile-current-file nil)
1370 (byte-compile-last-warned-form 'nothing)
1371 (value (eval (displaying-byte-compile-warnings
1372 (byte-compile-sexp (read (current-buffer)))))))
1374 (message "Compiling from buffer... done.")
1375 (prin1 value (current-buffer))
1377 ((message "%s" (prin1-to-string value)))))))
1380 (defun byte-compile-from-buffer (inbuffer &optional filename)
1381 ;; Filename is used for the loading-into-Emacs-18 error message.
1383 ;; Prevent truncation of flonums and lists as we read and print them
1384 (float-output-format nil)
1385 (case-fold-search nil)
1388 ;; Prevent edebug from interfering when we compile
1389 ;; and put the output into a file.
1390 (edebug-all-defs nil)
1391 (edebug-all-forms nil)
1392 ;; Simulate entry to byte-compile-top-level
1393 (byte-compile-constants nil)
1394 (byte-compile-variables nil)
1395 (byte-compile-tag-number 0)
1396 (byte-compile-depth 0)
1397 (byte-compile-maxdepth 0)
1398 (byte-compile-output nil)
1399 ;; #### This is bound in b-c-close-variables.
1400 ;; (byte-compile-warnings (if (eq byte-compile-warnings t)
1401 ;; byte-compile-warning-types
1402 ;; byte-compile-warnings))
1404 (byte-compile-close-variables
1407 (set-buffer (get-buffer-create " *Compiler Output*")))
1409 ;; (emacs-lisp-mode)
1410 (setq case-fold-search nil)
1411 (and filename (byte-compile-insert-header filename inbuffer outbuffer))
1413 ;; This is a kludge. Some operating systems (OS/2, DOS) need to
1414 ;; write files containing binary information specially.
1415 ;; Under most circumstances, such files will be in binary
1416 ;; overwrite mode, so those OS's use that flag to guess how
1417 ;; they should write their data. Advise them that .elc files
1418 ;; need to be written carefully.
1419 (setq overwrite-mode 'overwrite-mode-binary))
1420 (displaying-byte-compile-warnings
1422 (set-buffer inbuffer)
1425 ;; Compile the forms from the input buffer.
1427 (while (progn (skip-chars-forward " \t\n\^l")
1431 (byte-compile-file-form (read inbuffer)))
1433 ;; Compile pending forms at end of file.
1434 (byte-compile-flush-pending)
1435 (byte-compile-warn-about-unresolved-functions)
1436 ;; Should we always do this? When calling multiple files, it
1437 ;; would be useful to delay this warning until all have
1439 (setq byte-compile-unresolved-functions nil))))
1442 (defun byte-compile-insert-header (filename inbuffer outbuffer)
1443 (set-buffer inbuffer)
1444 (let ((dynamic-docstrings byte-compile-dynamic-docstrings)
1445 (dynamic byte-compile-dynamic))
1446 (set-buffer outbuffer)
1449 ;; The magic number of .elc files is ";ELC", or 0x3B454C43. After that is
1450 ;; the file-format version number (18 or 19) as a byte, followed by some
1451 ;; nulls. The primary motivation for doing this is to get some binary
1452 ;; characters up in the first line of the file so that `diff' will simply
1453 ;; say "Binary files differ" instead of actually doing a diff of two .elc
1454 ;; files. An extra benefit is that you can add this to /etc/magic:
1456 ;; 0 string ;ELC GNU Emacs Lisp compiled file,
1457 ;; >4 byte x version %d
1461 (if (byte-compile-version-cond byte-compile-compatibility) 18 19)
1464 (insert ";;; Compiled by "
1465 (or (and (boundp 'user-mail-address) user-mail-address)
1466 (concat (user-login-name) "@" (system-name)))
1468 (current-time-string) "\n;;; from file " filename "\n")
1469 (insert ";;; in Emacs version " emacs-version "\n")
1470 (insert ";;; with bytecomp version "
1471 (progn (string-match "[0-9.]+" byte-compile-version)
1472 (match-string 0 byte-compile-version))
1475 ((eq byte-optimize 'source) "with source-level optimization only")
1476 ((eq byte-optimize 'byte) "with byte-level optimization only")
1477 (byte-optimize "with all optimizations")
1478 (t "without optimization"))
1479 (if (byte-compile-version-cond byte-compile-compatibility)
1480 "; compiled with Emacs 18 compatibility.\n"
1483 (insert ";;; Function definitions are lazy-loaded.\n"))
1484 (if (not (byte-compile-version-cond byte-compile-compatibility))
1485 (insert ";;; This file uses opcodes which do not exist in Emacs 18.\n"
1486 ;; Have to check if emacs-version is bound so that this works
1487 ;; in files loaded early in loadup.el.
1488 "\n(if (and (boundp 'emacs-version)\n"
1489 ;; If there is a name at the end of emacs-version,
1490 ;; don't try to check the version number.
1491 "\t (< (aref emacs-version (1- (length emacs-version))) ?A)\n"
1492 "\t (or (and (boundp 'epoch::version) epoch::version)\n"
1493 (if dynamic-docstrings
1494 "\t (string-lessp emacs-version \"19.29\")))\n"
1495 "\t (string-lessp emacs-version \"19\")))\n")
1497 ;; prin1-to-string is used to quote backslashes.
1498 (substring (prin1-to-string (file-name-nondirectory filename))
1500 (if dynamic-docstrings
1501 "' was compiled for Emacs 19.29 or later\"))\n\n"
1502 "' was compiled for Emacs 19\"))\n\n"))
1503 (insert "(or (boundp 'current-load-list) (setq current-load-list nil))\n"
1508 (defun byte-compile-output-file-form (form)
1509 ;; writes the given form to the output buffer, being careful of docstrings
1510 ;; in defun, defmacro, defvar, defconst, autoload and
1511 ;; custom-declare-variable because make-docfile is so amazingly stupid.
1512 ;; defalias calls are output directly by byte-compile-file-form-defmumble;
1513 ;; it does not pay to first build the defalias in defmumble and then parse
1515 (if (and (memq (car-safe form) '(defun defmacro defvar defconst autoload
1516 custom-declare-variable))
1517 (stringp (nth 3 form)))
1518 (byte-compile-output-docform nil nil '("\n(" 3 ")") form nil
1520 '(autoload custom-declare-variable)))
1521 (let ((print-escape-newlines t)
1526 (princ "\n" outbuffer)
1527 (prin1 form outbuffer)
1530 (defun byte-compile-output-docform (preface name info form specindex quoted)
1531 "Print a form with a doc string. INFO is (prefix doc-index postfix).
1532 If PREFACE and NAME are non-nil, print them too,
1533 before INFO and the FORM but after the doc string itself.
1534 If SPECINDEX is non-nil, it is the index in FORM
1535 of the function bytecode string. In that case,
1536 we output that argument and the following argument (the constants vector)
1537 together, for lazy loading.
1538 QUOTED says that we have to put a quote before the
1539 list that represents a doc string reference.
1540 `autoload' and `custom-declare-variable' need that."
1541 ;; We need to examine byte-compile-dynamic-docstrings
1542 ;; in the input buffer (now current), not in the output buffer.
1543 (let ((dynamic-docstrings byte-compile-dynamic-docstrings))
1545 (prog1 (current-buffer)
1546 (set-buffer outbuffer)
1549 ;; Insert the doc string, and make it a comment with #@LENGTH.
1550 (and (>= (nth 1 info) 0)
1552 (not byte-compile-compatibility)
1554 ;; Make the doc string start at beginning of line
1555 ;; for make-docfile's sake.
1558 (byte-compile-output-as-comment
1559 (nth (nth 1 info) form) nil))
1560 (setq position (position-bytes position))
1561 ;; If the doc string starts with * (a user variable),
1563 (if (and (stringp (nth (nth 1 info) form))
1564 (> (length (nth (nth 1 info) form)) 0)
1565 (eq (aref (nth (nth 1 info) form) 0) ?*))
1566 (setq position (- position)))))
1571 (prin1 name outbuffer)))
1573 (let ((print-escape-newlines t)
1575 ;; Use a cons cell to say that we want
1576 ;; print-gensym-alist not to be cleared
1577 ;; between calls to print functions.
1581 (prin1 (car form) outbuffer)
1582 (while (setq form (cdr form))
1583 (setq index (1+ index))
1585 (cond ((and (numberp specindex) (= index specindex))
1587 (byte-compile-output-as-comment
1588 (cons (car form) (nth 1 form))
1590 (setq position (position-bytes position))
1591 (princ (format "(#$ . %d) nil" position) outbuffer)
1592 (setq form (cdr form))
1593 (setq index (1+ index))))
1594 ((= index (nth 1 info))
1596 (princ (format (if quoted "'(#$ . %d)" "(#$ . %d)")
1599 (let ((print-escape-newlines nil))
1600 (goto-char (prog1 (1+ (point))
1601 (prin1 (car form) outbuffer)))
1603 (goto-char (point-max)))))
1605 (prin1 (car form) outbuffer)))))
1606 (insert (nth 2 info))))))
1609 (defun byte-compile-keep-pending (form &optional handler)
1610 (if (memq byte-optimize '(t source))
1611 (setq form (byte-optimize-form form t)))
1613 (let ((for-effect t))
1614 ;; To avoid consing up monstrously large forms at load time, we split
1615 ;; the output regularly.
1616 (and (memq (car-safe form) '(fset defalias))
1617 (nthcdr 300 byte-compile-output)
1618 (byte-compile-flush-pending))
1619 (funcall handler form)
1621 (byte-compile-discard)))
1622 (byte-compile-form form t))
1625 (defun byte-compile-flush-pending ()
1626 (if byte-compile-output
1627 (let ((form (byte-compile-out-toplevel t 'file)))
1628 (cond ((eq (car-safe form) 'progn)
1629 (mapcar 'byte-compile-output-file-form (cdr form)))
1631 (byte-compile-output-file-form form)))
1632 (setq byte-compile-constants nil
1633 byte-compile-variables nil
1634 byte-compile-depth 0
1635 byte-compile-maxdepth 0
1636 byte-compile-output nil))))
1638 (defun byte-compile-file-form (form)
1639 (let ((byte-compile-current-form nil) ; close over this for warnings.
1643 (byte-compile-keep-pending form))
1644 ((and (symbolp (car form))
1645 (setq handler (get (car form) 'byte-hunk-handler)))
1646 (cond ((setq form (funcall handler form))
1647 (byte-compile-flush-pending)
1648 (byte-compile-output-file-form form))))
1649 ((eq form (setq form (macroexpand form byte-compile-macro-environment)))
1650 (byte-compile-keep-pending form))
1652 (byte-compile-file-form form)))))
1654 ;; Functions and variables with doc strings must be output separately,
1655 ;; so make-docfile can recognise them. Most other things can be output
1658 (put 'defsubst 'byte-hunk-handler 'byte-compile-file-form-defsubst)
1659 (defun byte-compile-file-form-defsubst (form)
1660 (cond ((assq (nth 1 form) byte-compile-unresolved-functions)
1661 (setq byte-compile-current-form (nth 1 form))
1662 (byte-compile-warn "defsubst %s was used before it was defined"
1664 (byte-compile-file-form
1665 (macroexpand form byte-compile-macro-environment))
1666 ;; Return nil so the form is not output twice.
1669 (put 'autoload 'byte-hunk-handler 'byte-compile-file-form-autoload)
1670 (defun byte-compile-file-form-autoload (form)
1671 (and (let ((form form))
1672 (while (if (setq form (cdr form)) (byte-compile-constp (car form))))
1673 (null form)) ;Constants only
1674 (eval (nth 5 form)) ;Macro
1675 (eval form)) ;Define the autoload.
1676 (if (stringp (nth 3 form))
1678 ;; No doc string, so we can compile this as a normal form.
1679 (byte-compile-keep-pending form 'byte-compile-normal-call)))
1681 (put 'defvar 'byte-hunk-handler 'byte-compile-file-form-defvar)
1682 (put 'defconst 'byte-hunk-handler 'byte-compile-file-form-defvar)
1683 (defun byte-compile-file-form-defvar (form)
1684 (if (null (nth 3 form))
1685 ;; Since there is no doc string, we can compile this as a normal form,
1686 ;; and not do a file-boundary.
1687 (byte-compile-keep-pending form)
1688 (if (memq 'free-vars byte-compile-warnings)
1689 (setq byte-compile-bound-variables
1690 (cons (nth 1 form) byte-compile-bound-variables)))
1691 (cond ((consp (nth 2 form))
1692 (setq form (copy-sequence form))
1693 (setcar (cdr (cdr form))
1694 (byte-compile-top-level (nth 2 form) nil 'file))))
1697 (put 'custom-declare-variable 'byte-hunk-handler
1698 'byte-compile-file-form-custom-declare-variable)
1699 (defun byte-compile-file-form-custom-declare-variable (form)
1700 (if (memq 'free-vars byte-compile-warnings)
1701 (setq byte-compile-bound-variables
1702 (cons (nth 1 (nth 1 form)) byte-compile-bound-variables)))
1705 (put 'require 'byte-hunk-handler 'byte-compile-file-form-eval-boundary)
1706 (defun byte-compile-file-form-eval-boundary (form)
1708 (byte-compile-keep-pending form 'byte-compile-normal-call))
1710 (put 'progn 'byte-hunk-handler 'byte-compile-file-form-progn)
1711 (put 'prog1 'byte-hunk-handler 'byte-compile-file-form-progn)
1712 (put 'prog2 'byte-hunk-handler 'byte-compile-file-form-progn)
1713 (defun byte-compile-file-form-progn (form)
1714 (mapcar 'byte-compile-file-form (cdr form))
1715 ;; Return nil so the forms are not output twice.
1718 ;; This handler is not necessary, but it makes the output from dont-compile
1719 ;; and similar macros cleaner.
1720 (put 'eval 'byte-hunk-handler 'byte-compile-file-form-eval)
1721 (defun byte-compile-file-form-eval (form)
1722 (if (eq (car-safe (nth 1 form)) 'quote)
1723 (nth 1 (nth 1 form))
1724 (byte-compile-keep-pending form)))
1726 (put 'defun 'byte-hunk-handler 'byte-compile-file-form-defun)
1727 (defun byte-compile-file-form-defun (form)
1728 (byte-compile-file-form-defmumble form nil))
1730 (put 'defmacro 'byte-hunk-handler 'byte-compile-file-form-defmacro)
1731 (defun byte-compile-file-form-defmacro (form)
1732 (byte-compile-file-form-defmumble form t))
1734 (defun byte-compile-file-form-defmumble (form macrop)
1735 (let* ((name (car (cdr form)))
1736 (this-kind (if macrop 'byte-compile-macro-environment
1737 'byte-compile-function-environment))
1738 (that-kind (if macrop 'byte-compile-function-environment
1739 'byte-compile-macro-environment))
1740 (this-one (assq name (symbol-value this-kind)))
1741 (that-one (assq name (symbol-value that-kind)))
1742 (byte-compile-free-references nil)
1743 (byte-compile-free-assignments nil))
1745 ;; When a function or macro is defined, add it to the call tree so that
1746 ;; we can tell when functions are not used.
1747 (if byte-compile-generate-call-tree
1748 (or (assq name byte-compile-call-tree)
1749 (setq byte-compile-call-tree
1750 (cons (list name nil nil) byte-compile-call-tree))))
1752 (setq byte-compile-current-form name) ; for warnings
1753 (if (memq 'redefine byte-compile-warnings)
1754 (byte-compile-arglist-warn form macrop))
1755 (if byte-compile-verbose
1756 (message "Compiling %s... (%s)" (or filename "") (nth 1 form)))
1758 (if (and (memq 'redefine byte-compile-warnings)
1759 ;; don't warn when compiling the stubs in byte-run...
1760 (not (assq (nth 1 form)
1761 byte-compile-initial-macro-environment)))
1763 "%s defined multiple times, as both function and macro"
1765 (setcdr that-one nil))
1767 (if (and (memq 'redefine byte-compile-warnings)
1768 ;; hack: don't warn when compiling the magic internal
1769 ;; byte-compiler macros in byte-run.el...
1770 (not (assq (nth 1 form)
1771 byte-compile-initial-macro-environment)))
1772 (byte-compile-warn "%s %s defined multiple times in this file"
1773 (if macrop "macro" "function")
1775 ((and (fboundp name)
1776 (eq (car-safe (symbol-function name))
1777 (if macrop 'lambda 'macro)))
1778 (if (memq 'redefine byte-compile-warnings)
1779 (byte-compile-warn "%s %s being redefined as a %s"
1780 (if macrop "function" "macro")
1782 (if macrop "macro" "function")))
1783 ;; shadow existing definition
1785 (cons (cons name nil) (symbol-value this-kind))))
1787 (let ((body (nthcdr 3 form)))
1788 (if (and (stringp (car body))
1789 (symbolp (car-safe (cdr-safe body)))
1790 (car-safe (cdr-safe body))
1791 (stringp (car-safe (cdr-safe (cdr-safe body)))))
1792 (byte-compile-warn "Probable `\"' without `\\' in doc string of %s"
1794 (let* ((new-one (byte-compile-lambda (cons 'lambda (nthcdr 2 form))))
1795 (code (byte-compile-byte-code-maker new-one)))
1797 (setcdr this-one new-one)
1799 (cons (cons name new-one) (symbol-value this-kind))))
1800 (if (and (stringp (nth 3 form))
1801 (eq 'quote (car-safe code))
1802 (eq 'lambda (car-safe (nth 1 code))))
1804 (cons name (cdr (nth 1 code))))
1805 (byte-compile-flush-pending)
1806 (if (not (stringp (nth 3 form)))
1807 ;; No doc string. Provide -1 as the "doc string index"
1808 ;; so that no element will be treated as a doc string.
1809 (byte-compile-output-docform
1810 (if (byte-compile-version-cond byte-compile-compatibility)
1811 "\n(fset '" "\n(defalias '")
1814 (if macrop '(" '(macro . #[" -1 "])") '(" #[" -1 "]")))
1815 ((eq (car code) 'quote)
1817 (if macrop '(" '(macro " -1 ")") '(" '(" -1 ")")))
1818 ((if macrop '(" (cons 'macro (" -1 "))") '(" (" -1 ")"))))
1820 (and (atom code) byte-compile-dynamic
1823 ;; Output the form by hand, that's much simpler than having
1824 ;; b-c-output-file-form analyze the defalias.
1825 (byte-compile-output-docform
1826 (if (byte-compile-version-cond byte-compile-compatibility)
1827 "\n(fset '" "\n(defalias '")
1830 (if macrop '(" '(macro . #[" 4 "])") '(" #[" 4 "]")))
1831 ((eq (car code) 'quote)
1833 (if macrop '(" '(macro " 2 ")") '(" '(" 2 ")")))
1834 ((if macrop '(" (cons 'macro (" 5 "))") '(" (" 5 ")"))))
1836 (and (atom code) byte-compile-dynamic
1839 (princ ")" outbuffer)
1842 ;; Print Lisp object EXP in the output file, inside a comment,
1843 ;; and return the file position it will have.
1844 ;; If QUOTED is non-nil, print with quoting; otherwise, print without quoting.
1845 (defun byte-compile-output-as-comment (exp quoted)
1846 (let ((position (point))
1849 (prog1 (current-buffer)
1850 (set-buffer outbuffer)
1852 ;; Insert EXP, and make it a comment with #@LENGTH.
1855 (prin1 exp outbuffer)
1856 (princ exp outbuffer))
1857 (goto-char position)
1858 ;; Quote certain special characters as needed.
1859 ;; get_doc_string in doc.c does the unquoting.
1860 (while (search-forward "\^A" nil t)
1861 (replace-match "\^A\^A" t t))
1862 (goto-char position)
1863 (while (search-forward "\000" nil t)
1864 (replace-match "\^A0" t t))
1865 (goto-char position)
1866 (while (search-forward "\037" nil t)
1867 (replace-match "\^A_" t t))
1868 (goto-char (point-max))
1870 (goto-char position)
1871 (setq total-bytes 0)
1873 (setq total-bytes (+ total-bytes (char-bytes (char-after (point)))))
1875 (goto-char position)
1876 (insert "#@" (format "%d" total-bytes))
1878 ;; Save the file position of the object.
1879 ;; Note we should add 1 to skip the space
1880 ;; that we inserted before the actual doc string,
1881 ;; and subtract 1 to convert from an 1-origin Emacs position
1882 ;; to a file position; they cancel.
1883 (setq position (point))
1884 (goto-char (point-max))))
1890 (defun byte-compile (form)
1891 "If FORM is a symbol, byte-compile its function definition.
1892 If FORM is a lambda or a macro, byte-compile it as a function."
1893 (displaying-byte-compile-warnings
1894 (byte-compile-close-variables
1895 (let* ((fun (if (symbolp form)
1896 (and (fboundp form) (symbol-function form))
1898 (macro (eq (car-safe fun) 'macro)))
1900 (setq fun (cdr fun)))
1901 (cond ((eq (car-safe fun) 'lambda)
1903 (cons 'macro (byte-compile-lambda fun))
1904 (byte-compile-lambda fun)))
1909 (defun byte-compile-sexp (sexp)
1910 "Compile and return SEXP."
1911 (displaying-byte-compile-warnings
1912 (byte-compile-close-variables
1913 (byte-compile-top-level sexp))))
1915 ;; Given a function made by byte-compile-lambda, make a form which produces it.
1916 (defun byte-compile-byte-code-maker (fun)
1918 ((byte-compile-version-cond byte-compile-compatibility)
1919 ;; Return (quote (lambda ...)).
1920 (list 'quote (byte-compile-byte-code-unmake fun)))
1921 ;; ## atom is faster than compiled-func-p.
1922 ((atom fun) ; compiled function.
1923 ;; generate-emacs19-bytecodes must be on, otherwise byte-compile-lambda
1924 ;; would have produced a lambda.
1926 ;; b-c-lambda didn't produce a compiled-function, so it's either a trivial
1927 ;; function, or this is Emacs 18, or generate-emacs19-bytecodes is off.
1929 (if (and (setq tmp (assq 'byte-code (cdr-safe (cdr fun))))
1930 (null (cdr (memq tmp fun))))
1931 ;; Generate a make-byte-code call.
1932 (let* ((interactive (assq 'interactive (cdr (cdr fun)))))
1933 (nconc (list 'make-byte-code
1934 (list 'quote (nth 1 fun)) ;arglist
1938 (cond ((stringp (nth 2 fun))
1939 (list (nth 2 fun))) ;doc
1943 (list (if (or (null (nth 1 interactive))
1944 (stringp (nth 1 interactive)))
1946 ;; Interactive spec is a list or a variable
1947 ;; (if it is correct).
1948 (list 'quote (nth 1 interactive))))))))
1949 ;; a non-compiled function (probably trivial)
1950 (list 'quote fun))))))
1952 ;; Turn a function into an ordinary lambda. Needed for v18 files.
1953 (defun byte-compile-byte-code-unmake (function)
1954 (if (consp function)
1955 function;;It already is a lambda.
1956 (setq function (append function nil)) ; turn it into a list
1957 (nconc (list 'lambda (nth 0 function))
1958 (and (nth 4 function) (list (nth 4 function)))
1959 (if (nthcdr 5 function)
1960 (list (cons 'interactive (if (nth 5 function)
1961 (nthcdr 5 function)))))
1962 (list (list 'byte-code
1963 (nth 1 function) (nth 2 function)
1964 (nth 3 function))))))
1967 ;; Byte-compile a lambda-expression and return a valid function.
1968 ;; The value is usually a compiled function but may be the original
1969 ;; lambda-expression.
1970 (defun byte-compile-lambda (fun)
1971 (let* ((arglist (nth 1 fun))
1972 (byte-compile-bound-variables
1973 (nconc (and (memq 'free-vars byte-compile-warnings)
1974 (delq '&rest (delq '&optional (copy-sequence arglist))))
1975 byte-compile-bound-variables))
1976 (body (cdr (cdr fun)))
1977 (doc (if (stringp (car body))
1979 ;; Discard the doc string
1980 ;; unless it is the last element of the body.
1982 (setq body (cdr body))))))
1983 (int (assq 'interactive body)))
1985 ;; Skip (interactive) if it is in front (the most usual location).
1986 (if (eq int (car body))
1987 (setq body (cdr body)))
1988 (cond ((consp (cdr int))
1990 (byte-compile-warn "malformed interactive spec: %s"
1991 (prin1-to-string int)))
1992 ;; If the interactive spec is a call to `list',
1993 ;; don't compile it, because `call-interactively'
1994 ;; looks at the args of `list'.
1995 (let ((form (nth 1 int)))
1996 (while (or (eq (car-safe form) 'let)
1997 (eq (car-safe form) 'let*)
1998 (eq (car-safe form) 'save-excursion))
1999 (while (consp (cdr form))
2000 (setq form (cdr form)))
2001 (setq form (car form)))
2002 (or (eq (car-safe form) 'list)
2003 (setq int (list 'interactive
2004 (byte-compile-top-level (nth 1 int)))))))
2006 (byte-compile-warn "malformed interactive spec: %s"
2007 (prin1-to-string int))))))
2008 (let ((compiled (byte-compile-top-level (cons 'progn body) nil 'lambda)))
2009 (if (and (eq 'byte-code (car-safe compiled))
2010 (not (byte-compile-version-cond
2011 byte-compile-compatibility)))
2012 (apply 'make-byte-code
2013 (append (list arglist)
2014 ;; byte-string, constants-vector, stack depth
2016 ;; optionally, the doc string.
2019 ;; optionally, the interactive spec.
2021 (list (nth 1 int)))))
2023 (nconc (if int (list int))
2024 (cond ((eq (car-safe compiled) 'progn) (cdr compiled))
2025 (compiled (list compiled)))))
2026 (nconc (list 'lambda arglist)
2027 (if (or doc (stringp (car compiled)))
2028 (cons doc (cond (compiled)
2032 (defun byte-compile-constants-vector ()
2033 ;; Builds the constants-vector from the current variables and constants.
2034 ;; This modifies the constants from (const . nil) to (const . offset).
2035 ;; To keep the byte-codes to look up the vector as short as possible:
2036 ;; First 6 elements are vars, as there are one-byte varref codes for those.
2037 ;; Next up to byte-constant-limit are constants, still with one-byte codes.
2038 ;; Next variables again, to get 2-byte codes for variable lookup.
2039 ;; The rest of the constants and variables need 3-byte byte-codes.
2041 (rest (nreverse byte-compile-variables)) ; nreverse because the first
2042 (other (nreverse byte-compile-constants)) ; vars often are used most.
2044 (limits '(5 ; Use the 1-byte varref codes,
2045 63 ; 1-constlim ; 1-byte byte-constant codes,
2046 255 ; 2-byte varref codes,
2047 65535)) ; 3-byte codes for the rest.
2049 (while (or rest other)
2050 (setq limit (car limits))
2051 (while (and rest (not (eq i limit)))
2052 (if (setq tmp (assq (car (car rest)) ret))
2053 (setcdr (car rest) (cdr tmp))
2054 (setcdr (car rest) (setq i (1+ i)))
2055 (setq ret (cons (car rest) ret)))
2056 (setq rest (cdr rest)))
2057 (setq limits (cdr limits)
2059 (setq other rest))))
2060 (apply 'vector (nreverse (mapcar 'car ret)))))
2062 ;; Given an expression FORM, compile it and return an equivalent byte-code
2063 ;; expression (a call to the function byte-code).
2064 (defun byte-compile-top-level (form &optional for-effect output-type)
2065 ;; OUTPUT-TYPE advises about how form is expected to be used:
2066 ;; 'eval or nil -> a single form,
2067 ;; 'progn or t -> a list of forms,
2068 ;; 'lambda -> body of a lambda,
2069 ;; 'file -> used at file-level.
2070 (let ((byte-compile-constants nil)
2071 (byte-compile-variables nil)
2072 (byte-compile-tag-number 0)
2073 (byte-compile-depth 0)
2074 (byte-compile-maxdepth 0)
2075 (byte-compile-output nil))
2076 (if (memq byte-optimize '(t source))
2077 (setq form (byte-optimize-form form for-effect)))
2078 (while (and (eq (car-safe form) 'progn) (null (cdr (cdr form))))
2079 (setq form (nth 1 form)))
2080 (if (and (eq 'byte-code (car-safe form))
2081 (not (memq byte-optimize '(t byte)))
2082 (stringp (nth 1 form)) (vectorp (nth 2 form))
2083 (natnump (nth 3 form)))
2085 (byte-compile-form form for-effect)
2086 (byte-compile-out-toplevel for-effect output-type))))
2088 (defun byte-compile-out-toplevel (&optional for-effect output-type)
2090 ;; The stack is empty. Push a value to be returned from (byte-code ..).
2091 (if (eq (car (car byte-compile-output)) 'byte-discard)
2092 (setq byte-compile-output (cdr byte-compile-output))
2093 (byte-compile-push-constant
2094 ;; Push any constant - preferably one which already is used, and
2095 ;; a number or symbol - ie not some big sequence. The return value
2096 ;; isn't returned, but it would be a shame if some textually large
2097 ;; constant was not optimized away because we chose to return it.
2098 (and (not (assq nil byte-compile-constants)) ; Nil is often there.
2099 (let ((tmp (reverse byte-compile-constants)))
2100 (while (and tmp (not (or (symbolp (car (car tmp)))
2101 (numberp (car (car tmp))))))
2102 (setq tmp (cdr tmp)))
2103 (car (car tmp)))))))
2104 (byte-compile-out 'byte-return 0)
2105 (setq byte-compile-output (nreverse byte-compile-output))
2106 (if (memq byte-optimize '(t byte))
2107 (setq byte-compile-output
2108 (byte-optimize-lapcode byte-compile-output for-effect)))
2110 ;; Decompile trivial functions:
2111 ;; only constants and variables, or a single funcall except in lambdas.
2112 ;; Except for Lisp_Compiled objects, forms like (foo "hi")
2113 ;; are still quicker than (byte-code "..." [foo "hi"] 2).
2114 ;; Note that even (quote foo) must be parsed just as any subr by the
2115 ;; interpreter, so quote should be compiled into byte-code in some contexts.
2116 ;; What to leave uncompiled:
2117 ;; lambda -> never. we used to leave it uncompiled if the body was
2118 ;; a single atom, but that causes confusion if the docstring
2119 ;; uses the (file . pos) syntax. Besides, now that we have
2120 ;; the Lisp_Compiled type, the compiled form is faster.
2121 ;; eval -> atom, quote or (function atom atom atom)
2122 ;; progn -> as <<same-as-eval>> or (progn <<same-as-eval>> atom)
2123 ;; file -> as progn, but takes both quotes and atoms, and longer forms.
2125 (maycall (not (eq output-type 'lambda))) ; t if we may make a funcall.
2128 ;; #### This should be split out into byte-compile-nontrivial-function-p.
2129 ((or (eq output-type 'lambda)
2130 (nthcdr (if (eq output-type 'file) 50 8) byte-compile-output)
2131 (assq 'TAG byte-compile-output) ; Not necessary, but speeds up a bit.
2132 (not (setq tmp (assq 'byte-return byte-compile-output)))
2134 (setq rest (nreverse
2135 (cdr (memq tmp (reverse byte-compile-output)))))
2137 ((memq (car (car rest)) '(byte-varref byte-constant))
2138 (setq tmp (car (cdr (car rest))))
2139 (if (if (eq (car (car rest)) 'byte-constant)
2142 (not (memq tmp '(nil t))))))
2144 (setq body (cons (list 'quote tmp) body)))
2145 (setq body (cons tmp body))))
2147 ;; Allow a funcall if at most one atom follows it.
2148 (null (nthcdr 3 rest))
2149 (setq tmp (get (car (car rest)) 'byte-opcode-invert))
2150 (or (null (cdr rest))
2151 (and (memq output-type '(file progn t))
2153 (eq (car (nth 1 rest)) 'byte-discard)
2154 (progn (setq rest (cdr rest)) t))))
2155 (setq maycall nil) ; Only allow one real function call.
2156 (setq body (nreverse body))
2158 (if (and (eq tmp 'funcall)
2159 (eq (car-safe (car body)) 'quote))
2160 (cons (nth 1 (car body)) (cdr body))
2162 (or (eq output-type 'file)
2163 (not (delq nil (mapcar 'consp (cdr (car body))))))))
2164 (setq rest (cdr rest)))
2166 (let ((byte-compile-vector (byte-compile-constants-vector)))
2167 (list 'byte-code (byte-compile-lapcode byte-compile-output)
2168 byte-compile-vector byte-compile-maxdepth)))
2169 ;; it's a trivial function
2170 ((cdr body) (cons 'progn (nreverse body)))
2173 ;; Given BODY, compile it and return a new body.
2174 (defun byte-compile-top-level-body (body &optional for-effect)
2175 (setq body (byte-compile-top-level (cons 'progn body) for-effect t))
2176 (cond ((eq (car-safe body) 'progn)
2181 ;; This is the recursive entry point for compiling each subform of an
2183 ;; If for-effect is non-nil, byte-compile-form will output a byte-discard
2184 ;; before terminating (ie no value will be left on the stack).
2185 ;; A byte-compile handler may, when for-effect is non-nil, choose output code
2186 ;; which does not leave a value on the stack, and then set for-effect to nil
2187 ;; (to prevent byte-compile-form from outputting the byte-discard).
2188 ;; If a handler wants to call another handler, it should do so via
2189 ;; byte-compile-form, or take extreme care to handle for-effect correctly.
2190 ;; (Use byte-compile-form-do-effect to reset the for-effect flag too.)
2192 (defun byte-compile-form (form &optional for-effect)
2193 (setq form (macroexpand form byte-compile-macro-environment))
2194 (cond ((not (consp form))
2195 (cond ((or (not (symbolp form)) (memq form '(nil t)))
2196 (byte-compile-constant form))
2197 ((and for-effect byte-compile-delete-errors)
2198 (setq for-effect nil))
2199 (t (byte-compile-variable-ref 'byte-varref form))))
2200 ((symbolp (car form))
2201 (let* ((fn (car form))
2202 (handler (get fn 'byte-compile)))
2203 (if (memq fn '(t nil))
2204 (byte-compile-warn "%s called as a function" fn))
2206 (or (not (byte-compile-version-cond
2207 byte-compile-compatibility))
2208 (not (get (get fn 'byte-opcode) 'emacs19-opcode))))
2209 (funcall handler form)
2210 (if (memq 'callargs byte-compile-warnings)
2211 (byte-compile-callargs-warn form))
2212 (byte-compile-normal-call form))))
2213 ((and (or (byte-code-function-p (car form))
2214 (eq (car-safe (car form)) 'lambda))
2215 ;; if the form comes out the same way it went in, that's
2216 ;; because it was malformed, and we couldn't unfold it.
2217 (not (eq form (setq form (byte-compile-unfold-lambda form)))))
2218 (byte-compile-form form for-effect)
2219 (setq for-effect nil))
2220 ((byte-compile-normal-call form)))
2222 (byte-compile-discard)))
2224 (defun byte-compile-normal-call (form)
2225 (if byte-compile-generate-call-tree
2226 (byte-compile-annotate-call-tree form))
2227 (byte-compile-push-constant (car form))
2228 (mapcar 'byte-compile-form (cdr form)) ; wasteful, but faster.
2229 (byte-compile-out 'byte-call (length (cdr form))))
2231 (defun byte-compile-variable-ref (base-op var)
2232 (if (or (not (symbolp var)) (memq var '(nil t)))
2233 (byte-compile-warn (if (eq base-op 'byte-varbind)
2234 "Attempt to let-bind %s %s"
2235 "Variable reference to %s %s")
2236 (if (symbolp var) "constant" "nonvariable")
2237 (prin1-to-string var))
2238 (if (and (get var 'byte-obsolete-variable)
2239 (memq 'obsolete byte-compile-warnings))
2240 (let ((ob (get var 'byte-obsolete-variable)))
2241 (byte-compile-warn "%s is an obsolete variable; %s" var
2244 (format "use %s instead." ob)))))
2245 (if (memq 'free-vars byte-compile-warnings)
2246 (if (eq base-op 'byte-varbind)
2247 (setq byte-compile-bound-variables
2248 (cons var byte-compile-bound-variables))
2250 (memq var byte-compile-bound-variables)
2251 (if (eq base-op 'byte-varset)
2252 (or (memq var byte-compile-free-assignments)
2254 (byte-compile-warn "assignment to free variable %s" var)
2255 (setq byte-compile-free-assignments
2256 (cons var byte-compile-free-assignments))))
2257 (or (memq var byte-compile-free-references)
2259 (byte-compile-warn "reference to free variable %s" var)
2260 (setq byte-compile-free-references
2261 (cons var byte-compile-free-references)))))))))
2262 (let ((tmp (assq var byte-compile-variables)))
2264 (setq tmp (list var)
2265 byte-compile-variables (cons tmp byte-compile-variables)))
2266 (byte-compile-out base-op tmp)))
2268 (defmacro byte-compile-get-constant (const)
2269 (` (or (if (stringp (, const))
2270 (assoc (, const) byte-compile-constants)
2271 (assq (, const) byte-compile-constants))
2272 (car (setq byte-compile-constants
2273 (cons (list (, const)) byte-compile-constants))))))
2275 ;; Use this when the value of a form is a constant. This obeys for-effect.
2276 (defun byte-compile-constant (const)
2278 (setq for-effect nil)
2279 (byte-compile-out 'byte-constant (byte-compile-get-constant const))))
2281 ;; Use this for a constant that is not the value of its containing form.
2282 ;; This ignores for-effect.
2283 (defun byte-compile-push-constant (const)
2284 (let ((for-effect nil))
2285 (inline (byte-compile-constant const))))
2288 ;; Compile those primitive ordinary functions
2289 ;; which have special byte codes just for speed.
2291 (defmacro byte-defop-compiler (function &optional compile-handler)
2292 ;; add a compiler-form for FUNCTION.
2293 ;; If function is a symbol, then the variable "byte-SYMBOL" must name
2294 ;; the opcode to be used. If function is a list, the first element
2295 ;; is the function and the second element is the bytecode-symbol.
2296 ;; COMPILE-HANDLER is the function to use to compile this byte-op, or
2297 ;; may be the abbreviations 0, 1, 2, 3, 0-1, or 1-2.
2298 ;; If it is nil, then the handler is "byte-compile-SYMBOL."
2300 (if (symbolp function)
2301 (setq opcode (intern (concat "byte-" (symbol-name function))))
2302 (setq opcode (car (cdr function))
2303 function (car function)))
2305 (list 'put (list 'quote function) ''byte-compile
2307 (or (cdr (assq compile-handler
2308 '((0 . byte-compile-no-args)
2309 (1 . byte-compile-one-arg)
2310 (2 . byte-compile-two-args)
2311 (3 . byte-compile-three-args)
2312 (0-1 . byte-compile-zero-or-one-arg)
2313 (1-2 . byte-compile-one-or-two-args)
2314 (2-3 . byte-compile-two-or-three-args)
2317 (intern (concat "byte-compile-"
2318 (symbol-name function))))))))
2321 (list 'put (list 'quote function)
2322 ''byte-opcode (list 'quote opcode))
2323 (list 'put (list 'quote opcode)
2324 ''byte-opcode-invert (list 'quote function)))
2327 (defmacro byte-defop-compiler19 (function &optional compile-handler)
2328 ;; Just like byte-defop-compiler, but defines an opcode that will only
2329 ;; be used when byte-compile-compatibility is false.
2330 (if (and (byte-compile-single-version)
2331 byte-compile-compatibility)
2332 ;; #### instead of doing nothing, this should do some remprops,
2333 ;; #### to protect against the case where a single-version compiler
2334 ;; #### is loaded into a world that has contained a multi-version one.
2339 (or (car (cdr-safe function))
2340 (intern (concat "byte-"
2341 (symbol-name (or (car-safe function) function))))))
2343 (list 'byte-defop-compiler function compile-handler))))
2345 (defmacro byte-defop-compiler-1 (function &optional compile-handler)
2346 (list 'byte-defop-compiler (list function nil) compile-handler))
2349 (put 'byte-call 'byte-opcode-invert 'funcall)
2350 (put 'byte-list1 'byte-opcode-invert 'list)
2351 (put 'byte-list2 'byte-opcode-invert 'list)
2352 (put 'byte-list3 'byte-opcode-invert 'list)
2353 (put 'byte-list4 'byte-opcode-invert 'list)
2354 (put 'byte-listN 'byte-opcode-invert 'list)
2355 (put 'byte-concat2 'byte-opcode-invert 'concat)
2356 (put 'byte-concat3 'byte-opcode-invert 'concat)
2357 (put 'byte-concat4 'byte-opcode-invert 'concat)
2358 (put 'byte-concatN 'byte-opcode-invert 'concat)
2359 (put 'byte-insertN 'byte-opcode-invert 'insert)
2361 (byte-defop-compiler (dot byte-point) 0)
2362 (byte-defop-compiler (dot-max byte-point-max) 0)
2363 (byte-defop-compiler (dot-min byte-point-min) 0)
2364 (byte-defop-compiler point 0)
2365 ;;(byte-defop-compiler mark 0) ;; obsolete
2366 (byte-defop-compiler point-max 0)
2367 (byte-defop-compiler point-min 0)
2368 (byte-defop-compiler following-char 0)
2369 (byte-defop-compiler preceding-char 0)
2370 (byte-defop-compiler current-column 0)
2371 (byte-defop-compiler eolp 0)
2372 (byte-defop-compiler eobp 0)
2373 (byte-defop-compiler bolp 0)
2374 (byte-defop-compiler bobp 0)
2375 (byte-defop-compiler current-buffer 0)
2376 ;;(byte-defop-compiler read-char 0) ;; obsolete
2377 (byte-defop-compiler interactive-p 0)
2378 (byte-defop-compiler19 widen 0)
2379 (byte-defop-compiler19 end-of-line 0-1)
2380 (byte-defop-compiler19 forward-char 0-1)
2381 (byte-defop-compiler19 forward-line 0-1)
2382 (byte-defop-compiler symbolp 1)
2383 (byte-defop-compiler consp 1)
2384 (byte-defop-compiler stringp 1)
2385 (byte-defop-compiler listp 1)
2386 (byte-defop-compiler not 1)
2387 (byte-defop-compiler (null byte-not) 1)
2388 (byte-defop-compiler car 1)
2389 (byte-defop-compiler cdr 1)
2390 (byte-defop-compiler length 1)
2391 (byte-defop-compiler symbol-value 1)
2392 (byte-defop-compiler symbol-function 1)
2393 (byte-defop-compiler (1+ byte-add1) 1)
2394 (byte-defop-compiler (1- byte-sub1) 1)
2395 (byte-defop-compiler goto-char 1)
2396 (byte-defop-compiler char-after 0-1)
2397 (byte-defop-compiler set-buffer 1)
2398 ;;(byte-defop-compiler set-mark 1) ;; obsolete
2399 (byte-defop-compiler19 forward-word 1)
2400 (byte-defop-compiler19 char-syntax 1)
2401 (byte-defop-compiler19 nreverse 1)
2402 (byte-defop-compiler19 car-safe 1)
2403 (byte-defop-compiler19 cdr-safe 1)
2404 (byte-defop-compiler19 numberp 1)
2405 (byte-defop-compiler19 integerp 1)
2406 (byte-defop-compiler19 skip-chars-forward 1-2)
2407 (byte-defop-compiler19 skip-chars-backward 1-2)
2408 (byte-defop-compiler eq 2)
2409 (byte-defop-compiler memq 2)
2410 (byte-defop-compiler cons 2)
2411 (byte-defop-compiler aref 2)
2412 (byte-defop-compiler set 2)
2413 (byte-defop-compiler (= byte-eqlsign) 2)
2414 (byte-defop-compiler (< byte-lss) 2)
2415 (byte-defop-compiler (> byte-gtr) 2)
2416 (byte-defop-compiler (<= byte-leq) 2)
2417 (byte-defop-compiler (>= byte-geq) 2)
2418 (byte-defop-compiler get 2)
2419 (byte-defop-compiler nth 2)
2420 (byte-defop-compiler substring 2-3)
2421 (byte-defop-compiler19 (move-marker byte-set-marker) 2-3)
2422 (byte-defop-compiler19 set-marker 2-3)
2423 (byte-defop-compiler19 match-beginning 1)
2424 (byte-defop-compiler19 match-end 1)
2425 (byte-defop-compiler19 upcase 1)
2426 (byte-defop-compiler19 downcase 1)
2427 (byte-defop-compiler19 string= 2)
2428 (byte-defop-compiler19 string< 2)
2429 (byte-defop-compiler19 (string-equal byte-string=) 2)
2430 (byte-defop-compiler19 (string-lessp byte-string<) 2)
2431 (byte-defop-compiler19 equal 2)
2432 (byte-defop-compiler19 nthcdr 2)
2433 (byte-defop-compiler19 elt 2)
2434 (byte-defop-compiler19 member 2)
2435 (byte-defop-compiler19 assq 2)
2436 (byte-defop-compiler19 (rplaca byte-setcar) 2)
2437 (byte-defop-compiler19 (rplacd byte-setcdr) 2)
2438 (byte-defop-compiler19 setcar 2)
2439 (byte-defop-compiler19 setcdr 2)
2440 (byte-defop-compiler19 buffer-substring 2)
2441 (byte-defop-compiler19 delete-region 2)
2442 (byte-defop-compiler19 narrow-to-region 2)
2443 (byte-defop-compiler19 (% byte-rem) 2)
2444 (byte-defop-compiler aset 3)
2446 (byte-defop-compiler max byte-compile-associative)
2447 (byte-defop-compiler min byte-compile-associative)
2448 (byte-defop-compiler (+ byte-plus) byte-compile-associative)
2449 (byte-defop-compiler19 (* byte-mult) byte-compile-associative)
2451 ;;####(byte-defop-compiler19 move-to-column 1)
2452 (byte-defop-compiler-1 interactive byte-compile-noop)
2455 (defun byte-compile-subr-wrong-args (form n)
2456 (byte-compile-warn "%s called with %d arg%s, but requires %s"
2457 (car form) (length (cdr form))
2458 (if (= 1 (length (cdr form))) "" "s") n)
2459 ;; get run-time wrong-number-of-args error.
2460 (byte-compile-normal-call form))
2462 (defun byte-compile-no-args (form)
2463 (if (not (= (length form) 1))
2464 (byte-compile-subr-wrong-args form "none")
2465 (byte-compile-out (get (car form) 'byte-opcode) 0)))
2467 (defun byte-compile-one-arg (form)
2468 (if (not (= (length form) 2))
2469 (byte-compile-subr-wrong-args form 1)
2470 (byte-compile-form (car (cdr form))) ;; Push the argument
2471 (byte-compile-out (get (car form) 'byte-opcode) 0)))
2473 (defun byte-compile-two-args (form)
2474 (if (not (= (length form) 3))
2475 (byte-compile-subr-wrong-args form 2)
2476 (byte-compile-form (car (cdr form))) ;; Push the arguments
2477 (byte-compile-form (nth 2 form))
2478 (byte-compile-out (get (car form) 'byte-opcode) 0)))
2480 (defun byte-compile-three-args (form)
2481 (if (not (= (length form) 4))
2482 (byte-compile-subr-wrong-args form 3)
2483 (byte-compile-form (car (cdr form))) ;; Push the arguments
2484 (byte-compile-form (nth 2 form))
2485 (byte-compile-form (nth 3 form))
2486 (byte-compile-out (get (car form) 'byte-opcode) 0)))
2488 (defun byte-compile-zero-or-one-arg (form)
2489 (let ((len (length form)))
2490 (cond ((= len 1) (byte-compile-one-arg (append form '(nil))))
2491 ((= len 2) (byte-compile-one-arg form))
2492 (t (byte-compile-subr-wrong-args form "0-1")))))
2494 (defun byte-compile-one-or-two-args (form)
2495 (let ((len (length form)))
2496 (cond ((= len 2) (byte-compile-two-args (append form '(nil))))
2497 ((= len 3) (byte-compile-two-args form))
2498 (t (byte-compile-subr-wrong-args form "1-2")))))
2500 (defun byte-compile-two-or-three-args (form)
2501 (let ((len (length form)))
2502 (cond ((= len 3) (byte-compile-three-args (append form '(nil))))
2503 ((= len 4) (byte-compile-three-args form))
2504 (t (byte-compile-subr-wrong-args form "2-3")))))
2506 (defun byte-compile-noop (form)
2507 (byte-compile-constant nil))
2509 (defun byte-compile-discard ()
2510 (byte-compile-out 'byte-discard 0))
2513 ;; Compile a function that accepts one or more args and is right-associative.
2514 ;; We do it by left-associativity so that the operations
2515 ;; are done in the same order as in interpreted code.
2516 ;; We treat the one-arg case, as in (+ x), like (+ x 0).
2517 ;; in order to convert markers to numbers, and trigger expected errors.
2518 (defun byte-compile-associative (form)
2520 (let ((opcode (get (car form) 'byte-opcode))
2521 (args (copy-sequence (cdr form))))
2522 (byte-compile-form (car args))
2523 (setq args (cdr args))
2524 (or args (setq args '(0)
2525 opcode (get '+ 'byte-opcode)))
2527 (byte-compile-form (car args))
2528 (byte-compile-out opcode 0)
2529 (setq args (cdr args))))
2530 (byte-compile-constant (eval form))))
2533 ;; more complicated compiler macros
2535 (byte-defop-compiler list)
2536 (byte-defop-compiler concat)
2537 (byte-defop-compiler fset)
2538 (byte-defop-compiler (indent-to-column byte-indent-to) byte-compile-indent-to)
2539 (byte-defop-compiler indent-to)
2540 (byte-defop-compiler insert)
2541 (byte-defop-compiler-1 function byte-compile-function-form)
2542 (byte-defop-compiler-1 - byte-compile-minus)
2543 (byte-defop-compiler19 (/ byte-quo) byte-compile-quo)
2544 (byte-defop-compiler19 nconc)
2545 (byte-defop-compiler-1 beginning-of-line)
2547 (defun byte-compile-list (form)
2548 (let ((count (length (cdr form))))
2550 (byte-compile-constant nil))
2552 (mapcar 'byte-compile-form (cdr form))
2554 (aref [byte-list1 byte-list2 byte-list3 byte-list4] (1- count)) 0))
2555 ((and (< count 256) (not (byte-compile-version-cond
2556 byte-compile-compatibility)))
2557 (mapcar 'byte-compile-form (cdr form))
2558 (byte-compile-out 'byte-listN count))
2559 (t (byte-compile-normal-call form)))))
2561 (defun byte-compile-concat (form)
2562 (let ((count (length (cdr form))))
2563 (cond ((and (< 1 count) (< count 5))
2564 (mapcar 'byte-compile-form (cdr form))
2566 (aref [byte-concat2 byte-concat3 byte-concat4] (- count 2))
2568 ;; Concat of one arg is not a no-op if arg is not a string.
2570 (byte-compile-form ""))
2571 ((and (< count 256) (not (byte-compile-version-cond
2572 byte-compile-compatibility)))
2573 (mapcar 'byte-compile-form (cdr form))
2574 (byte-compile-out 'byte-concatN count))
2575 ((byte-compile-normal-call form)))))
2577 (defun byte-compile-minus (form)
2578 (if (null (setq form (cdr form)))
2579 (byte-compile-constant 0)
2580 (byte-compile-form (car form))
2582 (while (setq form (cdr form))
2583 (byte-compile-form (car form))
2584 (byte-compile-out 'byte-diff 0))
2585 (byte-compile-out 'byte-negate 0))))
2587 (defun byte-compile-quo (form)
2588 (let ((len (length form)))
2590 (byte-compile-subr-wrong-args form "2 or more"))
2592 (byte-compile-form (car (setq form (cdr form))))
2593 (while (setq form (cdr form))
2594 (byte-compile-form (car form))
2595 (byte-compile-out 'byte-quo 0))))))
2597 (defun byte-compile-nconc (form)
2598 (let ((len (length form)))
2600 (byte-compile-constant nil))
2602 ;; nconc of one arg is a noop, even if that arg isn't a list.
2603 (byte-compile-form (nth 1 form)))
2605 (byte-compile-form (car (setq form (cdr form))))
2606 (while (setq form (cdr form))
2607 (byte-compile-form (car form))
2608 (byte-compile-out 'byte-nconc 0))))))
2610 (defun byte-compile-fset (form)
2611 ;; warn about forms like (fset 'foo '(lambda () ...))
2612 ;; (where the lambda expression is non-trivial...)
2613 (let ((fn (nth 2 form))
2615 (if (and (eq (car-safe fn) 'quote)
2616 (eq (car-safe (setq fn (nth 1 fn))) 'lambda))
2618 (setq body (cdr (cdr fn)))
2619 (if (stringp (car body)) (setq body (cdr body)))
2620 (if (eq 'interactive (car-safe (car body))) (setq body (cdr body)))
2621 (if (and (consp (car body))
2622 (not (eq 'byte-code (car (car body)))))
2624 "A quoted lambda form is the second argument of fset. This is probably
2625 not what you want, as that lambda cannot be compiled. Consider using
2626 the syntax (function (lambda (...) ...)) instead.")))))
2627 (byte-compile-two-args form))
2629 (defun byte-compile-funarg (form)
2630 ;; (mapcar '(lambda (x) ..) ..) ==> (mapcar (function (lambda (x) ..)) ..)
2631 ;; for cases where it's guaranteed that first arg will be used as a lambda.
2632 (byte-compile-normal-call
2633 (let ((fn (nth 1 form)))
2634 (if (and (eq (car-safe fn) 'quote)
2635 (eq (car-safe (nth 1 fn)) 'lambda))
2637 (cons (cons 'function (cdr fn))
2641 (defun byte-compile-funarg-2 (form)
2642 ;; (sort ... '(lambda (x) ..)) ==> (sort ... (function (lambda (x) ..)))
2643 ;; for cases where it's guaranteed that second arg will be used as a lambda.
2644 (byte-compile-normal-call
2645 (let ((fn (nth 2 form)))
2646 (if (and (eq (car-safe fn) 'quote)
2647 (eq (car-safe (nth 1 fn)) 'lambda))
2650 (cons (cons 'function (cdr fn))
2651 (cdr (cdr (cdr form))))))
2654 ;; (function foo) must compile like 'foo, not like (symbol-function 'foo).
2655 ;; Otherwise it will be incompatible with the interpreter,
2656 ;; and (funcall (function foo)) will lose with autoloads.
2658 (defun byte-compile-function-form (form)
2659 (byte-compile-constant
2660 (cond ((symbolp (nth 1 form))
2662 ;; If we're not allowed to use #[] syntax, then output a form like
2663 ;; '(lambda (..) (byte-code ..)) instead of a call to make-byte-code.
2664 ;; In this situation, calling make-byte-code at run-time will usually
2665 ;; be less efficient than processing a call to byte-code.
2666 ((byte-compile-version-cond byte-compile-compatibility)
2667 (byte-compile-byte-code-unmake (byte-compile-lambda (nth 1 form))))
2668 ((byte-compile-lambda (nth 1 form))))))
2670 (defun byte-compile-indent-to (form)
2671 (let ((len (length form)))
2673 (byte-compile-form (car (cdr form)))
2674 (byte-compile-out 'byte-indent-to 0))
2676 ;; no opcode for 2-arg case.
2677 (byte-compile-normal-call form))
2679 (byte-compile-subr-wrong-args form "1-2")))))
2681 (defun byte-compile-insert (form)
2682 (cond ((null (cdr form))
2683 (byte-compile-constant nil))
2684 ((and (not (byte-compile-version-cond
2685 byte-compile-compatibility))
2686 (<= (length form) 256))
2687 (mapcar 'byte-compile-form (cdr form))
2688 (if (cdr (cdr form))
2689 (byte-compile-out 'byte-insertN (length (cdr form)))
2690 (byte-compile-out 'byte-insert 0)))
2691 ((memq t (mapcar 'consp (cdr (cdr form))))
2692 (byte-compile-normal-call form))
2693 ;; We can split it; there is no function call after inserting 1st arg.
2695 (while (setq form (cdr form))
2696 (byte-compile-form (car form))
2697 (byte-compile-out 'byte-insert 0)
2699 (byte-compile-discard))))))
2701 (defun byte-compile-beginning-of-line (form)
2702 (if (not (byte-compile-constp (nth 1 form)))
2703 (byte-compile-normal-call form)
2706 (if (integerp (setq form (or (eval (nth 1 form)) 1)))
2708 (byte-compile-warn "Non-numeric arg to beginning-of-line: %s"
2710 (list '1- (list 'quote form))))
2712 (byte-compile-constant nil)))
2715 (byte-defop-compiler-1 setq)
2716 (byte-defop-compiler-1 setq-default)
2717 (byte-defop-compiler-1 quote)
2718 (byte-defop-compiler-1 quote-form)
2720 (defun byte-compile-setq (form)
2721 (let ((args (cdr form)))
2724 (byte-compile-form (car (cdr args)))
2725 (or for-effect (cdr (cdr args))
2726 (byte-compile-out 'byte-dup 0))
2727 (byte-compile-variable-ref 'byte-varset (car args))
2728 (setq args (cdr (cdr args))))
2729 ;; (setq), with no arguments.
2730 (byte-compile-form nil for-effect))
2731 (setq for-effect nil)))
2733 (defun byte-compile-setq-default (form)
2734 (let ((args (cdr form))
2738 (cons (list 'set-default (list 'quote (car args)) (car (cdr args)))
2740 (setq args (cdr (cdr args))))
2741 (byte-compile-form (cons 'progn (nreverse setters)))))
2743 (defun byte-compile-quote (form)
2744 (byte-compile-constant (car (cdr form))))
2746 (defun byte-compile-quote-form (form)
2747 (byte-compile-constant (byte-compile-top-level (nth 1 form))))
2750 ;;; control structures
2752 (defun byte-compile-body (body &optional for-effect)
2754 (byte-compile-form (car body) t)
2755 (setq body (cdr body)))
2756 (byte-compile-form (car body) for-effect))
2758 (defsubst byte-compile-body-do-effect (body)
2759 (byte-compile-body body for-effect)
2760 (setq for-effect nil))
2762 (defsubst byte-compile-form-do-effect (form)
2763 (byte-compile-form form for-effect)
2764 (setq for-effect nil))
2766 (byte-defop-compiler-1 inline byte-compile-progn)
2767 (byte-defop-compiler-1 progn)
2768 (byte-defop-compiler-1 prog1)
2769 (byte-defop-compiler-1 prog2)
2770 (byte-defop-compiler-1 if)
2771 (byte-defop-compiler-1 cond)
2772 (byte-defop-compiler-1 and)
2773 (byte-defop-compiler-1 or)
2774 (byte-defop-compiler-1 while)
2775 (byte-defop-compiler-1 funcall)
2776 (byte-defop-compiler-1 apply byte-compile-funarg)
2777 (byte-defop-compiler-1 mapcar byte-compile-funarg)
2778 (byte-defop-compiler-1 mapatoms byte-compile-funarg)
2779 (byte-defop-compiler-1 mapconcat byte-compile-funarg)
2780 (byte-defop-compiler-1 sort byte-compile-funarg-2)
2781 (byte-defop-compiler-1 let)
2782 (byte-defop-compiler-1 let*)
2784 (defun byte-compile-progn (form)
2785 (byte-compile-body-do-effect (cdr form)))
2787 (defun byte-compile-prog1 (form)
2788 (byte-compile-form-do-effect (car (cdr form)))
2789 (byte-compile-body (cdr (cdr form)) t))
2791 (defun byte-compile-prog2 (form)
2792 (byte-compile-form (nth 1 form) t)
2793 (byte-compile-form-do-effect (nth 2 form))
2794 (byte-compile-body (cdr (cdr (cdr form))) t))
2796 (defmacro byte-compile-goto-if (cond discard tag)
2797 (` (byte-compile-goto
2799 (if (, discard) 'byte-goto-if-not-nil 'byte-goto-if-not-nil-else-pop)
2800 (if (, discard) 'byte-goto-if-nil 'byte-goto-if-nil-else-pop))
2803 (defun byte-compile-if (form)
2804 (byte-compile-form (car (cdr form)))
2805 (if (null (nthcdr 3 form))
2807 (let ((donetag (byte-compile-make-tag)))
2808 (byte-compile-goto-if nil for-effect donetag)
2809 (byte-compile-form (nth 2 form) for-effect)
2810 (byte-compile-out-tag donetag))
2811 (let ((donetag (byte-compile-make-tag)) (elsetag (byte-compile-make-tag)))
2812 (byte-compile-goto 'byte-goto-if-nil elsetag)
2813 (byte-compile-form (nth 2 form) for-effect)
2814 (byte-compile-goto 'byte-goto donetag)
2815 (byte-compile-out-tag elsetag)
2816 (byte-compile-body (cdr (cdr (cdr form))) for-effect)
2817 (byte-compile-out-tag donetag)))
2818 (setq for-effect nil))
2820 (defun byte-compile-cond (clauses)
2821 (let ((donetag (byte-compile-make-tag))
2823 (while (setq clauses (cdr clauses))
2824 (setq clause (car clauses))
2825 (cond ((or (eq (car clause) t)
2826 (and (eq (car-safe (car clause)) 'quote)
2827 (car-safe (cdr-safe (car clause)))))
2828 ;; Unconditional clause
2829 (setq clause (cons t clause)
2832 (byte-compile-form (car clause))
2833 (if (null (cdr clause))
2834 ;; First clause is a singleton.
2835 (byte-compile-goto-if t for-effect donetag)
2836 (setq nexttag (byte-compile-make-tag))
2837 (byte-compile-goto 'byte-goto-if-nil nexttag)
2838 (byte-compile-body (cdr clause) for-effect)
2839 (byte-compile-goto 'byte-goto donetag)
2840 (byte-compile-out-tag nexttag)))))
2842 (and (cdr clause) (not (eq (car clause) t))
2843 (progn (byte-compile-form (car clause))
2844 (byte-compile-goto-if nil for-effect donetag)
2845 (setq clause (cdr clause))))
2846 (byte-compile-body-do-effect clause)
2847 (byte-compile-out-tag donetag)))
2849 (defun byte-compile-and (form)
2850 (let ((failtag (byte-compile-make-tag))
2853 (byte-compile-form-do-effect t)
2855 (byte-compile-form (car args))
2856 (byte-compile-goto-if nil for-effect failtag)
2857 (setq args (cdr args)))
2858 (byte-compile-form-do-effect (car args))
2859 (byte-compile-out-tag failtag))))
2861 (defun byte-compile-or (form)
2862 (let ((wintag (byte-compile-make-tag))
2865 (byte-compile-form-do-effect nil)
2867 (byte-compile-form (car args))
2868 (byte-compile-goto-if t for-effect wintag)
2869 (setq args (cdr args)))
2870 (byte-compile-form-do-effect (car args))
2871 (byte-compile-out-tag wintag))))
2873 (defun byte-compile-while (form)
2874 (let ((endtag (byte-compile-make-tag))
2875 (looptag (byte-compile-make-tag)))
2876 (byte-compile-out-tag looptag)
2877 (byte-compile-form (car (cdr form)))
2878 (byte-compile-goto-if nil for-effect endtag)
2879 (byte-compile-body (cdr (cdr form)) t)
2880 (byte-compile-goto 'byte-goto looptag)
2881 (byte-compile-out-tag endtag)
2882 (setq for-effect nil)))
2884 (defun byte-compile-funcall (form)
2885 (mapcar 'byte-compile-form (cdr form))
2886 (byte-compile-out 'byte-call (length (cdr (cdr form)))))
2889 (defun byte-compile-let (form)
2890 ;; First compute the binding values in the old scope.
2891 (let ((varlist (car (cdr form))))
2893 (if (consp (car varlist))
2894 (byte-compile-form (car (cdr (car varlist))))
2895 (byte-compile-push-constant nil))
2896 (setq varlist (cdr varlist))))
2897 (let ((byte-compile-bound-variables byte-compile-bound-variables) ;new scope
2898 (varlist (reverse (car (cdr form)))))
2900 (byte-compile-variable-ref 'byte-varbind (if (consp (car varlist))
2903 (setq varlist (cdr varlist)))
2904 (byte-compile-body-do-effect (cdr (cdr form)))
2905 (byte-compile-out 'byte-unbind (length (car (cdr form))))))
2907 (defun byte-compile-let* (form)
2908 (let ((byte-compile-bound-variables byte-compile-bound-variables) ;new scope
2909 (varlist (copy-sequence (car (cdr form)))))
2911 (if (atom (car varlist))
2912 (byte-compile-push-constant nil)
2913 (byte-compile-form (car (cdr (car varlist))))
2914 (setcar varlist (car (car varlist))))
2915 (byte-compile-variable-ref 'byte-varbind (car varlist))
2916 (setq varlist (cdr varlist)))
2917 (byte-compile-body-do-effect (cdr (cdr form)))
2918 (byte-compile-out 'byte-unbind (length (car (cdr form))))))
2921 (byte-defop-compiler-1 /= byte-compile-negated)
2922 (byte-defop-compiler-1 atom byte-compile-negated)
2923 (byte-defop-compiler-1 nlistp byte-compile-negated)
2925 (put '/= 'byte-compile-negated-op '=)
2926 (put 'atom 'byte-compile-negated-op 'consp)
2927 (put 'nlistp 'byte-compile-negated-op 'listp)
2929 (defun byte-compile-negated (form)
2930 (byte-compile-form-do-effect (byte-compile-negation-optimizer form)))
2932 ;; Even when optimization is off, /= is optimized to (not (= ...)).
2933 (defun byte-compile-negation-optimizer (form)
2934 ;; an optimizer for forms where <form1> is less efficient than (not <form2>)
2936 (cons (or (get (car form) 'byte-compile-negated-op)
2938 "Compiler error: `%s' has no `byte-compile-negated-op' property"
2942 ;;; other tricky macro-like special-forms
2944 (byte-defop-compiler-1 catch)
2945 (byte-defop-compiler-1 unwind-protect)
2946 (byte-defop-compiler-1 condition-case)
2947 (byte-defop-compiler-1 save-excursion)
2948 (byte-defop-compiler-1 save-current-buffer)
2949 (byte-defop-compiler-1 save-restriction)
2950 (byte-defop-compiler-1 save-window-excursion)
2951 (byte-defop-compiler-1 with-output-to-temp-buffer)
2952 (byte-defop-compiler-1 track-mouse)
2954 (defun byte-compile-catch (form)
2955 (byte-compile-form (car (cdr form)))
2956 (byte-compile-push-constant
2957 (byte-compile-top-level (cons 'progn (cdr (cdr form))) for-effect))
2958 (byte-compile-out 'byte-catch 0))
2960 (defun byte-compile-unwind-protect (form)
2961 (byte-compile-push-constant
2962 (byte-compile-top-level-body (cdr (cdr form)) t))
2963 (byte-compile-out 'byte-unwind-protect 0)
2964 (byte-compile-form-do-effect (car (cdr form)))
2965 (byte-compile-out 'byte-unbind 1))
2967 (defun byte-compile-track-mouse (form)
2974 (byte-compile-top-level-body (cdr form))))))))
2976 (defun byte-compile-condition-case (form)
2977 (let* ((var (nth 1 form))
2978 (byte-compile-bound-variables
2979 (if var (cons var byte-compile-bound-variables)
2980 byte-compile-bound-variables)))
2983 "%s is not a variable-name or nil (in condition-case)" var))
2984 (byte-compile-push-constant var)
2985 (byte-compile-push-constant (byte-compile-top-level
2986 (nth 2 form) for-effect))
2987 (let ((clauses (cdr (cdr (cdr form))))
2990 (let* ((clause (car clauses))
2991 (condition (car clause)))
2992 (cond ((not (or (symbolp condition)
2993 (and (listp condition)
2994 (let ((syms condition) (ok t))
2996 (if (not (symbolp (car syms)))
2998 (setq syms (cdr syms)))
3001 "%s is not a condition name or list of such (in condition-case)"
3002 (prin1-to-string condition)))
3003 ;; ((not (or (eq condition 't)
3004 ;; (and (stringp (get condition 'error-message))
3005 ;; (consp (get condition 'error-conditions)))))
3006 ;; (byte-compile-warn
3007 ;; "%s is not a known condition name (in condition-case)"
3010 (setq compiled-clauses
3011 (cons (cons condition
3012 (byte-compile-top-level-body
3013 (cdr clause) for-effect))
3015 (setq clauses (cdr clauses)))
3016 (byte-compile-push-constant (nreverse compiled-clauses)))
3017 (byte-compile-out 'byte-condition-case 0)))
3020 (defun byte-compile-save-excursion (form)
3021 (byte-compile-out 'byte-save-excursion 0)
3022 (byte-compile-body-do-effect (cdr form))
3023 (byte-compile-out 'byte-unbind 1))
3025 (defun byte-compile-save-restriction (form)
3026 (byte-compile-out 'byte-save-restriction 0)
3027 (byte-compile-body-do-effect (cdr form))
3028 (byte-compile-out 'byte-unbind 1))
3030 (defun byte-compile-save-current-buffer (form)
3031 (byte-compile-out 'byte-save-current-buffer 0)
3032 (byte-compile-body-do-effect (cdr form))
3033 (byte-compile-out 'byte-unbind 1))
3035 (defun byte-compile-save-window-excursion (form)
3036 (byte-compile-push-constant
3037 (byte-compile-top-level-body (cdr form) for-effect))
3038 (byte-compile-out 'byte-save-window-excursion 0))
3040 (defun byte-compile-with-output-to-temp-buffer (form)
3041 (byte-compile-form (car (cdr form)))
3042 (byte-compile-out 'byte-temp-output-buffer-setup 0)
3043 (byte-compile-body (cdr (cdr form)))
3044 (byte-compile-out 'byte-temp-output-buffer-show 0))
3047 ;;; top-level forms elsewhere
3049 (byte-defop-compiler-1 defun)
3050 (byte-defop-compiler-1 defmacro)
3051 (byte-defop-compiler-1 defvar)
3052 (byte-defop-compiler-1 defconst byte-compile-defvar)
3053 (byte-defop-compiler-1 autoload)
3054 (byte-defop-compiler-1 lambda byte-compile-lambda-form)
3055 (byte-defop-compiler-1 defalias)
3057 (defun byte-compile-defun (form)
3058 ;; This is not used for file-level defuns with doc strings.
3059 (byte-compile-two-args ; Use this to avoid byte-compile-fset's warning.
3060 (list 'fset (list 'quote (nth 1 form))
3061 (byte-compile-byte-code-maker
3062 (byte-compile-lambda (cons 'lambda (cdr (cdr form)))))))
3063 (byte-compile-discard)
3064 (byte-compile-constant (nth 1 form)))
3066 (defun byte-compile-defmacro (form)
3067 ;; This is not used for file-level defmacros with doc strings.
3068 (byte-compile-body-do-effect
3069 (list (list 'fset (list 'quote (nth 1 form))
3070 (let ((code (byte-compile-byte-code-maker
3071 (byte-compile-lambda
3072 (cons 'lambda (cdr (cdr form)))))))
3073 (if (eq (car-safe code) 'make-byte-code)
3074 (list 'cons ''macro code)
3075 (list 'quote (cons 'macro (eval code))))))
3076 (list 'quote (nth 1 form)))))
3078 (defun byte-compile-defvar (form)
3079 ;; This is not used for file-level defvar/consts with doc strings.
3080 (let ((var (nth 1 form))
3081 (value (nth 2 form))
3082 (string (nth 3 form)))
3083 (if (memq 'free-vars byte-compile-warnings)
3084 (setq byte-compile-bound-variables
3085 (cons var byte-compile-bound-variables)))
3086 (byte-compile-body-do-effect
3087 (list (if (cdr (cdr form))
3088 (if (eq (car form) 'defconst)
3089 (list 'setq var value)
3090 (list 'or (list 'boundp (list 'quote var))
3091 (list 'setq var value))))
3092 ;; Put the defined variable in this library's load-history entry
3093 ;; just as a real defvar would.
3094 (list 'setq 'current-load-list
3095 (list 'cons (list 'quote var)
3096 'current-load-list))
3098 (list 'put (list 'quote var) ''variable-documentation string))
3099 (list 'quote var)))))
3101 (defun byte-compile-autoload (form)
3102 (and (byte-compile-constp (nth 1 form))
3103 (byte-compile-constp (nth 5 form))
3104 (eval (nth 5 form)) ; macro-p
3105 (not (fboundp (eval (nth 1 form))))
3107 "The compiler ignores `autoload' except at top level. You should
3108 probably put the autoload of the macro `%s' at top-level."
3109 (eval (nth 1 form))))
3110 (byte-compile-normal-call form))
3112 ;; Lambda's in valid places are handled as special cases by various code.
3113 ;; The ones that remain are errors.
3114 (defun byte-compile-lambda-form (form)
3115 (error "`lambda' used as function name is invalid"))
3117 ;; Compile normally, but deal with warnings for the function being defined.
3118 (defun byte-compile-defalias (form)
3119 (if (and (consp (cdr form)) (consp (nth 1 form))
3120 (eq (car (nth 1 form)) 'quote)
3121 (consp (cdr (nth 1 form)))
3122 (symbolp (nth 1 (nth 1 form)))
3123 (consp (nthcdr 2 form))
3124 (consp (nth 2 form))
3125 (eq (car (nth 2 form)) 'quote)
3126 (consp (cdr (nth 2 form)))
3127 (symbolp (nth 1 (nth 2 form))))
3129 (byte-compile-defalias-warn (nth 1 (nth 1 form))
3130 (nth 1 (nth 2 form)))
3131 (setq byte-compile-function-environment
3132 (cons (cons (nth 1 (nth 1 form))
3133 (nth 1 (nth 2 form)))
3134 byte-compile-function-environment))))
3135 (byte-compile-normal-call form))
3137 ;; Turn off warnings about prior calls to the function being defalias'd.
3138 ;; This could be smarter and compare those calls with
3139 ;; the function it is being aliased to.
3140 (defun byte-compile-defalias-warn (new alias)
3141 (let ((calls (assq new byte-compile-unresolved-functions)))
3143 (setq byte-compile-unresolved-functions
3144 (delq calls byte-compile-unresolved-functions)))))
3148 ;; Note: Most operations will strip off the 'TAG, but it speeds up
3149 ;; optimization to have the 'TAG as a part of the tag.
3150 ;; Tags will be (TAG . (tag-number . stack-depth)).
3151 (defun byte-compile-make-tag ()
3152 (list 'TAG (setq byte-compile-tag-number (1+ byte-compile-tag-number))))
3155 (defun byte-compile-out-tag (tag)
3156 (setq byte-compile-output (cons tag byte-compile-output))
3159 ;; ## remove this someday
3160 (and byte-compile-depth
3161 (not (= (cdr (cdr tag)) byte-compile-depth))
3162 (error "Compiler bug: depth conflict at tag %d" (car (cdr tag))))
3163 (setq byte-compile-depth (cdr (cdr tag))))
3164 (setcdr (cdr tag) byte-compile-depth)))
3166 (defun byte-compile-goto (opcode tag)
3167 (setq byte-compile-output (cons (cons opcode tag) byte-compile-output))
3168 (setcdr (cdr tag) (if (memq opcode byte-goto-always-pop-ops)
3169 (1- byte-compile-depth)
3170 byte-compile-depth))
3171 (setq byte-compile-depth (and (not (eq opcode 'byte-goto))
3172 (1- byte-compile-depth))))
3174 (defun byte-compile-out (opcode offset)
3175 (setq byte-compile-output (cons (cons opcode offset) byte-compile-output))
3176 (cond ((eq opcode 'byte-call)
3177 (setq byte-compile-depth (- byte-compile-depth offset)))
3178 ((eq opcode 'byte-return)
3179 ;; This is actually an unnecessary case, because there should be
3180 ;; no more opcodes behind byte-return.
3181 (setq byte-compile-depth nil))
3183 (setq byte-compile-depth (+ byte-compile-depth
3184 (or (aref byte-stack+-info
3185 (symbol-value opcode))
3187 byte-compile-maxdepth (max byte-compile-depth
3188 byte-compile-maxdepth))))
3189 ;;(if (< byte-compile-depth 0) (error "Compiler error: stack underflow"))
3195 (defun byte-compile-annotate-call-tree (form)
3197 ;; annotate the current call
3198 (if (setq entry (assq (car form) byte-compile-call-tree))
3199 (or (memq byte-compile-current-form (nth 1 entry)) ;callers
3201 (cons byte-compile-current-form (nth 1 entry))))
3202 (setq byte-compile-call-tree
3203 (cons (list (car form) (list byte-compile-current-form) nil)
3204 byte-compile-call-tree)))
3205 ;; annotate the current function
3206 (if (setq entry (assq byte-compile-current-form byte-compile-call-tree))
3207 (or (memq (car form) (nth 2 entry)) ;called
3208 (setcar (cdr (cdr entry))
3209 (cons (car form) (nth 2 entry))))
3210 (setq byte-compile-call-tree
3211 (cons (list byte-compile-current-form nil (list (car form)))
3212 byte-compile-call-tree)))
3215 ;; Renamed from byte-compile-report-call-tree
3216 ;; to avoid interfering with completion of byte-compile-file.
3218 (defun display-call-tree (&optional filename)
3219 "Display a call graph of a specified file.
3220 This lists which functions have been called, what functions called
3221 them, and what functions they call. The list includes all functions
3222 whose definitions have been compiled in this Emacs session, as well as
3223 all functions called by those functions.
3225 The call graph does not include macros, inline functions, or
3226 primitives that the byte-code interpreter knows about directly \(eq,
3229 The call tree also lists those functions which are not known to be called
3230 \(that is, to which no calls have been compiled\), and which cannot be
3231 invoked interactively."
3233 (message "Generating call tree...")
3234 (with-output-to-temp-buffer "*Call-Tree*"
3235 (set-buffer "*Call-Tree*")
3237 (message "Generating call tree... (sorting on %s)"
3238 byte-compile-call-tree-sort)
3239 (insert "Call tree for "
3240 (cond ((null byte-compile-current-file) (or filename "???"))
3241 ((stringp byte-compile-current-file)
3242 byte-compile-current-file)
3243 (t (buffer-name byte-compile-current-file)))
3245 (prin1-to-string byte-compile-call-tree-sort)
3247 (if byte-compile-call-tree-sort
3248 (setq byte-compile-call-tree
3249 (sort byte-compile-call-tree
3250 (cond ((eq byte-compile-call-tree-sort 'callers)
3251 (function (lambda (x y) (< (length (nth 1 x))
3252 (length (nth 1 y))))))
3253 ((eq byte-compile-call-tree-sort 'calls)
3254 (function (lambda (x y) (< (length (nth 2 x))
3255 (length (nth 2 y))))))
3256 ((eq byte-compile-call-tree-sort 'calls+callers)
3257 (function (lambda (x y) (< (+ (length (nth 1 x))
3259 (+ (length (nth 1 y))
3260 (length (nth 2 y)))))))
3261 ((eq byte-compile-call-tree-sort 'name)
3262 (function (lambda (x y) (string< (car x)
3264 (t (error "`byte-compile-call-tree-sort': `%s' - unknown sort mode"
3265 byte-compile-call-tree-sort))))))
3266 (message "Generating call tree...")
3267 (let ((rest byte-compile-call-tree)
3268 (b (current-buffer))
3272 (prin1 (car (car rest)) b)
3273 (setq callers (nth 1 (car rest))
3274 calls (nth 2 (car rest)))
3276 (cond ((not (fboundp (setq f (car (car rest)))))
3278 " <top level>";; shouldn't insert nil then, actually -sk
3280 ((subrp (setq f (symbol-function f)))
3283 (format " ==> %s" f))
3284 ((byte-code-function-p f)
3285 "<compiled function>")
3287 "<malformed function>")
3288 ((eq 'macro (car f))
3289 (if (or (byte-code-function-p (cdr f))
3290 (assq 'byte-code (cdr (cdr (cdr f)))))
3293 ((assq 'byte-code (cdr (cdr f)))
3294 "<compiled lambda>")
3295 ((eq 'lambda (car f))
3298 (format " (%d callers + %d calls = %d)"
3299 ;; Does the optimizer eliminate common subexpressions?-sk
3302 (+ (length callers) (length calls)))
3306 (insert " called by:\n")
3308 (insert " " (if (car callers)
3309 (mapconcat 'symbol-name callers ", ")
3311 (let ((fill-prefix " "))
3312 (fill-region-as-paragraph p (point)))))
3315 (insert " calls:\n")
3317 (insert " " (mapconcat 'symbol-name calls ", "))
3318 (let ((fill-prefix " "))
3319 (fill-region-as-paragraph p (point)))))
3321 (setq rest (cdr rest)))
3323 (message "Generating call tree...(finding uncalled functions...)")
3324 (setq rest byte-compile-call-tree)
3325 (let ((uncalled nil))
3327 (or (nth 1 (car rest))
3328 (null (setq f (car (car rest))))
3329 (byte-compile-fdefinition f t)
3330 (commandp (byte-compile-fdefinition f nil))
3331 (setq uncalled (cons f uncalled)))
3332 (setq rest (cdr rest)))
3334 (let ((fill-prefix " "))
3335 (insert "Noninteractive functions not known to be called:\n ")
3337 (insert (mapconcat 'symbol-name (nreverse uncalled) ", "))
3338 (fill-region-as-paragraph p (point)))))
3340 (message "Generating call tree...done.")
3344 ;;; by crl@newton.purdue.edu
3345 ;;; Only works noninteractively.
3347 (defun batch-byte-compile ()
3348 "Run `byte-compile-file' on the files remaining on the command line.
3349 Use this from the command line, with `-batch';
3350 it won't work in an interactive Emacs.
3351 Each file is processed even if an error occurred previously.
3352 For example, invoke \"emacs -batch -f batch-byte-compile $emacs/ ~/*.el\""
3353 ;; command-line-args-left is what is left of the command line (from startup.el)
3354 (defvar command-line-args-left) ;Avoid 'free variable' warning
3355 (if (not noninteractive)
3356 (error "`batch-byte-compile' is to be used only with -batch"))
3358 (while command-line-args-left
3359 (if (file-directory-p (expand-file-name (car command-line-args-left)))
3360 (let ((files (directory-files (car command-line-args-left)))
3363 (if (and (string-match emacs-lisp-file-regexp (car files))
3364 (not (auto-save-file-name-p (car files)))
3365 (setq source (expand-file-name (car files)
3366 (car command-line-args-left)))
3367 (setq dest (byte-compile-dest-file source))
3368 (file-exists-p dest)
3369 (file-newer-than-file-p source dest))
3370 (if (null (batch-byte-compile-file source))
3372 (setq files (cdr files))))
3373 (if (null (batch-byte-compile-file (car command-line-args-left)))
3375 (setq command-line-args-left (cdr command-line-args-left)))
3377 (kill-emacs (if error 1 0))))
3379 (defun batch-byte-compile-file (file)
3381 (byte-compile-file file)
3383 (message (if (cdr err)
3384 ">>Error occurred processing %s: %s (%s)"
3385 ">>Error occurred processing %s: %s")
3387 (get (car err) 'error-message)
3388 (prin1-to-string (cdr err)))
3392 (defun batch-byte-recompile-directory ()
3393 "Runs `byte-recompile-directory' on the dirs remaining on the command line.
3394 Must be used only with `-batch', and kills Emacs on completion.
3395 For example, invoke `emacs -batch -f batch-byte-recompile-directory .'."
3396 ;; command-line-args-left is what is left of the command line (startup.el)
3397 (defvar command-line-args-left) ;Avoid 'free variable' warning
3398 (if (not noninteractive)
3399 (error "batch-byte-recompile-directory is to be used only with -batch"))
3400 (or command-line-args-left
3401 (setq command-line-args-left '(".")))
3402 (while command-line-args-left
3403 (byte-recompile-directory (car command-line-args-left))
3404 (setq command-line-args-left (cdr command-line-args-left)))
3408 (make-obsolete 'dot 'point)
3409 (make-obsolete 'dot-max 'point-max)
3410 (make-obsolete 'dot-min 'point-min)
3411 (make-obsolete 'dot-marker 'point-marker)
3413 (make-obsolete 'buffer-flush-undo 'buffer-disable-undo)
3414 (make-obsolete 'baud-rate "use the baud-rate variable instead")
3415 (make-obsolete 'compiled-function-p 'byte-code-function-p)
3416 (make-obsolete 'define-function 'defalias)
3417 (make-obsolete-variable 'auto-fill-hook 'auto-fill-function)
3418 (make-obsolete-variable 'blink-paren-hook 'blink-paren-function)
3419 (make-obsolete-variable 'lisp-indent-hook 'lisp-indent-function)
3420 (make-obsolete-variable 'temp-buffer-show-hook
3421 'temp-buffer-show-function)
3422 (make-obsolete-variable 'inhibit-local-variables
3423 "use enable-local-variables (with the reversed sense).")
3424 (make-obsolete-variable 'unread-command-char
3425 "use unread-command-events instead. That variable is a list of events to reread, so it now uses nil to mean `no event', instead of -1.")
3426 (make-obsolete-variable 'unread-command-event
3427 "use unread-command-events; which is a list of events rather than a single event.")
3428 (make-obsolete-variable 'suspend-hooks 'suspend-hook)
3429 (make-obsolete-variable 'comment-indent-hook 'comment-indent-function)
3430 (make-obsolete-variable 'meta-flag "Use the set-input-mode function instead.")
3431 (make-obsolete-variable 'executing-macro 'executing-kbd-macro)
3432 (make-obsolete-variable 'before-change-function
3433 "use before-change-functions; which is a list of functions rather than a single function.")
3434 (make-obsolete-variable 'after-change-function
3435 "use after-change-functions; which is a list of functions rather than a single function.")
3436 (make-obsolete-variable 'font-lock-doc-string-face 'font-lock-string-face)
3437 (make-obsolete-variable 'post-command-idle-hook
3438 "use timers instead, with `run-with-idle-timer'.")
3439 (make-obsolete-variable 'post-command-idle-delay
3440 "use timers instead, with `run-with-idle-timer'.")
3442 (provide 'byte-compile)
3446 ;;; report metering (see the hacks in bytecode.c)
3448 (defun byte-compile-report-ops ()
3449 (defvar byte-code-meter)
3450 (with-output-to-temp-buffer "*Meter*"
3451 (set-buffer "*Meter*")
3452 (let ((i 0) n op off)
3454 (setq n (aref (aref byte-code-meter 0) i)
3456 (if t ;(not (zerop n))
3460 (cond ((< op byte-nth)
3461 (setq off (logand op 7))
3462 (setq op (logand op 248)))
3463 ((>= op byte-constant)
3464 (setq off (- op byte-constant)
3466 (setq op (aref byte-code-vector op))
3467 (insert (format "%-4d" i))
3468 (insert (symbol-name op))
3469 (if off (insert " [" (int-to-string off) "]"))
3471 (insert (int-to-string n) "\n")))
3474 ;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when bytecomp compiles
3475 ;; itself, compile some of its most used recursive functions (at load time).
3478 (or (byte-code-function-p (symbol-function 'byte-compile-form))
3479 (assq 'byte-code (symbol-function 'byte-compile-form))
3480 (let ((byte-optimize nil) ; do it fast
3481 (byte-compile-warnings nil))
3482 (mapcar '(lambda (x)
3483 (or noninteractive (message "compiling %s..." x))
3485 (or noninteractive (message "compiling %s...done" x)))
3486 '(byte-compile-normal-call
3489 ;; Inserted some more than necessary, to speed it up.
3490 byte-compile-top-level
3491 byte-compile-out-toplevel
3492 byte-compile-constant
3493 byte-compile-variable-ref))))
3496 ;;; bytecomp.el ends here