1 ;;; bytecomp.el --- compilation of Lisp code into byte code.
3 ;;; Copyright (C) 1985-1987, 1991-1994 Free Software Foundation, Inc.
4 ;;; Copyright (C) 1996 Ben Wing.
6 ;; Author: Jamie Zawinski <jwz@jwz.org>
7 ;; Hallvard Furuseth <hbf@ulrik.uio.no>
10 ;; Subsequently modified by RMS and others.
12 (defconst byte-compile-version (purecopy "2.26 XEmacs; 1998-10-07."))
14 ;; This file is part of XEmacs.
16 ;; XEmacs is free software; you can redistribute it and/or modify it
17 ;; 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 ;; XEmacs is distributed in the hope that it will be useful, but
22 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 ;; General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with XEmacs; 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.
31 ;;; Synched up with: FSF 19.30.
35 ;; The Emacs Lisp byte compiler. This crunches lisp source into a
36 ;; sort of p-code which takes up less space and can be interpreted
37 ;; faster. The user entry points are byte-compile-file,
38 ;; byte-recompile-directory and byte-compile-buffer.
42 ;;; ========================================================================
44 ;;; byte-recompile-directory, byte-compile-file,
45 ;;; batch-byte-compile, batch-byte-recompile-directory,
46 ;;; byte-compile, compile-defun,
49 ;;; (byte-compile-buffer and byte-compile-and-load-file were turned off
50 ;;; because they are not terribly useful and get in the way of completion.)
51 ;;; But I'm leaving them. --ben
53 ;;; This version of the byte compiler has the following improvements:
54 ;;; + optimization of compiled code:
55 ;;; - removal of unreachable code;
56 ;;; - removal of calls to side-effectless functions whose return-value
58 ;;; - compile-time evaluation of safe constant forms, such as (consp nil)
60 ;;; - open-coding of literal lambdas;
61 ;;; - peephole optimization of emitted code;
62 ;;; - trivial functions are left uncompiled for speed.
63 ;;; + support for inline functions;
64 ;;; + compile-time evaluation of arbitrary expressions;
65 ;;; + compile-time warning messages for:
66 ;;; - functions being redefined with incompatible arglists;
67 ;;; - functions being redefined as macros, or vice-versa;
68 ;;; - functions or macros defined multiple times in the same file;
69 ;;; - functions being called with the incorrect number of arguments;
70 ;;; - functions being called which are not defined globally, in the
71 ;;; file, or as autoloads;
72 ;;; - assignment and reference of undeclared free variables;
73 ;;; - various syntax errors;
74 ;;; + correct compilation of nested defuns, defmacros, defvars and defsubsts;
75 ;;; + correct compilation of top-level uses of macros;
76 ;;; + the ability to generate a histogram of functions called.
78 ;;; User customization variables:
80 ;;; byte-compile-verbose Whether to report the function currently being
81 ;;; compiled in the minibuffer;
82 ;;; byte-optimize Whether to do optimizations; this may be
83 ;;; t, nil, 'source, or 'byte;
84 ;;; byte-optimize-log Whether to report (in excruciating detail)
85 ;;; exactly which optimizations have been made.
86 ;;; This may be t, nil, 'source, or 'byte;
87 ;;; byte-compile-error-on-warn Whether to stop compilation when a warning is
89 ;;; byte-compile-delete-errors Whether the optimizer may delete calls or
90 ;;; variable references that are side-effect-free
91 ;;; except that they may return an error.
92 ;;; byte-compile-generate-call-tree Whether to generate a histogram of
93 ;;; function calls. This can be useful for
94 ;;; finding unused functions, as well as simple
95 ;;; performance metering.
96 ;;; byte-compile-warnings List of warnings to issue, or t. May contain
97 ;;; 'free-vars (references to variables not in the
98 ;;; current lexical scope)
99 ;;; 'unused-vars (non-global variables bound but
101 ;;; 'unresolved (calls to unknown functions)
102 ;;; 'callargs (lambda calls with args that don't
103 ;;; match the lambda's definition)
104 ;;; 'subr-callargs (calls to subrs with args that
105 ;;; don't match the subr's definition)
106 ;;; 'redefine (function cell redefined from
107 ;;; a macro to a lambda or vice versa,
108 ;;; or redefined to take other args)
109 ;;; 'obsolete (obsolete variables and functions)
110 ;;; 'pedantic (references to Emacs-compatible
112 ;;; byte-compile-emacs19-compatibility Whether the compiler should
113 ;;; generate .elc files which can be loaded into
114 ;;; generic emacs 19.
115 ;;; emacs-lisp-file-regexp Regexp for the extension of source-files;
116 ;;; see also the function byte-compile-dest-file.
117 ;;; byte-compile-overwrite-file If nil, delete old .elc files before saving.
119 ;;; Most of the above parameters can also be set on a file-by-file basis; see
120 ;;; the documentation of the `byte-compiler-options' macro.
124 ;;; o The form `defsubst' is just like `defun', except that the function
125 ;;; generated will be open-coded in compiled code which uses it. This
126 ;;; means that no function call will be generated, it will simply be
127 ;;; spliced in. Lisp functions calls are very slow, so this can be a
130 ;;; You can generally accomplish the same thing with `defmacro', but in
131 ;;; that case, the defined procedure can't be used as an argument to
134 ;;; o You can make a given function be inline even if it has already been
135 ;;; defined with `defun' by using the `proclaim-inline' form like so:
136 ;;; (proclaim-inline my-function)
137 ;;; This is, in fact, exactly what `defsubst' does. To make a function no
138 ;;; longer be inline, you must use `proclaim-notinline'. Beware that if
139 ;;; you define a function with `defsubst' and later redefine it with
140 ;;; `defun', it will still be open-coded until you use proclaim-notinline.
142 ;;; o You can also open-code one particular call to a function without
143 ;;; open-coding all calls. Use the 'inline' form to do this, like so:
145 ;;; (inline (foo 1 2 3)) ;; `foo' will be open-coded
147 ;;; (inline ;; `foo' and `baz' will be
148 ;;; (foo 1 2 3 (bar 5)) ;; open-coded, but `bar' will not.
151 ;;; o It is possible to open-code a function in the same file it is defined
152 ;;; in without having to load that file before compiling it. the
153 ;;; byte-compiler has been modified to remember function definitions in
154 ;;; the compilation environment in the same way that it remembers macro
157 ;;; o Forms like ((lambda ...) ...) are open-coded.
159 ;;; o The form `eval-when-compile' is like progn, except that the body
160 ;;; is evaluated at compile-time. When it appears at top-level, this
161 ;;; is analogous to the Common Lisp idiom (eval-when (compile) ...).
162 ;;; When it does not appear at top-level, it is similar to the
163 ;;; Common Lisp #. reader macro (but not in interpreted code).
165 ;;; o The form `eval-and-compile' is similar to eval-when-compile, but
166 ;;; the whole form is evalled both at compile-time and at run-time.
168 ;;; o The command M-x byte-compile-and-load-file does what you'd think.
170 ;;; o The command compile-defun is analogous to eval-defun.
172 ;;; o If you run byte-compile-file on a filename which is visited in a
173 ;;; buffer, and that buffer is modified, you are asked whether you want
174 ;;; to save the buffer before compiling.
176 ;;; o You can add this to /etc/magic to make file(1) recognize the files
177 ;;; generated by this compiler:
179 ;;; 0 string ;ELC GNU Emacs Lisp compiled file,
180 ;;; >4 byte x version %d
184 ;;; o Should implement declarations and proclamations, notably special,
185 ;;; unspecial, and ignore. Do this in such a way as to not break cl.el.
186 ;;; o The bound-but-not-used warnings are not issued for variables whose
187 ;;; bindings were established in the arglist, due to the lack of an
188 ;;; ignore declaration. Once ignore exists, this should be turned on.
189 ;;; o Warn about functions and variables defined but not used?
190 ;;; Maybe add some kind of `export' declaration for this?
191 ;;; (With interactive functions being automatically exported?)
192 ;;; o Any reference to a variable, even one which is a no-op, will cause
193 ;;; the warning not to be given. Possibly we could use the for-effect
194 ;;; flag to determine when this reference is useless; possibly more
195 ;;; complex flow analysis would be necessary.
196 ;;; o If the optimizer deletes a variable reference, we might be left with
197 ;;; a bound-but-not-referenced warning. Generally this is ok, but not if
198 ;;; it's a synergistic result of macroexpansion. Need some way to note
199 ;;; that a varref is being optimized away? Of course it would be nice to
200 ;;; optimize away the binding too, someday, but it's unsafe today.
201 ;;; o (See byte-optimize.el for the optimization TODO list.)
205 (or (fboundp 'defsubst)
206 ;; This really ought to be loaded already!
207 (load-library "bytecomp-runtime"))
210 (defvar byte-compile-single-version nil
211 "If this is true, the choice of emacs version (v19 or v20) byte-codes will
212 be hard-coded into bytecomp when it compiles itself. If the compiler itself
213 is compiled with optimization, this causes a speedup.")
216 (byte-compile-single-version
217 (defmacro byte-compile-single-version () t)
218 (defmacro byte-compile-version-cond (cond) (list 'quote (eval cond))))
220 (defmacro byte-compile-single-version () nil)
221 (defmacro byte-compile-version-cond (cond) cond)))
224 (defvar emacs-lisp-file-regexp (purecopy "\\.el$")
225 "*Regexp which matches Emacs Lisp source files.
226 You may want to redefine `byte-compile-dest-file' if you change this.")
228 ;; This enables file name handlers such as jka-compr
229 ;; to remove parts of the file name that should not be copied
230 ;; through to the output file name.
231 (defun byte-compiler-base-file-name (filename)
232 (let ((handler (find-file-name-handler filename
233 'byte-compiler-base-file-name)))
235 (funcall handler 'byte-compiler-base-file-name filename)
238 (unless (fboundp 'byte-compile-dest-file)
239 ;; The user may want to redefine this along with emacs-lisp-file-regexp,
240 ;; so only define it if it is undefined.
241 (defun byte-compile-dest-file (filename)
242 "Convert an Emacs Lisp source file name to a compiled file name."
243 (setq filename (byte-compiler-base-file-name filename))
244 (setq filename (file-name-sans-versions filename))
245 (if (string-match emacs-lisp-file-regexp filename)
246 (concat (substring filename 0 (match-beginning 0)) ".elc")
247 (concat filename ".elc"))))
249 ;; This can be the 'byte-compile property of any symbol.
250 (autoload 'byte-compile-inline-expand "byte-optimize")
252 ;; This is the entrypoint to the lapcode optimizer pass1.
253 (autoload 'byte-optimize-form "byte-optimize")
254 ;; This is the entrypoint to the lapcode optimizer pass2.
255 (autoload 'byte-optimize-lapcode "byte-optimize")
256 (autoload 'byte-compile-unfold-lambda "byte-optimize")
258 ;; This is the entry point to the decompiler, which is used by the
259 ;; disassembler. The disassembler just requires 'byte-compile, but
260 ;; that doesn't define this function, so this seems to be a reasonable
262 (autoload 'byte-decompile-bytecode "byte-optimize")
264 (defvar byte-compile-verbose
265 (and (not noninteractive) (> (device-baud-rate) search-slow-speed))
266 "*Non-nil means print messages describing progress of byte-compiler.")
268 (defvar byte-compile-emacs19-compatibility
269 (not (emacs-version>= 20))
270 "*Non-nil means generate output that can run in Emacs 19.")
272 (defvar byte-compile-print-gensym t
273 "*Non-nil means generate code that creates unique symbols at run-time.
274 This is achieved by printing uninterned symbols using the `#:SYMBOL'
275 notation, so that they will be read uninterned when run.
277 With this feature, code that uses uninterned symbols in macros will
278 not be runnable under pre-21.0 XEmacsen.
280 When `byte-compile-emacs19-compatibility' is non-nil, this variable is
281 ignored and considered to be nil.")
283 (defvar byte-optimize t
284 "*Enables optimization in the byte compiler.
285 nil means don't do any optimization.
286 t means do all optimizations.
287 `source' means do source-level optimizations only.
288 `byte' means do code-level optimizations only.")
290 (defvar byte-compile-delete-errors t
291 "*If non-nil, the optimizer may delete forms that may signal an error.
292 This includes variable references and calls to functions such as `car'.")
295 (defvar byte-compile-new-bytecodes nil
296 "This is completely ignored. It is only around for backwards
300 ;; FSF enables byte-compile-dynamic-docstrings but not byte-compile-dynamic
301 ;; by default. This would be a reasonable conservative approach except
302 ;; for the fact that if you enable either of these, you get incompatible
303 ;; byte code that can't be read by XEmacs 19.13 or before or FSF 19.28 or
306 ;; Therefore, neither is enabled for 19.14. Both are enabled for 20.0
307 ;; because we have no reason to be conservative about changing the
308 ;; way things work. (Ben)
310 ;; However, I don't think that defaulting byte-compile-dynamic to nil
311 ;; is a compatibility issue - rather it is a performance issue.
312 ;; Therefore I am setting byte-compile-dynamic back to nil. (mrb)
314 (defvar byte-compile-dynamic nil
315 "*If non-nil, compile function bodies so they load lazily.
316 They are hidden comments in the compiled file, and brought into core when the
319 To enable this option, make it a file-local variable
320 in the source file you want it to apply to.
321 For example, add -*-byte-compile-dynamic: t;-*- on the first line.
323 When this option is true, if you load the compiled file and then move it,
324 the functions you loaded will not be able to run.")
326 (defvar byte-compile-dynamic-docstrings (emacs-version>= 20)
327 "*If non-nil, compile doc strings for lazy access.
328 We bury the doc strings of functions and variables
329 inside comments in the file, and bring them into core only when they
332 When this option is true, if you load the compiled file and then move it,
333 you won't be able to find the documentation of anything in that file.
335 To disable this option for a certain file, make it a file-local variable
336 in the source file. For example, add this to the first line:
337 -*-byte-compile-dynamic-docstrings:nil;-*-
338 You can also set the variable globally.
340 This option is enabled by default because it reduces Emacs memory usage.")
342 (defvar byte-optimize-log nil
343 "*If true, the byte-compiler will log its optimizations into *Compile-Log*.
344 If this is 'source, then only source-level optimizations will be logged.
345 If it is 'byte, then only byte-level optimizations will be logged.")
347 (defvar byte-compile-error-on-warn nil
348 "*If true, the byte-compiler reports warnings with `error'.")
350 ;; byte-compile-warning-types in FSF.
351 (defvar byte-compile-default-warnings
352 '(redefine callargs subr-callargs free-vars unresolved unused-vars obsolete)
353 "*The warnings used when byte-compile-warnings is t.")
355 (defvar byte-compile-warnings t
356 "*List of warnings that the compiler should issue (t for the default set).
357 Elements of the list may be:
359 free-vars references to variables not in the current lexical scope.
360 unused-vars references to non-global variables bound but not referenced.
361 unresolved calls to unknown functions.
362 callargs lambda calls with args that don't match the definition.
363 subr-callargs calls to subrs with args that don't match the definition.
364 redefine function cell redefined from a macro to a lambda or vice
365 versa, or redefined to take a different number of arguments.
366 obsolete use of an obsolete function or variable.
367 pedantic warn of use of compatible symbols.
369 The default set is specified by `byte-compile-default-warnings' and
370 normally encompasses all possible warnings.
372 See also the macro `byte-compiler-options'.")
374 (defvar byte-compile-generate-call-tree nil
375 "*Non-nil means collect call-graph information when compiling.
376 This records functions that were called and from where.
377 If the value is t, compilation displays the call graph when it finishes.
378 If the value is neither t nor nil, compilation asks you whether to display
381 The call tree only lists functions called, not macros used. Those functions
382 which the byte-code interpreter knows about directly (eq, cons, etc.) are
385 The call tree also lists those functions which are not known to be called
386 \(that is, to which no calls have been compiled). Functions which can be
387 invoked interactively are excluded from this list.")
389 (defconst byte-compile-call-tree nil "Alist of functions and their call tree.
390 Each element looks like
392 \(FUNCTION CALLERS CALLS\)
394 where CALLERS is a list of functions that call FUNCTION, and CALLS
395 is a list of functions for which calls were generated while compiling
398 (defvar byte-compile-call-tree-sort 'name
399 "*If non-nil, sort the call tree.
400 The values `name', `callers', `calls', `calls+callers'
401 specify different fields to sort on.")
403 (defvar byte-compile-overwrite-file t
404 "If nil, old .elc files are deleted before the new is saved, and .elc
405 files will have the same modes as the corresponding .el file. Otherwise,
406 existing .elc files will simply be overwritten, and the existing modes
407 will not be changed. If this variable is nil, then an .elc file which
408 is a symbolic link will be turned into a normal file, instead of the file
409 which the link points to being overwritten.")
411 (defvar byte-recompile-directory-ignore-errors-p nil
412 "If true, then `byte-recompile-directory' will continue compiling even
413 when an error occurs in a file. This is bound to t by
414 `batch-byte-recompile-directory'.")
416 (defvar byte-recompile-directory-recursively t
417 "*If true, then `byte-recompile-directory' will recurse on subdirectories.")
419 (defvar byte-compile-constants nil
420 "list of all constants encountered during compilation of this form")
421 (defvar byte-compile-variables nil
422 "list of all variables encountered during compilation of this form")
423 (defvar byte-compile-bound-variables nil
424 "Alist of variables bound in the context of the current form,
425 that is, the current lexical environment. This list lives partly
426 on the specbind stack. The cdr of each cell is an integer bitmask.")
428 (defconst byte-compile-referenced-bit 1)
429 (defconst byte-compile-assigned-bit 2)
430 (defconst byte-compile-arglist-bit 4)
431 (defconst byte-compile-global-bit 8)
433 (defvar byte-compile-free-references)
434 (defvar byte-compile-free-assignments)
436 (defvar byte-compiler-error-flag)
438 (defconst byte-compile-initial-macro-environment
440 '((byte-compiler-options . (lambda (&rest forms)
441 (apply 'byte-compiler-options-handler forms)))
442 (eval-when-compile . (lambda (&rest body)
443 (list 'quote (eval (byte-compile-top-level
444 (cons 'progn body))))))
445 (eval-and-compile . (lambda (&rest body)
446 (eval (cons 'progn body))
447 (cons 'progn body)))))
448 "The default macro-environment passed to macroexpand by the compiler.
449 Placing a macro here will cause a macro to have different semantics when
450 expanded by the compiler as when expanded by the interpreter.")
452 (defvar byte-compile-macro-environment byte-compile-initial-macro-environment
453 "Alist of macros defined in the file being compiled.
454 Each element looks like (MACRONAME . DEFINITION). It is
455 \(MACRONAME . nil) when a macro is redefined as a function.")
457 (defvar byte-compile-function-environment nil
458 "Alist of functions defined in the file being compiled.
459 This is so we can inline them when necessary.
460 Each element looks like (FUNCTIONNAME . DEFINITION). It is
461 \(FUNCTIONNAME . nil) when a function is redefined as a macro.")
463 (defvar byte-compile-autoload-environment nil
464 "Alist of functions and macros defined by autoload in the file being compiled.
465 This is so we can suppress warnings about calls to these functions, even though
466 they do not have `real' definitions.
467 Each element looks like (FUNCTIONNAME . CALL-TO-AUTOLOAD).")
469 (defvar byte-compile-unresolved-functions nil
470 "Alist of undefined functions to which calls have been compiled (used for
471 warnings when the function is later defined with incorrect args).")
473 (defvar byte-compile-file-domain) ; domain of file being compiled
475 (defvar byte-compile-tag-number 0)
476 (defvar byte-compile-output nil
477 "Alist describing contents to put in byte code string.
478 Each element is (INDEX . VALUE)")
479 (defvar byte-compile-depth 0 "Current depth of execution stack.")
480 (defvar byte-compile-maxdepth 0 "Maximum depth of execution stack.")
483 ;;; The byte codes; this information is duplicated in bytecode.c
485 (defconst byte-code-vector nil
486 "An array containing byte-code names indexed by byte-code values.")
488 (defconst byte-stack+-info nil
489 "An array with the stack adjustment for each byte-code.")
491 (defmacro byte-defop (opcode stack-adjust opname &optional docstring)
492 ;; This is a speed-hack for building the byte-code-vector at compile-time.
493 ;; We fill in the vector at macroexpand-time, and then after the last call
494 ;; to byte-defop, we write the vector out as a constant instead of writing
495 ;; out a bunch of calls to aset.
496 ;; Actually, we don't fill in the vector itself, because that could make
497 ;; it problematic to compile big changes to this compiler; we store the
498 ;; values on its plist, and remove them later in -extrude.
499 (let ((v1 (or (get 'byte-code-vector 'tmp-compile-time-value)
500 (put 'byte-code-vector 'tmp-compile-time-value
501 (make-vector 256 nil))))
502 (v2 (or (get 'byte-stack+-info 'tmp-compile-time-value)
503 (put 'byte-stack+-info 'tmp-compile-time-value
504 (make-vector 256 nil)))))
505 (aset v1 opcode opname)
506 (aset v2 opcode stack-adjust))
508 (list 'defconst opname opcode (concat "Byte code opcode " docstring "."))
509 (list 'defconst opname opcode)))
511 (defmacro byte-extrude-byte-code-vectors ()
512 (prog1 (list 'setq 'byte-code-vector
513 (get 'byte-code-vector 'tmp-compile-time-value)
515 (get 'byte-stack+-info 'tmp-compile-time-value))
516 (remprop 'byte-code-vector 'tmp-compile-time-value)
517 (remprop 'byte-stack+-info 'tmp-compile-time-value)))
522 ;; These opcodes are special in that they pack their argument into the
525 (byte-defop 8 1 byte-varref "for variable reference")
526 (byte-defop 16 -1 byte-varset "for setting a variable")
527 (byte-defop 24 -1 byte-varbind "for binding a variable")
528 (byte-defop 32 0 byte-call "for calling a function")
529 (byte-defop 40 0 byte-unbind "for unbinding special bindings")
530 ;; codes 8-47 are consumed by the preceding opcodes
534 (byte-defop 56 -1 byte-nth)
535 (byte-defop 57 0 byte-symbolp)
536 (byte-defop 58 0 byte-consp)
537 (byte-defop 59 0 byte-stringp)
538 (byte-defop 60 0 byte-listp)
539 (byte-defop 61 -1 byte-old-eq)
540 (byte-defop 62 -1 byte-old-memq)
541 (byte-defop 63 0 byte-not)
542 (byte-defop 64 0 byte-car)
543 (byte-defop 65 0 byte-cdr)
544 (byte-defop 66 -1 byte-cons)
545 (byte-defop 67 0 byte-list1)
546 (byte-defop 68 -1 byte-list2)
547 (byte-defop 69 -2 byte-list3)
548 (byte-defop 70 -3 byte-list4)
549 (byte-defop 71 0 byte-length)
550 (byte-defop 72 -1 byte-aref)
551 (byte-defop 73 -2 byte-aset)
552 (byte-defop 74 0 byte-symbol-value)
553 (byte-defop 75 0 byte-symbol-function) ; this was commented out
554 (byte-defop 76 -1 byte-set)
555 (byte-defop 77 -1 byte-fset) ; this was commented out
556 (byte-defop 78 -1 byte-get)
557 (byte-defop 79 -2 byte-substring)
558 (byte-defop 80 -1 byte-concat2)
559 (byte-defop 81 -2 byte-concat3)
560 (byte-defop 82 -3 byte-concat4)
561 (byte-defop 83 0 byte-sub1)
562 (byte-defop 84 0 byte-add1)
563 (byte-defop 85 -1 byte-eqlsign)
564 (byte-defop 86 -1 byte-gtr)
565 (byte-defop 87 -1 byte-lss)
566 (byte-defop 88 -1 byte-leq)
567 (byte-defop 89 -1 byte-geq)
568 (byte-defop 90 -1 byte-diff)
569 (byte-defop 91 0 byte-negate)
570 (byte-defop 92 -1 byte-plus)
571 (byte-defop 93 -1 byte-max)
572 (byte-defop 94 -1 byte-min)
573 (byte-defop 95 -1 byte-mult)
574 (byte-defop 96 1 byte-point)
575 (byte-defop 97 -1 byte-eq) ; new as of v20
576 (byte-defop 98 0 byte-goto-char)
577 (byte-defop 99 0 byte-insert)
578 (byte-defop 100 1 byte-point-max)
579 (byte-defop 101 1 byte-point-min)
580 (byte-defop 102 0 byte-char-after)
581 (byte-defop 103 1 byte-following-char)
582 (byte-defop 104 1 byte-preceding-char)
583 (byte-defop 105 1 byte-current-column)
584 (byte-defop 106 0 byte-indent-to)
585 (byte-defop 107 -1 byte-equal) ; new as of v20
586 (byte-defop 108 1 byte-eolp)
587 (byte-defop 109 1 byte-eobp)
588 (byte-defop 110 1 byte-bolp)
589 (byte-defop 111 1 byte-bobp)
590 (byte-defop 112 1 byte-current-buffer)
591 (byte-defop 113 0 byte-set-buffer)
592 (byte-defop 114 0 byte-save-current-buffer
593 "To make a binding to record the current buffer.")
594 ;;(byte-defop 114 1 byte-read-char-OBSOLETE) ;obsolete as of v19
595 (byte-defop 115 -1 byte-memq) ; new as of v20
596 (byte-defop 116 1 byte-interactive-p)
598 (byte-defop 117 0 byte-forward-char)
599 (byte-defop 118 0 byte-forward-word)
600 (byte-defop 119 -1 byte-skip-chars-forward)
601 (byte-defop 120 -1 byte-skip-chars-backward)
602 (byte-defop 121 0 byte-forward-line)
603 (byte-defop 122 0 byte-char-syntax)
604 (byte-defop 123 -1 byte-buffer-substring)
605 (byte-defop 124 -1 byte-delete-region)
606 (byte-defop 125 -1 byte-narrow-to-region)
607 (byte-defop 126 1 byte-widen)
608 (byte-defop 127 0 byte-end-of-line)
612 ;; These store their argument in the next two bytes
613 (byte-defop 129 1 byte-constant2
614 "for reference to a constant with vector index >= byte-constant-limit")
615 (byte-defop 130 0 byte-goto "for unconditional jump")
616 (byte-defop 131 -1 byte-goto-if-nil "to pop value and jump if it's nil")
617 (byte-defop 132 -1 byte-goto-if-not-nil
618 "to pop value and jump if it's not nil")
619 (byte-defop 133 -1 byte-goto-if-nil-else-pop
620 "to examine top-of-stack, jump and don't pop it if it's nil,
622 (byte-defop 134 -1 byte-goto-if-not-nil-else-pop
623 "to examine top-of-stack, jump and don't pop it if it's non-nil,
626 (byte-defop 135 -1 byte-return "to pop a value and return it from `byte-code'")
627 (byte-defop 136 -1 byte-discard "to discard one value from stack")
628 (byte-defop 137 1 byte-dup "to duplicate the top of the stack")
630 (byte-defop 138 0 byte-save-excursion
631 "to make a binding to record the buffer, point and mark")
632 (byte-defop 139 0 byte-save-window-excursion
633 "to make a binding to record entire window configuration")
634 (byte-defop 140 0 byte-save-restriction
635 "to make a binding to record the current buffer clipping restrictions")
636 (byte-defop 141 -1 byte-catch
637 "for catch. Takes, on stack, the tag and an expression for the body")
638 (byte-defop 142 -1 byte-unwind-protect
639 "for unwind-protect. Takes, on stack, an expression for the unwind-action")
641 ;; For condition-case. Takes, on stack, the variable to bind,
642 ;; an expression for the body, and a list of clauses.
643 (byte-defop 143 -2 byte-condition-case)
645 ;; For entry to with-output-to-temp-buffer.
646 ;; Takes, on stack, the buffer name.
647 ;; Binds standard-output and does some other things.
648 ;; Returns with temp buffer on the stack in place of buffer name.
649 (byte-defop 144 0 byte-temp-output-buffer-setup)
651 ;; For exit from with-output-to-temp-buffer.
652 ;; Expects the temp buffer on the stack underneath value to return.
653 ;; Pops them both, then pushes the value back on.
654 ;; Unbinds standard-output and makes the temp buffer visible.
655 (byte-defop 145 -1 byte-temp-output-buffer-show)
657 ;; To unbind back to the beginning of this frame.
658 ;; Not used yet, but will be needed for tail-recursion elimination.
659 (byte-defop 146 0 byte-unbind-all)
661 (byte-defop 147 -2 byte-set-marker)
662 (byte-defop 148 0 byte-match-beginning)
663 (byte-defop 149 0 byte-match-end)
664 (byte-defop 150 0 byte-upcase)
665 (byte-defop 151 0 byte-downcase)
666 (byte-defop 152 -1 byte-string=)
667 (byte-defop 153 -1 byte-string<)
668 (byte-defop 154 -1 byte-old-equal)
669 (byte-defop 155 -1 byte-nthcdr)
670 (byte-defop 156 -1 byte-elt)
671 (byte-defop 157 -1 byte-old-member)
672 (byte-defop 158 -1 byte-old-assq)
673 (byte-defop 159 0 byte-nreverse)
674 (byte-defop 160 -1 byte-setcar)
675 (byte-defop 161 -1 byte-setcdr)
676 (byte-defop 162 0 byte-car-safe)
677 (byte-defop 163 0 byte-cdr-safe)
678 (byte-defop 164 -1 byte-nconc)
679 (byte-defop 165 -1 byte-quo)
680 (byte-defop 166 -1 byte-rem)
681 (byte-defop 167 0 byte-numberp)
682 (byte-defop 168 0 byte-integerp)
686 ;; These are not present in FSF.
688 (byte-defop 170 0 byte-rel-goto)
689 (byte-defop 171 -1 byte-rel-goto-if-nil)
690 (byte-defop 172 -1 byte-rel-goto-if-not-nil)
691 (byte-defop 173 -1 byte-rel-goto-if-nil-else-pop)
692 (byte-defop 174 -1 byte-rel-goto-if-not-nil-else-pop)
694 (byte-defop 175 nil byte-listN)
695 (byte-defop 176 nil byte-concatN)
696 (byte-defop 177 nil byte-insertN)
700 ;; these ops are new to v20
701 (byte-defop 182 -1 byte-member)
702 (byte-defop 183 -1 byte-assq)
706 (byte-defop 192 1 byte-constant "for reference to a constant")
707 ;; codes 193-255 are consumed by byte-constant.
708 (defconst byte-constant-limit 64
709 "Exclusive maximum index usable in the `byte-constant' opcode.")
711 (defconst byte-goto-ops (purecopy
712 '(byte-goto byte-goto-if-nil byte-goto-if-not-nil
713 byte-goto-if-nil-else-pop
714 byte-goto-if-not-nil-else-pop))
715 "List of byte-codes whose offset is a pc.")
717 (defconst byte-goto-always-pop-ops
718 (purecopy '(byte-goto-if-nil byte-goto-if-not-nil)))
720 (defconst byte-rel-goto-ops
721 (purecopy '(byte-rel-goto byte-rel-goto-if-nil byte-rel-goto-if-not-nil
722 byte-rel-goto-if-nil-else-pop byte-rel-goto-if-not-nil-else-pop))
723 "byte-codes for relative jumps.")
725 (byte-extrude-byte-code-vectors)
727 ;;; lapcode generator
729 ;;; the byte-compiler now does source -> lapcode -> bytecode instead of
730 ;;; source -> bytecode, because it's a lot easier to make optimizations
731 ;;; on lapcode than on bytecode.
733 ;;; Elements of the lapcode list are of the form (<instruction> . <parameter>)
734 ;;; where instruction is a symbol naming a byte-code instruction,
735 ;;; and parameter is an argument to that instruction, if any.
737 ;;; The instruction can be the pseudo-op TAG, which means that this position
738 ;;; in the instruction stream is a target of a goto. (car PARAMETER) will be
739 ;;; the PC for this location, and the whole instruction "(TAG pc)" will be the
740 ;;; parameter for some goto op.
742 ;;; If the operation is varbind, varref, varset or push-constant, then the
743 ;;; parameter is (variable/constant . index_in_constant_vector).
745 ;;; First, the source code is macroexpanded and optimized in various ways.
746 ;;; Then the resultant code is compiled into lapcode. Another set of
747 ;;; optimizations are then run over the lapcode. Then the variables and
748 ;;; constants referenced by the lapcode are collected and placed in the
749 ;;; constants-vector. (This happens now so that variables referenced by dead
750 ;;; code don't consume space.) And finally, the lapcode is transformed into
751 ;;; compacted byte-code.
753 ;;; A distinction is made between variables and constants because the variable-
754 ;;; referencing instructions are more sensitive to the variables being near the
755 ;;; front of the constants-vector than the constant-referencing instructions.
756 ;;; Also, this lets us notice references to free variables.
758 (defun byte-compile-lapcode (lap)
759 "Turns lapcode into bytecode. The lapcode is destroyed."
760 ;; Lapcode modifications: changes the ID of a tag to be the tag's PC.
761 (let ((pc 0) ; Program counter
762 op off ; Operation & offset
763 (bytes '()) ; Put the output bytes here
764 (patchlist nil) ; List of tags and goto's to patch
767 (setq op (car (car lap))
769 (cond ((not (symbolp op))
770 (error "Non-symbolic opcode `%s'" op))
773 (push off patchlist))
774 ((memq op byte-goto-ops)
776 (setq bytes (cons (cons pc (cdr off))
778 (cons (symbol-value op) bytes))))
779 (push bytes patchlist))
782 (cond ((cond ((consp off)
783 ;; Variable or constant reference
785 (eq op 'byte-constant)))
786 (cond ((< off byte-constant-limit)
788 (cons (+ byte-constant off) bytes))
792 (cons (logand off 255)
793 (cons byte-constant2 bytes))))))
794 ((and (<= byte-listN (symbol-value op))
795 (<= (symbol-value op) byte-insertN))
797 (cons off (cons (symbol-value op) bytes)))
800 (cons (+ (symbol-value op) off) bytes))
803 (cons off (cons (+ (symbol-value op) 6) bytes)))
807 (cons (logand off 255)
808 (cons (+ (symbol-value op) 7)
810 (setq lap (cdr lap)))
811 ;;(if (not (= pc (length bytes)))
812 ;; (error "Compiler error: pc mismatch - %s %s" pc (length bytes)))
813 (cond (t ;; starting with Emacs 19.
814 ;; Make relative jumps
815 (setq patchlist (nreverse patchlist))
817 (setq off 0) ; PC change because of deleted bytes
818 (setq rest patchlist)
820 (setq tmp (car rest))
821 (and (consp (car tmp)) ; Jump
822 (prog1 (null (nth 1 tmp)) ; Absolute jump
823 (setq tmp (car tmp)))
825 (setq rel (- (car (cdr tmp)) (car tmp)))
826 (and (<= -129 rel) (< rel 128)))
828 ;; Convert to relative jump.
829 (setcdr (car rest) (cdr (cdr (car rest))))
830 (setcar (cdr (car rest))
831 (+ (car (cdr (car rest)))
832 (- byte-rel-goto byte-goto)))
833 (setq off (1- off))))
834 (setcar tmp (+ (car tmp) off)) ; Adjust PC
835 (setq rest (cdr rest)))
836 ;; If optimizing, repeat until no change.
838 (not (zerop off)))))))
839 ;; Patch PC into jumps
842 (setq bytes (car patchlist))
843 (cond ((atom (car bytes))) ; Tag
844 ((nth 1 bytes) ; Relative jump
845 (setcar bytes (+ (- (car (cdr (car bytes))) (car (car bytes)))
848 (setq pc (car (cdr (car bytes)))) ; Pick PC from tag
849 (setcar (cdr bytes) (logand pc 255))
850 (setcar bytes (lsh pc -8))))
851 (setq patchlist (cdr patchlist))))
852 (concat (nreverse bytes))))
855 ;;; byte compiler messages
857 (defvar byte-compile-current-form nil)
858 (defvar byte-compile-current-file nil)
859 (defvar byte-compile-dest-file nil)
861 (defmacro byte-compile-log (format-string &rest args)
862 `(when (and byte-optimize (memq byte-optimize-log '(t source)))
863 (let ((print-escape-newlines t)
866 (byte-compile-log-1 (format ,format-string ,@args)))))
868 (defconst byte-compile-last-warned-form 'nothing)
870 ;; Log a message STRING in *Compile-Log*.
871 ;; Also log the current function and file if not already done.
872 (defun byte-compile-log-1 (string &optional fill)
873 (let* ((this-form (or byte-compile-current-form "toplevel forms"))
875 (when (or byte-compile-current-file
876 (not (eq this-form byte-compile-last-warned-form)))
878 "While compiling %s%s:"
881 ((stringp byte-compile-current-file)
882 (concat " in file " byte-compile-current-file))
883 ((bufferp byte-compile-current-file)
884 (concat " in buffer "
885 (buffer-name byte-compile-current-file)))
889 (when while-compiling-msg (message "%s" while-compiling-msg))
890 (message " %s" string))
891 (with-current-buffer (get-buffer-create "*Compile-Log*")
892 (goto-char (point-max))
893 (when byte-compile-current-file
894 (when (> (point-max) (point-min))
896 (insert (current-time-string) "\n"))
897 (when while-compiling-msg (insert while-compiling-msg "\n"))
898 (insert " " string "\n")
899 (when (and fill (not (string-match "\n" string)))
900 (let ((fill-prefix " ")
902 (fill-paragraph nil)))))
903 (setq byte-compile-current-file nil)
904 (setq byte-compile-last-warned-form this-form)))
906 ;; Log the start of a file in *Compile-Log*, and mark it as done.
907 ;; But do nothing in batch mode.
908 (defun byte-compile-log-file ()
909 (when (and byte-compile-current-file (not noninteractive))
910 (with-current-buffer (get-buffer-create "*Compile-Log*")
911 (when (> (point-max) (point-min))
912 (goto-char (point-max))
915 (if (stringp byte-compile-current-file)
916 (concat "file " byte-compile-current-file)
917 (concat "buffer " (buffer-name byte-compile-current-file)))
918 " at " (current-time-string) "\n")
919 (setq byte-compile-current-file nil))))
921 (defun byte-compile-warn (format &rest args)
922 (setq format (apply 'format format args))
923 (if byte-compile-error-on-warn
924 (error "%s" format) ; byte-compile-file catches and logs it
925 (byte-compile-log-1 (concat "** " format) t)
927 ;;; It is useless to flash warnings too fast to be read.
928 ;;; Besides, they will all be shown at the end.
929 ;;; and comments out the next two lines.
930 (or noninteractive ; already written on stdout.
931 (message "Warning: %s" format))))
933 ;;; This function should be used to report errors that have halted
934 ;;; compilation of the current file.
935 (defun byte-compile-report-error (error-info)
936 (setq byte-compiler-error-flag t)
939 (format (if (cdr error-info) "%s (%s)" "%s")
940 (get (car error-info) 'error-message)
941 (prin1-to-string (cdr error-info))))))
943 ;;; Used by make-obsolete.
944 (defun byte-compile-obsolete (form)
945 (let ((new (get (car form) 'byte-obsolete-info)))
946 (if (memq 'obsolete byte-compile-warnings)
947 (byte-compile-warn "%s is an obsolete function; %s" (car form)
948 (if (stringp (car new))
950 (format "use %s instead." (car new)))))
951 (funcall (or (cdr new) 'byte-compile-normal-call) form)))
953 ;;; Used by make-obsolete.
954 (defun byte-compile-compatible (form)
955 (let ((new (get (car form) 'byte-compatible-info)))
956 (if (memq 'pedantic byte-compile-warnings)
957 (byte-compile-warn "%s is provided for compatibility; %s" (car form)
958 (if (stringp (car new))
960 (format "use %s instead." (car new)))))
961 (funcall (or (cdr new) 'byte-compile-normal-call) form)))
965 (defconst byte-compiler-legal-options
966 '((optimize byte-optimize (t nil source byte) val)
967 (file-format byte-compile-emacs19-compatibility (emacs19 emacs20)
969 (delete-errors byte-compile-delete-errors (t nil) val)
970 (verbose byte-compile-verbose (t nil) val)
971 (new-bytecodes byte-compile-new-bytecodes (t nil) val)
972 (warnings byte-compile-warnings
973 ((callargs subr-callargs redefine free-vars unused-vars unresolved))
977 (defconst byte-compiler-obsolete-options
978 '((new-bytecodes t)))
980 ;; Inhibit v19/v20 selectors if the version is hardcoded.
981 ;; #### This should print a warning if the user tries to change something
982 ;; than can't be changed because the running compiler doesn't support it.
984 ((byte-compile-single-version)
985 (setcar (cdr (cdr (assq 'file-format byte-compiler-legal-options)))
986 (if (byte-compile-version-cond byte-compile-emacs19-compatibility)
987 '(emacs19) '(emacs20)))))
989 ;; now we can copy it.
990 (setq byte-compiler-legal-options (purecopy byte-compiler-legal-options))
992 (defun byte-compiler-options-handler (&rest args)
993 (let (key val desc choices)
995 (if (or (atom (car args)) (nthcdr 2 (car args)) (null (cdr (car args))))
996 (error "malformed byte-compiler-option %s" (car args)))
997 (setq key (car (car args))
998 val (car (cdr (car args)))
999 desc (assq key byte-compiler-legal-options))
1001 (error "unknown byte-compiler option %s" key))
1002 (if (assq key byte-compiler-obsolete-options)
1003 (byte-compile-warn "%s is an obsolete byte-compiler option." key))
1004 (setq choices (nth 2 desc))
1005 (if (consp (car choices))
1009 (ret (and (memq (car val) '(+ -))
1010 (copy-sequence (if (eq t (symbol-value var))
1012 (symbol-value var))))))
1013 (setq choices (car choices))
1015 (setq this (car val))
1016 (cond ((memq this choices)
1017 (setq ret (funcall handler this ret)))
1018 ((eq this '+) (setq handler 'cons))
1019 ((eq this '-) (setq handler 'delq))
1020 ((error "%s only accepts %s." key choices)))
1021 (setq val (cdr val)))
1022 (set (nth 1 desc) ret))
1023 (or (memq val choices)
1024 (error "%s must be one of %s." key choices))
1025 (set (nth 1 desc) (eval (nth 3 desc))))
1026 (setq args (cdr args)))
1029 ;;; sanity-checking arglists
1031 (defun byte-compile-fdefinition (name macro-p)
1032 (let* ((list (if (memq macro-p '(nil subr))
1033 byte-compile-function-environment
1034 byte-compile-macro-environment))
1035 (env (cdr (assq name list))))
1038 (while (and (symbolp fn)
1040 (or (symbolp (symbol-function fn))
1041 (consp (symbol-function fn))
1043 (compiled-function-p (symbol-function fn)))
1044 (and (eq macro-p 'subr) (subrp fn))))
1045 (setq fn (symbol-function fn)))
1046 (if (or (and (not macro-p) (compiled-function-p fn))
1047 (and (eq macro-p 'subr) (subrp fn)))
1050 (not (eq macro-p 'subr))
1051 (if (eq 'macro (car fn))
1055 (if (eq 'autoload (car fn))
1059 (defun byte-compile-arglist-signature (arglist)
1064 (cond ((eq (car arglist) '&optional)
1065 (or opts (setq opts 0)))
1066 ((eq (car arglist) '&rest)
1072 (setq opts (1+ opts))
1073 (setq args (1+ args)))))
1074 (setq arglist (cdr arglist)))
1075 (cons args (if restp nil (if opts (+ args opts) args)))))
1078 (defun byte-compile-arglist-signatures-congruent-p (old new)
1080 (> (car new) (car old)) ; requires more args now
1081 (and (null (cdr old)) ; tooks rest-args, doesn't any more
1083 (and (cdr new) (cdr old) ; can't take as many args now
1084 (< (cdr new) (cdr old)))
1087 (defun byte-compile-arglist-signature-string (signature)
1088 (cond ((null (cdr signature))
1089 (format "%d+" (car signature)))
1090 ((= (car signature) (cdr signature))
1091 (format "%d" (car signature)))
1092 (t (format "%d-%d" (car signature) (cdr signature)))))
1095 ;; Warn if the form is calling a function with the wrong number of arguments.
1096 (defun byte-compile-callargs-warn (form)
1097 (let* ((def (or (byte-compile-fdefinition (car form) nil)
1098 (byte-compile-fdefinition (car form) t)))
1099 (sig (and def (byte-compile-arglist-signature
1100 (if (eq 'lambda (car-safe def))
1102 (if (compiled-function-p def)
1103 (compiled-function-arglist def)
1105 (ncall (length (cdr form))))
1107 (fboundp 'subr-min-args)
1108 (setq def (byte-compile-fdefinition (car form) 'subr)))
1109 (setq sig (cons (subr-min-args def) (subr-max-args def))))
1111 (if (or (< ncall (car sig))
1112 (and (cdr sig) (> ncall (cdr sig))))
1114 "%s called with %d argument%s, but %s %s"
1116 (if (= 1 ncall) "" "s")
1117 (if (< ncall (car sig))
1120 (byte-compile-arglist-signature-string sig)))
1121 (or (fboundp (car form)) ; might be a subr or autoload.
1122 ;; ## this doesn't work with recursion.
1123 (eq (car form) byte-compile-current-form)
1124 ;; It's a currently-undefined function.
1125 ;; Remember number of args in call.
1126 (let ((cons (assq (car form) byte-compile-unresolved-functions))
1127 (n (length (cdr form))))
1129 (or (memq n (cdr cons))
1130 (setcdr cons (cons n (cdr cons))))
1131 (setq byte-compile-unresolved-functions
1132 (cons (list (car form) n)
1133 byte-compile-unresolved-functions))))))))
1135 ;; Warn if the function or macro is being redefined with a different
1136 ;; number of arguments.
1137 (defun byte-compile-arglist-warn (form macrop)
1138 (let ((old (byte-compile-fdefinition (nth 1 form) macrop)))
1140 (let ((sig1 (byte-compile-arglist-signature
1141 (if (eq 'lambda (car-safe old))
1143 (if (compiled-function-p old)
1144 (compiled-function-arglist old)
1146 (sig2 (byte-compile-arglist-signature (nth 2 form))))
1147 (or (byte-compile-arglist-signatures-congruent-p sig1 sig2)
1148 (byte-compile-warn "%s %s used to take %s %s, now takes %s"
1149 (if (eq (car form) 'defun) "function" "macro")
1151 (byte-compile-arglist-signature-string sig1)
1152 (if (equal sig1 '(1 . 1)) "argument" "arguments")
1153 (byte-compile-arglist-signature-string sig2))))
1154 ;; This is the first definition. See if previous calls are compatible.
1155 (let ((calls (assq (nth 1 form) byte-compile-unresolved-functions))
1159 (setq sig (byte-compile-arglist-signature (nth 2 form))
1160 nums (sort (copy-sequence (cdr calls)) (function <))
1162 max (car (nreverse nums)))
1163 (if (or (< min (car sig))
1164 (and (cdr sig) (> max (cdr sig))))
1166 "%s being defined to take %s%s, but was previously called with %s"
1168 (byte-compile-arglist-signature-string sig)
1169 (if (equal sig '(1 . 1)) " arg" " args")
1170 (byte-compile-arglist-signature-string (cons min max))))
1172 (setq byte-compile-unresolved-functions
1173 (delq calls byte-compile-unresolved-functions)))))
1176 ;; If we have compiled any calls to functions which are not known to be
1177 ;; defined, issue a warning enumerating them.
1178 ;; `unresolved' in the list `byte-compile-warnings' disables this.
1179 (defun byte-compile-warn-about-unresolved-functions (&optional msg)
1180 (if (memq 'unresolved byte-compile-warnings)
1181 (let ((byte-compile-current-form (or msg "the end of the data")))
1182 ;; First delete the autoloads from the list.
1183 (if byte-compile-autoload-environment
1184 (let ((rest byte-compile-unresolved-functions))
1186 (if (assq (car (car rest)) byte-compile-autoload-environment)
1187 (setq byte-compile-unresolved-functions
1188 (delq (car rest) byte-compile-unresolved-functions)))
1189 (setq rest (cdr rest)))))
1191 (if (cdr byte-compile-unresolved-functions)
1192 (let* ((str "The following functions are not known to be defined: ")
1193 (L (+ (length str) 5))
1194 (rest (reverse byte-compile-unresolved-functions))
1197 (setq s (symbol-name (car (car rest)))
1198 L (+ L (length s) 2)
1200 (if (<= L (1- fill-column))
1201 (setq str (concat str " " s (and rest ",")))
1202 (setq str (concat str "\n " s (and rest ","))
1203 L (+ (length s) 4))))
1204 (byte-compile-warn "%s" str))
1205 (if byte-compile-unresolved-functions
1206 (byte-compile-warn "the function %s is not known to be defined."
1207 (car (car byte-compile-unresolved-functions)))))))
1210 (defun byte-compile-defvar-p (var)
1211 ;; Whether the byte compiler thinks that non-lexical references to this
1213 (or (globally-boundp var)
1214 (let ((rest byte-compile-bound-variables))
1215 (while (and rest var)
1216 (if (and (eq var (car-safe (car rest)))
1217 (not (= 0 (logand (cdr (car rest))
1218 byte-compile-global-bit))))
1220 (setq rest (cdr rest)))
1221 ;; if var is nil at this point, it's a defvar in this file.
1225 ;;; If we have compiled bindings of variables which have no referents, warn.
1226 (defun byte-compile-warn-about-unused-variables ()
1227 (let ((rest byte-compile-bound-variables)
1231 ;; only warn about variables whose lifetime is now ending,
1232 ;; that is, variables from the lexical scope that is now
1233 ;; terminating. (Think nested lets.)
1234 (not (eq (car rest) 'new-scope)))
1235 (setq cell (car rest))
1236 (if (and (= 0 (logand byte-compile-referenced-bit (cdr cell)))
1237 ;; Don't warn about declared-but-unused arguments,
1238 ;; for two reasons: first, the arglist structure
1239 ;; might be imposed by external forces, and we don't
1240 ;; have (declare (ignore x)) yet; and second, inline
1241 ;; expansion produces forms like
1242 ;; ((lambda (arg) (byte-code "..." [arg])) x)
1243 ;; which we can't (ok, well, don't) recognize as
1244 ;; containing a reference to arg, so every inline
1245 ;; expansion would generate a warning. (If we had
1246 ;; `ignore' then inline expansion could emit an
1247 ;; ignore declaration.)
1248 (= 0 (logand byte-compile-arglist-bit (cdr cell)))
1249 ;; Don't warn about defvars because this is a
1250 ;; legitimate special binding.
1251 (not (byte-compile-defvar-p (car cell))))
1252 (setq unreferenced (cons (car cell) unreferenced)))
1253 (setq rest (cdr rest)))
1254 (setq unreferenced (nreverse unreferenced))
1257 (format "variable %s bound but not referenced" (car unreferenced)))
1258 (setq unreferenced (cdr unreferenced)))))
1261 (defmacro byte-compile-constant-symbol-p (symbol)
1262 `(or (keywordp ,symbol) (memq ,symbol '(nil t))))
1264 (defmacro byte-compile-constp (form)
1265 ;; Returns non-nil if FORM is a constant.
1266 `(cond ((consp ,form) (eq (car ,form) 'quote))
1267 ((symbolp ,form) (byte-compile-constant-symbol-p ,form))
1270 (defmacro byte-compile-close-variables (&rest body)
1273 ;; Close over these variables to encapsulate the
1274 ;; compilation state
1276 (byte-compile-macro-environment
1277 ;; Copy it because the compiler may patch into the
1278 ;; macroenvironment.
1279 (copy-alist byte-compile-initial-macro-environment))
1280 (byte-compile-function-environment nil)
1281 (byte-compile-autoload-environment nil)
1282 (byte-compile-unresolved-functions nil)
1283 (byte-compile-bound-variables nil)
1284 (byte-compile-free-references nil)
1285 (byte-compile-free-assignments nil)
1287 ;; Close over these variables so that `byte-compiler-options'
1288 ;; can change them on a per-file basis.
1290 (byte-compile-verbose byte-compile-verbose)
1291 (byte-optimize byte-optimize)
1292 (byte-compile-emacs19-compatibility
1293 byte-compile-emacs19-compatibility)
1294 (byte-compile-dynamic byte-compile-dynamic)
1295 (byte-compile-dynamic-docstrings
1296 byte-compile-dynamic-docstrings)
1297 (byte-compile-warnings (if (eq byte-compile-warnings t)
1298 byte-compile-default-warnings
1299 byte-compile-warnings))
1300 (byte-compile-file-domain nil))
1303 (if (memq 'unused-vars byte-compile-warnings)
1304 ;; done compiling in this scope, warn now.
1305 (byte-compile-warn-about-unused-variables)))))
1308 (defmacro displaying-byte-compile-warnings (&rest body)
1309 `(let* ((byte-compile-log-buffer (get-buffer-create "*Compile-Log*"))
1310 (byte-compile-point-max-prev (point-max byte-compile-log-buffer)))
1311 ;; Log the file name or buffer name.
1312 (byte-compile-log-file)
1313 ;; Record how much is logged now.
1314 ;; We will display the log buffer if anything more is logged
1315 ;; before the end of BODY.
1316 (defvar byte-compile-warnings-beginning)
1317 (let ((byte-compile-warnings-beginning
1318 (if (boundp 'byte-compile-warnings-beginning)
1319 byte-compile-warnings-beginning
1320 (point-max byte-compile-log-buffer))))
1323 (condition-case error-info
1326 (byte-compile-report-error error-info)))
1328 ;; Always set point in log to start of interesting output.
1329 (with-current-buffer byte-compile-log-buffer
1331 (progn (goto-char byte-compile-point-max-prev)
1332 (skip-chars-forward "\^L\n")
1334 ;; If there were compilation warnings, display them.
1335 (if temp-buffer-show-function
1336 (let ((show-buffer (get-buffer-create "*Compile-Log-Show*")))
1337 ;; Always clean show-buffer, even when not displaying it,
1338 ;; so that misleading previous messages aren't left around.
1339 (with-current-buffer show-buffer
1340 (setq buffer-read-only nil)
1342 (copy-to-buffer show-buffer show-begin (point-max))
1343 (when (< byte-compile-warnings-beginning (point-max))
1344 (funcall temp-buffer-show-function show-buffer)))
1345 (when (< byte-compile-warnings-beginning (point-max))
1347 (prog1 (selected-window)
1348 (select-window (display-buffer (current-buffer)))
1349 (goto-char show-begin)
1350 (recenter 1)))))))))))
1354 (defun byte-force-recompile (directory)
1355 "Recompile every `.el' file in DIRECTORY that already has a `.elc' file.
1356 Files in subdirectories of DIRECTORY are processed also."
1357 (interactive "DByte force recompile (directory): ")
1358 (byte-recompile-directory directory nil nil t))
1361 (defun byte-recompile-directory (directory &optional arg norecursion force)
1362 "Recompile every `.el' file in DIRECTORY that needs recompilation.
1363 This is if a `.elc' file exists but is older than the `.el' file.
1364 Files in subdirectories of DIRECTORY are processed also unless argument
1365 NORECURSION is non-nil.
1367 If the `.elc' file does not exist, normally the `.el' file is *not* compiled.
1368 But a prefix argument (optional second arg) means ask user,
1369 for each such `.el' file, whether to compile it. Prefix argument 0 means
1370 don't ask and compile the file anyway.
1372 A nonzero prefix argument also means ask about each subdirectory.
1374 If the fourth argument FORCE is non-nil,
1375 recompile every `.el' file that already has a `.elc' file."
1376 (interactive "DByte recompile directory: \nP")
1378 (setq arg (prefix-numeric-value arg)))
1383 (let ((directories (list (expand-file-name directory)))
1387 (displaying-byte-compile-warnings
1389 (setq directory (file-name-as-directory (car directories)))
1390 (or noninteractive (message "Checking %s..." directory))
1391 (let ((files (directory-files directory))
1394 (setq source (expand-file-name (car files) directory))
1395 (if (and (not (member (car files) '("." ".." "RCS" "CVS" "SCCS")))
1396 ;; Stay away from directory back-links, etc:
1397 (not (file-symlink-p source))
1398 (file-directory-p source)
1399 byte-recompile-directory-recursively)
1400 ;; This file is a subdirectory. Handle them differently.
1403 (y-or-n-p (concat "Check " source "? ")))
1405 (nconc directories (list source))))
1406 ;; It is an ordinary file. Decide whether to compile it.
1407 (if (and (string-match emacs-lisp-file-regexp source)
1408 (not (auto-save-file-name-p source))
1409 (setq dest (byte-compile-dest-file source))
1410 (if (file-exists-p dest)
1411 ;; File was already compiled.
1412 (or force (file-newer-than-file-p source dest))
1413 ;; No compiled file exists yet.
1416 (y-or-n-p (concat "Compile " source "? "))))))
1417 (progn ;(if (and noninteractive (not byte-compile-verbose))
1418 ; (message "Compiling %s..." source))
1419 ; we do this in byte-compile-file.
1420 (if byte-recompile-directory-ignore-errors-p
1421 (batch-byte-compile-1 source)
1422 (byte-compile-file source))
1424 (message "Checking %s..." directory))
1425 (setq file-count (1+ file-count))
1426 (if (not (eq last-dir directory))
1427 (setq last-dir directory
1428 dir-count (1+ dir-count)))
1430 (setq files (cdr files))))
1431 (setq directories (cdr directories))))
1432 (message "Done (Total of %d file%s compiled%s)"
1433 file-count (if (= file-count 1) "" "s")
1434 (if (> dir-count 1) (format " in %d directories" dir-count) ""))))
1437 (defun byte-recompile-file (filename &optional force)
1438 "Recompile a file of Lisp code named FILENAME if it needs recompilation.
1439 This is if the `.elc' file exists but is older than the `.el' file.
1441 If the `.elc' file does not exist, normally the `.el' file is *not*
1442 compiled. But a prefix argument (optional second arg) means ask user
1443 whether to compile it. Prefix argument 0 don't ask and recompile anyway."
1444 (interactive "fByte recompile file: \nP")
1446 (if (and (string-match emacs-lisp-file-regexp filename)
1447 (not (auto-save-file-name-p filename))
1448 (setq dest (byte-compile-dest-file filename))
1449 (if (file-exists-p dest)
1450 (file-newer-than-file-p filename dest)
1453 (y-or-n-p (concat "Compile " filename "? "))))))
1454 (byte-compile-file filename))))
1457 (defun byte-compile-file (filename &optional load)
1458 "Compile a file of Lisp code named FILENAME into a file of byte code.
1459 The output file's name is made by appending `c' to the end of FILENAME.
1460 With prefix arg (noninteractively: 2nd arg), load the file after compiling."
1461 ;; (interactive "fByte compile file: \nP")
1463 (let ((file buffer-file-name)
1467 (eq (cdr (assq 'major-mode (buffer-local-variables)))
1469 (setq file-name (file-name-nondirectory file)
1470 file-dir (file-name-directory file)))
1471 (list (read-file-name (if current-prefix-arg
1472 "Byte compile and load file: "
1473 "Byte compile file: ")
1474 file-dir nil nil file-name)
1475 current-prefix-arg)))
1476 ;; Expand now so we get the current buffer's defaults
1477 (setq filename (expand-file-name filename))
1479 ;; If we're compiling a file that's in a buffer and is modified, offer
1480 ;; to save it first.
1482 (let ((b (get-file-buffer (expand-file-name filename))))
1483 (if (and b (buffer-modified-p b)
1484 (y-or-n-p (format "save buffer %s first? " (buffer-name b))))
1485 (save-excursion (set-buffer b) (save-buffer)))))
1487 (if (or noninteractive byte-compile-verbose) ; XEmacs change
1488 (message "Compiling %s..." filename))
1489 (let (;;(byte-compile-current-file (file-name-nondirectory filename))
1490 (byte-compile-current-file filename)
1491 target-file input-buffer output-buffer
1492 byte-compile-dest-file)
1493 (setq target-file (byte-compile-dest-file filename))
1494 (setq byte-compile-dest-file target-file)
1496 (setq input-buffer (get-buffer-create " *Compiler Input*"))
1497 (set-buffer input-buffer)
1499 (insert-file-contents filename)
1500 ;; Run hooks including the uncompression hook.
1501 ;; If they change the file name, then change it for the output also.
1502 (let ((buffer-file-name filename)
1503 (default-major-mode 'emacs-lisp-mode)
1504 (enable-local-eval nil))
1506 (setq filename buffer-file-name)))
1507 (setq byte-compiler-error-flag nil)
1508 ;; It is important that input-buffer not be current at this call,
1509 ;; so that the value of point set in input-buffer
1510 ;; within byte-compile-from-buffer lingers in that buffer.
1511 (setq output-buffer (byte-compile-from-buffer input-buffer filename))
1512 (if byte-compiler-error-flag
1514 (if byte-compile-verbose
1515 (message "Compiling %s...done" filename))
1516 (kill-buffer input-buffer)
1518 (set-buffer output-buffer)
1519 (goto-char (point-max))
1520 (insert "\n") ; aaah, unix.
1521 (setq target-file (byte-compile-dest-file filename))
1522 (unless byte-compile-overwrite-file
1523 (ignore-file-errors (delete-file target-file)))
1524 (if (file-writable-p target-file)
1525 (write-region 1 (point-max) target-file)
1526 ;; This is just to give a better error message than write-region
1528 (list "Opening output file"
1529 (if (file-exists-p target-file)
1530 "cannot overwrite file"
1531 "directory not writable or nonexistent")
1533 (or byte-compile-overwrite-file
1535 (set-file-modes target-file (file-modes filename))
1537 (kill-buffer (current-buffer)))
1538 (if (and byte-compile-generate-call-tree
1539 (or (eq t byte-compile-generate-call-tree)
1540 (y-or-n-p (format "Report call tree for %s? " filename))))
1542 (display-call-tree filename)))
1547 ;; RMS comments the next two out.
1550 (defun byte-compile-and-load-file (&optional filename)
1551 "Compile a file of Lisp code named FILENAME into a file of byte code,
1552 and then load it. The output file's name is made by appending \"c\" to
1553 the end of FILENAME."
1555 (if filename ; I don't get it, (interactive-p) doesn't always work
1556 (byte-compile-file filename t)
1557 (let ((current-prefix-arg '(4)))
1558 (call-interactively 'byte-compile-file))))
1561 (defun byte-compile-buffer (&optional buffer)
1562 "Byte-compile and evaluate contents of BUFFER (default: the current buffer)."
1563 (interactive "bByte compile buffer: ")
1564 (setq buffer (if buffer (get-buffer buffer) (current-buffer)))
1565 (message "Compiling %s..." buffer)
1566 (let* ((filename (or (buffer-file-name buffer)
1567 (prin1-to-string buffer)))
1568 (byte-compile-current-file buffer))
1569 (byte-compile-from-buffer buffer filename t))
1570 (message "Compiling %s...done" buffer)
1573 ;;; compiling a single function
1575 (defun compile-defun (&optional arg)
1576 "Compile and evaluate the current top-level form.
1577 Print the result in the minibuffer.
1578 With argument, insert value in current buffer after the form."
1582 (beginning-of-defun)
1583 (let* ((byte-compile-current-file (buffer-file-name))
1584 (load-file-name (buffer-file-name))
1585 (byte-compile-last-warned-form 'nothing)
1586 (value (eval (displaying-byte-compile-warnings
1587 (byte-compile-sexp (read (current-buffer))
1588 "toplevel forms")))))
1590 (message "Compiling from buffer... done.")
1591 (prin1 value (current-buffer))
1593 ((message "%s" (prin1-to-string value)))))))
1595 (defvar byte-compile-inbuffer)
1596 (defvar byte-compile-outbuffer)
1598 (defun byte-compile-from-buffer (byte-compile-inbuffer filename &optional eval)
1599 ;; buffer --> output-buffer, or buffer --> eval form, return nil
1600 (let (byte-compile-outbuffer
1601 ;; Prevent truncation of flonums and lists as we read and print them
1602 (float-output-format nil)
1603 (case-fold-search nil)
1606 ;; Simulate entry to byte-compile-top-level
1607 (byte-compile-constants nil)
1608 (byte-compile-variables nil)
1609 (byte-compile-tag-number 0)
1610 (byte-compile-depth 0)
1611 (byte-compile-maxdepth 0)
1612 (byte-compile-output nil)
1613 ;; #### This is bound in b-c-close-variables.
1614 ;; (byte-compile-warnings (if (eq byte-compile-warnings t)
1615 ;; byte-compile-warning-types
1616 ;; byte-compile-warnings))
1618 (byte-compile-close-variables
1620 (setq byte-compile-outbuffer
1621 (set-buffer (get-buffer-create " *Compiler Output*")))
1623 ;; (emacs-lisp-mode)
1624 (setq case-fold-search nil)
1627 (byte-compile-insert-header filename
1628 byte-compile-inbuffer
1629 byte-compile-outbuffer))
1631 ;; This is a kludge. Some operating systems (OS/2, DOS) need to
1632 ;; write files containing binary information specially.
1633 ;; Under most circumstances, such files will be in binary
1634 ;; overwrite mode, so those OS's use that flag to guess how
1635 ;; they should write their data. Advise them that .elc files
1636 ;; need to be written carefully.
1637 (setq overwrite-mode 'overwrite-mode-binary))
1638 (displaying-byte-compile-warnings
1640 (set-buffer byte-compile-inbuffer)
1643 ;; Compile the forms from the input buffer.
1645 (while (progn (skip-chars-forward " \t\n\^L")
1649 (byte-compile-file-form (read byte-compile-inbuffer)))
1651 ;; Compile pending forms at end of file.
1652 (byte-compile-flush-pending)
1653 (byte-compile-warn-about-unresolved-functions)
1654 ;; Should we always do this? When calling multiple files, it
1655 ;; would be useful to delay this warning until all have
1657 (setq byte-compile-unresolved-functions nil)))
1659 (set-buffer byte-compile-outbuffer)
1660 (goto-char (point-min))))
1662 byte-compile-outbuffer
1664 (while (condition-case nil
1665 (progn (setq form (read byte-compile-outbuffer))
1669 (kill-buffer byte-compile-outbuffer)
1672 (defun byte-compile-insert-header (filename byte-compile-inbuffer
1673 byte-compile-outbuffer)
1674 (set-buffer byte-compile-inbuffer)
1675 (let ((dynamic-docstrings byte-compile-dynamic-docstrings))
1676 (set-buffer byte-compile-outbuffer)
1679 ;; The magic number of .elc files is ";ELC", or 0x3B454C43. After that is
1680 ;; the file-format version number (19 or 20) as a byte, followed by some
1681 ;; nulls. The primary motivation for doing this is to get some binary
1682 ;; characters up in the first line of the file so that `diff' will simply
1683 ;; say "Binary files differ" instead of actually doing a diff of two .elc
1684 ;; files. An extra benefit is that you can add this to /etc/magic:
1686 ;; 0 string ;ELC GNU Emacs Lisp compiled file,
1687 ;; >4 byte x version %d
1691 (if (byte-compile-version-cond byte-compile-emacs19-compatibility) 19 20)
1694 (insert ";;; compiled by "
1695 (or (and (boundp 'user-mail-address) user-mail-address)
1696 (concat (user-login-name) "@" (system-name)))
1698 (current-time-string) "\n;;; from file " filename "\n")
1699 (insert ";;; emacs version " emacs-version ".\n")
1700 (insert ";;; bytecomp version " byte-compile-version "\n;;; "
1702 ((eq byte-optimize 'source) "source-level optimization only")
1703 ((eq byte-optimize 'byte) "byte-level optimization only")
1704 (byte-optimize "optimization is on")
1705 (t "optimization is off"))
1706 (if (byte-compile-version-cond byte-compile-emacs19-compatibility)
1707 "; compiled with Emacs 19 compatibility.\n"
1709 (if (not (byte-compile-version-cond byte-compile-emacs19-compatibility))
1710 (insert ";;; this file uses opcodes which do not exist in Emacs 19.\n"
1711 ;; Have to check if emacs-version is bound so that this works
1712 ;; in files loaded early in loadup.el.
1713 "\n(if (and (boundp 'emacs-version)\n"
1714 "\t (or (and (boundp 'epoch::version) epoch::version)\n"
1715 "\t (string-lessp emacs-version \"20\")))\n"
1717 ;; prin1-to-string is used to quote backslashes.
1718 (substring (prin1-to-string (file-name-nondirectory filename))
1720 "' was compiled for Emacs 20\"))\n\n"))
1721 (insert "(or (boundp 'current-load-list) (setq current-load-list nil))\n"
1723 (if (and (byte-compile-version-cond byte-compile-emacs19-compatibility)
1725 (insert ";;; this file uses opcodes which do not exist prior to\n"
1726 ";;; XEmacs 19.14/GNU Emacs 19.29 or later."
1727 ;; Have to check if emacs-version is bound so that this works
1728 ;; in files loaded early in loadup.el.
1729 "\n(if (and (boundp 'emacs-version)\n"
1730 "\t (or (and (boundp 'epoch::version) epoch::version)\n"
1731 "\t (and (not (string-match \"XEmacs\" emacs-version))\n"
1732 "\t (string-lessp emacs-version \"19.29\"))\n"
1733 "\t (string-lessp emacs-version \"19.14\")))\n"
1735 ;; prin1-to-string is used to quote backslashes.
1736 (substring (prin1-to-string (file-name-nondirectory filename))
1738 "' was compiled for XEmacs 19.14/Emacs 19.29 or later\"))\n\n"
1742 ;; back in the inbuffer; determine and set the coding system for the .elc
1743 ;; file if under Mule. If there are any extended characters in the
1744 ;; input file, use `escape-quoted' to make sure that both binary and
1745 ;; extended characters are output properly and distinguished properly.
1746 ;; Otherwise, use `binary' for maximum portability with non-Mule
1748 (when (featurep '(or mule file-coding))
1749 (defvar buffer-file-coding-system)
1751 (if (featurep 'mule)
1753 (set-buffer byte-compile-inbuffer)
1754 (goto-char (point-min))
1755 ;; mrb- There must be a better way than skip-chars-forward
1756 (skip-chars-forward (concat (char-to-string 0) "-"
1757 (char-to-string 255)))
1758 (if (eq (point) (point-max))
1760 (goto-char (point-min))
1761 (while (< (point)(point-max))
1762 (cond ((eq (char-after) ?\;)
1763 (delete-region (point)(point-at-eol))
1764 (if (eq (char-after) ?\n)
1768 ((eq (char-after) ?\?)
1771 ((eq (char-after) ?\n)
1774 ((eq (char-after) ?\")
1776 (while (and (< (point)(point-max))
1777 (not (when (eq (char-after) ?\")
1780 (if (eq (char-after) ?\\)
1786 (goto-char (point-min))
1787 (skip-chars-forward (concat (char-to-string 0) "-"
1788 (char-to-string 255))))
1790 (if (eq (point) (point-max))
1791 (if (and (featurep 'utf-2000)
1792 (re-search-backward "\\\\u[0-9A-Fa-f]+" nil t))
1796 (if (eq ces 'binary)
1797 (setq buffer-file-coding-system 'binary)
1798 (cond ((eq ces 'utf-8-unix)
1799 (insert "(require 'mule)\n;;;###coding system: utf-8-unix\n")
1800 (setq buffer-file-coding-system 'utf-8-unix)
1803 (insert "(require 'mule)\n;;;###coding system: escape-quoted\n")
1804 (setq buffer-file-coding-system 'escape-quoted)
1806 ;; #### Lazy loading not yet implemented for MULE files
1807 ;; mrb - Fix this someday.
1809 (set-buffer byte-compile-inbuffer)
1810 (setq byte-compile-dynamic nil
1811 byte-compile-dynamic-docstrings nil))
1812 ;; (external-debugging-output
1813 ;; (prin1-to-string (buffer-local-variables)))
1818 (defun byte-compile-output-file-form (form)
1819 ;; writes the given form to the output buffer, being careful of docstrings
1820 ;; in defun, defmacro, defvar, defconst and autoload because make-docfile is
1821 ;; so amazingly stupid.
1822 ;; defalias calls are output directly by byte-compile-file-form-defmumble;
1823 ;; it does not pay to first build the defalias in defmumble and then parse
1825 (if (and (memq (car-safe form) '(defun defmacro defvar defconst autoload))
1826 (stringp (nth 3 form)))
1827 (byte-compile-output-docform nil nil '("\n(" 3 ")") form nil
1828 (eq (car form) 'autoload))
1829 (let ((print-escape-newlines t)
1832 (print-readably t) ; print #[] for bytecode, 'x for (quote x)
1833 (print-gensym (if (and byte-compile-print-gensym
1834 (not byte-compile-emacs19-compatibility))
1836 (princ "\n" byte-compile-outbuffer)
1837 (prin1 form byte-compile-outbuffer)
1840 (defun byte-compile-output-docform (preface name info form specindex quoted)
1841 "Print a form with a doc string. INFO is (prefix doc-index postfix).
1842 If PREFACE and NAME are non-nil, print them too,
1843 before INFO and the FORM but after the doc string itself.
1844 If SPECINDEX is non-nil, it is the index in FORM
1845 of the function bytecode string. In that case,
1846 we output that argument and the following argument (the constants vector)
1847 together, for lazy loading.
1848 QUOTED says that we have to put a quote before the
1849 list that represents a doc string reference.
1850 `autoload' needs that."
1851 ;; We need to examine byte-compile-dynamic-docstrings
1852 ;; in the input buffer (now current), not in the output buffer.
1853 (let ((dynamic-docstrings byte-compile-dynamic-docstrings))
1855 (prog1 (current-buffer)
1856 (set-buffer byte-compile-outbuffer)
1859 ;; Insert the doc string, and make it a comment with #@LENGTH.
1860 (and (>= (nth 1 info) 0)
1863 ;; Make the doc string start at beginning of line
1864 ;; for make-docfile's sake.
1867 (byte-compile-output-as-comment
1868 (nth (nth 1 info) form) nil))
1869 ;; If the doc string starts with * (a user variable),
1871 (if (and (stringp (nth (nth 1 info) form))
1872 (> (length (nth (nth 1 info) form)) 0)
1873 (char= (aref (nth (nth 1 info) form) 0) ?*))
1874 (setq position (- position)))))
1879 (prin1 name byte-compile-outbuffer)))
1881 (let ((print-escape-newlines t)
1882 (print-readably t) ; print #[] for bytecode, 'x for (quote x)
1883 ;; Use a cons cell to say that we want
1884 ;; print-gensym-alist not to be cleared between calls
1885 ;; to print functions.
1886 (print-gensym (if (and byte-compile-print-gensym
1887 (not byte-compile-emacs19-compatibility))
1891 (prin1 (car form) byte-compile-outbuffer)
1892 (while (setq form (cdr form))
1893 (setq index (1+ index))
1895 (cond ((and (numberp specindex) (= index specindex))
1897 (byte-compile-output-as-comment
1898 (cons (car form) (nth 1 form))
1900 (princ (format "(#$ . %d) nil" position)
1901 byte-compile-outbuffer)
1902 (setq form (cdr form))
1903 (setq index (1+ index))))
1904 ((= index (nth 1 info))
1906 (princ (format (if quoted "'(#$ . %d)" "(#$ . %d)")
1908 byte-compile-outbuffer)
1909 (let ((print-escape-newlines nil))
1910 (goto-char (prog1 (1+ (point))
1912 byte-compile-outbuffer)))
1914 (goto-char (point-max)))))
1916 (prin1 (car form) byte-compile-outbuffer)))))
1917 (insert (nth 2 info))))))
1920 (defvar for-effect) ; ## Kludge! This should be an arg, not a special.
1922 (defun byte-compile-keep-pending (form &optional handler)
1923 (if (memq byte-optimize '(t source))
1924 (setq form (byte-optimize-form form t)))
1926 (let ((for-effect t))
1927 ;; To avoid consing up monstrously large forms at load time, we split
1928 ;; the output regularly.
1929 (and (memq (car-safe form) '(fset defalias define-function))
1930 (nthcdr 300 byte-compile-output)
1931 (byte-compile-flush-pending))
1932 (funcall handler form)
1934 (byte-compile-discard)))
1935 (byte-compile-form form t))
1938 (defun byte-compile-flush-pending ()
1939 (if byte-compile-output
1940 (let ((form (byte-compile-out-toplevel t 'file)))
1941 (cond ((eq (car-safe form) 'progn)
1942 (mapcar 'byte-compile-output-file-form (cdr form)))
1944 (byte-compile-output-file-form form)))
1945 (setq byte-compile-constants nil
1946 byte-compile-variables nil
1947 byte-compile-depth 0
1948 byte-compile-maxdepth 0
1949 byte-compile-output nil))))
1951 (defun byte-compile-file-form (form)
1952 (let ((byte-compile-current-form nil) ; close over this for warnings.
1956 (byte-compile-keep-pending form))
1957 ((and (symbolp (car form))
1958 (setq handler (get (car form) 'byte-hunk-handler)))
1959 (cond ((setq form (funcall handler form))
1960 (byte-compile-flush-pending)
1961 (byte-compile-output-file-form form))))
1962 ((eq form (setq form (macroexpand form byte-compile-macro-environment)))
1963 (byte-compile-keep-pending form))
1965 (byte-compile-file-form form)))))
1967 ;; Functions and variables with doc strings must be output separately,
1968 ;; so make-docfile can recognize them. Most other things can be output
1971 (put 'defsubst 'byte-hunk-handler 'byte-compile-file-form-defsubst)
1972 (defun byte-compile-file-form-defsubst (form)
1973 (cond ((assq (nth 1 form) byte-compile-unresolved-functions)
1974 (setq byte-compile-current-form (nth 1 form))
1975 (byte-compile-warn "defsubst %s was used before it was defined"
1977 (byte-compile-file-form
1978 (macroexpand form byte-compile-macro-environment))
1979 ;; Return nil so the form is not output twice.
1982 (put 'autoload 'byte-hunk-handler 'byte-compile-file-form-autoload)
1983 (defun byte-compile-file-form-autoload (form)
1985 ;; If this is an autoload of a macro, and all arguments are constants (that
1986 ;; is, there is no hairy computation going on here) then evaluate the form
1987 ;; at compile-time. This is so that we can make use of macros which we
1988 ;; have autoloaded from the file being compiled. Normal function autoloads
1989 ;; are not automatically evaluated at compile time, because there's not
1990 ;; much point to it (so why bother cluttering up the compile-time namespace.)
1992 ;; If this is an autoload of a function, then record its definition in the
1993 ;; byte-compile-autoload-environment to suppress any `not known to be
1994 ;; defined' warnings at the end of this file (this only matters for
1995 ;; functions which are autoloaded and compiled in the same file, if the
1996 ;; autoload already exists in the compilation environment, we wouldn't have
1999 (let* ((name (if (byte-compile-constp (nth 1 form))
2000 (eval (nth 1 form))))
2001 ;; In v19, the 5th arg to autoload can be t, nil, 'macro, or 'keymap.
2002 (macrop (and (byte-compile-constp (nth 5 form))
2003 (memq (eval (nth 5 form)) '(t macro))))
2004 ;; (functionp (and (byte-compile-constp (nth 5 form))
2005 ;; (eq 'nil (eval (nth 5 form)))))
2009 ;; all forms are constant
2010 (while (if (setq form (cdr form))
2011 (byte-compile-constp (car form))))
2013 ;; eval the macro autoload into the compilation environment
2017 (let ((old (assq name byte-compile-autoload-environment)))
2019 (if (memq 'redefine byte-compile-warnings)
2020 (byte-compile-warn "multiple autoloads for %s" name))
2023 ;; We only use the names in the autoload environment, but
2024 ;; it might be useful to have the bodies some day.
2025 (setq byte-compile-autoload-environment
2026 (cons (cons name form)
2027 byte-compile-autoload-environment)))))))
2029 ;; Now output the form.
2030 (if (stringp (nth 3 form))
2032 ;; No doc string, so we can compile this as a normal form.
2033 (byte-compile-keep-pending form 'byte-compile-normal-call)))
2035 (put 'defvar 'byte-hunk-handler 'byte-compile-file-form-defvar)
2036 (put 'defconst 'byte-hunk-handler 'byte-compile-file-form-defvar)
2037 (defun byte-compile-file-form-defvar (form)
2038 (if (> (length form) 4)
2039 (byte-compile-warn "%s used with too many args (%s)"
2040 (car form) (nth 1 form)))
2041 (if (and (> (length form) 3) (not (stringp (nth 3 form))))
2042 (byte-compile-warn "Third arg to %s %s is not a string: %s"
2043 (car form) (nth 1 form) (nth 3 form)))
2044 (if (null (nth 3 form))
2045 ;; Since there is no doc string, we can compile this as a normal form,
2046 ;; and not do a file-boundary.
2047 (byte-compile-keep-pending form)
2048 (if (memq 'free-vars byte-compile-warnings)
2049 (setq byte-compile-bound-variables
2050 (cons (cons (nth 1 form) byte-compile-global-bit)
2051 byte-compile-bound-variables)))
2052 (cond ((consp (nth 2 form))
2053 (setq form (copy-sequence form))
2054 (setcar (cdr (cdr form))
2055 (byte-compile-top-level (nth 2 form) nil 'file))))
2057 ;; The following turns out not to be necessary, since we emit a call to
2058 ;; defvar, which can hack Vfile_domain by itself!
2060 ;; If a file domain has been set, emit (put 'VAR 'variable-domain ...)
2061 ;; after this defvar.
2062 ; (if byte-compile-file-domain
2064 ; ;; Actually, this will emit the (put ...) before the (defvar ...)
2065 ; ;; but I don't think that can matter in this case.
2066 ; (byte-compile-keep-pending
2067 ; (list 'put (list 'quote (nth 1 form)) ''variable-domain
2068 ; (list 'quote byte-compile-file-domain)))))
2071 (put 'require 'byte-hunk-handler 'byte-compile-file-form-eval-boundary)
2072 (defun byte-compile-file-form-eval-boundary (form)
2074 (byte-compile-keep-pending form 'byte-compile-normal-call))
2076 (put 'progn 'byte-hunk-handler 'byte-compile-file-form-progn)
2077 (put 'prog1 'byte-hunk-handler 'byte-compile-file-form-progn)
2078 (put 'prog2 'byte-hunk-handler 'byte-compile-file-form-progn)
2079 (defun byte-compile-file-form-progn (form)
2080 (mapcar 'byte-compile-file-form (cdr form))
2081 ;; Return nil so the forms are not output twice.
2084 ;; This handler is not necessary, but it makes the output from dont-compile
2085 ;; and similar macros cleaner.
2086 (put 'eval 'byte-hunk-handler 'byte-compile-file-form-eval)
2087 (defun byte-compile-file-form-eval (form)
2088 (if (eq (car-safe (nth 1 form)) 'quote)
2089 (nth 1 (nth 1 form))
2090 (byte-compile-keep-pending form)))
2092 (put 'defun 'byte-hunk-handler 'byte-compile-file-form-defun)
2093 (defun byte-compile-file-form-defun (form)
2094 (byte-compile-file-form-defmumble form nil))
2096 (put 'defmacro 'byte-hunk-handler 'byte-compile-file-form-defmacro)
2097 (defun byte-compile-file-form-defmacro (form)
2098 (byte-compile-file-form-defmumble form t))
2100 (defun byte-compile-compiled-obj-to-list (obj)
2101 ;; #### this is fairly disgusting. Rewrite the code instead
2102 ;; so that it doesn't create compiled objects in the first place!
2103 ;; Much better than creating them and then "uncreating" them
2106 (substring (let ((print-readably t)
2108 (if (and byte-compile-print-gensym
2109 (not byte-compile-emacs19-compatibility))
2111 (print-gensym-alist nil))
2112 (prin1-to-string obj))
2116 (defun byte-compile-file-form-defmumble (form macrop)
2117 (let* ((name (car (cdr form)))
2118 (this-kind (if macrop 'byte-compile-macro-environment
2119 'byte-compile-function-environment))
2120 (that-kind (if macrop 'byte-compile-function-environment
2121 'byte-compile-macro-environment))
2122 (this-one (assq name (symbol-value this-kind)))
2123 (that-one (assq name (symbol-value that-kind)))
2124 (byte-compile-free-references nil)
2125 (byte-compile-free-assignments nil))
2127 ;; When a function or macro is defined, add it to the call tree so that
2128 ;; we can tell when functions are not used.
2129 (if byte-compile-generate-call-tree
2130 (or (assq name byte-compile-call-tree)
2131 (setq byte-compile-call-tree
2132 (cons (list name nil nil) byte-compile-call-tree))))
2134 (setq byte-compile-current-form name) ; for warnings
2135 (when (memq 'redefine byte-compile-warnings)
2136 (byte-compile-arglist-warn form macrop))
2137 (defvar filename) ; #### filename used free
2138 (when byte-compile-verbose
2139 (message "Compiling %s... (%s)"
2140 (if filename (file-name-nondirectory filename) "")
2143 (when (and (memq 'redefine byte-compile-warnings)
2144 ;; hack hack: don't warn when compiling the stubs in
2145 ;; bytecomp-runtime...
2146 (not (assq (nth 1 form)
2147 byte-compile-initial-macro-environment)))
2149 "%s defined multiple times, as both function and macro"
2151 (setcdr that-one nil))
2153 (when (and (memq 'redefine byte-compile-warnings)
2154 ;; hack: don't warn when compiling the magic internal
2155 ;; byte-compiler macros in bytecomp-runtime.el...
2156 (not (assq (nth 1 form)
2157 byte-compile-initial-macro-environment)))
2158 (byte-compile-warn "%s %s defined multiple times in this file"
2159 (if macrop "macro" "function")
2161 ((and (fboundp name)
2162 (or (subrp (symbol-function name))
2163 (eq (car-safe (symbol-function name))
2164 (if macrop 'lambda 'macro))))
2165 (if (memq 'redefine byte-compile-warnings)
2166 (byte-compile-warn "%s %s being redefined as a %s"
2167 (if (subrp (symbol-function name))
2169 (if macrop "function" "macro"))
2171 (if macrop "macro" "function")))
2172 ;; shadow existing definition
2174 (cons (cons name nil) (symbol-value this-kind)))))
2175 (let ((body (nthcdr 3 form)))
2176 (if (and (stringp (car body))
2177 (symbolp (car-safe (cdr-safe body)))
2178 (car-safe (cdr-safe body))
2179 (stringp (car-safe (cdr-safe (cdr-safe body)))))
2180 (byte-compile-warn "Probable `\"' without `\\' in doc string of %s"
2182 (let* ((new-one (byte-compile-lambda (cons 'lambda (nthcdr 2 form))))
2183 (code (byte-compile-byte-code-maker new-one)))
2185 (setcdr this-one new-one)
2187 (cons (cons name new-one) (symbol-value this-kind))))
2188 (if (and (stringp (nth 3 form))
2189 (eq 'quote (car-safe code))
2190 (eq 'lambda (car-safe (nth 1 code))))
2192 (cons name (cdr (nth 1 code))))
2193 (byte-compile-flush-pending)
2194 (if (not (stringp (nth 3 form)))
2195 ;; No doc string. Provide -1 as the "doc string index"
2196 ;; so that no element will be treated as a doc string.
2197 (byte-compile-output-docform
2201 (if macrop '(" '(macro . #[" -1 "])") '(" #[" -1 "]")))
2202 ((eq (car code) 'quote)
2204 (if macrop '(" '(macro " -1 ")") '(" '(" -1 ")")))
2205 ((if macrop '(" (cons 'macro (" -1 "))") '(" (" -1 ")"))))
2206 ;; FSF just calls `(append code nil)' here but that relies
2207 ;; on horrible C kludges in concat() that accept byte-
2208 ;; compiled objects and pretend they're vectors.
2209 (if (compiled-function-p code)
2210 (byte-compile-compiled-obj-to-list code)
2212 (and (atom code) byte-compile-dynamic
2215 ;; Output the form by hand, that's much simpler than having
2216 ;; b-c-output-file-form analyze the defalias.
2217 (byte-compile-output-docform
2220 (cond ((atom code) ; compiled-function-p
2221 (if macrop '(" '(macro . #[" 4 "])") '(" #[" 4 "]")))
2222 ((eq (car code) 'quote)
2224 (if macrop '(" '(macro " 2 ")") '(" '(" 2 ")")))
2225 ((if macrop '(" (cons 'macro (" 5 "))") '(" (" 5 ")"))))
2226 ;; The result of byte-compile-byte-code-maker is either a
2227 ;; compiled-function object, or a list of some kind. If it's
2228 ;; not a cons, we must coerce it into a list of the elements
2229 ;; to be printed to the file.
2233 (compiled-function-arglist code)
2234 (compiled-function-instructions code)
2235 (compiled-function-constants code)
2236 (compiled-function-stack-depth code))
2237 (let ((doc (documentation code t)))
2238 (if doc (list doc)))
2240 (list (nth 1 (compiled-function-interactive code))))))
2241 (and (atom code) byte-compile-dynamic
2244 (princ ")" byte-compile-outbuffer)
2247 ;; Print Lisp object EXP in the output file, inside a comment,
2248 ;; and return the file position it will have.
2249 ;; If QUOTED is non-nil, print with quoting; otherwise, print without quoting.
2250 (defun byte-compile-output-as-comment (exp quoted)
2251 (let ((position (point)))
2253 (prog1 (current-buffer)
2254 (set-buffer byte-compile-outbuffer)
2256 ;; Insert EXP, and make it a comment with #@LENGTH.
2259 (prin1 exp byte-compile-outbuffer)
2260 (princ exp byte-compile-outbuffer))
2261 (goto-char position)
2262 ;; Quote certain special characters as needed.
2263 ;; get_doc_string in doc.c does the unquoting.
2264 (while (search-forward "\^A" nil t)
2265 (replace-match "\^A\^A" t t))
2266 (goto-char position)
2267 (while (search-forward "\000" nil t)
2268 (replace-match "\^A0" t t))
2269 (goto-char position)
2270 (while (search-forward "\037" nil t)
2271 (replace-match "\^A_" t t))
2272 (goto-char (point-max))
2274 (goto-char position)
2275 (insert "#@" (format "%d" (- (point-max) position)))
2277 ;; Save the file position of the object.
2278 ;; Note we should add 1 to skip the space
2279 ;; that we inserted before the actual doc string,
2280 ;; and subtract 1 to convert from an 1-origin Emacs position
2281 ;; to a file position; they cancel.
2282 (setq position (point))
2283 (goto-char (point-max))))
2288 ;; The `domain' declaration. This is legal only at top-level in a file, and
2289 ;; should generally be the first form in the file. It is not legal inside
2292 (put 'domain 'byte-hunk-handler 'byte-compile-file-form-domain)
2293 (defun byte-compile-file-form-domain (form)
2294 (if (not (null (cdr (cdr form))))
2295 (byte-compile-warn "domain used with too many arguments: %s" form))
2296 (let ((domain (nth 1 form)))
2301 "argument to `domain' declaration must be a literal string: %s"
2304 (setq byte-compile-file-domain domain))
2305 (byte-compile-keep-pending form 'byte-compile-normal-call))
2307 (defun byte-compile-domain (form)
2308 (byte-compile-warn "The `domain' declaration is legal only at top-level: %s"
2309 (let ((print-escape-newlines t)
2312 (prin1-to-string form)))
2313 (byte-compile-normal-call
2314 (list 'signal ''error
2315 (list 'quote (list "`domain' used inside a function" form)))))
2317 ;; This is part of bytecomp.el in 19.35:
2318 (put 'custom-declare-variable 'byte-hunk-handler
2319 'byte-compile-file-form-custom-declare-variable)
2320 (defun byte-compile-file-form-custom-declare-variable (form)
2321 (if (memq 'free-vars byte-compile-warnings)
2322 (setq byte-compile-bound-variables
2323 (cons (cons (nth 1 (nth 1 form))
2324 byte-compile-global-bit)
2325 byte-compile-bound-variables)))
2330 (defun byte-compile (form)
2331 "If FORM is a symbol, byte-compile its function definition.
2332 If FORM is a lambda or a macro, byte-compile it as a function."
2333 (displaying-byte-compile-warnings
2334 (byte-compile-close-variables
2335 (let* ((fun (if (symbolp form)
2336 (and (fboundp form) (symbol-function form))
2338 (macro (eq (car-safe fun) 'macro)))
2340 (setq fun (cdr fun)))
2341 (cond ((eq (car-safe fun) 'lambda)
2343 (cons 'macro (byte-compile-lambda fun))
2344 (byte-compile-lambda fun)))
2350 (defun byte-compile-sexp (sexp &optional msg)
2351 "Compile and return SEXP."
2352 (displaying-byte-compile-warnings
2353 (byte-compile-close-variables
2355 (byte-compile-top-level sexp)
2356 (byte-compile-warn-about-unresolved-functions msg)))))
2358 ;; Given a function made by byte-compile-lambda, make a form which produces it.
2359 (defun byte-compile-byte-code-maker (fun)
2361 ;; ## atom is faster than compiled-func-p.
2362 ((atom fun) ; compiled-function-p
2364 ;; b-c-lambda didn't produce a compiled-function, so it must be a trivial
2367 (if (and (setq tmp (assq 'byte-code (cdr-safe (cdr fun))))
2368 (null (cdr (memq tmp fun))))
2369 ;; Generate a make-byte-code call.
2370 (let* ((interactive (assq 'interactive (cdr (cdr fun)))))
2371 (nconc (list 'make-byte-code
2372 (list 'quote (nth 1 fun)) ;arglist
2373 (nth 1 tmp) ;instructions
2374 (nth 2 tmp) ;constants
2375 (nth 3 tmp)) ;stack-depth
2376 (cond ((stringp (nth 2 fun))
2377 (list (nth 2 fun))) ;docstring
2381 (list (if (or (null (nth 1 interactive))
2382 (stringp (nth 1 interactive)))
2384 ;; Interactive spec is a list or a variable
2385 ;; (if it is correct).
2386 (list 'quote (nth 1 interactive))))))))
2387 ;; a non-compiled function (probably trivial)
2388 (list 'quote fun))))))
2390 ;; Byte-compile a lambda-expression and return a valid function.
2391 ;; The value is usually a compiled function but may be the original
2392 ;; lambda-expression.
2393 (defun byte-compile-lambda (fun)
2394 (or (eq 'lambda (car-safe fun))
2395 (error "not a lambda -- %s" (prin1-to-string fun)))
2396 (let* ((arglist (nth 1 fun))
2397 (byte-compile-bound-variables
2399 (mapcar #'(lambda (x) (cons x byte-compile-arglist-bit))
2400 (and (memq 'free-vars byte-compile-warnings)
2401 (delq '&rest (delq '&optional
2402 (copy-sequence arglist)))))))
2404 (cons 'new-scope byte-compile-bound-variables))))
2405 (body (cdr (cdr fun)))
2406 (doc (if (stringp (car body))
2408 (setq body (cdr body)))))
2409 (int (assq 'interactive body)))
2410 (dolist (arg arglist)
2411 (cond ((not (symbolp arg))
2412 (byte-compile-warn "non-symbol in arglist: %S" arg))
2413 ((byte-compile-constant-symbol-p arg)
2414 (byte-compile-warn "constant symbol in arglist: %s" arg))
2415 ((and (char= ?\& (aref (symbol-name arg) 0))
2416 (not (eq arg '&optional))
2417 (not (eq arg '&rest)))
2418 (byte-compile-warn "unrecognized `&' keyword in arglist: %s"
2421 ;; Skip (interactive) if it is in front (the most usual location).
2422 (if (eq int (car body))
2423 (setq body (cdr body)))
2424 (cond ((consp (cdr int))
2426 (byte-compile-warn "malformed interactive spec: %s"
2427 (prin1-to-string int)))
2428 ;; If the interactive spec is a call to `list',
2429 ;; don't compile it, because `call-interactively'
2430 ;; looks at the args of `list'.
2431 (let ((form (nth 1 int)))
2432 (while (or (eq (car-safe form) 'let)
2433 (eq (car-safe form) 'let*)
2434 (eq (car-safe form) 'save-excursion))
2435 (while (consp (cdr form))
2436 (setq form (cdr form)))
2437 (setq form (car form)))
2438 (or (eq (car-safe form) 'list)
2439 (setq int (list 'interactive
2440 (byte-compile-top-level (nth 1 int)))))))
2442 (byte-compile-warn "malformed interactive spec: %s"
2443 (prin1-to-string int))))))
2444 (let ((compiled (byte-compile-top-level (cons 'progn body) nil 'lambda)))
2445 (if (memq 'unused-vars byte-compile-warnings)
2446 ;; done compiling in this scope, warn now.
2447 (byte-compile-warn-about-unused-variables))
2448 (if (eq 'byte-code (car-safe compiled))
2449 (apply 'make-byte-code
2450 (append (list arglist)
2451 ;; byte-string, constants-vector, stack depth
2453 ;; optionally, the doc string.
2456 ;; optionally, the interactive spec.
2458 (list (nth 1 int)))))
2460 (nconc (if int (list int))
2461 (cond ((eq (car-safe compiled) 'progn) (cdr compiled))
2462 (compiled (list compiled)))))
2463 (nconc (list 'lambda arglist)
2464 (if (or doc (stringp (car compiled)))
2465 (cons doc (cond (compiled)
2469 (defun byte-compile-constants-vector ()
2470 ;; Builds the constants-vector from the current variables and constants.
2471 ;; This modifies the constants from (const . nil) to (const . offset).
2472 ;; To keep the byte-codes to look up the vector as short as possible:
2473 ;; First 6 elements are vars, as there are one-byte varref codes for those.
2474 ;; Next up to byte-constant-limit are constants, still with one-byte codes.
2475 ;; Next variables again, to get 2-byte codes for variable lookup.
2476 ;; The rest of the constants and variables need 3-byte byte-codes.
2478 (rest (nreverse byte-compile-variables)) ; nreverse because the first
2479 (other (nreverse byte-compile-constants)) ; vars often are used most.
2481 (limits '(5 ; Use the 1-byte varref codes,
2482 63 ; 1-constlim ; 1-byte byte-constant codes,
2483 255 ; 2-byte varref codes,
2484 65535)) ; 3-byte codes for the rest.
2486 (while (or rest other)
2487 (setq limit (car limits))
2488 (while (and rest (not (eq i limit)))
2489 (if (setq tmp (assq (car (car rest)) ret))
2490 (setcdr (car rest) (cdr tmp))
2491 (setcdr (car rest) (setq i (1+ i)))
2492 (setq ret (cons (car rest) ret)))
2493 (setq rest (cdr rest)))
2494 (setq limits (cdr limits)
2496 (setq other rest))))
2497 (apply 'vector (nreverse (mapcar 'car ret)))))
2499 ;; Given an expression FORM, compile it and return an equivalent byte-code
2500 ;; expression (a call to the function byte-code).
2501 (defun byte-compile-top-level (form &optional for-effect output-type)
2502 ;; OUTPUT-TYPE advises about how form is expected to be used:
2503 ;; 'eval or nil -> a single form,
2504 ;; 'progn or t -> a list of forms,
2505 ;; 'lambda -> body of a lambda,
2506 ;; 'file -> used at file-level.
2507 (let ((byte-compile-constants nil)
2508 (byte-compile-variables nil)
2509 (byte-compile-tag-number 0)
2510 (byte-compile-depth 0)
2511 (byte-compile-maxdepth 0)
2512 (byte-compile-output nil))
2513 (if (memq byte-optimize '(t source))
2514 (setq form (byte-optimize-form form for-effect)))
2515 (while (and (eq (car-safe form) 'progn) (null (cdr (cdr form))))
2516 (setq form (nth 1 form)))
2517 (if (and (eq 'byte-code (car-safe form))
2518 (not (memq byte-optimize '(t byte)))
2519 (stringp (nth 1 form))
2520 (vectorp (nth 2 form))
2521 (natnump (nth 3 form)))
2523 (byte-compile-form form for-effect)
2524 (byte-compile-out-toplevel for-effect output-type))))
2526 (defun byte-compile-out-toplevel (&optional for-effect output-type)
2528 ;; The stack is empty. Push a value to be returned from (byte-code ..).
2529 (if (eq (car (car byte-compile-output)) 'byte-discard)
2530 (setq byte-compile-output (cdr byte-compile-output))
2531 (byte-compile-push-constant
2532 ;; Push any constant - preferably one which already is used, and
2533 ;; a number or symbol - ie not some big sequence. The return value
2534 ;; isn't returned, but it would be a shame if some textually large
2535 ;; constant was not optimized away because we chose to return it.
2536 (and (not (assq nil byte-compile-constants)) ; Nil is often there.
2537 (let ((tmp (reverse byte-compile-constants)))
2538 (while (and tmp (not (or (symbolp (car (car tmp)))
2539 (numberp (car (car tmp))))))
2540 (setq tmp (cdr tmp)))
2541 (car (car tmp)))))))
2542 (byte-compile-out 'byte-return 0)
2543 (setq byte-compile-output (nreverse byte-compile-output))
2544 (if (memq byte-optimize '(t byte))
2545 (setq byte-compile-output
2546 (byte-optimize-lapcode byte-compile-output for-effect)))
2548 ;; Decompile trivial functions:
2549 ;; only constants and variables, or a single funcall except in lambdas.
2550 ;; Except for Lisp_Compiled objects, forms like (foo "hi")
2551 ;; are still quicker than (byte-code "..." [foo "hi"] 2).
2552 ;; Note that even (quote foo) must be parsed just as any subr by the
2553 ;; interpreter, so quote should be compiled into byte-code in some contexts.
2554 ;; What to leave uncompiled:
2555 ;; lambda -> never. we used to leave it uncompiled if the body was
2556 ;; a single atom, but that causes confusion if the docstring
2557 ;; uses the (file . pos) syntax. Besides, now that we have
2558 ;; the Lisp_Compiled type, the compiled form is faster.
2559 ;; eval -> atom, quote or (function atom atom atom)
2560 ;; progn -> as <<same-as-eval>> or (progn <<same-as-eval>> atom)
2561 ;; file -> as progn, but takes both quotes and atoms, and longer forms.
2563 (maycall (not (eq output-type 'lambda))) ; t if we may make a funcall.
2566 ;; #### This should be split out into byte-compile-nontrivial-function-p.
2567 ((or (eq output-type 'lambda)
2568 (nthcdr (if (eq output-type 'file) 50 8) byte-compile-output)
2569 (assq 'TAG byte-compile-output) ; Not necessary, but speeds up a bit.
2570 (not (setq tmp (assq 'byte-return byte-compile-output)))
2572 (setq rest (nreverse
2573 (cdr (memq tmp (reverse byte-compile-output)))))
2575 ((memq (car (car rest)) '(byte-varref byte-constant))
2576 (setq tmp (car (cdr (car rest))))
2577 (if (if (eq (car (car rest)) 'byte-constant)
2580 (not (byte-compile-constant-symbol-p tmp)))))
2582 (setq body (cons (list 'quote tmp) body)))
2583 (setq body (cons tmp body))))
2585 ;; Allow a funcall if at most one atom follows it.
2586 (null (nthcdr 3 rest))
2588 ;; XEmacs change for rms funs
2590 (byte-compile-version-cond
2591 byte-compile-emacs19-compatibility)
2592 (get (car (car rest))
2593 'byte-opcode19-invert))
2594 (get (car (car rest))
2595 'byte-opcode-invert)))
2596 (or (null (cdr rest))
2597 (and (memq output-type '(file progn t))
2599 (eq (car (nth 1 rest)) 'byte-discard)
2600 (progn (setq rest (cdr rest)) t))))
2601 (setq maycall nil) ; Only allow one real function call.
2602 (setq body (nreverse body))
2604 (if (and (eq tmp 'funcall)
2605 (eq (car-safe (car body)) 'quote))
2606 (cons (nth 1 (car body)) (cdr body))
2608 (or (eq output-type 'file)
2609 (not (delq nil (mapcar 'consp (cdr (car body))))))))
2610 (setq rest (cdr rest)))
2612 (let ((byte-compile-vector (byte-compile-constants-vector)))
2613 (list 'byte-code (byte-compile-lapcode byte-compile-output)
2614 byte-compile-vector byte-compile-maxdepth)))
2615 ;; it's a trivial function
2616 ((cdr body) (cons 'progn (nreverse body)))
2619 ;; Given BODY, compile it and return a new body.
2620 (defun byte-compile-top-level-body (body &optional for-effect)
2621 (setq body (byte-compile-top-level (cons 'progn body) for-effect t))
2622 (cond ((eq (car-safe body) 'progn)
2627 ;; This is the recursive entry point for compiling each subform of an
2629 ;; If for-effect is non-nil, byte-compile-form will output a byte-discard
2630 ;; before terminating (ie. no value will be left on the stack).
2631 ;; A byte-compile handler may, when for-effect is non-nil, choose output code
2632 ;; which does not leave a value on the stack, and then set for-effect to nil
2633 ;; (to prevent byte-compile-form from outputting the byte-discard).
2634 ;; If a handler wants to call another handler, it should do so via
2635 ;; byte-compile-form, or take extreme care to handle for-effect correctly.
2636 ;; (Use byte-compile-form-do-effect to reset the for-effect flag too.)
2638 (defun byte-compile-form (form &optional for-effect)
2639 (setq form (macroexpand form byte-compile-macro-environment))
2640 (cond ((not (consp form))
2641 (cond ((or (not (symbolp form))
2642 (byte-compile-constant-symbol-p form))
2643 (byte-compile-constant form))
2644 ((and for-effect byte-compile-delete-errors)
2645 (setq for-effect nil))
2646 (t (byte-compile-variable-ref 'byte-varref form))))
2647 ((symbolp (car form))
2648 (let* ((fn (car form))
2649 (handler (get fn 'byte-compile)))
2650 (if (memq fn '(t nil))
2651 (byte-compile-warn "%s called as a function" fn))
2653 (or (not (byte-compile-version-cond
2654 byte-compile-emacs19-compatibility))
2655 (not (get (get fn 'byte-opcode) 'emacs20-opcode))))
2656 (funcall handler form)
2657 (if (memq 'callargs byte-compile-warnings)
2658 (byte-compile-callargs-warn form))
2659 (byte-compile-normal-call form))))
2660 ((and (or (compiled-function-p (car form))
2661 (eq (car-safe (car form)) 'lambda))
2662 ;; if the form comes out the same way it went in, that's
2663 ;; because it was malformed, and we couldn't unfold it.
2664 (not (eq form (setq form (byte-compile-unfold-lambda form)))))
2665 (byte-compile-form form for-effect)
2666 (setq for-effect nil))
2667 ((byte-compile-normal-call form)))
2669 (byte-compile-discard)))
2671 (defun byte-compile-normal-call (form)
2672 (if byte-compile-generate-call-tree
2673 (byte-compile-annotate-call-tree form))
2674 (byte-compile-push-constant (car form))
2675 (mapcar 'byte-compile-form (cdr form)) ; wasteful, but faster.
2676 (byte-compile-out 'byte-call (length (cdr form))))
2678 ;; kludge added to XEmacs to work around the bogosities of a nonlexical lisp.
2679 (or (fboundp 'globally-boundp) (fset 'globally-boundp 'boundp))
2681 (defun byte-compile-variable-ref (base-op var &optional varbind-flags)
2682 (if (or (not (symbolp var)) (byte-compile-constant-symbol-p var))
2685 (byte-varref "Variable reference to %s %s")
2686 (byte-varset "Attempt to set %s %s")
2687 (byte-varbind "Attempt to let-bind %s %s"))
2688 (if (symbolp var) "constant symbol" "non-symbol")
2690 (if (and (get var 'byte-obsolete-variable)
2691 (memq 'obsolete byte-compile-warnings))
2692 (let ((ob (get var 'byte-obsolete-variable)))
2693 (byte-compile-warn "%s is an obsolete variable; %s" var
2696 (format "use %s instead." ob)))))
2697 (if (and (get var 'byte-compatible-variable)
2698 (memq 'pedantic byte-compile-warnings))
2699 (let ((ob (get var 'byte-compatible-variable)))
2700 (byte-compile-warn "%s is provided for compatibility; %s" var
2703 (format "use %s instead." ob)))))
2704 (if (memq 'free-vars byte-compile-warnings)
2705 (if (eq base-op 'byte-varbind)
2706 (setq byte-compile-bound-variables
2707 (cons (cons var (or varbind-flags 0))
2708 byte-compile-bound-variables))
2709 (or (globally-boundp var)
2710 (let ((cell (assq var byte-compile-bound-variables)))
2711 (if cell (setcdr cell
2713 (if (eq base-op 'byte-varset)
2714 byte-compile-assigned-bit
2715 byte-compile-referenced-bit)))))
2716 (if (eq base-op 'byte-varset)
2717 (or (memq var byte-compile-free-assignments)
2719 (byte-compile-warn "assignment to free variable %s"
2721 (setq byte-compile-free-assignments
2722 (cons var byte-compile-free-assignments))))
2723 (or (memq var byte-compile-free-references)
2725 (byte-compile-warn "reference to free variable %s" var)
2726 (setq byte-compile-free-references
2727 (cons var byte-compile-free-references)))))))))
2728 (let ((tmp (assq var byte-compile-variables)))
2730 (setq tmp (list var)
2731 byte-compile-variables (cons tmp byte-compile-variables)))
2732 (byte-compile-out base-op tmp)))
2734 (defmacro byte-compile-get-constant (const)
2735 `(or (if (stringp ,const)
2736 (assoc ,const byte-compile-constants)
2737 (assq ,const byte-compile-constants))
2738 (car (setq byte-compile-constants
2739 (cons (list ,const) byte-compile-constants)))))
2741 ;; Use this when the value of a form is a constant. This obeys for-effect.
2742 (defun byte-compile-constant (const)
2744 (setq for-effect nil)
2745 (byte-compile-out 'byte-constant (byte-compile-get-constant const))))
2747 ;; Use this for a constant that is not the value of its containing form.
2748 ;; This ignores for-effect.
2749 (defun byte-compile-push-constant (const)
2750 (let ((for-effect nil))
2751 (inline (byte-compile-constant const))))
2754 ;; Compile those primitive ordinary functions
2755 ;; which have special byte codes just for speed.
2757 (defmacro byte-defop-compiler (function &optional compile-handler)
2758 ;; add a compiler-form for FUNCTION.
2759 ;; If function is a symbol, then the variable "byte-SYMBOL" must name
2760 ;; the opcode to be used. If function is a list, the first element
2761 ;; is the function and the second element is the bytecode-symbol.
2762 ;; COMPILE-HANDLER is the function to use to compile this byte-op, or
2763 ;; may be the abbreviations 0, 1, 2, 3, 0-1, 1-2, 2-3, 0+1, 1+1, 2+1,
2764 ;; 0-1+1, 1-2+1, 2-3+1, 0+2, or 1+2. If it is nil, then the handler is
2765 ;; "byte-compile-SYMBOL."
2767 (if (symbolp function)
2768 (setq opcode (intern (concat "byte-" (symbol-name function))))
2769 (setq opcode (car (cdr function))
2770 function (car function)))
2772 (list 'put (list 'quote function) ''byte-compile
2774 (or (cdr (assq compile-handler
2775 '((0 . byte-compile-no-args)
2776 (1 . byte-compile-one-arg)
2777 (2 . byte-compile-two-args)
2778 (3 . byte-compile-three-args)
2779 (0-1 . byte-compile-zero-or-one-arg)
2780 (1-2 . byte-compile-one-or-two-args)
2781 (2-3 . byte-compile-two-or-three-args)
2782 (0+1 . byte-compile-no-args-with-one-extra)
2783 (1+1 . byte-compile-one-arg-with-one-extra)
2784 (2+1 . byte-compile-two-args-with-one-extra)
2785 (0-1+1 . byte-compile-zero-or-one-arg-with-one-extra)
2786 (1-2+1 . byte-compile-one-or-two-args-with-one-extra)
2787 (2-3+1 . byte-compile-two-or-three-args-with-one-extra)
2788 (0+2 . byte-compile-no-args-with-two-extra)
2789 (1+2 . byte-compile-one-arg-with-two-extra)
2793 (intern (concat "byte-compile-"
2794 (symbol-name function))))))))
2797 (list 'put (list 'quote function)
2798 ''byte-opcode (list 'quote opcode))
2799 (list 'put (list 'quote opcode)
2800 ''byte-opcode-invert (list 'quote function)))
2803 (defmacro byte-defop-compiler20 (function &optional compile-handler)
2804 ;; Just like byte-defop-compiler, but defines an opcode that will only
2805 ;; be used when byte-compile-emacs19-compatibility is false.
2806 (if (and (byte-compile-single-version)
2807 byte-compile-emacs19-compatibility)
2808 ;; #### instead of doing nothing, this should do some remprops,
2809 ;; #### to protect against the case where a single-version compiler
2810 ;; #### is loaded into a world that has contained a multi-version one.
2815 (or (car (cdr-safe function))
2816 (intern (concat "byte-"
2817 (symbol-name (or (car-safe function) function))))))
2819 (list 'byte-defop-compiler function compile-handler))))
2822 (defmacro byte-defop-compiler-rmsfun (function &optional compile-handler)
2823 ;; for functions like `eq' that compile into different opcodes depending
2824 ;; on the Emacs version: byte-old-eq for v19, byte-eq for v20.
2825 (let ((opcode (intern (concat "byte-" (symbol-name function))))
2826 (opcode19 (intern (concat "byte-old-" (symbol-name function))))
2828 (list 'put (list 'quote function) ''byte-compile
2830 (or (cdr (assq compile-handler
2831 '((2 . byte-compile-two-args-19->20)
2834 (intern (concat "byte-compile-"
2835 (symbol-name function))))))))
2837 (list 'put (list 'quote function)
2838 ''byte-opcode (list 'quote opcode))
2839 (list 'put (list 'quote function)
2840 ''byte-opcode19 (list 'quote opcode19))
2841 (list 'put (list 'quote opcode)
2842 ''byte-opcode-invert (list 'quote function))
2843 (list 'put (list 'quote opcode19)
2844 ''byte-opcode19-invert (list 'quote function)))))
2846 (defmacro byte-defop-compiler-1 (function &optional compile-handler)
2847 (list 'byte-defop-compiler (list function nil) compile-handler))
2850 (put 'byte-call 'byte-opcode-invert 'funcall)
2851 (put 'byte-list1 'byte-opcode-invert 'list)
2852 (put 'byte-list2 'byte-opcode-invert 'list)
2853 (put 'byte-list3 'byte-opcode-invert 'list)
2854 (put 'byte-list4 'byte-opcode-invert 'list)
2855 (put 'byte-listN 'byte-opcode-invert 'list)
2856 (put 'byte-concat2 'byte-opcode-invert 'concat)
2857 (put 'byte-concat3 'byte-opcode-invert 'concat)
2858 (put 'byte-concat4 'byte-opcode-invert 'concat)
2859 (put 'byte-concatN 'byte-opcode-invert 'concat)
2860 (put 'byte-insertN 'byte-opcode-invert 'insert)
2862 ;; How old is this stuff? -slb
2863 ;(byte-defop-compiler (dot byte-point) 0+1)
2864 ;(byte-defop-compiler (dot-max byte-point-max) 0+1)
2865 ;(byte-defop-compiler (dot-min byte-point-min) 0+1)
2866 (byte-defop-compiler point 0+1)
2867 (byte-defop-compiler-rmsfun eq 2)
2868 (byte-defop-compiler point-max 0+1)
2869 (byte-defop-compiler point-min 0+1)
2870 (byte-defop-compiler following-char 0+1)
2871 (byte-defop-compiler preceding-char 0+1)
2872 (byte-defop-compiler current-column 0+1)
2873 ;; FSF has special function here; generalized here by the 1+2 stuff.
2874 (byte-defop-compiler (indent-to-column byte-indent-to) 1+2)
2875 (byte-defop-compiler indent-to 1+2)
2876 (byte-defop-compiler-rmsfun equal 2)
2877 (byte-defop-compiler eolp 0+1)
2878 (byte-defop-compiler eobp 0+1)
2879 (byte-defop-compiler bolp 0+1)
2880 (byte-defop-compiler bobp 0+1)
2881 (byte-defop-compiler current-buffer 0)
2882 ;;(byte-defop-compiler read-char 0) ;; obsolete
2883 (byte-defop-compiler-rmsfun memq 2)
2884 (byte-defop-compiler interactive-p 0)
2885 (byte-defop-compiler widen 0+1)
2886 (byte-defop-compiler end-of-line 0-1+1)
2887 (byte-defop-compiler forward-char 0-1+1)
2888 (byte-defop-compiler forward-line 0-1+1)
2889 (byte-defop-compiler symbolp 1)
2890 (byte-defop-compiler consp 1)
2891 (byte-defop-compiler stringp 1)
2892 (byte-defop-compiler listp 1)
2893 (byte-defop-compiler not 1)
2894 (byte-defop-compiler (null byte-not) 1)
2895 (byte-defop-compiler car 1)
2896 (byte-defop-compiler cdr 1)
2897 (byte-defop-compiler length 1)
2898 (byte-defop-compiler symbol-value 1)
2899 (byte-defop-compiler symbol-function 1)
2900 (byte-defop-compiler (1+ byte-add1) 1)
2901 (byte-defop-compiler (1- byte-sub1) 1)
2902 (byte-defop-compiler goto-char 1+1)
2903 (byte-defop-compiler char-after 0-1+1)
2904 (byte-defop-compiler set-buffer 1)
2905 ;;(byte-defop-compiler set-mark 1) ;; obsolete
2906 (byte-defop-compiler forward-word 1+1)
2907 (byte-defop-compiler char-syntax 1+1)
2908 (byte-defop-compiler nreverse 1)
2909 (byte-defop-compiler car-safe 1)
2910 (byte-defop-compiler cdr-safe 1)
2911 (byte-defop-compiler numberp 1)
2912 (byte-defop-compiler integerp 1)
2913 (byte-defop-compiler skip-chars-forward 1-2+1)
2914 (byte-defop-compiler skip-chars-backward 1-2+1)
2915 (byte-defop-compiler (eql byte-eq) 2)
2916 (byte-defop-compiler20 old-eq 2)
2917 (byte-defop-compiler20 old-memq 2)
2918 (byte-defop-compiler cons 2)
2919 (byte-defop-compiler aref 2)
2920 (byte-defop-compiler get 2+1)
2921 (byte-defop-compiler nth 2)
2922 (byte-defop-compiler substring 2-3)
2923 (byte-defop-compiler (move-marker byte-set-marker) 2-3)
2924 (byte-defop-compiler set-marker 2-3)
2925 (byte-defop-compiler match-beginning 1)
2926 (byte-defop-compiler match-end 1)
2927 (byte-defop-compiler upcase 1+1)
2928 (byte-defop-compiler downcase 1+1)
2929 (byte-defop-compiler string= 2)
2930 (byte-defop-compiler string< 2)
2931 (byte-defop-compiler (string-equal byte-string=) 2)
2932 (byte-defop-compiler (string-lessp byte-string<) 2)
2933 (byte-defop-compiler20 old-equal 2)
2934 (byte-defop-compiler nthcdr 2)
2935 (byte-defop-compiler elt 2)
2936 (byte-defop-compiler20 old-member 2)
2937 (byte-defop-compiler20 old-assq 2)
2938 (byte-defop-compiler (rplaca byte-setcar) 2)
2939 (byte-defop-compiler (rplacd byte-setcdr) 2)
2940 (byte-defop-compiler setcar 2)
2941 (byte-defop-compiler setcdr 2)
2942 (byte-defop-compiler delete-region 2+1)
2943 (byte-defop-compiler narrow-to-region 2+1)
2944 (byte-defop-compiler (% byte-rem) 2)
2945 (byte-defop-compiler aset 3)
2947 (byte-defop-compiler-rmsfun member 2)
2948 (byte-defop-compiler-rmsfun assq 2)
2950 (byte-defop-compiler max byte-compile-associative)
2951 (byte-defop-compiler min byte-compile-associative)
2952 (byte-defop-compiler (+ byte-plus) byte-compile-associative)
2953 (byte-defop-compiler (* byte-mult) byte-compile-associative)
2955 ;;####(byte-defop-compiler move-to-column 1)
2956 (byte-defop-compiler-1 interactive byte-compile-noop)
2957 (byte-defop-compiler-1 domain byte-compile-domain)
2959 ;; As of GNU Emacs 19.18 and Lucid Emacs 19.8, mod and % are different: `%'
2960 ;; means integral remainder and may have a negative result; `mod' is always
2961 ;; positive, and accepts floating point args. All code which uses `mod' and
2962 ;; requires the new interpretation must be compiled with bytecomp version 2.18
2963 ;; or newer, or the emitted code will run the byte-code for `%' instead of an
2964 ;; actual call to `mod'. So be careful of compiling new code with an old
2965 ;; compiler. Note also that `%' is more efficient than `mod' because the
2966 ;; former is byte-coded and the latter is not.
2967 ;;(byte-defop-compiler (mod byte-rem) 2)
2970 (defun byte-compile-subr-wrong-args (form n)
2971 (when (memq 'subr-callargs byte-compile-warnings)
2972 (byte-compile-warn "%s called with %d arg%s, but requires %s"
2973 (car form) (length (cdr form))
2974 (if (= 1 (length (cdr form))) "" "s") n))
2975 ;; get run-time wrong-number-of-args error.
2976 (byte-compile-normal-call form))
2978 (defun byte-compile-no-args (form)
2979 (case (length (cdr form))
2980 (0 (byte-compile-out (get (car form) 'byte-opcode) 0))
2981 (t (byte-compile-subr-wrong-args form "none"))))
2983 (defun byte-compile-one-arg (form)
2984 (case (length (cdr form))
2985 (1 (byte-compile-form (car (cdr form))) ;; Push the argument
2986 (byte-compile-out (get (car form) 'byte-opcode) 0))
2987 (t (byte-compile-subr-wrong-args form 1))))
2989 (defun byte-compile-two-args (form)
2990 (case (length (cdr form))
2991 (2 (byte-compile-form (nth 1 form)) ;; Push the arguments
2992 (byte-compile-form (nth 2 form))
2993 (byte-compile-out (get (car form) 'byte-opcode) 0))
2994 (t (byte-compile-subr-wrong-args form 2))))
2996 (defun byte-compile-three-args (form)
2997 (case (length (cdr form))
2998 (3 (byte-compile-form (nth 1 form)) ;; Push the arguments
2999 (byte-compile-form (nth 2 form))
3000 (byte-compile-form (nth 3 form))
3001 (byte-compile-out (get (car form) 'byte-opcode) 0))
3002 (t (byte-compile-subr-wrong-args form 3))))
3004 (defun byte-compile-zero-or-one-arg (form)
3005 (case (length (cdr form))
3006 (0 (byte-compile-one-arg (append form '(nil))))
3007 (1 (byte-compile-one-arg form))
3008 (t (byte-compile-subr-wrong-args form "0-1"))))
3010 (defun byte-compile-one-or-two-args (form)
3011 (case (length (cdr form))
3012 (1 (byte-compile-two-args (append form '(nil))))
3013 (2 (byte-compile-two-args form))
3014 (t (byte-compile-subr-wrong-args form "1-2"))))
3016 (defun byte-compile-two-or-three-args (form)
3017 (case (length (cdr form))
3018 (2 (byte-compile-three-args (append form '(nil))))
3019 (3 (byte-compile-three-args form))
3020 (t (byte-compile-subr-wrong-args form "2-3"))))
3022 ;; from Ben Wing <ben@xemacs.org>: some inlined functions have extra
3023 ;; optional args added to them in XEmacs 19.12. Changing the byte
3024 ;; interpreter to deal with these args would be wrong and cause
3025 ;; incompatibility, so we generate non-inlined calls for those cases.
3026 ;; Without the following functions, spurious warnings will be generated;
3027 ;; however, they would still compile correctly because
3028 ;; `byte-compile-subr-wrong-args' also converts the call to non-inlined.
3030 (defun byte-compile-no-args-with-one-extra (form)
3031 (case (length (cdr form))
3032 (0 (byte-compile-no-args form))
3033 (1 (byte-compile-normal-call form))
3034 (t (byte-compile-subr-wrong-args form "0-1"))))
3036 (defun byte-compile-one-arg-with-one-extra (form)
3037 (case (length (cdr form))
3038 (1 (byte-compile-one-arg form))
3039 (2 (byte-compile-normal-call form))
3040 (t (byte-compile-subr-wrong-args form "1-2"))))
3042 (defun byte-compile-two-args-with-one-extra (form)
3043 (case (length (cdr form))
3044 (2 (byte-compile-two-args form))
3045 (3 (byte-compile-normal-call form))
3046 (t (byte-compile-subr-wrong-args form "2-3"))))
3048 (defun byte-compile-zero-or-one-arg-with-one-extra (form)
3049 (case (length (cdr form))
3050 (0 (byte-compile-one-arg (append form '(nil))))
3051 (1 (byte-compile-one-arg form))
3052 (2 (byte-compile-normal-call form))
3053 (t (byte-compile-subr-wrong-args form "0-2"))))
3055 (defun byte-compile-one-or-two-args-with-one-extra (form)
3056 (case (length (cdr form))
3057 (1 (byte-compile-two-args (append form '(nil))))
3058 (2 (byte-compile-two-args form))
3059 (3 (byte-compile-normal-call form))
3060 (t (byte-compile-subr-wrong-args form "1-3"))))
3062 (defun byte-compile-two-or-three-args-with-one-extra (form)
3063 (case (length (cdr form))
3064 (2 (byte-compile-three-args (append form '(nil))))
3065 (3 (byte-compile-three-args form))
3066 (4 (byte-compile-normal-call form))
3067 (t (byte-compile-subr-wrong-args form "2-4"))))
3069 (defun byte-compile-no-args-with-two-extra (form)
3070 (case (length (cdr form))
3071 (0 (byte-compile-no-args form))
3072 ((1 2) (byte-compile-normal-call form))
3073 (t (byte-compile-subr-wrong-args form "0-2"))))
3075 (defun byte-compile-one-arg-with-two-extra (form)
3076 (case (length (cdr form))
3077 (1 (byte-compile-one-arg form))
3078 ((2 3) (byte-compile-normal-call form))
3079 (t (byte-compile-subr-wrong-args form "1-3"))))
3081 ;; XEmacs: used for functions that have a different opcode in v19 than v20.
3082 ;; this includes `eq', `equal', and other old-ified functions.
3083 (defun byte-compile-two-args-19->20 (form)
3084 (if (not (= (length form) 3))
3085 (byte-compile-subr-wrong-args form 2)
3086 (byte-compile-form (car (cdr form))) ;; Push the arguments
3087 (byte-compile-form (nth 2 form))
3088 (if (byte-compile-version-cond byte-compile-emacs19-compatibility)
3089 (byte-compile-out (get (car form) 'byte-opcode19) 0)
3090 (byte-compile-out (get (car form) 'byte-opcode) 0))))
3092 (defun byte-compile-noop (form)
3093 (byte-compile-constant nil))
3095 (defun byte-compile-discard ()
3096 (byte-compile-out 'byte-discard 0))
3098 ;; Compile a function that accepts one or more args and is right-associative.
3099 ;; We do it by left-associativity so that the operations
3100 ;; are done in the same order as in interpreted code.
3101 ;(defun byte-compile-associative (form)
3103 ; (let ((opcode (get (car form) 'byte-opcode))
3104 ; (args (copy-sequence (cdr form))))
3105 ; (byte-compile-form (car args))
3106 ; (setq args (cdr args))
3108 ; (byte-compile-form (car args))
3109 ; (byte-compile-out opcode 0)
3110 ; (setq args (cdr args))))
3111 ; (byte-compile-constant (eval form))))
3113 ;; Compile a function that accepts one or more args and is right-associative.
3114 ;; We do it by left-associativity so that the operations
3115 ;; are done in the same order as in interpreted code.
3116 (defun byte-compile-associative (form)
3117 (let ((args (cdr form))
3118 (opcode (get (car form) 'byte-opcode)))
3120 (0 (byte-compile-constant (eval form)))
3121 (t (byte-compile-form (car args))
3122 (dolist (arg (cdr args))
3123 (byte-compile-form arg)
3124 (byte-compile-out opcode 0))))))
3127 ;; more complicated compiler macros
3129 (byte-defop-compiler list)
3130 (byte-defop-compiler concat)
3131 (byte-defop-compiler fset)
3132 (byte-defop-compiler insert)
3133 (byte-defop-compiler-1 function byte-compile-function-form)
3134 (byte-defop-compiler-1 - byte-compile-minus)
3135 (byte-defop-compiler (/ byte-quo) byte-compile-quo)
3136 (byte-defop-compiler nconc)
3137 (byte-defop-compiler-1 beginning-of-line)
3139 (byte-defop-compiler (= byte-eqlsign) byte-compile-arithcompare)
3140 (byte-defop-compiler (< byte-lss) byte-compile-arithcompare)
3141 (byte-defop-compiler (> byte-gtr) byte-compile-arithcompare)
3142 (byte-defop-compiler (<= byte-leq) byte-compile-arithcompare)
3143 (byte-defop-compiler (>= byte-geq) byte-compile-arithcompare)
3145 (defun byte-compile-arithcompare (form)
3146 (case (length (cdr form))
3147 (0 (byte-compile-subr-wrong-args form "1 or more"))
3148 (1 (byte-compile-constant t))
3149 (2 (byte-compile-two-args form))
3150 (t (byte-compile-normal-call form))))
3152 (byte-defop-compiler /= byte-compile-/=)
3154 (defun byte-compile-/= (form)
3155 (case (length (cdr form))
3156 (0 (byte-compile-subr-wrong-args form "1 or more"))
3157 (1 (byte-compile-constant t))
3158 ;; optimize (/= X Y) to (not (= X Y))
3159 (2 (byte-compile-form-do-effect `(not (= ,@(cdr form)))))
3160 (t (byte-compile-normal-call form))))
3162 ;; buffer-substring now has its own function. This used to be
3163 ;; 2+1, but now all args are optional.
3164 (byte-defop-compiler buffer-substring)
3166 (defun byte-compile-buffer-substring (form)
3167 ;; buffer-substring used to take exactly two args, but now takes 0-3.
3168 ;; convert 0-2 to two args and use special bytecode operand.
3169 ;; convert 3 args to a normal call.
3170 (case (length (cdr form))
3171 (0 (byte-compile-two-args (append form '(nil nil))))
3172 (1 (byte-compile-two-args (append form '(nil))))
3173 (2 (byte-compile-two-args form))
3174 (3 (byte-compile-normal-call form))
3175 (t (byte-compile-subr-wrong-args form "0-3"))))
3177 (defun byte-compile-list (form)
3178 (let* ((args (cdr form))
3179 (nargs (length args)))
3182 (byte-compile-constant nil))
3184 (mapcar 'byte-compile-form args)
3186 (aref [byte-list1 byte-list2 byte-list3 byte-list4] (1- nargs))
3189 (mapcar 'byte-compile-form args)
3190 (byte-compile-out 'byte-listN nargs))
3191 (t (byte-compile-normal-call form)))))
3193 (defun byte-compile-concat (form)
3194 (let* ((args (cdr form))
3195 (nargs (length args)))
3196 ;; Concat of one arg is not a no-op if arg is not a string.
3198 ((memq nargs '(2 3 4))
3199 (mapcar 'byte-compile-form args)
3201 (aref [byte-concat2 byte-concat3 byte-concat4] (- nargs 2))
3204 (byte-compile-form ""))
3206 (mapcar 'byte-compile-form args)
3207 (byte-compile-out 'byte-concatN nargs))
3208 ((byte-compile-normal-call form)))))
3210 (defun byte-compile-minus (form)
3211 (let ((args (cdr form)))
3213 (0 (byte-compile-subr-wrong-args form "1 or more"))
3214 (1 (byte-compile-form (car args))
3215 (byte-compile-out 'byte-negate 0))
3216 (t (byte-compile-form (car args))
3217 (dolist (elt (cdr args))
3218 (byte-compile-form elt)
3219 (byte-compile-out 'byte-diff 0))))))
3221 (defun byte-compile-quo (form)
3222 (let ((args (cdr form)))
3224 (0 (byte-compile-subr-wrong-args form "1 or more"))
3225 (1 (byte-compile-constant 1)
3226 (byte-compile-form (car args))
3227 (byte-compile-out 'byte-quo 0))
3228 (t (byte-compile-form (car args))
3229 (dolist (elt (cdr args))
3230 (byte-compile-form elt)
3231 (byte-compile-out 'byte-quo 0))))))
3233 (defun byte-compile-nconc (form)
3234 (let ((args (cdr form)))
3236 (0 (byte-compile-constant nil))
3237 ;; nconc of one arg is a noop, even if that arg isn't a list.
3238 (1 (byte-compile-form (car args)))
3239 (t (byte-compile-form (car args))
3240 (dolist (elt (cdr args))
3241 (byte-compile-form elt)
3242 (byte-compile-out 'byte-nconc 0))))))
3244 (defun byte-compile-fset (form)
3245 ;; warn about forms like (fset 'foo '(lambda () ...))
3246 ;; (where the lambda expression is non-trivial...)
3247 ;; Except don't warn if the first argument is 'make-byte-code, because
3248 ;; I'm sick of getting mail asking me whether that warning is a problem.
3249 (let ((fn (nth 2 form))
3251 (when (and (eq (car-safe fn) 'quote)
3252 (eq (car-safe (setq fn (nth 1 fn))) 'lambda)
3253 (not (eq (car-safe (cdr-safe (nth 1 form))) 'make-byte-code)))
3254 (setq body (cdr (cdr fn)))
3255 (if (stringp (car body)) (setq body (cdr body)))
3256 (if (eq 'interactive (car-safe (car body))) (setq body (cdr body)))
3257 (if (and (consp (car body))
3258 (not (eq 'byte-code (car (car body)))))
3260 "A quoted lambda form is the second argument of fset. This is probably
3261 not what you want, as that lambda cannot be compiled. Consider using
3262 the syntax (function (lambda (...) ...)) instead."))))
3263 (byte-compile-two-args form))
3265 (defun byte-compile-funarg (form)
3266 ;; (mapcar '(lambda (x) ..) ..) ==> (mapcar (function (lambda (x) ..)) ..)
3267 ;; for cases where it's guaranteed that first arg will be used as a lambda.
3268 (byte-compile-normal-call
3269 (let ((fn (nth 1 form)))
3270 (if (and (eq (car-safe fn) 'quote)
3271 (eq (car-safe (nth 1 fn)) 'lambda))
3273 (cons (cons 'function (cdr fn))
3277 ;; (function foo) must compile like 'foo, not like (symbol-function 'foo).
3278 ;; Otherwise it will be incompatible with the interpreter,
3279 ;; and (funcall (function foo)) will lose with autoloads.
3281 (defun byte-compile-function-form (form)
3282 (byte-compile-constant
3283 (cond ((symbolp (nth 1 form))
3285 ((byte-compile-lambda (nth 1 form))))))
3287 (defun byte-compile-insert (form)
3288 (cond ((null (cdr form))
3289 (byte-compile-constant nil))
3290 ((<= (length form) 256)
3291 (mapcar 'byte-compile-form (cdr form))
3292 (if (cdr (cdr form))
3293 (byte-compile-out 'byte-insertN (length (cdr form)))
3294 (byte-compile-out 'byte-insert 0)))
3295 ((memq t (mapcar 'consp (cdr (cdr form))))
3296 (byte-compile-normal-call form))
3297 ;; We can split it; there is no function call after inserting 1st arg.
3299 (while (setq form (cdr form))
3300 (byte-compile-form (car form))
3301 (byte-compile-out 'byte-insert 0)
3303 (byte-compile-discard))))))
3305 ;; alas, the old (pre-19.12, and all existing versions of FSFmacs 19)
3306 ;; byte compiler will generate incorrect code for
3307 ;; (beginning-of-line nil buffer) because it buggily doesn't
3308 ;; check the number of arguments passed to beginning-of-line.
3310 (defun byte-compile-beginning-of-line (form)
3311 (let ((len (length form)))
3313 (byte-compile-subr-wrong-args form "0-2"))
3314 ((or (= len 3) (not (byte-compile-constp (nth 1 form))))
3315 (byte-compile-normal-call form))
3319 (if (integerp (setq form (or (eval (nth 1 form)) 1)))
3322 "Non-numeric arg to beginning-of-line: %s" form)
3323 (list '1- (list 'quote form))))
3325 (byte-compile-constant nil)))))
3328 (byte-defop-compiler set)
3329 (byte-defop-compiler-1 setq)
3330 (byte-defop-compiler-1 set-default)
3331 (byte-defop-compiler-1 setq-default)
3333 (byte-defop-compiler-1 quote)
3334 (byte-defop-compiler-1 quote-form)
3336 (defun byte-compile-setq (form)
3337 (let ((args (cdr form)) var val)
3339 ;; (setq), with no arguments.
3340 (byte-compile-form nil for-effect)
3342 (setq var (pop args))
3344 ;; Odd number of args? Let `set' get the error.
3345 (byte-compile-form `(set ',var) for-effect)
3346 (setq val (pop args))
3348 ;; (setq :foo ':foo) compatibility kludge
3349 (byte-compile-form `(set ',var ,val) (if args t for-effect))
3350 (byte-compile-form val)
3351 (unless (or args for-effect)
3352 (byte-compile-out 'byte-dup 0))
3353 (byte-compile-variable-ref 'byte-varset var))))))
3354 (setq for-effect nil))
3356 (defun byte-compile-set (form)
3357 ;; Compile (set 'foo x) as (setq foo x) for trivially better code and so
3358 ;; that we get applicable warnings. Compile everything else (including
3359 ;; malformed calls) like a normal 2-arg byte-coded function.
3360 (let ((symform (nth 1 form))
3361 (valform (nth 2 form))
3363 (if (and (= (length form) 3)
3364 (= (safe-length symform) 2)
3365 (eq (car symform) 'quote)
3366 (symbolp (setq sym (car (cdr symform))))
3367 (not (byte-compile-constant-symbol-p sym)))
3368 (byte-compile-setq `(setq ,sym ,valform))
3369 (byte-compile-two-args form))))
3371 (defun byte-compile-setq-default (form)
3372 (let ((args (cdr form)))
3374 ;; (setq-default), with no arguments.
3375 (byte-compile-form nil for-effect)
3376 ;; emit multiple calls to `set-default' if necessary
3379 ;; Odd number of args? Let `set-default' get the error.
3380 `(set-default ',(pop args) ,@(if args (list (pop args)) nil))
3381 (if args t for-effect)))))
3382 (setq for-effect nil))
3385 (defun byte-compile-set-default (form)
3386 (let* ((args (cdr form))
3387 (nargs (length args))
3389 (when (and (= (safe-length var) 2)
3390 (eq (car var) 'quote))
3391 (let ((sym (nth 1 var)))
3393 ((not (symbolp sym))
3394 (byte-compile-warn "Attempt to set-globally non-symbol %s" sym))
3395 ((byte-compile-constant-symbol-p sym)
3396 (byte-compile-warn "Attempt to set-globally constant symbol %s" sym))
3397 ((let ((cell (assq sym byte-compile-bound-variables)))
3399 (setcdr cell (logior (cdr cell) byte-compile-assigned-bit))
3401 ;; notice calls to set-default/setq-default for variables which
3402 ;; have not been declared with defvar/defconst.
3403 ((globally-boundp sym)) ; OK
3404 ((not (memq 'free-vars byte-compile-warnings))) ; warnings suppressed?
3405 ((memq sym byte-compile-free-assignments)) ; already warned about sym
3407 (byte-compile-warn "assignment to free variable %s" sym)
3408 (push sym byte-compile-free-assignments)))))
3410 ;; now emit a normal call to set-default
3411 (byte-compile-normal-call form)
3412 (byte-compile-subr-wrong-args form 2))))
3415 (defun byte-compile-quote (form)
3416 (byte-compile-constant (car (cdr form))))
3418 (defun byte-compile-quote-form (form)
3419 (byte-compile-constant (byte-compile-top-level (nth 1 form))))
3422 ;;; control structures
3424 (defun byte-compile-body (body &optional for-effect)
3426 (byte-compile-form (car body) t)
3427 (setq body (cdr body)))
3428 (byte-compile-form (car body) for-effect))
3430 (proclaim-inline byte-compile-body-do-effect)
3431 (defun byte-compile-body-do-effect (body)
3432 (byte-compile-body body for-effect)
3433 (setq for-effect nil))
3435 (proclaim-inline byte-compile-form-do-effect)
3436 (defun byte-compile-form-do-effect (form)
3437 (byte-compile-form form for-effect)
3438 (setq for-effect nil))
3440 (byte-defop-compiler-1 inline byte-compile-progn)
3441 (byte-defop-compiler-1 progn)
3442 (byte-defop-compiler-1 prog1)
3443 (byte-defop-compiler-1 prog2)
3444 (byte-defop-compiler-1 if)
3445 (byte-defop-compiler-1 cond)
3446 (byte-defop-compiler-1 and)
3447 (byte-defop-compiler-1 or)
3448 (byte-defop-compiler-1 while)
3449 (byte-defop-compiler-1 funcall)
3450 (byte-defop-compiler-1 apply byte-compile-funarg)
3451 (byte-defop-compiler-1 mapcar byte-compile-funarg)
3452 (byte-defop-compiler-1 mapatoms byte-compile-funarg)
3453 (byte-defop-compiler-1 mapconcat byte-compile-funarg)
3454 (byte-defop-compiler-1 let)
3455 (byte-defop-compiler-1 let*)
3457 (defun byte-compile-progn (form)
3458 (byte-compile-body-do-effect (cdr form)))
3460 (defun byte-compile-prog1 (form)
3461 (setq form (cdr form))
3462 (byte-compile-form-do-effect (pop form))
3463 (byte-compile-body form t))
3465 (defun byte-compile-prog2 (form)
3466 (setq form (cdr form))
3467 (byte-compile-form (pop form) t)
3468 (byte-compile-form-do-effect (pop form))
3469 (byte-compile-body form t))
3471 (defmacro byte-compile-goto-if (cond discard tag)
3474 (if ,discard 'byte-goto-if-not-nil 'byte-goto-if-not-nil-else-pop)
3475 (if ,discard 'byte-goto-if-nil 'byte-goto-if-nil-else-pop))
3478 (defun byte-compile-if (form)
3479 (byte-compile-form (car (cdr form)))
3480 (if (null (nthcdr 3 form))
3482 (let ((donetag (byte-compile-make-tag)))
3483 (byte-compile-goto-if nil for-effect donetag)
3484 (byte-compile-form (nth 2 form) for-effect)
3485 (byte-compile-out-tag donetag))
3486 (let ((donetag (byte-compile-make-tag)) (elsetag (byte-compile-make-tag)))
3487 (byte-compile-goto 'byte-goto-if-nil elsetag)
3488 (byte-compile-form (nth 2 form) for-effect)
3489 (byte-compile-goto 'byte-goto donetag)
3490 (byte-compile-out-tag elsetag)
3491 (byte-compile-body (cdr (cdr (cdr form))) for-effect)
3492 (byte-compile-out-tag donetag)))
3493 (setq for-effect nil))
3495 (defun byte-compile-cond (clauses)
3496 (let ((donetag (byte-compile-make-tag))
3498 (while (setq clauses (cdr clauses))
3499 (setq clause (car clauses))
3500 (cond ((or (eq (car clause) t)
3501 (and (eq (car-safe (car clause)) 'quote)
3502 (car-safe (cdr-safe (car clause)))))
3503 ;; Unconditional clause
3504 (setq clause (cons t clause)
3507 (byte-compile-form (car clause))
3508 (if (null (cdr clause))
3509 ;; First clause is a singleton.
3510 (byte-compile-goto-if t for-effect donetag)
3511 (setq nexttag (byte-compile-make-tag))
3512 (byte-compile-goto 'byte-goto-if-nil nexttag)
3513 (byte-compile-body (cdr clause) for-effect)
3514 (byte-compile-goto 'byte-goto donetag)
3515 (byte-compile-out-tag nexttag)))))
3517 (and (cdr clause) (not (eq (car clause) t))
3518 (progn (byte-compile-form (car clause))
3519 (byte-compile-goto-if nil for-effect donetag)
3520 (setq clause (cdr clause))))
3521 (byte-compile-body-do-effect clause)
3522 (byte-compile-out-tag donetag)))
3524 (defun byte-compile-and (form)
3525 (let ((failtag (byte-compile-make-tag))
3528 (byte-compile-form-do-effect t)
3530 (byte-compile-form (car args))
3531 (byte-compile-goto-if nil for-effect failtag)
3532 (setq args (cdr args)))
3533 (byte-compile-form-do-effect (car args))
3534 (byte-compile-out-tag failtag))))
3536 (defun byte-compile-or (form)
3537 (let ((wintag (byte-compile-make-tag))
3540 (byte-compile-form-do-effect nil)
3542 (byte-compile-form (car args))
3543 (byte-compile-goto-if t for-effect wintag)
3544 (setq args (cdr args)))
3545 (byte-compile-form-do-effect (car args))
3546 (byte-compile-out-tag wintag))))
3548 (defun byte-compile-while (form)
3549 (let ((endtag (byte-compile-make-tag))
3550 (looptag (byte-compile-make-tag)))
3551 (byte-compile-out-tag looptag)
3552 (byte-compile-form (car (cdr form)))
3553 (byte-compile-goto-if nil for-effect endtag)
3554 (byte-compile-body (cdr (cdr form)) t)
3555 (byte-compile-goto 'byte-goto looptag)
3556 (byte-compile-out-tag endtag)
3557 (setq for-effect nil)))
3559 (defun byte-compile-funcall (form)
3560 (mapcar 'byte-compile-form (cdr form))
3561 (byte-compile-out 'byte-call (length (cdr (cdr form)))))
3564 (defun byte-compile-let (form)
3565 ;; First compute the binding values in the old scope.
3566 (let ((varlist (car (cdr form))))
3568 (if (consp (car varlist))
3569 (byte-compile-form (car (cdr (car varlist))))
3570 (byte-compile-push-constant nil))
3571 (setq varlist (cdr varlist))))
3572 (let ((byte-compile-bound-variables
3573 (cons 'new-scope byte-compile-bound-variables))
3574 (varlist (reverse (car (cdr form))))
3576 ;; If this let is of the form (let (...) (byte-code ...))
3577 ;; then assume that it is the result of a transformation of
3578 ;; ((lambda (...) (byte-code ... )) ...) and thus compile
3579 ;; the variable bindings as if they were arglist bindings
3580 ;; (which matters for what warnings.)
3581 (if (eq 'byte-code (car-safe (nth 2 form)))
3582 byte-compile-arglist-bit
3585 (byte-compile-variable-ref 'byte-varbind
3586 (if (consp (car varlist))
3590 (setq varlist (cdr varlist)))
3591 (byte-compile-body-do-effect (cdr (cdr form)))
3592 (if (memq 'unused-vars byte-compile-warnings)
3593 ;; done compiling in this scope, warn now.
3594 (byte-compile-warn-about-unused-variables))
3595 (byte-compile-out 'byte-unbind (length (car (cdr form))))))
3597 (defun byte-compile-let* (form)
3598 (let ((byte-compile-bound-variables
3599 (cons 'new-scope byte-compile-bound-variables))
3600 (varlist (copy-sequence (car (cdr form)))))
3602 (if (atom (car varlist))
3603 (byte-compile-push-constant nil)
3604 (byte-compile-form (car (cdr (car varlist))))
3605 (setcar varlist (car (car varlist))))
3606 (byte-compile-variable-ref 'byte-varbind (car varlist))
3607 (setq varlist (cdr varlist)))
3608 (byte-compile-body-do-effect (cdr (cdr form)))
3609 (if (memq 'unused-vars byte-compile-warnings)
3610 ;; done compiling in this scope, warn now.
3611 (byte-compile-warn-about-unused-variables))
3612 (byte-compile-out 'byte-unbind (length (car (cdr form))))))
3615 ;;(byte-defop-compiler-1 /= byte-compile-negated)
3616 (byte-defop-compiler-1 atom byte-compile-negated)
3617 (byte-defop-compiler-1 nlistp byte-compile-negated)
3619 ;;(put '/= 'byte-compile-negated-op '=)
3620 (put 'atom 'byte-compile-negated-op 'consp)
3621 (put 'nlistp 'byte-compile-negated-op 'listp)
3623 (defun byte-compile-negated (form)
3624 (byte-compile-form-do-effect (byte-compile-negation-optimizer form)))
3626 ;; Even when optimization is off, atom is optimized to (not (consp ...)).
3627 (defun byte-compile-negation-optimizer (form)
3628 ;; an optimizer for forms where <form1> is less efficient than (not <form2>)
3630 (cons (or (get (car form) 'byte-compile-negated-op)
3632 "Compiler error: `%s' has no `byte-compile-negated-op' property"
3636 ;;; other tricky macro-like special-forms
3638 (byte-defop-compiler-1 catch)
3639 (byte-defop-compiler-1 unwind-protect)
3640 (byte-defop-compiler-1 condition-case)
3641 (byte-defop-compiler-1 save-excursion)
3642 (byte-defop-compiler-1 save-current-buffer)
3643 (byte-defop-compiler-1 save-restriction)
3644 (byte-defop-compiler-1 save-window-excursion)
3645 (byte-defop-compiler-1 with-output-to-temp-buffer)
3648 (defun byte-compile-catch (form)
3649 (byte-compile-form (car (cdr form)))
3650 (byte-compile-push-constant
3651 (byte-compile-top-level (cons 'progn (cdr (cdr form))) for-effect))
3652 (byte-compile-out 'byte-catch 0))
3654 (defun byte-compile-unwind-protect (form)
3655 (byte-compile-push-constant
3656 (byte-compile-top-level-body (cdr (cdr form)) t))
3657 (byte-compile-out 'byte-unwind-protect 0)
3658 (byte-compile-form-do-effect (car (cdr form)))
3659 (byte-compile-out 'byte-unbind 1))
3661 ;;(defun byte-compile-track-mouse (form)
3662 ;; (byte-compile-form
3666 ;; (list 'lambda nil
3667 ;; (cons 'track-mouse
3668 ;; (byte-compile-top-level-body (cdr form))))))))
3670 (defun byte-compile-condition-case (form)
3671 (let* ((var (nth 1 form))
3672 (byte-compile-bound-variables
3675 (cons 'new-scope byte-compile-bound-variables))
3676 (cons 'new-scope byte-compile-bound-variables))))
3679 "%s is not a variable-name or nil (in condition-case)"
3680 (prin1-to-string var)))
3681 (byte-compile-push-constant var)
3682 (byte-compile-push-constant (byte-compile-top-level
3683 (nth 2 form) for-effect))
3684 (let ((clauses (cdr (cdr (cdr form))))
3687 (let* ((clause (car clauses))
3688 (condition (car clause)))
3689 (cond ((not (or (symbolp condition)
3690 (and (listp condition)
3691 (let ((syms condition) (ok t))
3693 (if (not (symbolp (car syms)))
3695 (setq syms (cdr syms)))
3698 "%s is not a symbol naming a condition or a list of such (in condition-case)"
3699 (prin1-to-string condition)))
3700 ;; ((not (or (eq condition 't)
3701 ;; (and (stringp (get condition 'error-message))
3702 ;; (consp (get condition 'error-conditions)))))
3703 ;; (byte-compile-warn
3704 ;; "%s is not a known condition name (in condition-case)"
3707 (setq compiled-clauses
3708 (cons (cons condition
3709 (byte-compile-top-level-body
3710 (cdr clause) for-effect))
3712 (setq clauses (cdr clauses)))
3713 (byte-compile-push-constant (nreverse compiled-clauses)))
3714 (if (memq 'unused-vars byte-compile-warnings)
3715 ;; done compiling in this scope, warn now.
3716 (byte-compile-warn-about-unused-variables))
3717 (byte-compile-out 'byte-condition-case 0)))
3720 (defun byte-compile-save-excursion (form)
3721 (byte-compile-out 'byte-save-excursion 0)
3722 (byte-compile-body-do-effect (cdr form))
3723 (byte-compile-out 'byte-unbind 1))
3725 (defun byte-compile-save-restriction (form)
3726 (byte-compile-out 'byte-save-restriction 0)
3727 (byte-compile-body-do-effect (cdr form))
3728 (byte-compile-out 'byte-unbind 1))
3730 (defun byte-compile-save-current-buffer (form)
3731 (if (byte-compile-version-cond byte-compile-emacs19-compatibility)
3732 ;; `save-current-buffer' special form is not available in XEmacs 19.
3734 `(let ((_byte_compiler_save_buffer_emulation_closure_ (current-buffer)))
3736 (progn ,@(cdr form))
3737 (and (buffer-live-p _byte_compiler_save_buffer_emulation_closure_)
3738 (set-buffer _byte_compiler_save_buffer_emulation_closure_)))))
3739 (byte-compile-out 'byte-save-current-buffer 0)
3740 (byte-compile-body-do-effect (cdr form))
3741 (byte-compile-out 'byte-unbind 1)))
3743 (defun byte-compile-save-window-excursion (form)
3744 (byte-compile-push-constant
3745 (byte-compile-top-level-body (cdr form) for-effect))
3746 (byte-compile-out 'byte-save-window-excursion 0))
3748 (defun byte-compile-with-output-to-temp-buffer (form)
3749 (byte-compile-form (car (cdr form)))
3750 (byte-compile-out 'byte-temp-output-buffer-setup 0)
3751 (byte-compile-body (cdr (cdr form)))
3752 (byte-compile-out 'byte-temp-output-buffer-show 0))
3755 ;;; top-level forms elsewhere
3757 (byte-defop-compiler-1 defun)
3758 (byte-defop-compiler-1 defmacro)
3759 (byte-defop-compiler-1 defvar)
3760 (byte-defop-compiler-1 defconst byte-compile-defvar)
3761 (byte-defop-compiler-1 autoload)
3762 ;; According to Mly this can go now that lambda is a macro
3763 ;(byte-defop-compiler-1 lambda byte-compile-lambda-form)
3764 (byte-defop-compiler-1 defalias)
3765 (byte-defop-compiler-1 define-function)
3767 (defun byte-compile-defun (form)
3768 ;; This is not used for file-level defuns with doc strings.
3769 (byte-compile-two-args ; Use this to avoid byte-compile-fset's warning.
3770 (list 'fset (list 'quote (nth 1 form))
3771 (byte-compile-byte-code-maker
3772 (byte-compile-lambda (cons 'lambda (cdr (cdr form)))))))
3773 (byte-compile-discard)
3774 (byte-compile-constant (nth 1 form)))
3776 (defun byte-compile-defmacro (form)
3777 ;; This is not used for file-level defmacros with doc strings.
3778 (byte-compile-body-do-effect
3779 (list (list 'fset (list 'quote (nth 1 form))
3780 (let ((code (byte-compile-byte-code-maker
3781 (byte-compile-lambda
3782 (cons 'lambda (cdr (cdr form)))))))
3783 (if (eq (car-safe code) 'make-byte-code)
3784 (list 'cons ''macro code)
3785 (list 'quote (cons 'macro (eval code))))))
3786 (list 'quote (nth 1 form)))))
3788 (defun byte-compile-defvar (form)
3789 ;; This is not used for file-level defvar/consts with doc strings:
3790 ;; byte-compile-file-form-defvar will be used in that case.
3791 (let ((var (nth 1 form))
3792 (value (nth 2 form))
3793 (string (nth 3 form)))
3794 (if (> (length form) 4)
3795 (byte-compile-warn "%s used with too many args" (car form)))
3796 (if (memq 'free-vars byte-compile-warnings)
3797 (setq byte-compile-bound-variables
3798 (cons (cons var byte-compile-global-bit)
3799 byte-compile-bound-variables)))
3800 (byte-compile-body-do-effect
3801 (list (if (cdr (cdr form))
3802 (if (eq (car form) 'defconst)
3803 (list 'setq var value)
3804 (list 'or (list 'boundp (list 'quote var))
3805 (list 'setq var value))))
3806 ;; Put the defined variable in this library's load-history entry
3807 ;; just as a real defvar would.
3808 (list 'setq 'current-load-list
3809 (list 'cons (list 'quote var)
3810 'current-load-list))
3812 (list 'put (list 'quote var) ''variable-documentation string))
3813 (list 'quote var)))))
3815 (defun byte-compile-autoload (form)
3816 (and (byte-compile-constp (nth 1 form))
3817 (byte-compile-constp (nth 5 form))
3818 (memq (eval (nth 5 form)) '(t macro)) ; macro-p
3819 (not (fboundp (eval (nth 1 form))))
3821 "The compiler ignores `autoload' except at top level. You should
3822 probably put the autoload of the macro `%s' at top-level."
3823 (eval (nth 1 form))))
3824 (byte-compile-normal-call form))
3826 ;; Lambda's in valid places are handled as special cases by various code.
3827 ;; The ones that remain are errors.
3828 ;; According to Mly this can go now that lambda is a macro
3829 ;(defun byte-compile-lambda-form (form)
3830 ; (byte-compile-warn
3831 ; "`lambda' used in function position is invalid: probably you mean #'%s"
3832 ; (let ((print-escape-newlines t)
3835 ; (prin1-to-string form)))
3836 ; (byte-compile-normal-call
3837 ; (list 'signal ''error
3838 ; (list 'quote (list "`lambda' used in function position" form)))))
3840 ;; Compile normally, but deal with warnings for the function being defined.
3841 (defun byte-compile-defalias (form)
3842 (if (and (consp (cdr form)) (consp (nth 1 form))
3843 (eq (car (nth 1 form)) 'quote)
3844 (consp (cdr (nth 1 form)))
3845 (symbolp (nth 1 (nth 1 form)))
3846 (consp (nthcdr 2 form))
3847 (consp (nth 2 form))
3848 (eq (car (nth 2 form)) 'quote)
3849 (consp (cdr (nth 2 form)))
3850 (symbolp (nth 1 (nth 2 form))))
3852 (byte-compile-defalias-warn (nth 1 (nth 1 form))
3853 (nth 1 (nth 2 form)))
3854 (setq byte-compile-function-environment
3855 (cons (cons (nth 1 (nth 1 form))
3856 (nth 1 (nth 2 form)))
3857 byte-compile-function-environment))))
3858 (byte-compile-normal-call form))
3860 (defun byte-compile-define-function (form)
3861 (byte-compile-defalias form))
3863 ;; Turn off warnings about prior calls to the function being defalias'd.
3864 ;; This could be smarter and compare those calls with
3865 ;; the function it is being aliased to.
3866 (defun byte-compile-defalias-warn (new alias)
3867 (let ((calls (assq new byte-compile-unresolved-functions)))
3869 (setq byte-compile-unresolved-functions
3870 (delq calls byte-compile-unresolved-functions)))))
3874 ;; Note: Most operations will strip off the 'TAG, but it speeds up
3875 ;; optimization to have the 'TAG as a part of the tag.
3876 ;; Tags will be (TAG . (tag-number . stack-depth)).
3877 (defun byte-compile-make-tag ()
3878 (list 'TAG (setq byte-compile-tag-number (1+ byte-compile-tag-number))))
3881 (defun byte-compile-out-tag (tag)
3882 (push tag byte-compile-output)
3885 ;; ## remove this someday
3886 (and byte-compile-depth
3887 (not (= (cdr (cdr tag)) byte-compile-depth))
3888 (error "Compiler bug: depth conflict at tag %d" (car (cdr tag))))
3889 (setq byte-compile-depth (cdr (cdr tag))))
3890 (setcdr (cdr tag) byte-compile-depth)))
3892 (defun byte-compile-goto (opcode tag)
3893 (push (cons opcode tag) byte-compile-output)
3894 (setcdr (cdr tag) (if (memq opcode byte-goto-always-pop-ops)
3895 (1- byte-compile-depth)
3896 byte-compile-depth))
3897 (setq byte-compile-depth (and (not (eq opcode 'byte-goto))
3898 (1- byte-compile-depth))))
3900 (defun byte-compile-out (opcode offset)
3901 (push (cons opcode offset) byte-compile-output)
3904 (setq byte-compile-depth (- byte-compile-depth offset)))
3906 ;; This is actually an unnecessary case, because there should be
3907 ;; no more opcodes behind byte-return.
3908 (setq byte-compile-depth nil))
3910 (setq byte-compile-depth (+ byte-compile-depth
3911 (or (aref byte-stack+-info
3912 (symbol-value opcode))
3914 byte-compile-maxdepth (max byte-compile-depth
3915 byte-compile-maxdepth))))
3916 ;;(if (< byte-compile-depth 0) (error "Compiler error: stack underflow"))
3922 (defun byte-compile-annotate-call-tree (form)
3924 ;; annotate the current call
3925 (if (setq entry (assq (car form) byte-compile-call-tree))
3926 (or (memq byte-compile-current-form (nth 1 entry)) ;callers
3928 (cons byte-compile-current-form (nth 1 entry))))
3929 (push (list (car form) (list byte-compile-current-form) nil)
3930 byte-compile-call-tree))
3931 ;; annotate the current function
3932 (if (setq entry (assq byte-compile-current-form byte-compile-call-tree))
3933 (or (memq (car form) (nth 2 entry)) ;called
3934 (setcar (cdr (cdr entry))
3935 (cons (car form) (nth 2 entry))))
3936 (push (list byte-compile-current-form nil (list (car form)))
3937 byte-compile-call-tree))))
3939 ;; Renamed from byte-compile-report-call-tree
3940 ;; to avoid interfering with completion of byte-compile-file.
3942 (defun display-call-tree (&optional filename)
3943 "Display a call graph of a specified file.
3944 This lists which functions have been called, what functions called
3945 them, and what functions they call. The list includes all functions
3946 whose definitions have been compiled in this Emacs session, as well as
3947 all functions called by those functions.
3949 The call graph does not include macros, inline functions, or
3950 primitives that the byte-code interpreter knows about directly \(eq,
3953 The call tree also lists those functions which are not known to be called
3954 \(that is, to which no calls have been compiled\), and which cannot be
3955 invoked interactively."
3957 (message "Generating call tree...")
3958 (with-output-to-temp-buffer "*Call-Tree*"
3959 (set-buffer "*Call-Tree*")
3961 (message "Generating call tree... (sorting on %s)"
3962 byte-compile-call-tree-sort)
3963 (insert "Call tree for "
3964 (cond ((null byte-compile-current-file) (or filename "???"))
3965 ((stringp byte-compile-current-file)
3966 byte-compile-current-file)
3967 (t (buffer-name byte-compile-current-file)))
3969 (prin1-to-string byte-compile-call-tree-sort)
3971 (if byte-compile-call-tree-sort
3972 (setq byte-compile-call-tree
3973 (sort byte-compile-call-tree
3975 ((eq byte-compile-call-tree-sort 'callers)
3976 #'(lambda (x y) (< (length (nth 1 x))
3977 (length (nth 1 y)))))
3978 ((eq byte-compile-call-tree-sort 'calls)
3979 #'(lambda (x y) (< (length (nth 2 x))
3980 (length (nth 2 y)))))
3981 ((eq byte-compile-call-tree-sort 'calls+callers)
3982 #'(lambda (x y) (< (+ (length (nth 1 x))
3984 (+ (length (nth 1 y))
3985 (length (nth 2 y))))))
3986 ((eq byte-compile-call-tree-sort 'name)
3987 #'(lambda (x y) (string< (car x)
3990 "`byte-compile-call-tree-sort': `%s' - unknown sort mode"
3991 byte-compile-call-tree-sort))))))
3992 (message "Generating call tree...")
3993 (let ((rest byte-compile-call-tree)
3994 (b (current-buffer))
3998 (prin1 (car (car rest)) b)
3999 (setq callers (nth 1 (car rest))
4000 calls (nth 2 (car rest)))
4002 (cond ((not (fboundp (setq f (car (car rest)))))
4004 " <top level>";; shouldn't insert nil then, actually -sk
4006 ((subrp (setq f (symbol-function f)))
4009 (format " ==> %s" f))
4010 ((compiled-function-p f)
4011 "<compiled function>")
4013 "<malformed function>")
4014 ((eq 'macro (car f))
4015 (if (or (compiled-function-p (cdr f))
4016 (assq 'byte-code (cdr (cdr (cdr f)))))
4019 ((assq 'byte-code (cdr (cdr f)))
4020 "<compiled lambda>")
4021 ((eq 'lambda (car f))
4024 (format " (%d callers + %d calls = %d)"
4025 ;; Does the optimizer eliminate common subexpressions?-sk
4028 (+ (length callers) (length calls)))
4032 (insert " called by:\n")
4034 (insert " " (if (car callers)
4035 (mapconcat 'symbol-name callers ", ")
4037 (let ((fill-prefix " "))
4038 (fill-region-as-paragraph p (point)))))
4041 (insert " calls:\n")
4043 (insert " " (mapconcat 'symbol-name calls ", "))
4044 (let ((fill-prefix " "))
4045 (fill-region-as-paragraph p (point)))))
4047 (setq rest (cdr rest)))
4049 (message "Generating call tree...(finding uncalled functions...)")
4050 (setq rest byte-compile-call-tree)
4051 (let ((uncalled nil))
4053 (or (nth 1 (car rest))
4054 (null (setq f (car (car rest))))
4055 (byte-compile-fdefinition f t)
4056 (commandp (byte-compile-fdefinition f nil))
4057 (setq uncalled (cons f uncalled)))
4058 (setq rest (cdr rest)))
4060 (let ((fill-prefix " "))
4061 (insert "Noninteractive functions not known to be called:\n ")
4063 (insert (mapconcat 'symbol-name (nreverse uncalled) ", "))
4064 (fill-region-as-paragraph p (point)))))
4066 (message "Generating call tree...done.")
4070 ;;; by crl@newton.purdue.edu
4071 ;;; Only works noninteractively.
4073 (defun batch-byte-compile ()
4074 "Run `byte-compile-file' on the files remaining on the command line.
4075 Use this from the command line, with `-batch';
4076 it won't work in an interactive Emacs.
4077 Each file is processed even if an error occurred previously.
4078 For example, invoke \"xemacs -batch -f batch-byte-compile $emacs/ ~/*.el\""
4079 ;; command-line-args-left is what is left of the command line (from
4081 (defvar command-line-args-left) ;Avoid 'free variable' warning
4082 (if (not noninteractive)
4083 (error "`batch-byte-compile' is to be used only with -batch"))
4085 (while command-line-args-left
4086 (if (null (batch-byte-compile-one-file))
4089 (kill-emacs (if error 1 0))))
4092 (defun batch-byte-compile-one-file ()
4093 "Run `byte-compile-file' on a single file remaining on the command line.
4094 Use this from the command line, with `-batch';
4095 it won't work in an interactive Emacs."
4096 ;; command-line-args-left is what is left of the command line (from
4098 (defvar command-line-args-left) ;Avoid 'free variable' warning
4099 (if (not noninteractive)
4100 (error "`batch-byte-compile-one-file' is to be used only with -batch"))
4102 (file-to-process (car command-line-args-left)))
4103 (setq command-line-args-left (cdr command-line-args-left))
4104 (if (file-directory-p (expand-file-name file-to-process))
4105 (let ((files (directory-files file-to-process))
4108 (if (and (string-match emacs-lisp-file-regexp (car files))
4109 (not (auto-save-file-name-p (car files)))
4110 (setq source (expand-file-name
4113 (setq dest (byte-compile-dest-file source))
4114 (file-exists-p dest)
4115 (file-newer-than-file-p source dest))
4116 (if (null (batch-byte-compile-1 source))
4118 (setq files (cdr files)))
4120 (batch-byte-compile-1 file-to-process))))
4122 (defun batch-byte-compile-1 (file)
4124 (progn (byte-compile-file file) t)
4126 (princ ">>Error occurred processing ")
4129 (if (fboundp 'display-error) ; XEmacs 19.8+
4130 (display-error err nil)
4131 (princ (or (get (car err) 'error-message) (car err)))
4132 (mapcar #'(lambda (x) (princ " ") (prin1 x)) (cdr err)))
4137 (defun batch-byte-recompile-directory-norecurse ()
4138 "Same as `batch-byte-recompile-directory' but without recursion."
4139 (setq byte-recompile-directory-recursively nil)
4140 (batch-byte-recompile-directory))
4143 (defun batch-byte-recompile-directory ()
4144 "Runs `byte-recompile-directory' on the dirs remaining on the command line.
4145 Must be used only with `-batch', and kills Emacs on completion.
4146 For example, invoke `xemacs -batch -f batch-byte-recompile-directory .'."
4147 ;; command-line-args-left is what is left of the command line (startup.el)
4148 (defvar command-line-args-left) ;Avoid 'free variable' warning
4149 (if (not noninteractive)
4150 (error "batch-byte-recompile-directory is to be used only with -batch"))
4151 (or command-line-args-left
4152 (setq command-line-args-left '(".")))
4153 (let ((byte-recompile-directory-ignore-errors-p t))
4154 (while command-line-args-left
4155 (byte-recompile-directory (car command-line-args-left))
4156 (setq command-line-args-left (cdr command-line-args-left))))
4159 (make-obsolete 'elisp-compile-defun 'compile-defun)
4160 (make-obsolete 'byte-compile-report-call-tree 'display-call-tree)
4162 ;; other make-obsolete calls in obsolete.el.
4164 (provide 'byte-compile)
4168 ;;; report metering (see the hacks in bytecode.c)
4170 (if (boundp 'byte-code-meter)
4171 (defun byte-compile-report-ops ()
4172 (defvar byte-code-meter)
4173 (with-output-to-temp-buffer "*Meter*"
4174 (set-buffer "*Meter*")
4175 (let ((i 0) n op off)
4177 (setq n (aref (aref byte-code-meter 0) i)
4179 (if t ;(not (zerop n))
4183 (cond ((< op byte-nth)
4184 (setq off (logand op 7))
4185 (setq op (logand op 248)))
4186 ((>= op byte-constant)
4187 (setq off (- op byte-constant)
4189 (setq op (aref byte-code-vector op))
4190 (insert (format "%-4d" i))
4191 (insert (symbol-name op))
4192 (if off (insert " [" (int-to-string off) "]"))
4194 (insert (int-to-string n) "\n")))
4195 (setq i (1+ i)))))))
4198 ;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when bytecomp compiles
4199 ;; itself, compile some of its most used recursive functions (at load time).
4202 (or (compiled-function-p (symbol-function 'byte-compile-form))
4203 (assq 'byte-code (symbol-function 'byte-compile-form))
4204 (let ((byte-optimize nil) ; do it fast
4205 (byte-compile-warnings nil))
4206 (mapcar #'(lambda (x)
4207 (or noninteractive (message "compiling %s..." x))
4209 (or noninteractive (message "compiling %s...done" x)))
4210 '(byte-compile-normal-call
4213 ;; Inserted some more than necessary, to speed it up.
4214 byte-compile-top-level
4215 byte-compile-out-toplevel
4216 byte-compile-constant
4217 byte-compile-variable-ref))))
4220 ;;; bytecomp.el ends here