Better format of files-by-directory menus.
[bpt/emacs.git] / lisp / emacs-lisp / bytecomp.el
CommitLineData
fd5285f3
RS
1;;; bytecomp.el --- compilation of Lisp code into byte code.
2
d733c5ec 3;;; Copyright (C) 1985, 1986, 1987, 1992, 1994 Free Software Foundation, Inc.
3a801d0c 4
fd5285f3
RS
5;; Author: Jamie Zawinski <jwz@lucid.com>
6;; Hallvard Furuseth <hbf@ulrik.uio.no>
fd5285f3 7;; Keywords: internal
1c393159 8
52799cb8 9;; Subsequently modified by RMS.
1c393159 10
e27c3564 11;;; This version incorporates changes up to version 2.10 of the
9e2b097b 12;;; Zawinski-Furuseth compiler.
e27c3564 13(defconst byte-compile-version "FSF 2.10")
1c393159
JB
14
15;; This file is part of GNU Emacs.
16
17;; GNU Emacs is free software; you can redistribute it and/or modify
18;; it under the terms of the GNU General Public License as published by
fd5285f3 19;; the Free Software Foundation; either version 2, or (at your option)
1c393159
JB
20;; any later version.
21
22;; GNU Emacs is distributed in the hope that it will be useful,
23;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25;; GNU General Public License for more details.
26
27;; You should have received a copy of the GNU General Public License
28;; along with GNU Emacs; see the file COPYING. If not, write to
29;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
30
e41b2db1
ER
31;;; Commentary:
32
33;; The Emacs Lisp byte compiler. This crunches lisp source into a sort
34;; of p-code which takes up less space and can be interpreted faster.
35;; The user entry points are byte-compile-file and byte-recompile-directory.
36
fd5285f3
RS
37;;; Code:
38
1c393159
JB
39;;; ========================================================================
40;;; Entry points:
e27c3564
JB
41;;; byte-recompile-directory, byte-compile-file,
42;;; batch-byte-compile, batch-byte-recompile-directory,
43;;; byte-compile, compile-defun,
52799cb8
RS
44;;; display-call-tree
45;;; (byte-compile-buffer and byte-compile-and-load-file were turned off
46;;; because they are not terribly useful and get in the way of completion.)
1c393159 47
52799cb8 48;;; This version of the byte compiler has the following improvements:
1c393159
JB
49;;; + optimization of compiled code:
50;;; - removal of unreachable code;
51;;; - removal of calls to side-effectless functions whose return-value
52;;; is unused;
53;;; - compile-time evaluation of safe constant forms, such as (consp nil)
54;;; and (ash 1 6);
55;;; - open-coding of literal lambdas;
56;;; - peephole optimization of emitted code;
57;;; - trivial functions are left uncompiled for speed.
58;;; + support for inline functions;
59;;; + compile-time evaluation of arbitrary expressions;
60;;; + compile-time warning messages for:
61;;; - functions being redefined with incompatible arglists;
62;;; - functions being redefined as macros, or vice-versa;
63;;; - functions or macros defined multiple times in the same file;
64;;; - functions being called with the incorrect number of arguments;
65;;; - functions being called which are not defined globally, in the
66;;; file, or as autoloads;
67;;; - assignment and reference of undeclared free variables;
68;;; - various syntax errors;
69;;; + correct compilation of nested defuns, defmacros, defvars and defsubsts;
70;;; + correct compilation of top-level uses of macros;
71;;; + the ability to generate a histogram of functions called.
72
73;;; User customization variables:
74;;;
75;;; byte-compile-verbose Whether to report the function currently being
76;;; compiled in the minibuffer;
77;;; byte-optimize Whether to do optimizations; this may be
78;;; t, nil, 'source, or 'byte;
79;;; byte-optimize-log Whether to report (in excruciating detail)
80;;; exactly which optimizations have been made.
81;;; This may be t, nil, 'source, or 'byte;
82;;; byte-compile-error-on-warn Whether to stop compilation when a warning is
83;;; produced;
84;;; byte-compile-delete-errors Whether the optimizer may delete calls or
85;;; variable references that are side-effect-free
86;;; except that they may return an error.
87;;; byte-compile-generate-call-tree Whether to generate a histogram of
88;;; function calls. This can be useful for
89;;; finding unused functions, as well as simple
90;;; performance metering.
91;;; byte-compile-warnings List of warnings to issue, or t. May contain
92;;; 'free-vars (references to variables not in the
93;;; current lexical scope)
94;;; 'unresolved (calls to unknown functions)
95;;; 'callargs (lambda calls with args that don't
96;;; match the lambda's definition)
97;;; 'redefine (function cell redefined from
98;;; a macro to a lambda or vice versa,
99;;; or redefined to take other args)
9290191f 100;;; 'obsolete (obsolete variables and functions)
52799cb8 101;;; byte-compile-compatibility Whether the compiler should
1c393159 102;;; generate .elc files which can be loaded into
52799cb8 103;;; generic emacs 18.
79c6071d 104;;; emacs-lisp-file-regexp Regexp for the extension of source-files;
e27c3564 105;;; see also the function byte-compile-dest-file.
1c393159
JB
106
107;;; New Features:
108;;;
109;;; o The form `defsubst' is just like `defun', except that the function
110;;; generated will be open-coded in compiled code which uses it. This
111;;; means that no function call will be generated, it will simply be
52799cb8 112;;; spliced in. Lisp functions calls are very slow, so this can be a
1c393159
JB
113;;; big win.
114;;;
115;;; You can generally accomplish the same thing with `defmacro', but in
116;;; that case, the defined procedure can't be used as an argument to
117;;; mapcar, etc.
1c393159
JB
118;;;
119;;; o You can also open-code one particular call to a function without
120;;; open-coding all calls. Use the 'inline' form to do this, like so:
121;;;
122;;; (inline (foo 1 2 3)) ;; `foo' will be open-coded
123;;; or...
124;;; (inline ;; `foo' and `baz' will be
125;;; (foo 1 2 3 (bar 5)) ;; open-coded, but `bar' will not.
126;;; (baz 0))
127;;;
128;;; o It is possible to open-code a function in the same file it is defined
129;;; in without having to load that file before compiling it. the
130;;; byte-compiler has been modified to remember function definitions in
131;;; the compilation environment in the same way that it remembers macro
132;;; definitions.
133;;;
134;;; o Forms like ((lambda ...) ...) are open-coded.
135;;;
136;;; o The form `eval-when-compile' is like progn, except that the body
137;;; is evaluated at compile-time. When it appears at top-level, this
eb8c3be9 138;;; is analogous to the Common Lisp idiom (eval-when (compile) ...).
1c393159
JB
139;;; When it does not appear at top-level, it is similar to the
140;;; Common Lisp #. reader macro (but not in interpreted code.)
141;;;
142;;; o The form `eval-and-compile' is similar to eval-when-compile, but
143;;; the whole form is evalled both at compile-time and at run-time.
144;;;
52799cb8 145;;; o The command compile-defun is analogous to eval-defun.
1c393159
JB
146;;;
147;;; o If you run byte-compile-file on a filename which is visited in a
148;;; buffer, and that buffer is modified, you are asked whether you want
149;;; to save the buffer before compiling.
e27c3564 150;;;
79c6071d
RS
151;;; o byte-compiled files now start with the string `;ELC'.
152;;; Some versions of `file' can be customized to recognize that.
1c393159 153
79d52eea
JB
154(require 'backquote)
155
1c393159
JB
156(or (fboundp 'defsubst)
157 ;; This really ought to be loaded already!
52799cb8 158 (load-library "byte-run"))
1c393159 159
52799cb8
RS
160;;; The feature of compiling in a specific target Emacs version
161;;; has been turned off because compile time options are a bad idea.
162(defmacro byte-compile-single-version () nil)
163(defmacro byte-compile-version-cond (cond) cond)
1c393159
JB
164
165;;; The crud you see scattered through this file of the form
166;;; (or (and (boundp 'epoch::version) epoch::version)
167;;; (string-lessp emacs-version "19"))
168;;; is because the Epoch folks couldn't be bothered to follow the
169;;; normal emacs version numbering convention.
170
52799cb8
RS
171;; (if (byte-compile-version-cond
172;; (or (and (boundp 'epoch::version) epoch::version)
173;; (string-lessp emacs-version "19")))
174;; (progn
175;; ;; emacs-18 compatibility.
176;; (defvar baud-rate (baud-rate)) ;Define baud-rate if it's undefined
177;;
178;; (if (byte-compile-single-version)
ed015bdd
JB
179;; (defmacro byte-code-function-p (x) "Emacs 18 doesn't have these." nil)
180;; (defun byte-code-function-p (x) "Emacs 18 doesn't have these." nil))
52799cb8
RS
181;;
182;; (or (and (fboundp 'member)
183;; ;; avoid using someone else's possibly bogus definition of this.
184;; (subrp (symbol-function 'member)))
185;; (defun member (elt list)
186;; "like memq, but uses equal instead of eq. In v19, this is a subr."
187;; (while (and list (not (equal elt (car list))))
188;; (setq list (cdr list)))
189;; list))))
190
191
192(defvar emacs-lisp-file-regexp (if (eq system-type 'vax-vms)
193 "\\.EL\\(;[0-9]+\\)?$"
194 "\\.el$")
195 "*Regexp which matches Emacs Lisp source files.
196You may want to redefine `byte-compile-dest-file' if you change this.")
1c393159
JB
197
198(or (fboundp 'byte-compile-dest-file)
e27c3564 199 ;; The user may want to redefine this along with emacs-lisp-file-regexp,
1c393159
JB
200 ;; so only define it if it is undefined.
201 (defun byte-compile-dest-file (filename)
52799cb8 202 "Convert an Emacs Lisp source file name to a compiled file name."
1c393159
JB
203 (setq filename (file-name-sans-versions filename))
204 (cond ((eq system-type 'vax-vms)
205 (concat (substring filename 0 (string-match ";" filename)) "c"))
e27c3564
JB
206 ((string-match emacs-lisp-file-regexp filename)
207 (concat (substring filename 0 (match-beginning 0)) ".elc"))
e9681c45 208 (t (concat filename ".elc")))))
1c393159
JB
209
210;; This can be the 'byte-compile property of any symbol.
52799cb8 211(autoload 'byte-compile-inline-expand "byte-opt")
1c393159
JB
212
213;; This is the entrypoint to the lapcode optimizer pass1.
52799cb8 214(autoload 'byte-optimize-form "byte-opt")
1c393159 215;; This is the entrypoint to the lapcode optimizer pass2.
52799cb8
RS
216(autoload 'byte-optimize-lapcode "byte-opt")
217(autoload 'byte-compile-unfold-lambda "byte-opt")
1c393159 218
ed015bdd
JB
219;; This is the entry point to the decompiler, which is used by the
220;; disassembler. The disassembler just requires 'byte-compile, but
221;; that doesn't define this function, so this seems to be a reasonable
222;; thing to do.
223(autoload 'byte-decompile-bytecode "byte-opt")
224
1c393159
JB
225(defvar byte-compile-verbose
226 (and (not noninteractive) (> baud-rate search-slow-speed))
227 "*Non-nil means print messages describing progress of byte-compiler.")
228
52799cb8
RS
229(defvar byte-compile-compatibility nil
230 "*Non-nil means generate output that can run in Emacs 18.")
231
232;; (defvar byte-compile-generate-emacs19-bytecodes
233;; (not (or (and (boundp 'epoch::version) epoch::version)
234;; (string-lessp emacs-version "19")))
235;; "*If this is true, then the byte-compiler will generate bytecode which
236;; makes use of byte-ops which are present only in Emacs 19. Code generated
237;; this way can never be run in Emacs 18, and may even cause it to crash.")
1c393159
JB
238
239(defvar byte-optimize t
ab94e6e7
RS
240 "*Enables optimization in the byte compiler.
241nil means don't do any optimization.
242t means do all optimizations.
243`source' means do source-level optimizations only.
244`byte' means do code-level optimizations only.")
1c393159
JB
245
246(defvar byte-compile-delete-errors t
ab94e6e7
RS
247 "*If non-nil, the optimizer may delete forms that may signal an error.
248This includes variable references and calls to functions such as `car'.")
1c393159 249
d82e848c
RS
250(defvar byte-compile-dynamic nil
251 "*If non-nil, compile function bodies so they load lazily.
252They are hidden comments in the compiled file, and brought into core when the
253function is called.
254
255To enable this option, make it a file-local variable
256in the source file you want it to apply to.
257For example, add -*-byte-compile-dynamic: t;-*- on the first line.
258
259When this option is true, if you load the compiled file and then move it,
260the functions you loaded will not be able to run.")
261
262(defvar byte-compile-dynamic-docstrings t
263 "*If non-nil, compile doc strings for lazy access.
264We bury the doc strings of functions and variables
265inside comments in the file, and bring them into core only when they
266are actually needed.
267
268When this option is true, if you load the compiled file and then move it,
269you won't be able to find the documentation of anything in that file.
270
271This option is enabled by default because it reduces Emacs memory usage.")
272
1c393159
JB
273(defvar byte-optimize-log nil
274 "*If true, the byte-compiler will log its optimizations into *Compile-Log*.
275If this is 'source, then only source-level optimizations will be logged.
276If it is 'byte, then only byte-level optimizations will be logged.")
277
278(defvar byte-compile-error-on-warn nil
ab94e6e7 279 "*If true, the byte-compiler reports warnings with `error'.")
1c393159 280
9290191f
RS
281(defconst byte-compile-warning-types
282 '(redefine callargs free-vars unresolved obsolete))
9e2b097b 283(defvar byte-compile-warnings t
1c393159 284 "*List of warnings that the byte-compiler should issue (t for all).
9e2b097b
JB
285Elements of the list may be be:
286
287 free-vars references to variables not in the current lexical scope.
288 unresolved calls to unknown functions.
289 callargs lambda calls with args that don't match the definition.
290 redefine function cell redefined from a macro to a lambda or vice
291 versa, or redefined to take a different number of arguments.
9290191f 292 obsolete obsolete variables and functions.
9e2b097b 293
ab94e6e7 294See also the macro `byte-compiler-options'.")
1c393159
JB
295
296(defvar byte-compile-generate-call-tree nil
52799cb8
RS
297 "*Non-nil means collect call-graph information when compiling.
298This records functions were called and from where.
299If the value is t, compilation displays the call graph when it finishes.
300If the value is neither t nor nil, compilation asks you whether to display
301the graph.
1c393159
JB
302
303The call tree only lists functions called, not macros used. Those functions
304which the byte-code interpreter knows about directly (eq, cons, etc.) are
305not reported.
306
307The call tree also lists those functions which are not known to be called
52799cb8 308\(that is, to which no calls have been compiled.) Functions which can be
1c393159
JB
309invoked interactively are excluded from this list.")
310
311(defconst byte-compile-call-tree nil "Alist of functions and their call tree.
312Each element looks like
313
314 \(FUNCTION CALLERS CALLS\)
315
316where CALLERS is a list of functions that call FUNCTION, and CALLS
317is a list of functions for which calls were generated while compiling
318FUNCTION.")
319
320(defvar byte-compile-call-tree-sort 'name
52799cb8
RS
321 "*If non-nil, sort the call tree.
322The values `name', `callers', `calls', `calls+callers'
323specify different fields to sort on.")
324
325;; (defvar byte-compile-overwrite-file t
326;; "If nil, old .elc files are deleted before the new is saved, and .elc
327;; files will have the same modes as the corresponding .el file. Otherwise,
328;; existing .elc files will simply be overwritten, and the existing modes
329;; will not be changed. If this variable is nil, then an .elc file which
330;; is a symbolic link will be turned into a normal file, instead of the file
331;; which the link points to being overwritten.")
1c393159
JB
332
333(defvar byte-compile-constants nil
334 "list of all constants encountered during compilation of this form")
335(defvar byte-compile-variables nil
336 "list of all variables encountered during compilation of this form")
337(defvar byte-compile-bound-variables nil
338 "list of variables bound in the context of the current form; this list
339lives partly on the stack.")
340(defvar byte-compile-free-references)
341(defvar byte-compile-free-assignments)
342
ab94e6e7
RS
343(defvar byte-compiler-error-flag)
344
1c393159 345(defconst byte-compile-initial-macro-environment
52799cb8
RS
346 '(
347;; (byte-compiler-options . (lambda (&rest forms)
348;; (apply 'byte-compiler-options-handler forms)))
1c393159
JB
349 (eval-when-compile . (lambda (&rest body)
350 (list 'quote (eval (byte-compile-top-level
351 (cons 'progn body))))))
352 (eval-and-compile . (lambda (&rest body)
353 (eval (cons 'progn body))
354 (cons 'progn body))))
355 "The default macro-environment passed to macroexpand by the compiler.
356Placing a macro here will cause a macro to have different semantics when
357expanded by the compiler as when expanded by the interpreter.")
358
359(defvar byte-compile-macro-environment byte-compile-initial-macro-environment
52799cb8
RS
360 "Alist of macros defined in the file being compiled.
361Each element looks like (MACRONAME . DEFINITION). It is
e27c3564 362\(MACRONAME . nil) when a macro is redefined as a function.")
1c393159
JB
363
364(defvar byte-compile-function-environment nil
52799cb8
RS
365 "Alist of functions defined in the file being compiled.
366This is so we can inline them when necessary.
367Each element looks like (FUNCTIONNAME . DEFINITION). It is
368\(FUNCTIONNAME . nil) when a function is redefined as a macro.")
1c393159
JB
369
370(defvar byte-compile-unresolved-functions nil
371 "Alist of undefined functions to which calls have been compiled (used for
372warnings when the function is later defined with incorrect args).")
373
374(defvar byte-compile-tag-number 0)
375(defvar byte-compile-output nil
376 "Alist describing contents to put in byte code string.
377Each element is (INDEX . VALUE)")
378(defvar byte-compile-depth 0 "Current depth of execution stack.")
379(defvar byte-compile-maxdepth 0 "Maximum depth of execution stack.")
380
381\f
382;;; The byte codes; this information is duplicated in bytecomp.c
383
384(defconst byte-code-vector nil
385 "An array containing byte-code names indexed by byte-code values.")
386
387(defconst byte-stack+-info nil
388 "An array with the stack adjustment for each byte-code.")
389
390(defmacro byte-defop (opcode stack-adjust opname &optional docstring)
391 ;; This is a speed-hack for building the byte-code-vector at compile-time.
392 ;; We fill in the vector at macroexpand-time, and then after the last call
393 ;; to byte-defop, we write the vector out as a constant instead of writing
394 ;; out a bunch of calls to aset.
395 ;; Actually, we don't fill in the vector itself, because that could make
396 ;; it problematic to compile big changes to this compiler; we store the
397 ;; values on its plist, and remove them later in -extrude.
398 (let ((v1 (or (get 'byte-code-vector 'tmp-compile-time-value)
399 (put 'byte-code-vector 'tmp-compile-time-value
400 (make-vector 256 nil))))
401 (v2 (or (get 'byte-stack+-info 'tmp-compile-time-value)
402 (put 'byte-stack+-info 'tmp-compile-time-value
403 (make-vector 256 nil)))))
404 (aset v1 opcode opname)
405 (aset v2 opcode stack-adjust))
406 (if docstring
407 (list 'defconst opname opcode (concat "Byte code opcode " docstring "."))
408 (list 'defconst opname opcode)))
409
410(defmacro byte-extrude-byte-code-vectors ()
411 (prog1 (list 'setq 'byte-code-vector
412 (get 'byte-code-vector 'tmp-compile-time-value)
413 'byte-stack+-info
414 (get 'byte-stack+-info 'tmp-compile-time-value))
415 ;; emacs-18 has no REMPROP.
416 (put 'byte-code-vector 'tmp-compile-time-value nil)
417 (put 'byte-stack+-info 'tmp-compile-time-value nil)))
418
419
420;; unused: 0-7
421
422;; These opcodes are special in that they pack their argument into the
423;; opcode word.
424;;
425(byte-defop 8 1 byte-varref "for variable reference")
426(byte-defop 16 -1 byte-varset "for setting a variable")
427(byte-defop 24 -1 byte-varbind "for binding a variable")
428(byte-defop 32 0 byte-call "for calling a function")
429(byte-defop 40 0 byte-unbind "for unbinding special bindings")
eb8c3be9 430;; codes 8-47 are consumed by the preceding opcodes
1c393159
JB
431
432;; unused: 48-55
433
434(byte-defop 56 -1 byte-nth)
435(byte-defop 57 0 byte-symbolp)
436(byte-defop 58 0 byte-consp)
437(byte-defop 59 0 byte-stringp)
438(byte-defop 60 0 byte-listp)
439(byte-defop 61 -1 byte-eq)
440(byte-defop 62 -1 byte-memq)
441(byte-defop 63 0 byte-not)
442(byte-defop 64 0 byte-car)
443(byte-defop 65 0 byte-cdr)
444(byte-defop 66 -1 byte-cons)
445(byte-defop 67 0 byte-list1)
446(byte-defop 68 -1 byte-list2)
447(byte-defop 69 -2 byte-list3)
448(byte-defop 70 -3 byte-list4)
449(byte-defop 71 0 byte-length)
450(byte-defop 72 -1 byte-aref)
451(byte-defop 73 -2 byte-aset)
452(byte-defop 74 0 byte-symbol-value)
453(byte-defop 75 0 byte-symbol-function) ; this was commented out
454(byte-defop 76 -1 byte-set)
455(byte-defop 77 -1 byte-fset) ; this was commented out
456(byte-defop 78 -1 byte-get)
457(byte-defop 79 -2 byte-substring)
458(byte-defop 80 -1 byte-concat2)
459(byte-defop 81 -2 byte-concat3)
460(byte-defop 82 -3 byte-concat4)
461(byte-defop 83 0 byte-sub1)
462(byte-defop 84 0 byte-add1)
463(byte-defop 85 -1 byte-eqlsign)
464(byte-defop 86 -1 byte-gtr)
465(byte-defop 87 -1 byte-lss)
466(byte-defop 88 -1 byte-leq)
467(byte-defop 89 -1 byte-geq)
468(byte-defop 90 -1 byte-diff)
469(byte-defop 91 0 byte-negate)
470(byte-defop 92 -1 byte-plus)
471(byte-defop 93 -1 byte-max)
472(byte-defop 94 -1 byte-min)
473(byte-defop 95 -1 byte-mult) ; v19 only
474(byte-defop 96 1 byte-point)
475(byte-defop 97 1 byte-mark-OBSOLETE) ; no longer generated as of v18
476(byte-defop 98 0 byte-goto-char)
477(byte-defop 99 0 byte-insert)
478(byte-defop 100 1 byte-point-max)
479(byte-defop 101 1 byte-point-min)
480(byte-defop 102 0 byte-char-after)
481(byte-defop 103 1 byte-following-char)
482(byte-defop 104 1 byte-preceding-char)
483(byte-defop 105 1 byte-current-column)
484(byte-defop 106 0 byte-indent-to)
485(byte-defop 107 0 byte-scan-buffer-OBSOLETE) ; no longer generated as of v18
486(byte-defop 108 1 byte-eolp)
487(byte-defop 109 1 byte-eobp)
488(byte-defop 110 1 byte-bolp)
489(byte-defop 111 1 byte-bobp)
490(byte-defop 112 1 byte-current-buffer)
491(byte-defop 113 0 byte-set-buffer)
492(byte-defop 114 1 byte-read-char-OBSOLETE)
493(byte-defop 115 0 byte-set-mark-OBSOLETE)
494(byte-defop 116 1 byte-interactive-p)
495
496;; These ops are new to v19
497(byte-defop 117 0 byte-forward-char)
498(byte-defop 118 0 byte-forward-word)
499(byte-defop 119 -1 byte-skip-chars-forward)
500(byte-defop 120 -1 byte-skip-chars-backward)
501(byte-defop 121 0 byte-forward-line)
502(byte-defop 122 0 byte-char-syntax)
503(byte-defop 123 -1 byte-buffer-substring)
504(byte-defop 124 -1 byte-delete-region)
505(byte-defop 125 -1 byte-narrow-to-region)
506(byte-defop 126 1 byte-widen)
507(byte-defop 127 0 byte-end-of-line)
508
509;; unused: 128
510
511;; These store their argument in the next two bytes
512(byte-defop 129 1 byte-constant2
513 "for reference to a constant with vector index >= byte-constant-limit")
514(byte-defop 130 0 byte-goto "for unconditional jump")
515(byte-defop 131 -1 byte-goto-if-nil "to pop value and jump if it's nil")
516(byte-defop 132 -1 byte-goto-if-not-nil "to pop value and jump if it's not nil")
517(byte-defop 133 -1 byte-goto-if-nil-else-pop
518 "to examine top-of-stack, jump and don't pop it if it's nil,
519otherwise pop it")
520(byte-defop 134 -1 byte-goto-if-not-nil-else-pop
521 "to examine top-of-stack, jump and don't pop it if it's non nil,
522otherwise pop it")
523
524(byte-defop 135 -1 byte-return "to pop a value and return it from `byte-code'")
525(byte-defop 136 -1 byte-discard "to discard one value from stack")
526(byte-defop 137 1 byte-dup "to duplicate the top of the stack")
527
528(byte-defop 138 0 byte-save-excursion
529 "to make a binding to record the buffer, point and mark")
530(byte-defop 139 0 byte-save-window-excursion
531 "to make a binding to record entire window configuration")
532(byte-defop 140 0 byte-save-restriction
533 "to make a binding to record the current buffer clipping restrictions")
534(byte-defop 141 -1 byte-catch
535 "for catch. Takes, on stack, the tag and an expression for the body")
536(byte-defop 142 -1 byte-unwind-protect
537 "for unwind-protect. Takes, on stack, an expression for the unwind-action")
538
52799cb8
RS
539;; For condition-case. Takes, on stack, the variable to bind,
540;; an expression for the body, and a list of clauses.
541(byte-defop 143 -2 byte-condition-case)
1c393159 542
52799cb8
RS
543;; For entry to with-output-to-temp-buffer.
544;; Takes, on stack, the buffer name.
545;; Binds standard-output and does some other things.
546;; Returns with temp buffer on the stack in place of buffer name.
547(byte-defop 144 0 byte-temp-output-buffer-setup)
1c393159 548
52799cb8
RS
549;; For exit from with-output-to-temp-buffer.
550;; Expects the temp buffer on the stack underneath value to return.
551;; Pops them both, then pushes the value back on.
552;; Unbinds standard-output and makes the temp buffer visible.
553(byte-defop 145 -1 byte-temp-output-buffer-show)
1c393159
JB
554
555;; these ops are new to v19
52799cb8
RS
556
557;; To unbind back to the beginning of this frame.
558;; Not used yet, but wil be needed for tail-recursion elimination.
559(byte-defop 146 0 byte-unbind-all)
1c393159
JB
560
561;; these ops are new to v19
562(byte-defop 147 -2 byte-set-marker)
563(byte-defop 148 0 byte-match-beginning)
564(byte-defop 149 0 byte-match-end)
565(byte-defop 150 0 byte-upcase)
566(byte-defop 151 0 byte-downcase)
567(byte-defop 152 -1 byte-string=)
568(byte-defop 153 -1 byte-string<)
569(byte-defop 154 -1 byte-equal)
570(byte-defop 155 -1 byte-nthcdr)
571(byte-defop 156 -1 byte-elt)
572(byte-defop 157 -1 byte-member)
573(byte-defop 158 -1 byte-assq)
574(byte-defop 159 0 byte-nreverse)
575(byte-defop 160 -1 byte-setcar)
576(byte-defop 161 -1 byte-setcdr)
577(byte-defop 162 0 byte-car-safe)
578(byte-defop 163 0 byte-cdr-safe)
579(byte-defop 164 -1 byte-nconc)
580(byte-defop 165 -1 byte-quo)
581(byte-defop 166 -1 byte-rem)
582(byte-defop 167 0 byte-numberp)
583(byte-defop 168 0 byte-integerp)
584
3eac9910 585;; unused: 169-174
1c393159
JB
586(byte-defop 175 nil byte-listN)
587(byte-defop 176 nil byte-concatN)
588(byte-defop 177 nil byte-insertN)
589
590;; unused: 178-191
591
592(byte-defop 192 1 byte-constant "for reference to a constant")
593;; codes 193-255 are consumed by byte-constant.
594(defconst byte-constant-limit 64
595 "Exclusive maximum index usable in the `byte-constant' opcode.")
596
597(defconst byte-goto-ops '(byte-goto byte-goto-if-nil byte-goto-if-not-nil
598 byte-goto-if-nil-else-pop
599 byte-goto-if-not-nil-else-pop)
52799cb8 600 "List of byte-codes whose offset is a pc.")
1c393159
JB
601
602(defconst byte-goto-always-pop-ops '(byte-goto-if-nil byte-goto-if-not-nil))
603
1c393159
JB
604(byte-extrude-byte-code-vectors)
605\f
606;;; lapcode generator
607;;;
608;;; the byte-compiler now does source -> lapcode -> bytecode instead of
609;;; source -> bytecode, because it's a lot easier to make optimizations
610;;; on lapcode than on bytecode.
611;;;
612;;; Elements of the lapcode list are of the form (<instruction> . <parameter>)
613;;; where instruction is a symbol naming a byte-code instruction,
614;;; and parameter is an argument to that instruction, if any.
615;;;
616;;; The instruction can be the pseudo-op TAG, which means that this position
617;;; in the instruction stream is a target of a goto. (car PARAMETER) will be
618;;; the PC for this location, and the whole instruction "(TAG pc)" will be the
619;;; parameter for some goto op.
620;;;
621;;; If the operation is varbind, varref, varset or push-constant, then the
622;;; parameter is (variable/constant . index_in_constant_vector).
623;;;
624;;; First, the source code is macroexpanded and optimized in various ways.
625;;; Then the resultant code is compiled into lapcode. Another set of
626;;; optimizations are then run over the lapcode. Then the variables and
627;;; constants referenced by the lapcode are collected and placed in the
628;;; constants-vector. (This happens now so that variables referenced by dead
629;;; code don't consume space.) And finally, the lapcode is transformed into
630;;; compacted byte-code.
631;;;
632;;; A distinction is made between variables and constants because the variable-
633;;; referencing instructions are more sensitive to the variables being near the
634;;; front of the constants-vector than the constant-referencing instructions.
635;;; Also, this lets us notice references to free variables.
636
637(defun byte-compile-lapcode (lap)
638 "Turns lapcode into bytecode. The lapcode is destroyed."
639 ;; Lapcode modifications: changes the ID of a tag to be the tag's PC.
640 (let ((pc 0) ; Program counter
641 op off ; Operation & offset
642 (bytes '()) ; Put the output bytes here
643 (patchlist nil) ; List of tags and goto's to patch
644 rest rel tmp)
645 (while lap
646 (setq op (car (car lap))
647 off (cdr (car lap)))
648 (cond ((not (symbolp op))
52799cb8 649 (error "Non-symbolic opcode `%s'" op))
1c393159
JB
650 ((eq op 'TAG)
651 (setcar off pc)
652 (setq patchlist (cons off patchlist)))
653 ((memq op byte-goto-ops)
654 (setq pc (+ pc 3))
655 (setq bytes (cons (cons pc (cdr off))
656 (cons nil
657 (cons (symbol-value op) bytes))))
658 (setq patchlist (cons bytes patchlist)))
659 (t
660 (setq bytes
661 (cond ((cond ((consp off)
662 ;; Variable or constant reference
663 (setq off (cdr off))
664 (eq op 'byte-constant)))
665 (cond ((< off byte-constant-limit)
666 (setq pc (1+ pc))
667 (cons (+ byte-constant off) bytes))
668 (t
669 (setq pc (+ 3 pc))
670 (cons (lsh off -8)
671 (cons (logand off 255)
672 (cons byte-constant2 bytes))))))
673 ((<= byte-listN (symbol-value op))
674 (setq pc (+ 2 pc))
675 (cons off (cons (symbol-value op) bytes)))
676 ((< off 6)
677 (setq pc (1+ pc))
678 (cons (+ (symbol-value op) off) bytes))
679 ((< off 256)
680 (setq pc (+ 2 pc))
681 (cons off (cons (+ (symbol-value op) 6) bytes)))
682 (t
683 (setq pc (+ 3 pc))
684 (cons (lsh off -8)
685 (cons (logand off 255)
686 (cons (+ (symbol-value op) 7)
687 bytes))))))))
688 (setq lap (cdr lap)))
689 ;;(if (not (= pc (length bytes)))
52799cb8 690 ;; (error "Compiler error: pc mismatch - %s %s" pc (length bytes)))
1c393159
JB
691 ;; Patch PC into jumps
692 (let (bytes)
693 (while patchlist
694 (setq bytes (car patchlist))
695 (cond ((atom (car bytes))) ; Tag
1c393159
JB
696 (t ; Absolute jump
697 (setq pc (car (cdr (car bytes)))) ; Pick PC from tag
698 (setcar (cdr bytes) (logand pc 255))
699 (setcar bytes (lsh pc -8))))
700 (setq patchlist (cdr patchlist))))
701 (concat (nreverse bytes))))
702
703\f
704;;; byte compiler messages
705
d82e848c
RS
706(defvar byte-compile-current-form nil)
707(defvar byte-compile-current-file nil)
708(defvar byte-compile-dest-file nil)
1c393159
JB
709
710(defmacro byte-compile-log (format-string &rest args)
711 (list 'and
712 'byte-optimize
713 '(memq byte-optimize-log '(t source))
714 (list 'let '((print-escape-newlines t)
715 (print-level 4)
716 (print-length 4))
717 (list 'byte-compile-log-1
718 (cons 'format
719 (cons format-string
720 (mapcar
721 '(lambda (x)
722 (if (symbolp x) (list 'prin1-to-string x) x))
723 args)))))))
724
725(defconst byte-compile-last-warned-form nil)
726
9e2b097b 727(defun byte-compile-log-1 (string &optional fill)
1c393159
JB
728 (cond (noninteractive
729 (if (or byte-compile-current-file
730 (and byte-compile-last-warned-form
731 (not (eq byte-compile-current-form
732 byte-compile-last-warned-form))))
733 (message (format "While compiling %s%s:"
734 (or byte-compile-current-form "toplevel forms")
735 (if byte-compile-current-file
736 (if (stringp byte-compile-current-file)
737 (concat " in file " byte-compile-current-file)
738 (concat " in buffer "
739 (buffer-name byte-compile-current-file)))
740 ""))))
741 (message " %s" string))
742 (t
743 (save-excursion
744 (set-buffer (get-buffer-create "*Compile-Log*"))
745 (goto-char (point-max))
746 (cond ((or byte-compile-current-file
747 (and byte-compile-last-warned-form
748 (not (eq byte-compile-current-form
749 byte-compile-last-warned-form))))
750 (if byte-compile-current-file
751 (insert "\n\^L\n" (current-time-string) "\n"))
752 (insert "While compiling "
753 (if byte-compile-current-form
754 (format "%s" byte-compile-current-form)
755 "toplevel forms"))
756 (if byte-compile-current-file
757 (if (stringp byte-compile-current-file)
758 (insert " in file " byte-compile-current-file)
759 (insert " in buffer "
760 (buffer-name byte-compile-current-file))))
761 (insert ":\n")))
9e2b097b
JB
762 (insert " " string "\n")
763 (if (and fill (not (string-match "\n" string)))
764 (let ((fill-prefix " ")
765 (fill-column 78))
766 (fill-paragraph nil)))
767 )))
1c393159
JB
768 (setq byte-compile-current-file nil
769 byte-compile-last-warned-form byte-compile-current-form))
770
771(defun byte-compile-warn (format &rest args)
772 (setq format (apply 'format format args))
773 (if byte-compile-error-on-warn
774 (error "%s" format) ; byte-compile-file catches and logs it
9e2b097b 775 (byte-compile-log-1 (concat "** " format) t)
fd5285f3
RS
776;;; It is useless to flash warnings too fast to be read.
777;;; Besides, they will all be shown at the end.
778;;; (or noninteractive ; already written on stdout.
779;;; (message "Warning: %s" format))
780 ))
1c393159 781
0b030df7
JB
782;;; This function should be used to report errors that have halted
783;;; compilation of the current file.
784(defun byte-compile-report-error (error-info)
ab94e6e7 785 (setq byte-compiler-error-flag t)
9e2b097b
JB
786 (byte-compile-log-1
787 (concat "!! "
788 (format (if (cdr error-info) "%s (%s)" "%s")
789 (get (car error-info) 'error-message)
790 (prin1-to-string (cdr error-info))))))
0b030df7 791
1c393159
JB
792;;; Used by make-obsolete.
793(defun byte-compile-obsolete (form)
9290191f
RS
794 (if (memq 'obsolete byte-compile-warnings)
795 (let ((new (get (car form) 'byte-obsolete-info)))
796 (byte-compile-warn "%s is an obsolete function; %s" (car form)
797 (if (stringp (car new))
798 (car new)
799 (format "use %s instead." (car new))))
800 (funcall (or (cdr new) 'byte-compile-normal-call) form))))
1c393159
JB
801\f
802;; Compiler options
803
52799cb8
RS
804;; (defvar byte-compiler-valid-options
805;; '((optimize byte-optimize (t nil source byte) val)
806;; (file-format byte-compile-compatibility (emacs18 emacs19)
807;; (eq val 'emacs18))
808;; ;; (new-bytecodes byte-compile-generate-emacs19-bytecodes (t nil) val)
809;; (delete-errors byte-compile-delete-errors (t nil) val)
810;; (verbose byte-compile-verbose (t nil) val)
811;; (warnings byte-compile-warnings ((callargs redefine free-vars unresolved))
812;; val)))
1c393159
JB
813
814;; Inhibit v18/v19 selectors if the version is hardcoded.
815;; #### This should print a warning if the user tries to change something
816;; than can't be changed because the running compiler doesn't support it.
52799cb8
RS
817;; (cond
818;; ((byte-compile-single-version)
819;; (setcar (cdr (cdr (assq 'new-bytecodes byte-compiler-valid-options)))
820;; (list (byte-compile-version-cond
821;; byte-compile-generate-emacs19-bytecodes)))
822;; (setcar (cdr (cdr (assq 'file-format byte-compiler-valid-options)))
823;; (if (byte-compile-version-cond byte-compile-compatibility)
824;; '(emacs18) '(emacs19)))))
825
826;; (defun byte-compiler-options-handler (&rest args)
827;; (let (key val desc choices)
828;; (while args
829;; (if (or (atom (car args)) (nthcdr 2 (car args)) (null (cdr (car args))))
830;; (error "Malformed byte-compiler option `%s'" (car args)))
831;; (setq key (car (car args))
832;; val (car (cdr (car args)))
833;; desc (assq key byte-compiler-valid-options))
834;; (or desc
835;; (error "Unknown byte-compiler option `%s'" key))
836;; (setq choices (nth 2 desc))
837;; (if (consp (car choices))
838;; (let (this
839;; (handler 'cons)
840;; (ret (and (memq (car val) '(+ -))
841;; (copy-sequence (if (eq t (symbol-value (nth 1 desc)))
842;; choices
843;; (symbol-value (nth 1 desc)))))))
844;; (setq choices (car choices))
845;; (while val
846;; (setq this (car val))
847;; (cond ((memq this choices)
848;; (setq ret (funcall handler this ret)))
849;; ((eq this '+) (setq handler 'cons))
850;; ((eq this '-) (setq handler 'delq))
851;; ((error "`%s' only accepts %s" key choices)))
852;; (setq val (cdr val)))
853;; (set (nth 1 desc) ret))
854;; (or (memq val choices)
855;; (error "`%s' must be one of `%s'" key choices))
856;; (set (nth 1 desc) (eval (nth 3 desc))))
857;; (setq args (cdr args)))
858;; nil))
1c393159
JB
859\f
860;;; sanity-checking arglists
861
862(defun byte-compile-fdefinition (name macro-p)
863 (let* ((list (if macro-p
864 byte-compile-macro-environment
5286a842 865 byte-compile-function-environment))
1c393159
JB
866 (env (cdr (assq name list))))
867 (or env
868 (let ((fn name))
869 (while (and (symbolp fn)
870 (fboundp fn)
871 (or (symbolp (symbol-function fn))
872 (consp (symbol-function fn))
873 (and (not macro-p)
ed015bdd 874 (byte-code-function-p (symbol-function fn)))))
1c393159 875 (setq fn (symbol-function fn)))
ed015bdd 876 (if (and (not macro-p) (byte-code-function-p fn))
1c393159
JB
877 fn
878 (and (consp fn)
879 (if (eq 'macro (car fn))
880 (cdr fn)
881 (if macro-p
882 nil
883 (if (eq 'autoload (car fn))
884 nil
885 fn)))))))))
886
887(defun byte-compile-arglist-signature (arglist)
888 (let ((args 0)
889 opts
890 restp)
891 (while arglist
892 (cond ((eq (car arglist) '&optional)
893 (or opts (setq opts 0)))
894 ((eq (car arglist) '&rest)
895 (if (cdr arglist)
896 (setq restp t
897 arglist nil)))
898 (t
899 (if opts
900 (setq opts (1+ opts))
901 (setq args (1+ args)))))
902 (setq arglist (cdr arglist)))
903 (cons args (if restp nil (if opts (+ args opts) args)))))
904
905
906(defun byte-compile-arglist-signatures-congruent-p (old new)
907 (not (or
908 (> (car new) (car old)) ; requires more args now
909 (and (null (cdr old)) ; tooks rest-args, doesn't any more
910 (cdr new))
911 (and (cdr new) (cdr old) ; can't take as many args now
912 (< (cdr new) (cdr old)))
913 )))
914
915(defun byte-compile-arglist-signature-string (signature)
916 (cond ((null (cdr signature))
917 (format "%d+" (car signature)))
918 ((= (car signature) (cdr signature))
919 (format "%d" (car signature)))
920 (t (format "%d-%d" (car signature) (cdr signature)))))
921
922
52799cb8 923;; Warn if the form is calling a function with the wrong number of arguments.
1c393159 924(defun byte-compile-callargs-warn (form)
1c393159
JB
925 (let* ((def (or (byte-compile-fdefinition (car form) nil)
926 (byte-compile-fdefinition (car form) t)))
927 (sig (and def (byte-compile-arglist-signature
928 (if (eq 'lambda (car-safe def))
929 (nth 1 def)
d82e848c 930 (if (byte-code-function-p def)
5286a842
RS
931 (aref def 0)
932 '(&rest def))))))
1c393159
JB
933 (ncall (length (cdr form))))
934 (if sig
935 (if (or (< ncall (car sig))
936 (and (cdr sig) (> ncall (cdr sig))))
937 (byte-compile-warn
938 "%s called with %d argument%s, but %s %s"
939 (car form) ncall
940 (if (= 1 ncall) "" "s")
941 (if (< ncall (car sig))
942 "requires"
943 "accepts only")
944 (byte-compile-arglist-signature-string sig)))
945 (or (fboundp (car form)) ; might be a subr or autoload.
946 (eq (car form) byte-compile-current-form) ; ## this doesn't work with recursion.
947 ;; It's a currently-undefined function. Remember number of args in call.
948 (let ((cons (assq (car form) byte-compile-unresolved-functions))
949 (n (length (cdr form))))
950 (if cons
951 (or (memq n (cdr cons))
952 (setcdr cons (cons n (cdr cons))))
953 (setq byte-compile-unresolved-functions
954 (cons (list (car form) n)
955 byte-compile-unresolved-functions))))))))
956
52799cb8
RS
957;; Warn if the function or macro is being redefined with a different
958;; number of arguments.
1c393159 959(defun byte-compile-arglist-warn (form macrop)
1c393159
JB
960 (let ((old (byte-compile-fdefinition (nth 1 form) macrop)))
961 (if old
962 (let ((sig1 (byte-compile-arglist-signature
963 (if (eq 'lambda (car-safe old))
964 (nth 1 old)
d82e848c 965 (if (byte-code-function-p old)
5286a842
RS
966 (aref old 0)
967 '(&rest def)))))
1c393159
JB
968 (sig2 (byte-compile-arglist-signature (nth 2 form))))
969 (or (byte-compile-arglist-signatures-congruent-p sig1 sig2)
970 (byte-compile-warn "%s %s used to take %s %s, now takes %s"
971 (if (eq (car form) 'defun) "function" "macro")
972 (nth 1 form)
973 (byte-compile-arglist-signature-string sig1)
974 (if (equal sig1 '(1 . 1)) "argument" "arguments")
975 (byte-compile-arglist-signature-string sig2))))
976 ;; This is the first definition. See if previous calls are compatible.
977 (let ((calls (assq (nth 1 form) byte-compile-unresolved-functions))
978 nums sig min max)
979 (if calls
980 (progn
981 (setq sig (byte-compile-arglist-signature (nth 2 form))
982 nums (sort (copy-sequence (cdr calls)) (function <))
983 min (car nums)
984 max (car (nreverse nums)))
985 (if (or (< min (car sig))
986 (and (cdr sig) (> max (cdr sig))))
987 (byte-compile-warn
988 "%s being defined to take %s%s, but was previously called with %s"
989 (nth 1 form)
990 (byte-compile-arglist-signature-string sig)
991 (if (equal sig '(1 . 1)) " arg" " args")
992 (byte-compile-arglist-signature-string (cons min max))))
993
994 (setq byte-compile-unresolved-functions
995 (delq calls byte-compile-unresolved-functions)))))
996 )))
997
52799cb8
RS
998;; If we have compiled any calls to functions which are not known to be
999;; defined, issue a warning enumerating them.
1000;; `unresolved' in the list `byte-compile-warnings' disables this.
1c393159 1001(defun byte-compile-warn-about-unresolved-functions ()
1c393159
JB
1002 (if (memq 'unresolved byte-compile-warnings)
1003 (let ((byte-compile-current-form "the end of the data"))
1004 (if (cdr byte-compile-unresolved-functions)
1005 (let* ((str "The following functions are not known to be defined: ")
1006 (L (length str))
1007 (rest (reverse byte-compile-unresolved-functions))
1008 s)
1009 (while rest
1010 (setq s (symbol-name (car (car rest)))
1011 L (+ L (length s) 2)
1012 rest (cdr rest))
1013 (if (< L (1- fill-column))
1014 (setq str (concat str " " s (and rest ",")))
1015 (setq str (concat str "\n " s (and rest ","))
1016 L (+ (length s) 4))))
1017 (byte-compile-warn "%s" str))
1018 (if byte-compile-unresolved-functions
1019 (byte-compile-warn "the function %s is not known to be defined."
1020 (car (car byte-compile-unresolved-functions)))))))
1021 nil)
1022
1023\f
1024(defmacro byte-compile-constp (form)
1025 ;; Returns non-nil if FORM is a constant.
1026 (` (cond ((consp (, form)) (eq (car (, form)) 'quote))
1027 ((not (symbolp (, form))))
1028 ((memq (, form) '(nil t))))))
1029
1030(defmacro byte-compile-close-variables (&rest body)
1031 (cons 'let
1032 (cons '(;;
1033 ;; Close over these variables to encapsulate the
1034 ;; compilation state
1035 ;;
1036 (byte-compile-macro-environment
1037 ;; Copy it because the compiler may patch into the
1038 ;; macroenvironment.
1039 (copy-alist byte-compile-initial-macro-environment))
1040 (byte-compile-function-environment nil)
1041 (byte-compile-bound-variables nil)
1042 (byte-compile-free-references nil)
1043 (byte-compile-free-assignments nil)
1044 ;;
1045 ;; Close over these variables so that `byte-compiler-options'
1046 ;; can change them on a per-file basis.
1047 ;;
1048 (byte-compile-verbose byte-compile-verbose)
1049 (byte-optimize byte-optimize)
d82e848c
RS
1050 (byte-compile-compatibility byte-compile-compatibility)
1051 (byte-compile-dynamic byte-compile-dynamic)
1052 (byte-compile-dynamic-docstrings
1053 byte-compile-dynamic-docstrings)
52799cb8
RS
1054;; (byte-compile-generate-emacs19-bytecodes
1055;; byte-compile-generate-emacs19-bytecodes)
1c393159
JB
1056 (byte-compile-warnings (if (eq byte-compile-warnings t)
1057 byte-compile-warning-types
1058 byte-compile-warnings))
1059 )
1060 body)))
1061
1062(defvar byte-compile-warnings-point-max)
1063(defmacro displaying-byte-compile-warnings (&rest body)
1064 (list 'let
1065 '((byte-compile-warnings-point-max
1066 (if (boundp 'byte-compile-warnings-point-max)
1067 byte-compile-warnings-point-max
1068 (save-excursion
1069 (set-buffer (get-buffer-create "*Compile-Log*"))
1070 (point-max)))))
0b030df7
JB
1071 (list 'unwind-protect
1072 (list 'condition-case 'error-info
1073 (cons 'progn body)
1074 '(error
1075 (byte-compile-report-error error-info)))
1c393159
JB
1076 '(save-excursion
1077 ;; If there were compilation warnings, display them.
1078 (set-buffer "*Compile-Log*")
1079 (if (= byte-compile-warnings-point-max (point-max))
1080 nil
1081 (select-window
1082 (prog1 (selected-window)
1083 (select-window (display-buffer (current-buffer)))
1084 (goto-char byte-compile-warnings-point-max)
1085 (recenter 1))))))))
1086
1087\f
fd5285f3 1088;;;###autoload
1c393159
JB
1089(defun byte-recompile-directory (directory &optional arg)
1090 "Recompile every `.el' file in DIRECTORY that needs recompilation.
1091This is if a `.elc' file exists but is older than the `.el' file.
691e7e76 1092Files in subdirectories of DIRECTORY are processed also.
1c393159
JB
1093
1094If the `.elc' file does not exist, normally the `.el' file is *not* compiled.
1095But a prefix argument (optional second arg) means ask user,
9e2b097b 1096for each such `.el' file, whether to compile it. Prefix argument 0 means
691e7e76
RS
1097don't ask and compile the file anyway.
1098
1099A nonzero prefix argument also means ask about each subdirectory."
1c393159 1100 (interactive "DByte recompile directory: \nP")
0dea0bbe
RM
1101 (if arg
1102 (setq arg (prefix-numeric-value arg)))
e27c3564
JB
1103 (if noninteractive
1104 nil
1105 (save-some-buffers)
1106 (set-buffer-modified-p (buffer-modified-p))) ;Update the mode line.
9e2b097b
JB
1107 (let ((directories (list (expand-file-name directory)))
1108 (file-count 0)
1109 (dir-count 0)
1110 last-dir)
1111 (displaying-byte-compile-warnings
1112 (while directories
1113 (setq directory (car directories))
e27c3564 1114 (or noninteractive (message "Checking %s..." directory))
9e2b097b
JB
1115 (let ((files (directory-files directory))
1116 source dest)
1117 (while files
1118 (setq source (expand-file-name (car files) directory))
1119 (if (and (not (member (car files) '("." ".." "RCS" "CVS")))
e9681c45
RS
1120 (file-directory-p source)
1121 (not (file-symlink-p source)))
9e2b097b 1122 (if (or (null arg)
0dea0bbe 1123 (eq 0 arg)
9e2b097b
JB
1124 (y-or-n-p (concat "Check " source "? ")))
1125 (setq directories
1126 (nconc directories (list source))))
1127 (if (and (string-match emacs-lisp-file-regexp source)
1128 (not (auto-save-file-name-p source))
1129 (setq dest (byte-compile-dest-file source))
1130 (if (file-exists-p dest)
1131 (file-newer-than-file-p source dest)
1132 (and arg
0dea0bbe 1133 (or (eq 0 arg)
9e2b097b 1134 (y-or-n-p (concat "Compile " source "? "))))))
e27c3564
JB
1135 (progn (if (and noninteractive (not byte-compile-verbose))
1136 (message "Compiling %s..." source))
1137 (byte-compile-file source)
47082fcd
RS
1138 (or noninteractive
1139 (message "Checking %s..." directory))
9e2b097b
JB
1140 (setq file-count (1+ file-count))
1141 (if (not (eq last-dir directory))
1142 (setq last-dir directory
1143 dir-count (1+ dir-count)))
1144 )))
1145 (setq files (cdr files))))
1146 (setq directories (cdr directories))))
1147 (message "Done (Total of %d file%s compiled%s)"
1148 file-count (if (= file-count 1) "" "s")
1149 (if (> dir-count 1) (format " in %d directories" dir-count) ""))))
1c393159 1150
fd5285f3 1151;;;###autoload
1c393159
JB
1152(defun byte-compile-file (filename &optional load)
1153 "Compile a file of Lisp code named FILENAME into a file of byte code.
1154The output file's name is made by appending `c' to the end of FILENAME.
1155With prefix arg (noninteractively: 2nd arg), load the file after compiling."
1156;; (interactive "fByte compile file: \nP")
1157 (interactive
1158 (let ((file buffer-file-name)
1159 (file-name nil)
1160 (file-dir nil))
1161 (and file
1162 (eq (cdr (assq 'major-mode (buffer-local-variables)))
1163 'emacs-lisp-mode)
1164 (setq file-name (file-name-nondirectory file)
1165 file-dir (file-name-directory file)))
52799cb8
RS
1166 (list (read-file-name (if current-prefix-arg
1167 "Byte compile and load file: "
1168 "Byte compile file: ")
79c6071d 1169 file-dir file-name nil)
fd5285f3 1170 current-prefix-arg)))
1c393159
JB
1171 ;; Expand now so we get the current buffer's defaults
1172 (setq filename (expand-file-name filename))
1173
1174 ;; If we're compiling a file that's in a buffer and is modified, offer
1175 ;; to save it first.
1176 (or noninteractive
1177 (let ((b (get-file-buffer (expand-file-name filename))))
1178 (if (and b (buffer-modified-p b)
1179 (y-or-n-p (format "save buffer %s first? " (buffer-name b))))
1180 (save-excursion (set-buffer b) (save-buffer)))))
1181
1182 (if byte-compile-verbose
1183 (message "Compiling %s..." filename))
3ea1f391 1184 (let ((byte-compile-current-file filename)
d82e848c
RS
1185 target-file input-buffer output-buffer
1186 byte-compile-dest-file)
1187 (setq target-file (byte-compile-dest-file filename))
1188 (setq byte-compile-dest-file target-file)
1c393159 1189 (save-excursion
ab94e6e7
RS
1190 (setq input-buffer (get-buffer-create " *Compiler Input*"))
1191 (set-buffer input-buffer)
1c393159
JB
1192 (erase-buffer)
1193 (insert-file-contents filename)
1194 ;; Run hooks including the uncompression hook.
1195 ;; If they change the file name, then change it for the output also.
d82e848c
RS
1196 (let ((buffer-file-name filename)
1197 (enable-local-eval nil))
1198 (normal-mode)
ab94e6e7
RS
1199 (setq filename buffer-file-name)))
1200 (setq byte-compiler-error-flag nil)
1201 ;; It is important that input-buffer not be current at this call,
1202 ;; so that the value of point set in input-buffer
1203 ;; within byte-compile-from-buffer lingers in that buffer.
8a5dd086 1204 (setq output-buffer (byte-compile-from-buffer input-buffer filename))
189db152
RS
1205 (if byte-compiler-error-flag
1206 nil
1207 (kill-buffer input-buffer)
1208 (save-excursion
1209 (set-buffer output-buffer)
1210 (goto-char (point-max))
1211 (insert "\n") ; aaah, unix.
1212 (let ((vms-stmlf-recfm t))
189db152
RS
1213 (if (file-writable-p target-file)
1214 (let ((kanji-flag nil)) ; for nemacs, from Nakagawa Takayuki
2cd0169d 1215 (if (or (eq system-type 'ms-dos) (eq system-type 'windows-nt))
e3c72369 1216 (setq buffer-file-type t))
189db152
RS
1217 (write-region 1 (point-max) target-file))
1218 ;; This is just to give a better error message than
1219 ;; write-region
1220 (signal 'file-error
1221 (list "Opening output file"
1222 (if (file-exists-p target-file)
1223 "cannot overwrite file"
1224 "directory not writable or nonexistent")
d82e848c 1225 target-file))))
189db152
RS
1226 (kill-buffer (current-buffer)))
1227 (if (and byte-compile-generate-call-tree
1228 (or (eq t byte-compile-generate-call-tree)
1229 (y-or-n-p (format "Report call tree for %s? " filename))))
1230 (save-excursion
1231 (display-call-tree filename)))
1232 (if load
a2bb8f73
KH
1233 (load target-file))
1234 t)))
1c393159 1235
52799cb8
RS
1236;;(defun byte-compile-and-load-file (&optional filename)
1237;; "Compile a file of Lisp code named FILENAME into a file of byte code,
1238;;and then load it. The output file's name is made by appending \"c\" to
1239;;the end of FILENAME."
1240;; (interactive)
1241;; (if filename ; I don't get it, (interactive-p) doesn't always work
1242;; (byte-compile-file filename t)
1243;; (let ((current-prefix-arg '(4)))
1244;; (call-interactively 'byte-compile-file))))
1245
1246;;(defun byte-compile-buffer (&optional buffer)
1247;; "Byte-compile and evaluate contents of BUFFER (default: the current buffer)."
1248;; (interactive "bByte compile buffer: ")
1249;; (setq buffer (if buffer (get-buffer buffer) (current-buffer)))
1250;; (message "Compiling %s..." (buffer-name buffer))
1251;; (let* ((filename (or (buffer-file-name buffer)
1252;; (concat "#<buffer " (buffer-name buffer) ">")))
1253;; (byte-compile-current-file buffer))
8a5dd086 1254;; (byte-compile-from-buffer buffer nil))
52799cb8
RS
1255;; (message "Compiling %s...done" (buffer-name buffer))
1256;; t)
1c393159
JB
1257
1258;;; compiling a single function
fd5285f3 1259;;;###autoload
52799cb8 1260(defun compile-defun (&optional arg)
1c393159
JB
1261 "Compile and evaluate the current top-level form.
1262Print the result in the minibuffer.
1263With argument, insert value in current buffer after the form."
1264 (interactive "P")
1265 (save-excursion
1266 (end-of-defun)
1267 (beginning-of-defun)
1268 (let* ((byte-compile-current-file nil)
1269 (byte-compile-last-warned-form 'nothing)
fd5285f3
RS
1270 (value (eval (displaying-byte-compile-warnings
1271 (byte-compile-sexp (read (current-buffer)))))))
1c393159
JB
1272 (cond (arg
1273 (message "Compiling from buffer... done.")
1274 (prin1 value (current-buffer))
1275 (insert "\n"))
1276 ((message "%s" (prin1-to-string value)))))))
1277
1278
8a5dd086
RS
1279(defun byte-compile-from-buffer (inbuffer &optional filename)
1280 ;; Filename is used for the loading-into-Emacs-18 error message.
d82e848c
RS
1281 (let (outbuffer
1282 ;; Prevent truncation of flonums and lists as we read and print them
1283 (float-output-format nil)
1284 (case-fold-search nil)
1285 (print-length nil)
1286 ;; Simulate entry to byte-compile-top-level
1287 (byte-compile-constants nil)
1288 (byte-compile-variables nil)
1289 (byte-compile-tag-number 0)
1290 (byte-compile-depth 0)
1291 (byte-compile-maxdepth 0)
1292 (byte-compile-output nil)
1293 ;; #### This is bound in b-c-close-variables.
1294 ;; (byte-compile-warnings (if (eq byte-compile-warnings t)
1295 ;; byte-compile-warning-types
1296 ;; byte-compile-warnings))
1297 )
1298 (byte-compile-close-variables
1299 (save-excursion
1300 (setq outbuffer
1301 (set-buffer (get-buffer-create " *Compiler Output*")))
1302 (erase-buffer)
1303 ;; (emacs-lisp-mode)
1304 (setq case-fold-search nil)
1305 (and filename (byte-compile-insert-header filename))
1306
1307 ;; This is a kludge. Some operating systems (OS/2, DOS) need to
1308 ;; write files containing binary information specially.
1309 ;; Under most circumstances, such files will be in binary
1310 ;; overwrite mode, so those OS's use that flag to guess how
1311 ;; they should write their data. Advise them that .elc files
1312 ;; need to be written carefully.
1313 (setq overwrite-mode 'overwrite-mode-binary))
1314 (displaying-byte-compile-warnings
1315 (save-excursion
1316 (set-buffer inbuffer)
1317 (goto-char 1)
1318
1319 ;; Compile the forms from the input buffer.
1320 (while (progn
1321 (while (progn (skip-chars-forward " \t\n\^l")
1322 (looking-at ";"))
1323 (forward-line 1))
1324 (not (eobp)))
1325 (byte-compile-file-form (read inbuffer)))
1326
1327 ;; Compile pending forms at end of file.
1328 (byte-compile-flush-pending)
1329 (byte-compile-warn-about-unresolved-functions)
1330 ;; SHould we always do this? When calling multiple files, it
1331 ;; would be useful to delay this warning until all have
1332 ;; been compiled.
1333 (setq byte-compile-unresolved-functions nil))))
285cdf4e 1334 outbuffer))
8a5dd086
RS
1335
1336(defun byte-compile-insert-header (filename)
d82e848c
RS
1337 (set-buffer outbuffer)
1338 (goto-char 1)
1339 ;;
1340 ;; The magic number of .elc files is ";ELC", or 0x3B454C43. After that is
1341 ;; the file-format version number (18 or 19) as a byte, followed by some
1342 ;; nulls. The primary motivation for doing this is to get some binary
1343 ;; characters up in the first line of the file so that `diff' will simply
1344 ;; say "Binary files differ" instead of actually doing a diff of two .elc
1345 ;; files. An extra benefit is that you can add this to /etc/magic:
1346 ;;
1347 ;; 0 string ;ELC GNU Emacs Lisp compiled file,
1348 ;; >4 byte x version %d
1349 ;;
1350 (insert
1351 ";ELC"
1352 (if (byte-compile-version-cond byte-compile-compatibility) 18 19)
1353 "\000\000\000\n"
1354 )
1355 (insert ";;; compiled by " user-mail-address " on "
1356 (current-time-string) "\n;;; from file " filename "\n")
1357 (insert ";;; emacs version " emacs-version ".\n")
1358 (insert ";;; bytecomp version " byte-compile-version "\n;;; "
1359 (cond
1360 ((eq byte-optimize 'source) "source-level optimization only")
1361 ((eq byte-optimize 'byte) "byte-level optimization only")
1362 (byte-optimize "optimization is on")
1363 (t "optimization is off"))
1364 (if (byte-compile-version-cond byte-compile-compatibility)
1365 "; compiled with Emacs 18 compatibility.\n"
1366 ".\n"))
1367 (if (not (byte-compile-version-cond byte-compile-compatibility))
1368 (insert ";;; this file uses opcodes which do not exist in Emacs 18.\n"
1369 ;; Have to check if emacs-version is bound so that this works
1370 ;; in files loaded early in loadup.el.
1371 "\n(if (and (boundp 'emacs-version)\n"
1372 "\t (or (and (boundp 'epoch::version) epoch::version)\n"
1373 "\t (string-lessp emacs-version \"19\")))\n"
1374 " (error \"`"
1375 ;; This escapes all backslashes in FILENAME. Needed on Windows.
1376 (substring (prin1-to-string filename) 1 -1)
1377 "' was compiled for Emacs 19\"))\n\n"
1378 )))
1c393159
JB
1379
1380
1381(defun byte-compile-output-file-form (form)
1382 ;; writes the given form to the output buffer, being careful of docstrings
1383 ;; in defun, defmacro, defvar, defconst and autoload because make-docfile is
1384 ;; so amazingly stupid.
c36881cf
ER
1385 ;; defalias calls are output directly by byte-compile-file-form-defmumble;
1386 ;; it does not pay to first build the defalias in defmumble and then parse
1387 ;; it here.
1c393159
JB
1388 (if (and (memq (car-safe form) '(defun defmacro defvar defconst autoload))
1389 (stringp (nth 3 form)))
d82e848c
RS
1390 (byte-compile-output-docform nil nil '("\n(" 3 ")") form nil
1391 (eq (car form) 'autoload))
1c393159 1392 (let ((print-escape-newlines t)
9e2b097b
JB
1393 (print-readably t) ; print #[] for bytecode, 'x for (quote x)
1394 (print-gensym nil)) ; this is too dangerous for now
1c393159
JB
1395 (princ "\n" outbuffer)
1396 (prin1 form outbuffer)
1397 nil)))
1398
d82e848c 1399(defun byte-compile-output-docform (preface name info form specindex quoted)
1c393159 1400 ;; Print a form with a doc string. INFO is (prefix doc-index postfix).
d82e848c
RS
1401 ;; If PREFACE and NAME are non-nil, print them too,
1402 ;; before INFO and the FORM but after the doc string itself.
1403 ;; If SPECINDEX is non-nil, it is the index in FORM
1404 ;; of the function bytecode string. In that case,
1405 ;; we output that argument and the following argument (the constants vector)
1406 ;; together, for lazy loading.
1407 ;; QUOTED says that we have to put a quote before the
1408 ;; list that represents a doc string reference.
1409 ;; `autoload' needs that.
1c393159
JB
1410 (set-buffer
1411 (prog1 (current-buffer)
1412 (set-buffer outbuffer)
d82e848c
RS
1413 (let (position)
1414
1415 ;; Insert the doc string, and make it a comment with #@LENGTH.
1416 (and (>= (nth 1 info) 0)
1417 byte-compile-dynamic-docstrings
1418 (progn
1419 ;; Make the doc string start at beginning of line
1420 ;; for make-docfile's sake.
1421 (insert "\n")
1422 (setq position
1423 (byte-compile-output-as-comment
1424 (nth (nth 1 info) form) nil))))
1425
1426 (if preface
1427 (progn
1428 (insert preface)
1429 (prin1 name outbuffer)))
1430 (insert (car info))
1431 (let ((print-escape-newlines t)
1432 (print-readably t) ; print #[] for bytecode, 'x for (quote x)
1433 (print-gensym nil) ; this is too dangerous for now
1434 (index 0))
1435 (prin1 (car form) outbuffer)
1436 (while (setq form (cdr form))
1437 (setq index (1+ index))
1438 (insert " ")
1439 (cond ((and (numberp specindex) (= index specindex))
1440 (let ((position
1441 (byte-compile-output-as-comment
1442 (cons (car form) (nth 1 form))
1443 t)))
1444 (princ (format "(#$ . %d) nil" position) outbuffer)
1445 (setq form (cdr form))
1446 (setq index (1+ index))))
1447 ((= index (nth 1 info))
1448 (if position
1449 (princ (format (if quoted "'(#$ . %d)" "(#$ . %d)")
1450 position)
1451 outbuffer)
1452 (let ((print-escape-newlines nil))
1453 (goto-char (prog1 (1+ (point))
1454 (prin1 (car form) outbuffer)))
1455 (insert "\\\n")
1456 (goto-char (point-max)))))
1457 (t
1458 (prin1 (car form) outbuffer)))))
1459 (insert (nth 2 info)))))
1c393159
JB
1460 nil)
1461
1462(defun byte-compile-keep-pending (form &optional handler)
1463 (if (memq byte-optimize '(t source))
1464 (setq form (byte-optimize-form form t)))
1465 (if handler
1466 (let ((for-effect t))
1467 ;; To avoid consing up monstrously large forms at load time, we split
1468 ;; the output regularly.
b4ff4a23
RS
1469 (and (memq (car-safe form) '(fset defalias))
1470 (nthcdr 300 byte-compile-output)
1c393159
JB
1471 (byte-compile-flush-pending))
1472 (funcall handler form)
1473 (if for-effect
1474 (byte-compile-discard)))
1475 (byte-compile-form form t))
1476 nil)
1477
1478(defun byte-compile-flush-pending ()
1479 (if byte-compile-output
1480 (let ((form (byte-compile-out-toplevel t 'file)))
1481 (cond ((eq (car-safe form) 'progn)
1482 (mapcar 'byte-compile-output-file-form (cdr form)))
1483 (form
1484 (byte-compile-output-file-form form)))
1485 (setq byte-compile-constants nil
1486 byte-compile-variables nil
1487 byte-compile-depth 0
1488 byte-compile-maxdepth 0
1489 byte-compile-output nil))))
1490
1491(defun byte-compile-file-form (form)
1492 (let ((byte-compile-current-form nil) ; close over this for warnings.
1493 handler)
1494 (cond
1495 ((not (consp form))
1496 (byte-compile-keep-pending form))
1497 ((and (symbolp (car form))
1498 (setq handler (get (car form) 'byte-hunk-handler)))
1499 (cond ((setq form (funcall handler form))
1500 (byte-compile-flush-pending)
1501 (byte-compile-output-file-form form))))
1502 ((eq form (setq form (macroexpand form byte-compile-macro-environment)))
1503 (byte-compile-keep-pending form))
1504 (t
1505 (byte-compile-file-form form)))))
1506
1507;; Functions and variables with doc strings must be output separately,
1508;; so make-docfile can recognise them. Most other things can be output
1509;; as byte-code.
1510
1511(put 'defsubst 'byte-hunk-handler 'byte-compile-file-form-defsubst)
1512(defun byte-compile-file-form-defsubst (form)
1513 (cond ((assq (nth 1 form) byte-compile-unresolved-functions)
1514 (setq byte-compile-current-form (nth 1 form))
1515 (byte-compile-warn "defsubst %s was used before it was defined"
1516 (nth 1 form))))
1517 (byte-compile-file-form
1518 (macroexpand form byte-compile-macro-environment))
1519 ;; Return nil so the form is not output twice.
1520 nil)
1521
1522(put 'autoload 'byte-hunk-handler 'byte-compile-file-form-autoload)
1523(defun byte-compile-file-form-autoload (form)
1524 (and (let ((form form))
1525 (while (if (setq form (cdr form)) (byte-compile-constp (car form))))
1526 (null form)) ;Constants only
1527 (eval (nth 5 form)) ;Macro
1528 (eval form)) ;Define the autoload.
1529 (if (stringp (nth 3 form))
1530 form
1531 ;; No doc string, so we can compile this as a normal form.
1532 (byte-compile-keep-pending form 'byte-compile-normal-call)))
1533
1534(put 'defvar 'byte-hunk-handler 'byte-compile-file-form-defvar)
1535(put 'defconst 'byte-hunk-handler 'byte-compile-file-form-defvar)
1536(defun byte-compile-file-form-defvar (form)
1537 (if (null (nth 3 form))
1538 ;; Since there is no doc string, we can compile this as a normal form,
1539 ;; and not do a file-boundary.
1540 (byte-compile-keep-pending form)
1541 (if (memq 'free-vars byte-compile-warnings)
1542 (setq byte-compile-bound-variables
1543 (cons (nth 1 form) byte-compile-bound-variables)))
1544 (cond ((consp (nth 2 form))
1545 (setq form (copy-sequence form))
1546 (setcar (cdr (cdr form))
1547 (byte-compile-top-level (nth 2 form) nil 'file))))
1548 form))
1549
1550(put 'require 'byte-hunk-handler 'byte-compile-file-form-eval-boundary)
1551(defun byte-compile-file-form-eval-boundary (form)
1552 (eval form)
1553 (byte-compile-keep-pending form 'byte-compile-normal-call))
1554
1555(put 'progn 'byte-hunk-handler 'byte-compile-file-form-progn)
1556(put 'prog1 'byte-hunk-handler 'byte-compile-file-form-progn)
1557(put 'prog2 'byte-hunk-handler 'byte-compile-file-form-progn)
1558(defun byte-compile-file-form-progn (form)
1559 (mapcar 'byte-compile-file-form (cdr form))
1560 ;; Return nil so the forms are not output twice.
1561 nil)
1562
1563;; This handler is not necessary, but it makes the output from dont-compile
1564;; and similar macros cleaner.
1565(put 'eval 'byte-hunk-handler 'byte-compile-file-form-eval)
1566(defun byte-compile-file-form-eval (form)
1567 (if (eq (car-safe (nth 1 form)) 'quote)
1568 (nth 1 (nth 1 form))
1569 (byte-compile-keep-pending form)))
1570
1571(put 'defun 'byte-hunk-handler 'byte-compile-file-form-defun)
1572(defun byte-compile-file-form-defun (form)
1573 (byte-compile-file-form-defmumble form nil))
1574
1575(put 'defmacro 'byte-hunk-handler 'byte-compile-file-form-defmacro)
1576(defun byte-compile-file-form-defmacro (form)
1577 (byte-compile-file-form-defmumble form t))
1578
1579(defun byte-compile-file-form-defmumble (form macrop)
1580 (let* ((name (car (cdr form)))
1581 (this-kind (if macrop 'byte-compile-macro-environment
1582 'byte-compile-function-environment))
1583 (that-kind (if macrop 'byte-compile-function-environment
1584 'byte-compile-macro-environment))
1585 (this-one (assq name (symbol-value this-kind)))
1586 (that-one (assq name (symbol-value that-kind)))
1587 (byte-compile-free-references nil)
1588 (byte-compile-free-assignments nil))
1589
1590 ;; When a function or macro is defined, add it to the call tree so that
1591 ;; we can tell when functions are not used.
1592 (if byte-compile-generate-call-tree
1593 (or (assq name byte-compile-call-tree)
1594 (setq byte-compile-call-tree
1595 (cons (list name nil nil) byte-compile-call-tree))))
1596
1597 (setq byte-compile-current-form name) ; for warnings
1598 (if (memq 'redefine byte-compile-warnings)
1599 (byte-compile-arglist-warn form macrop))
1600 (if byte-compile-verbose
1601 (message "Compiling %s (%s)..." (or filename "") (nth 1 form)))
1602 (cond (that-one
1603 (if (and (memq 'redefine byte-compile-warnings)
52799cb8 1604 ;; don't warn when compiling the stubs in byte-run...
1c393159
JB
1605 (not (assq (nth 1 form)
1606 byte-compile-initial-macro-environment)))
1607 (byte-compile-warn
1608 "%s defined multiple times, as both function and macro"
1609 (nth 1 form)))
1610 (setcdr that-one nil))
1611 (this-one
1612 (if (and (memq 'redefine byte-compile-warnings)
1613 ;; hack: don't warn when compiling the magic internal
52799cb8 1614 ;; byte-compiler macros in byte-run.el...
1c393159
JB
1615 (not (assq (nth 1 form)
1616 byte-compile-initial-macro-environment)))
1617 (byte-compile-warn "%s %s defined multiple times in this file"
1618 (if macrop "macro" "function")
1619 (nth 1 form))))
1620 ((and (fboundp name)
1621 (eq (car-safe (symbol-function name))
1622 (if macrop 'lambda 'macro)))
1623 (if (memq 'redefine byte-compile-warnings)
1624 (byte-compile-warn "%s %s being redefined as a %s"
1625 (if macrop "function" "macro")
1626 (nth 1 form)
1627 (if macrop "macro" "function")))
1628 ;; shadow existing definition
1629 (set this-kind
1630 (cons (cons name nil) (symbol-value this-kind))))
1631 )
1632 (let ((body (nthcdr 3 form)))
1633 (if (and (stringp (car body))
1634 (symbolp (car-safe (cdr-safe body)))
1635 (car-safe (cdr-safe body))
1636 (stringp (car-safe (cdr-safe (cdr-safe body)))))
1637 (byte-compile-warn "Probable `\"' without `\\' in doc string of %s"
1638 (nth 1 form))))
1639 (let* ((new-one (byte-compile-lambda (cons 'lambda (nthcdr 2 form))))
1640 (code (byte-compile-byte-code-maker new-one)))
1641 (if this-one
1642 (setcdr this-one new-one)
1643 (set this-kind
1644 (cons (cons name new-one) (symbol-value this-kind))))
1645 (if (and (stringp (nth 3 form))
1646 (eq 'quote (car-safe code))
1647 (eq 'lambda (car-safe (nth 1 code))))
1648 (cons (car form)
1649 (cons name (cdr (nth 1 code))))
d82e848c 1650 (byte-compile-flush-pending)
1c393159 1651 (if (not (stringp (nth 3 form)))
d82e848c
RS
1652 ;; No doc string. Provide -1 as the "doc string index"
1653 ;; so that no element will be treated as a doc string.
1654 (byte-compile-output-docform
1655 (if (byte-compile-version-cond byte-compile-compatibility)
1656 "\n(fset '" "\n(defalias '")
1657 name
1658 (cond ((atom code)
1659 (if macrop '(" '(macro . #[" -1 "])") '(" #[" -1 "]")))
1660 ((eq (car code) 'quote)
1661 (setq code new-one)
1662 (if macrop '(" '(macro " -1 ")") '(" '(" -1 ")")))
1663 ((if macrop '(" (cons 'macro (" -1 "))") '(" (" -1 ")"))))
1664 (append code nil)
1665 (and (atom code) byte-compile-dynamic
1666 1)
1667 nil)
1c393159 1668 ;; Output the form by hand, that's much simpler than having
c36881cf 1669 ;; b-c-output-file-form analyze the defalias.
1c393159 1670 (byte-compile-output-docform
d82e848c
RS
1671 (if (byte-compile-version-cond byte-compile-compatibility)
1672 "\n(fset '" "\n(defalias '")
1673 name
1c393159
JB
1674 (cond ((atom code)
1675 (if macrop '(" '(macro . #[" 4 "])") '(" #[" 4 "]")))
1676 ((eq (car code) 'quote)
1677 (setq code new-one)
1678 (if macrop '(" '(macro " 2 ")") '(" '(" 2 ")")))
1679 ((if macrop '(" (cons 'macro (" 5 "))") '(" (" 5 ")"))))
d82e848c
RS
1680 (append code nil)
1681 (and (atom code) byte-compile-dynamic
1682 1)
1683 nil))
1684 (princ ")" outbuffer)
1685 nil))))
1686
1687;; Print Lisp object EXP in the output file, inside a comment,
1688;; and return the file position it will have.
1689;; If QUOTED is non-nil, print with quoting; otherwise, print without quoting.
1690(defun byte-compile-output-as-comment (exp quoted)
1691 (let ((position (point)))
1692 (set-buffer
1693 (prog1 (current-buffer)
1694 (set-buffer outbuffer)
1695
1696 ;; Insert EXP, and make it a comment with #@LENGTH.
1697 (insert " ")
1698 (if quoted
1699 (prin1 exp outbuffer)
1700 (princ exp outbuffer))
1701 (goto-char position)
1702 ;; Quote certain special characters as needed.
1703 ;; get_doc_string in doc.c does the unquoting.
1704 (while (search-forward "\^A" nil t)
1705 (replace-match "\^A\^A" t t))
1706 (goto-char position)
1707 (while (search-forward "\000" nil t)
1708 (replace-match "\^A0" t t))
1709 (goto-char position)
1710 (while (search-forward "\037" nil t)
1711 (replace-match "\^A_" t t))
1712 (goto-char (point-max))
1713 (insert "\037")
1714 (goto-char position)
1715 (insert "#@" (format "%d" (- (point-max) position)))
1716
1717 ;; Save the file position of the object.
1718 ;; Note we should add 1 to skip the space
1719 ;; that we inserted before the actual doc string,
1720 ;; and subtract 1 to convert from an 1-origin Emacs position
1721 ;; to a file position; they cancel.
1722 (setq position (point))
1723 (goto-char (point-max))))
1724 position))
1725
1c393159
JB
1726
1727\f
fd5285f3 1728;;;###autoload
1c393159
JB
1729(defun byte-compile (form)
1730 "If FORM is a symbol, byte-compile its function definition.
1731If FORM is a lambda or a macro, byte-compile it as a function."
1732 (displaying-byte-compile-warnings
1733 (byte-compile-close-variables
1734 (let* ((fun (if (symbolp form)
1735 (and (fboundp form) (symbol-function form))
1736 form))
1737 (macro (eq (car-safe fun) 'macro)))
1738 (if macro
1739 (setq fun (cdr fun)))
1740 (cond ((eq (car-safe fun) 'lambda)
1741 (setq fun (if macro
1742 (cons 'macro (byte-compile-lambda fun))
1743 (byte-compile-lambda fun)))
1744 (if (symbolp form)
c36881cf 1745 (defalias form fun)
1c393159
JB
1746 fun)))))))
1747
1748(defun byte-compile-sexp (sexp)
1749 "Compile and return SEXP."
1750 (displaying-byte-compile-warnings
1751 (byte-compile-close-variables
1752 (byte-compile-top-level sexp))))
1753
1754;; Given a function made by byte-compile-lambda, make a form which produces it.
1755(defun byte-compile-byte-code-maker (fun)
1756 (cond
52799cb8 1757 ((byte-compile-version-cond byte-compile-compatibility)
1c393159
JB
1758 ;; Return (quote (lambda ...)).
1759 (list 'quote (byte-compile-byte-code-unmake fun)))
1760 ;; ## atom is faster than compiled-func-p.
1761 ((atom fun) ; compiled function.
1762 ;; generate-emacs19-bytecodes must be on, otherwise byte-compile-lambda
1763 ;; would have produced a lambda.
1764 fun)
1765 ;; b-c-lambda didn't produce a compiled-function, so it's either a trivial
52799cb8 1766 ;; function, or this is Emacs 18, or generate-emacs19-bytecodes is off.
1c393159
JB
1767 ((let (tmp)
1768 (if (and (setq tmp (assq 'byte-code (cdr-safe (cdr fun))))
1769 (null (cdr (memq tmp fun))))
1770 ;; Generate a make-byte-code call.
1771 (let* ((interactive (assq 'interactive (cdr (cdr fun)))))
1772 (nconc (list 'make-byte-code
1773 (list 'quote (nth 1 fun)) ;arglist
1774 (nth 1 tmp) ;bytes
1775 (nth 2 tmp) ;consts
1776 (nth 3 tmp)) ;depth
1777 (cond ((stringp (nth 2 fun))
1778 (list (nth 2 fun))) ;doc
1779 (interactive
1780 (list nil)))
1781 (cond (interactive
1782 (list (if (or (null (nth 1 interactive))
1783 (stringp (nth 1 interactive)))
1784 (nth 1 interactive)
1785 ;; Interactive spec is a list or a variable
1786 ;; (if it is correct).
1787 (list 'quote (nth 1 interactive))))))))
1788 ;; a non-compiled function (probably trivial)
1789 (list 'quote fun))))))
1790
1791;; Turn a function into an ordinary lambda. Needed for v18 files.
1792(defun byte-compile-byte-code-unmake (function)
1793 (if (consp function)
1794 function;;It already is a lambda.
1795 (setq function (append function nil)) ; turn it into a list
1796 (nconc (list 'lambda (nth 0 function))
1797 (and (nth 4 function) (list (nth 4 function)))
1798 (if (nthcdr 5 function)
1799 (list (cons 'interactive (if (nth 5 function)
1800 (nthcdr 5 function)))))
1801 (list (list 'byte-code
1802 (nth 1 function) (nth 2 function)
1803 (nth 3 function))))))
1804
1805
1806;; Byte-compile a lambda-expression and return a valid function.
1807;; The value is usually a compiled function but may be the original
1808;; lambda-expression.
1809(defun byte-compile-lambda (fun)
1810 (let* ((arglist (nth 1 fun))
1811 (byte-compile-bound-variables
1812 (nconc (and (memq 'free-vars byte-compile-warnings)
1813 (delq '&rest (delq '&optional (copy-sequence arglist))))
1814 byte-compile-bound-variables))
1815 (body (cdr (cdr fun)))
1816 (doc (if (stringp (car body))
1817 (prog1 (car body)
1818 (setq body (cdr body)))))
1819 (int (assq 'interactive body)))
1820 (cond (int
1821 ;; Skip (interactive) if it is in front (the most usual location).
1822 (if (eq int (car body))
1823 (setq body (cdr body)))
ffc394dd 1824 (cond ((consp (cdr int))
1c393159
JB
1825 (if (cdr (cdr int))
1826 (byte-compile-warn "malformed interactive spec: %s"
1827 (prin1-to-string int)))
ffc394dd
RS
1828 ;; If the interactive spec is a call to `list',
1829 ;; don't compile it, because `call-interactively'
1830 ;; looks at the args of `list'.
1831 (or (eq (car-safe (nth 1 int)) 'list)
1832 (setq int (list 'interactive
1833 (byte-compile-top-level (nth 1 int))))))
1834 ((cdr int)
1835 (byte-compile-warn "malformed interactive spec: %s"
1836 (prin1-to-string int))))))
1c393159
JB
1837 (let ((compiled (byte-compile-top-level (cons 'progn body) nil 'lambda)))
1838 (if (and (eq 'byte-code (car-safe compiled))
b890df1a
RS
1839 (not (byte-compile-version-cond
1840 byte-compile-compatibility)))
1c393159
JB
1841 (apply 'make-byte-code
1842 (append (list arglist)
1843 ;; byte-string, constants-vector, stack depth
1844 (cdr compiled)
1845 ;; optionally, the doc string.
1846 (if (or doc int)
1847 (list doc))
1848 ;; optionally, the interactive spec.
1849 (if int
1850 (list (nth 1 int)))))
1851 (setq compiled
1852 (nconc (if int (list int))
1853 (cond ((eq (car-safe compiled) 'progn) (cdr compiled))
1854 (compiled (list compiled)))))
1855 (nconc (list 'lambda arglist)
1856 (if (or doc (stringp (car compiled)))
1857 (cons doc (cond (compiled)
1858 (body (list nil))))
1859 compiled))))))
1860
1861(defun byte-compile-constants-vector ()
1862 ;; Builds the constants-vector from the current variables and constants.
1863 ;; This modifies the constants from (const . nil) to (const . offset).
1864 ;; To keep the byte-codes to look up the vector as short as possible:
1865 ;; First 6 elements are vars, as there are one-byte varref codes for those.
1866 ;; Next up to byte-constant-limit are constants, still with one-byte codes.
1867 ;; Next variables again, to get 2-byte codes for variable lookup.
1868 ;; The rest of the constants and variables need 3-byte byte-codes.
1869 (let* ((i -1)
1870 (rest (nreverse byte-compile-variables)) ; nreverse because the first
1871 (other (nreverse byte-compile-constants)) ; vars often are used most.
1872 ret tmp
1873 (limits '(5 ; Use the 1-byte varref codes,
1874 63 ; 1-constlim ; 1-byte byte-constant codes,
1875 255 ; 2-byte varref codes,
1876 65535)) ; 3-byte codes for the rest.
1877 limit)
1878 (while (or rest other)
1879 (setq limit (car limits))
1880 (while (and rest (not (eq i limit)))
1881 (if (setq tmp (assq (car (car rest)) ret))
1882 (setcdr (car rest) (cdr tmp))
1883 (setcdr (car rest) (setq i (1+ i)))
1884 (setq ret (cons (car rest) ret)))
1885 (setq rest (cdr rest)))
1886 (setq limits (cdr limits)
1887 rest (prog1 other
1888 (setq other rest))))
1889 (apply 'vector (nreverse (mapcar 'car ret)))))
1890
1891;; Given an expression FORM, compile it and return an equivalent byte-code
1892;; expression (a call to the function byte-code).
1893(defun byte-compile-top-level (form &optional for-effect output-type)
1894 ;; OUTPUT-TYPE advises about how form is expected to be used:
1895 ;; 'eval or nil -> a single form,
1896 ;; 'progn or t -> a list of forms,
1897 ;; 'lambda -> body of a lambda,
1898 ;; 'file -> used at file-level.
285cdf4e
RS
1899 (let ((byte-compile-constants nil)
1900 (byte-compile-variables nil)
1901 (byte-compile-tag-number 0)
1902 (byte-compile-depth 0)
1903 (byte-compile-maxdepth 0)
1904 (byte-compile-output nil))
d9e42bcf
RS
1905 (if (memq byte-optimize '(t source))
1906 (setq form (byte-optimize-form form for-effect)))
1907 (while (and (eq (car-safe form) 'progn) (null (cdr (cdr form))))
1908 (setq form (nth 1 form)))
1909 (if (and (eq 'byte-code (car-safe form))
1910 (not (memq byte-optimize '(t byte)))
1911 (stringp (nth 1 form)) (vectorp (nth 2 form))
1912 (natnump (nth 3 form)))
1913 form
1914 (byte-compile-form form for-effect)
285cdf4e 1915 (byte-compile-out-toplevel for-effect output-type))))
1c393159
JB
1916
1917(defun byte-compile-out-toplevel (&optional for-effect output-type)
1918 (if for-effect
1919 ;; The stack is empty. Push a value to be returned from (byte-code ..).
1920 (if (eq (car (car byte-compile-output)) 'byte-discard)
1921 (setq byte-compile-output (cdr byte-compile-output))
1922 (byte-compile-push-constant
1923 ;; Push any constant - preferably one which already is used, and
1924 ;; a number or symbol - ie not some big sequence. The return value
1925 ;; isn't returned, but it would be a shame if some textually large
1926 ;; constant was not optimized away because we chose to return it.
1927 (and (not (assq nil byte-compile-constants)) ; Nil is often there.
1928 (let ((tmp (reverse byte-compile-constants)))
1929 (while (and tmp (not (or (symbolp (car (car tmp)))
1930 (numberp (car (car tmp))))))
1931 (setq tmp (cdr tmp)))
1932 (car (car tmp)))))))
1933 (byte-compile-out 'byte-return 0)
1934 (setq byte-compile-output (nreverse byte-compile-output))
1935 (if (memq byte-optimize '(t byte))
1936 (setq byte-compile-output
1937 (byte-optimize-lapcode byte-compile-output for-effect)))
1938
1939 ;; Decompile trivial functions:
1940 ;; only constants and variables, or a single funcall except in lambdas.
1941 ;; Except for Lisp_Compiled objects, forms like (foo "hi")
1942 ;; are still quicker than (byte-code "..." [foo "hi"] 2).
1943 ;; Note that even (quote foo) must be parsed just as any subr by the
1944 ;; interpreter, so quote should be compiled into byte-code in some contexts.
1945 ;; What to leave uncompiled:
1946 ;; lambda -> a single atom.
1947 ;; eval -> atom, quote or (function atom atom atom)
1948 ;; progn -> as <<same-as-eval>> or (progn <<same-as-eval>> atom)
1949 ;; file -> as progn, but takes both quotes and atoms, and longer forms.
1950 (let (rest
1951 (maycall (not (eq output-type 'lambda))) ; t if we may make a funcall.
1952 tmp body)
1953 (cond
1954 ;; #### This should be split out into byte-compile-nontrivial-function-p.
1955 ((or (nthcdr (if (eq output-type 'file) 50 8) byte-compile-output)
1956 (assq 'TAG byte-compile-output) ; Not necessary, but speeds up a bit.
1957 (not (setq tmp (assq 'byte-return byte-compile-output)))
1958 (progn
1959 (setq rest (nreverse
1960 (cdr (memq tmp (reverse byte-compile-output)))))
1961 (while (cond
1962 ((memq (car (car rest)) '(byte-varref byte-constant))
1963 (setq tmp (car (cdr (car rest))))
1964 (if (if (eq (car (car rest)) 'byte-constant)
1965 (or (consp tmp)
1966 (and (symbolp tmp)
1967 (not (memq tmp '(nil t))))))
1968 (if maycall
1969 (setq body (cons (list 'quote tmp) body)))
1970 (setq body (cons tmp body))))
1971 ((and maycall
1972 ;; Allow a funcall if at most one atom follows it.
1973 (null (nthcdr 3 rest))
1974 (setq tmp (get (car (car rest)) 'byte-opcode-invert))
1975 (or (null (cdr rest))
1976 (and (memq output-type '(file progn t))
1977 (cdr (cdr rest))
1978 (eq (car (nth 1 rest)) 'byte-discard)
1979 (progn (setq rest (cdr rest)) t))))
1980 (setq maycall nil) ; Only allow one real function call.
1981 (setq body (nreverse body))
1982 (setq body (list
1983 (if (and (eq tmp 'funcall)
1984 (eq (car-safe (car body)) 'quote))
1985 (cons (nth 1 (car body)) (cdr body))
1986 (cons tmp body))))
1987 (or (eq output-type 'file)
1988 (not (delq nil (mapcar 'consp (cdr (car body))))))))
1989 (setq rest (cdr rest)))
1990 rest)
1991 (and (consp (car body)) (eq output-type 'lambda)))
1992 (let ((byte-compile-vector (byte-compile-constants-vector)))
1993 (list 'byte-code (byte-compile-lapcode byte-compile-output)
1994 byte-compile-vector byte-compile-maxdepth)))
1995 ;; it's a trivial function
1996 ((cdr body) (cons 'progn (nreverse body)))
1997 ((car body)))))
1998
1999;; Given BODY, compile it and return a new body.
2000(defun byte-compile-top-level-body (body &optional for-effect)
2001 (setq body (byte-compile-top-level (cons 'progn body) for-effect t))
2002 (cond ((eq (car-safe body) 'progn)
2003 (cdr body))
2004 (body
2005 (list body))))
2006\f
2007;; This is the recursive entry point for compiling each subform of an
2008;; expression.
2009;; If for-effect is non-nil, byte-compile-form will output a byte-discard
2010;; before terminating (ie no value will be left on the stack).
2011;; A byte-compile handler may, when for-effect is non-nil, choose output code
2012;; which does not leave a value on the stack, and then set for-effect to nil
2013;; (to prevent byte-compile-form from outputting the byte-discard).
2014;; If a handler wants to call another handler, it should do so via
2015;; byte-compile-form, or take extreme care to handle for-effect correctly.
2016;; (Use byte-compile-form-do-effect to reset the for-effect flag too.)
2017;;
2018(defun byte-compile-form (form &optional for-effect)
2019 (setq form (macroexpand form byte-compile-macro-environment))
2020 (cond ((not (consp form))
2021 (cond ((or (not (symbolp form)) (memq form '(nil t)))
2022 (byte-compile-constant form))
2023 ((and for-effect byte-compile-delete-errors)
2024 (setq for-effect nil))
2025 (t (byte-compile-variable-ref 'byte-varref form))))
2026 ((symbolp (car form))
2027 (let* ((fn (car form))
2028 (handler (get fn 'byte-compile)))
9e2b097b
JB
2029 (if (memq fn '(t nil))
2030 (byte-compile-warn "%s called as a function" fn))
1c393159 2031 (if (and handler
e27c3564
JB
2032 (or (not (byte-compile-version-cond
2033 byte-compile-compatibility))
1c393159
JB
2034 (not (get (get fn 'byte-opcode) 'emacs19-opcode))))
2035 (funcall handler form)
2036 (if (memq 'callargs byte-compile-warnings)
2037 (byte-compile-callargs-warn form))
2038 (byte-compile-normal-call form))))
ed015bdd 2039 ((and (or (byte-code-function-p (car form))
1c393159
JB
2040 (eq (car-safe (car form)) 'lambda))
2041 ;; if the form comes out the same way it went in, that's
2042 ;; because it was malformed, and we couldn't unfold it.
2043 (not (eq form (setq form (byte-compile-unfold-lambda form)))))
2044 (byte-compile-form form for-effect)
2045 (setq for-effect nil))
2046 ((byte-compile-normal-call form)))
2047 (if for-effect
2048 (byte-compile-discard)))
2049
2050(defun byte-compile-normal-call (form)
2051 (if byte-compile-generate-call-tree
2052 (byte-compile-annotate-call-tree form))
2053 (byte-compile-push-constant (car form))
2054 (mapcar 'byte-compile-form (cdr form)) ; wasteful, but faster.
2055 (byte-compile-out 'byte-call (length (cdr form))))
2056
2057(defun byte-compile-variable-ref (base-op var)
2058 (if (or (not (symbolp var)) (memq var '(nil t)))
2059 (byte-compile-warn (if (eq base-op 'byte-varbind)
2060 "Attempt to let-bind %s %s"
2061 "Variable reference to %s %s")
2062 (if (symbolp var) "constant" "nonvariable")
2063 (prin1-to-string var))
9290191f
RS
2064 (if (and (get var 'byte-obsolete-variable)
2065 (memq 'obsolete byte-compile-warnings))
9e2b097b
JB
2066 (let ((ob (get var 'byte-obsolete-variable)))
2067 (byte-compile-warn "%s is an obsolete variable; %s" var
2068 (if (stringp ob)
2069 ob
2070 (format "use %s instead." ob)))))
1c393159
JB
2071 (if (memq 'free-vars byte-compile-warnings)
2072 (if (eq base-op 'byte-varbind)
2073 (setq byte-compile-bound-variables
2074 (cons var byte-compile-bound-variables))
2075 (or (boundp var)
2076 (memq var byte-compile-bound-variables)
2077 (if (eq base-op 'byte-varset)
2078 (or (memq var byte-compile-free-assignments)
2079 (progn
2080 (byte-compile-warn "assignment to free variable %s" var)
2081 (setq byte-compile-free-assignments
2082 (cons var byte-compile-free-assignments))))
2083 (or (memq var byte-compile-free-references)
2084 (progn
2085 (byte-compile-warn "reference to free variable %s" var)
2086 (setq byte-compile-free-references
2087 (cons var byte-compile-free-references)))))))))
2088 (let ((tmp (assq var byte-compile-variables)))
2089 (or tmp
2090 (setq tmp (list var)
2091 byte-compile-variables (cons tmp byte-compile-variables)))
2092 (byte-compile-out base-op tmp)))
2093
2094(defmacro byte-compile-get-constant (const)
2095 (` (or (if (stringp (, const))
2096 (assoc (, const) byte-compile-constants)
2097 (assq (, const) byte-compile-constants))
2098 (car (setq byte-compile-constants
2099 (cons (list (, const)) byte-compile-constants))))))
2100
2101;; Use this when the value of a form is a constant. This obeys for-effect.
2102(defun byte-compile-constant (const)
2103 (if for-effect
2104 (setq for-effect nil)
2105 (byte-compile-out 'byte-constant (byte-compile-get-constant const))))
2106
2107;; Use this for a constant that is not the value of its containing form.
2108;; This ignores for-effect.
2109(defun byte-compile-push-constant (const)
2110 (let ((for-effect nil))
2111 (inline (byte-compile-constant const))))
2112
2113\f
2114;; Compile those primitive ordinary functions
2115;; which have special byte codes just for speed.
2116
2117(defmacro byte-defop-compiler (function &optional compile-handler)
2118 ;; add a compiler-form for FUNCTION.
2119 ;; If function is a symbol, then the variable "byte-SYMBOL" must name
2120 ;; the opcode to be used. If function is a list, the first element
2121 ;; is the function and the second element is the bytecode-symbol.
2122 ;; COMPILE-HANDLER is the function to use to compile this byte-op, or
2123 ;; may be the abbreviations 0, 1, 2, 3, 0-1, or 1-2.
2124 ;; If it is nil, then the handler is "byte-compile-SYMBOL."
2125 (let (opcode)
2126 (if (symbolp function)
2127 (setq opcode (intern (concat "byte-" (symbol-name function))))
2128 (setq opcode (car (cdr function))
2129 function (car function)))
2130 (let ((fnform
2131 (list 'put (list 'quote function) ''byte-compile
2132 (list 'quote
2133 (or (cdr (assq compile-handler
2134 '((0 . byte-compile-no-args)
2135 (1 . byte-compile-one-arg)
2136 (2 . byte-compile-two-args)
2137 (3 . byte-compile-three-args)
2138 (0-1 . byte-compile-zero-or-one-arg)
2139 (1-2 . byte-compile-one-or-two-args)
2140 (2-3 . byte-compile-two-or-three-args)
2141 )))
2142 compile-handler
2143 (intern (concat "byte-compile-"
2144 (symbol-name function))))))))
2145 (if opcode
2146 (list 'progn fnform
2147 (list 'put (list 'quote function)
2148 ''byte-opcode (list 'quote opcode))
2149 (list 'put (list 'quote opcode)
2150 ''byte-opcode-invert (list 'quote function)))
2151 fnform))))
2152
2153(defmacro byte-defop-compiler19 (function &optional compile-handler)
2154 ;; Just like byte-defop-compiler, but defines an opcode that will only
e27c3564 2155 ;; be used when byte-compile-compatibility is false.
1c393159 2156 (if (and (byte-compile-single-version)
e27c3564 2157 byte-compile-compatibility)
9e2b097b
JB
2158 ;; #### instead of doing nothing, this should do some remprops,
2159 ;; #### to protect against the case where a single-version compiler
2160 ;; #### is loaded into a world that has contained a multi-version one.
1c393159
JB
2161 nil
2162 (list 'progn
2163 (list 'put
2164 (list 'quote
2165 (or (car (cdr-safe function))
2166 (intern (concat "byte-"
2167 (symbol-name (or (car-safe function) function))))))
2168 ''emacs19-opcode t)
2169 (list 'byte-defop-compiler function compile-handler))))
2170
2171(defmacro byte-defop-compiler-1 (function &optional compile-handler)
2172 (list 'byte-defop-compiler (list function nil) compile-handler))
2173
2174\f
2175(put 'byte-call 'byte-opcode-invert 'funcall)
2176(put 'byte-list1 'byte-opcode-invert 'list)
2177(put 'byte-list2 'byte-opcode-invert 'list)
2178(put 'byte-list3 'byte-opcode-invert 'list)
2179(put 'byte-list4 'byte-opcode-invert 'list)
2180(put 'byte-listN 'byte-opcode-invert 'list)
2181(put 'byte-concat2 'byte-opcode-invert 'concat)
2182(put 'byte-concat3 'byte-opcode-invert 'concat)
2183(put 'byte-concat4 'byte-opcode-invert 'concat)
2184(put 'byte-concatN 'byte-opcode-invert 'concat)
2185(put 'byte-insertN 'byte-opcode-invert 'insert)
2186
2187(byte-defop-compiler (dot byte-point) 0)
2188(byte-defop-compiler (dot-max byte-point-max) 0)
2189(byte-defop-compiler (dot-min byte-point-min) 0)
2190(byte-defop-compiler point 0)
2191;;(byte-defop-compiler mark 0) ;; obsolete
2192(byte-defop-compiler point-max 0)
2193(byte-defop-compiler point-min 0)
2194(byte-defop-compiler following-char 0)
2195(byte-defop-compiler preceding-char 0)
2196(byte-defop-compiler current-column 0)
2197(byte-defop-compiler eolp 0)
2198(byte-defop-compiler eobp 0)
2199(byte-defop-compiler bolp 0)
2200(byte-defop-compiler bobp 0)
2201(byte-defop-compiler current-buffer 0)
2202;;(byte-defop-compiler read-char 0) ;; obsolete
2203(byte-defop-compiler interactive-p 0)
2204(byte-defop-compiler19 widen 0)
2205(byte-defop-compiler19 end-of-line 0-1)
2206(byte-defop-compiler19 forward-char 0-1)
2207(byte-defop-compiler19 forward-line 0-1)
2208(byte-defop-compiler symbolp 1)
2209(byte-defop-compiler consp 1)
2210(byte-defop-compiler stringp 1)
2211(byte-defop-compiler listp 1)
2212(byte-defop-compiler not 1)
2213(byte-defop-compiler (null byte-not) 1)
2214(byte-defop-compiler car 1)
2215(byte-defop-compiler cdr 1)
2216(byte-defop-compiler length 1)
2217(byte-defop-compiler symbol-value 1)
2218(byte-defop-compiler symbol-function 1)
2219(byte-defop-compiler (1+ byte-add1) 1)
2220(byte-defop-compiler (1- byte-sub1) 1)
2221(byte-defop-compiler goto-char 1)
2222(byte-defop-compiler char-after 1)
2223(byte-defop-compiler set-buffer 1)
2224;;(byte-defop-compiler set-mark 1) ;; obsolete
2225(byte-defop-compiler19 forward-word 1)
2226(byte-defop-compiler19 char-syntax 1)
2227(byte-defop-compiler19 nreverse 1)
2228(byte-defop-compiler19 car-safe 1)
2229(byte-defop-compiler19 cdr-safe 1)
2230(byte-defop-compiler19 numberp 1)
2231(byte-defop-compiler19 integerp 1)
2232(byte-defop-compiler19 skip-chars-forward 1-2)
2233(byte-defop-compiler19 skip-chars-backward 1-2)
2234(byte-defop-compiler (eql byte-eq) 2)
2235(byte-defop-compiler eq 2)
2236(byte-defop-compiler memq 2)
2237(byte-defop-compiler cons 2)
2238(byte-defop-compiler aref 2)
2239(byte-defop-compiler set 2)
2240(byte-defop-compiler (= byte-eqlsign) 2)
2241(byte-defop-compiler (< byte-lss) 2)
2242(byte-defop-compiler (> byte-gtr) 2)
2243(byte-defop-compiler (<= byte-leq) 2)
2244(byte-defop-compiler (>= byte-geq) 2)
2245(byte-defop-compiler get 2)
2246(byte-defop-compiler nth 2)
2247(byte-defop-compiler substring 2-3)
9e2b097b 2248(byte-defop-compiler19 (move-marker byte-set-marker) 2-3)
1c393159
JB
2249(byte-defop-compiler19 set-marker 2-3)
2250(byte-defop-compiler19 match-beginning 1)
2251(byte-defop-compiler19 match-end 1)
2252(byte-defop-compiler19 upcase 1)
2253(byte-defop-compiler19 downcase 1)
2254(byte-defop-compiler19 string= 2)
2255(byte-defop-compiler19 string< 2)
9e2b097b
JB
2256(byte-defop-compiler19 (string-equal byte-string=) 2)
2257(byte-defop-compiler19 (string-lessp byte-string<) 2)
1c393159
JB
2258(byte-defop-compiler19 equal 2)
2259(byte-defop-compiler19 nthcdr 2)
2260(byte-defop-compiler19 elt 2)
2261(byte-defop-compiler19 member 2)
2262(byte-defop-compiler19 assq 2)
9e2b097b
JB
2263(byte-defop-compiler19 (rplaca byte-setcar) 2)
2264(byte-defop-compiler19 (rplacd byte-setcdr) 2)
1c393159
JB
2265(byte-defop-compiler19 setcar 2)
2266(byte-defop-compiler19 setcdr 2)
2267(byte-defop-compiler19 buffer-substring 2)
2268(byte-defop-compiler19 delete-region 2)
2269(byte-defop-compiler19 narrow-to-region 2)
1c393159
JB
2270(byte-defop-compiler19 (% byte-rem) 2)
2271(byte-defop-compiler aset 3)
2272
2273(byte-defop-compiler max byte-compile-associative)
2274(byte-defop-compiler min byte-compile-associative)
2275(byte-defop-compiler (+ byte-plus) byte-compile-associative)
2276(byte-defop-compiler19 (* byte-mult) byte-compile-associative)
2277
2278;;####(byte-defop-compiler19 move-to-column 1)
2279(byte-defop-compiler-1 interactive byte-compile-noop)
2280
2281\f
2282(defun byte-compile-subr-wrong-args (form n)
2283 (byte-compile-warn "%s called with %d arg%s, but requires %s"
2284 (car form) (length (cdr form))
2285 (if (= 1 (length (cdr form))) "" "s") n)
2286 ;; get run-time wrong-number-of-args error.
2287 (byte-compile-normal-call form))
2288
2289(defun byte-compile-no-args (form)
2290 (if (not (= (length form) 1))
2291 (byte-compile-subr-wrong-args form "none")
2292 (byte-compile-out (get (car form) 'byte-opcode) 0)))
2293
2294(defun byte-compile-one-arg (form)
2295 (if (not (= (length form) 2))
2296 (byte-compile-subr-wrong-args form 1)
2297 (byte-compile-form (car (cdr form))) ;; Push the argument
2298 (byte-compile-out (get (car form) 'byte-opcode) 0)))
2299
2300(defun byte-compile-two-args (form)
2301 (if (not (= (length form) 3))
2302 (byte-compile-subr-wrong-args form 2)
2303 (byte-compile-form (car (cdr form))) ;; Push the arguments
2304 (byte-compile-form (nth 2 form))
2305 (byte-compile-out (get (car form) 'byte-opcode) 0)))
2306
2307(defun byte-compile-three-args (form)
2308 (if (not (= (length form) 4))
2309 (byte-compile-subr-wrong-args form 3)
2310 (byte-compile-form (car (cdr form))) ;; Push the arguments
2311 (byte-compile-form (nth 2 form))
2312 (byte-compile-form (nth 3 form))
2313 (byte-compile-out (get (car form) 'byte-opcode) 0)))
2314
2315(defun byte-compile-zero-or-one-arg (form)
2316 (let ((len (length form)))
2317 (cond ((= len 1) (byte-compile-one-arg (append form '(nil))))
2318 ((= len 2) (byte-compile-one-arg form))
2319 (t (byte-compile-subr-wrong-args form "0-1")))))
2320
2321(defun byte-compile-one-or-two-args (form)
2322 (let ((len (length form)))
2323 (cond ((= len 2) (byte-compile-two-args (append form '(nil))))
2324 ((= len 3) (byte-compile-two-args form))
2325 (t (byte-compile-subr-wrong-args form "1-2")))))
2326
2327(defun byte-compile-two-or-three-args (form)
2328 (let ((len (length form)))
2329 (cond ((= len 3) (byte-compile-three-args (append form '(nil))))
2330 ((= len 4) (byte-compile-three-args form))
2331 (t (byte-compile-subr-wrong-args form "2-3")))))
2332
2333(defun byte-compile-noop (form)
2334 (byte-compile-constant nil))
2335
2336(defun byte-compile-discard ()
2337 (byte-compile-out 'byte-discard 0))
2338
2339
2340;; Compile a function that accepts one or more args and is right-associative.
2341(defun byte-compile-associative (form)
2342 (if (cdr form)
2343 (let ((opcode (get (car form) 'byte-opcode)))
eb8c3be9 2344 ;; To compile all the args first may enable some optimizations.
1c393159
JB
2345 (mapcar 'byte-compile-form (setq form (cdr form)))
2346 (while (setq form (cdr form))
2347 (byte-compile-out opcode 0)))
2348 (byte-compile-constant (eval form))))
2349
2350\f
2351;; more complicated compiler macros
2352
2353(byte-defop-compiler list)
2354(byte-defop-compiler concat)
2355(byte-defop-compiler fset)
2356(byte-defop-compiler (indent-to-column byte-indent-to) byte-compile-indent-to)
2357(byte-defop-compiler indent-to)
2358(byte-defop-compiler insert)
2359(byte-defop-compiler-1 function byte-compile-function-form)
2360(byte-defop-compiler-1 - byte-compile-minus)
2361(byte-defop-compiler19 (/ byte-quo) byte-compile-quo)
2362(byte-defop-compiler19 nconc)
2363(byte-defop-compiler-1 beginning-of-line)
2364
2365(defun byte-compile-list (form)
2366 (let ((count (length (cdr form))))
2367 (cond ((= count 0)
2368 (byte-compile-constant nil))
2369 ((< count 5)
2370 (mapcar 'byte-compile-form (cdr form))
2371 (byte-compile-out
2372 (aref [byte-list1 byte-list2 byte-list3 byte-list4] (1- count)) 0))
e27c3564
JB
2373 ((and (< count 256) (not (byte-compile-version-cond
2374 byte-compile-compatibility)))
1c393159
JB
2375 (mapcar 'byte-compile-form (cdr form))
2376 (byte-compile-out 'byte-listN count))
2377 (t (byte-compile-normal-call form)))))
2378
2379(defun byte-compile-concat (form)
2380 (let ((count (length (cdr form))))
2381 (cond ((and (< 1 count) (< count 5))
2382 (mapcar 'byte-compile-form (cdr form))
2383 (byte-compile-out
2384 (aref [byte-concat2 byte-concat3 byte-concat4] (- count 2))
2385 0))
2386 ;; Concat of one arg is not a no-op if arg is not a string.
2387 ((= count 0)
2388 (byte-compile-form ""))
e27c3564
JB
2389 ((and (< count 256) (not (byte-compile-version-cond
2390 byte-compile-compatibility)))
1c393159
JB
2391 (mapcar 'byte-compile-form (cdr form))
2392 (byte-compile-out 'byte-concatN count))
2393 ((byte-compile-normal-call form)))))
2394
2395(defun byte-compile-minus (form)
2396 (if (null (setq form (cdr form)))
2397 (byte-compile-constant 0)
2398 (byte-compile-form (car form))
2399 (if (cdr form)
2400 (while (setq form (cdr form))
2401 (byte-compile-form (car form))
2402 (byte-compile-out 'byte-diff 0))
2403 (byte-compile-out 'byte-negate 0))))
2404
2405(defun byte-compile-quo (form)
2406 (let ((len (length form)))
2407 (cond ((<= len 2)
2408 (byte-compile-subr-wrong-args form "2 or more"))
2409 (t
2410 (byte-compile-form (car (setq form (cdr form))))
2411 (while (setq form (cdr form))
2412 (byte-compile-form (car form))
2413 (byte-compile-out 'byte-quo 0))))))
2414
2415(defun byte-compile-nconc (form)
2416 (let ((len (length form)))
2417 (cond ((= len 1)
2418 (byte-compile-constant nil))
2419 ((= len 2)
2420 ;; nconc of one arg is a noop, even if that arg isn't a list.
2421 (byte-compile-form (nth 1 form)))
2422 (t
2423 (byte-compile-form (car (setq form (cdr form))))
2424 (while (setq form (cdr form))
2425 (byte-compile-form (car form))
2426 (byte-compile-out 'byte-nconc 0))))))
2427
2428(defun byte-compile-fset (form)
2429 ;; warn about forms like (fset 'foo '(lambda () ...))
2430 ;; (where the lambda expression is non-trivial...)
2431 (let ((fn (nth 2 form))
2432 body)
2433 (if (and (eq (car-safe fn) 'quote)
2434 (eq (car-safe (setq fn (nth 1 fn))) 'lambda))
2435 (progn
2436 (setq body (cdr (cdr fn)))
2437 (if (stringp (car body)) (setq body (cdr body)))
2438 (if (eq 'interactive (car-safe (car body))) (setq body (cdr body)))
2439 (if (and (consp (car body))
2440 (not (eq 'byte-code (car (car body)))))
2441 (byte-compile-warn
2442 "A quoted lambda form is the second argument of fset. This is probably
2443 not what you want, as that lambda cannot be compiled. Consider using
2444 the syntax (function (lambda (...) ...)) instead.")))))
2445 (byte-compile-two-args form))
2446
2447(defun byte-compile-funarg (form)
2448 ;; (mapcar '(lambda (x) ..) ..) ==> (mapcar (function (lambda (x) ..)) ..)
eb8c3be9 2449 ;; for cases where it's guaranteed that first arg will be used as a lambda.
1c393159
JB
2450 (byte-compile-normal-call
2451 (let ((fn (nth 1 form)))
2452 (if (and (eq (car-safe fn) 'quote)
2453 (eq (car-safe (nth 1 fn)) 'lambda))
2454 (cons (car form)
2455 (cons (cons 'function (cdr fn))
2456 (cdr (cdr form))))
2457 form))))
2458
2459;; (function foo) must compile like 'foo, not like (symbol-function 'foo).
2460;; Otherwise it will be incompatible with the interpreter,
2461;; and (funcall (function foo)) will lose with autoloads.
2462
2463(defun byte-compile-function-form (form)
2464 (byte-compile-constant
2465 (cond ((symbolp (nth 1 form))
2466 (nth 1 form))
2467 ;; If we're not allowed to use #[] syntax, then output a form like
2468 ;; '(lambda (..) (byte-code ..)) instead of a call to make-byte-code.
2469 ;; In this situation, calling make-byte-code at run-time will usually
2470 ;; be less efficient than processing a call to byte-code.
52799cb8 2471 ((byte-compile-version-cond byte-compile-compatibility)
1c393159
JB
2472 (byte-compile-byte-code-unmake (byte-compile-lambda (nth 1 form))))
2473 ((byte-compile-lambda (nth 1 form))))))
2474
2475(defun byte-compile-indent-to (form)
2476 (let ((len (length form)))
2477 (cond ((= len 2)
2478 (byte-compile-form (car (cdr form)))
2479 (byte-compile-out 'byte-indent-to 0))
2480 ((= len 3)
2481 ;; no opcode for 2-arg case.
2482 (byte-compile-normal-call form))
2483 (t
2484 (byte-compile-subr-wrong-args form "1-2")))))
2485
2486(defun byte-compile-insert (form)
2487 (cond ((null (cdr form))
2488 (byte-compile-constant nil))
e27c3564
JB
2489 ((and (not (byte-compile-version-cond
2490 byte-compile-compatibility))
1c393159
JB
2491 (<= (length form) 256))
2492 (mapcar 'byte-compile-form (cdr form))
2493 (if (cdr (cdr form))
2494 (byte-compile-out 'byte-insertN (length (cdr form)))
2495 (byte-compile-out 'byte-insert 0)))
2496 ((memq t (mapcar 'consp (cdr (cdr form))))
2497 (byte-compile-normal-call form))
2498 ;; We can split it; there is no function call after inserting 1st arg.
2499 (t
2500 (while (setq form (cdr form))
2501 (byte-compile-form (car form))
2502 (byte-compile-out 'byte-insert 0)
2503 (if (cdr form)
2504 (byte-compile-discard))))))
2505
2506(defun byte-compile-beginning-of-line (form)
2507 (if (not (byte-compile-constp (nth 1 form)))
2508 (byte-compile-normal-call form)
2509 (byte-compile-form
2510 (list 'forward-line
2511 (if (integerp (setq form (or (eval (nth 1 form)) 1)))
2512 (1- form)
2513 (byte-compile-warn "Non-numeric arg to beginning-of-line: %s"
2514 form)
2515 (list '1- (list 'quote form))))
2516 t)
2517 (byte-compile-constant nil)))
2518
2519\f
2520(byte-defop-compiler-1 setq)
2521(byte-defop-compiler-1 setq-default)
2522(byte-defop-compiler-1 quote)
2523(byte-defop-compiler-1 quote-form)
2524
2525(defun byte-compile-setq (form)
2526 (let ((args (cdr form)))
2527 (if args
2528 (while args
2529 (byte-compile-form (car (cdr args)))
2530 (or for-effect (cdr (cdr args))
2531 (byte-compile-out 'byte-dup 0))
2532 (byte-compile-variable-ref 'byte-varset (car args))
2533 (setq args (cdr (cdr args))))
2534 ;; (setq), with no arguments.
2535 (byte-compile-form nil for-effect))
2536 (setq for-effect nil)))
2537
2538(defun byte-compile-setq-default (form)
ca38179a
RS
2539 (let ((args (cdr form))
2540 setters)
2541 (while args
2542 (setq setters
2543 (cons (list 'set-default (list 'quote (car args)) (car (cdr args)))
2544 setters))
2545 (setq args (cdr (cdr args))))
2546 (byte-compile-form (cons 'progn (nreverse setters)))))
1c393159
JB
2547
2548(defun byte-compile-quote (form)
2549 (byte-compile-constant (car (cdr form))))
2550
2551(defun byte-compile-quote-form (form)
2552 (byte-compile-constant (byte-compile-top-level (nth 1 form))))
2553
2554\f
2555;;; control structures
2556
2557(defun byte-compile-body (body &optional for-effect)
2558 (while (cdr body)
2559 (byte-compile-form (car body) t)
2560 (setq body (cdr body)))
2561 (byte-compile-form (car body) for-effect))
2562
52799cb8 2563(defsubst byte-compile-body-do-effect (body)
1c393159
JB
2564 (byte-compile-body body for-effect)
2565 (setq for-effect nil))
2566
52799cb8 2567(defsubst byte-compile-form-do-effect (form)
1c393159
JB
2568 (byte-compile-form form for-effect)
2569 (setq for-effect nil))
2570
2571(byte-defop-compiler-1 inline byte-compile-progn)
2572(byte-defop-compiler-1 progn)
2573(byte-defop-compiler-1 prog1)
2574(byte-defop-compiler-1 prog2)
2575(byte-defop-compiler-1 if)
2576(byte-defop-compiler-1 cond)
2577(byte-defop-compiler-1 and)
2578(byte-defop-compiler-1 or)
2579(byte-defop-compiler-1 while)
2580(byte-defop-compiler-1 funcall)
2581(byte-defop-compiler-1 apply byte-compile-funarg)
2582(byte-defop-compiler-1 mapcar byte-compile-funarg)
2583(byte-defop-compiler-1 mapatoms byte-compile-funarg)
2584(byte-defop-compiler-1 mapconcat byte-compile-funarg)
2585(byte-defop-compiler-1 let)
2586(byte-defop-compiler-1 let*)
2587
2588(defun byte-compile-progn (form)
2589 (byte-compile-body-do-effect (cdr form)))
2590
2591(defun byte-compile-prog1 (form)
2592 (byte-compile-form-do-effect (car (cdr form)))
2593 (byte-compile-body (cdr (cdr form)) t))
2594
2595(defun byte-compile-prog2 (form)
2596 (byte-compile-form (nth 1 form) t)
2597 (byte-compile-form-do-effect (nth 2 form))
2598 (byte-compile-body (cdr (cdr (cdr form))) t))
2599
2600(defmacro byte-compile-goto-if (cond discard tag)
2601 (` (byte-compile-goto
2602 (if (, cond)
2603 (if (, discard) 'byte-goto-if-not-nil 'byte-goto-if-not-nil-else-pop)
2604 (if (, discard) 'byte-goto-if-nil 'byte-goto-if-nil-else-pop))
2605 (, tag))))
2606
2607(defun byte-compile-if (form)
2608 (byte-compile-form (car (cdr form)))
2609 (if (null (nthcdr 3 form))
2610 ;; No else-forms
2611 (let ((donetag (byte-compile-make-tag)))
2612 (byte-compile-goto-if nil for-effect donetag)
2613 (byte-compile-form (nth 2 form) for-effect)
2614 (byte-compile-out-tag donetag))
2615 (let ((donetag (byte-compile-make-tag)) (elsetag (byte-compile-make-tag)))
2616 (byte-compile-goto 'byte-goto-if-nil elsetag)
2617 (byte-compile-form (nth 2 form) for-effect)
2618 (byte-compile-goto 'byte-goto donetag)
2619 (byte-compile-out-tag elsetag)
2620 (byte-compile-body (cdr (cdr (cdr form))) for-effect)
2621 (byte-compile-out-tag donetag)))
2622 (setq for-effect nil))
2623
2624(defun byte-compile-cond (clauses)
2625 (let ((donetag (byte-compile-make-tag))
2626 nexttag clause)
2627 (while (setq clauses (cdr clauses))
2628 (setq clause (car clauses))
2629 (cond ((or (eq (car clause) t)
2630 (and (eq (car-safe (car clause)) 'quote)
2631 (car-safe (cdr-safe (car clause)))))
2632 ;; Unconditional clause
2633 (setq clause (cons t clause)
2634 clauses nil))
2635 ((cdr clauses)
2636 (byte-compile-form (car clause))
2637 (if (null (cdr clause))
2638 ;; First clause is a singleton.
2639 (byte-compile-goto-if t for-effect donetag)
2640 (setq nexttag (byte-compile-make-tag))
2641 (byte-compile-goto 'byte-goto-if-nil nexttag)
2642 (byte-compile-body (cdr clause) for-effect)
2643 (byte-compile-goto 'byte-goto donetag)
2644 (byte-compile-out-tag nexttag)))))
2645 ;; Last clause
2646 (and (cdr clause) (not (eq (car clause) t))
2647 (progn (byte-compile-form (car clause))
2648 (byte-compile-goto-if nil for-effect donetag)
2649 (setq clause (cdr clause))))
2650 (byte-compile-body-do-effect clause)
2651 (byte-compile-out-tag donetag)))
2652
2653(defun byte-compile-and (form)
2654 (let ((failtag (byte-compile-make-tag))
2655 (args (cdr form)))
2656 (if (null args)
2657 (byte-compile-form-do-effect t)
2658 (while (cdr args)
2659 (byte-compile-form (car args))
2660 (byte-compile-goto-if nil for-effect failtag)
2661 (setq args (cdr args)))
2662 (byte-compile-form-do-effect (car args))
2663 (byte-compile-out-tag failtag))))
2664
2665(defun byte-compile-or (form)
2666 (let ((wintag (byte-compile-make-tag))
2667 (args (cdr form)))
2668 (if (null args)
2669 (byte-compile-form-do-effect nil)
2670 (while (cdr args)
2671 (byte-compile-form (car args))
2672 (byte-compile-goto-if t for-effect wintag)
2673 (setq args (cdr args)))
2674 (byte-compile-form-do-effect (car args))
2675 (byte-compile-out-tag wintag))))
2676
2677(defun byte-compile-while (form)
2678 (let ((endtag (byte-compile-make-tag))
2679 (looptag (byte-compile-make-tag)))
2680 (byte-compile-out-tag looptag)
2681 (byte-compile-form (car (cdr form)))
2682 (byte-compile-goto-if nil for-effect endtag)
2683 (byte-compile-body (cdr (cdr form)) t)
2684 (byte-compile-goto 'byte-goto looptag)
2685 (byte-compile-out-tag endtag)
2686 (setq for-effect nil)))
2687
2688(defun byte-compile-funcall (form)
2689 (mapcar 'byte-compile-form (cdr form))
2690 (byte-compile-out 'byte-call (length (cdr (cdr form)))))
2691
2692
2693(defun byte-compile-let (form)
2694 ;; First compute the binding values in the old scope.
2695 (let ((varlist (car (cdr form))))
2696 (while varlist
2697 (if (consp (car varlist))
2698 (byte-compile-form (car (cdr (car varlist))))
2699 (byte-compile-push-constant nil))
2700 (setq varlist (cdr varlist))))
2701 (let ((byte-compile-bound-variables byte-compile-bound-variables) ;new scope
2702 (varlist (reverse (car (cdr form)))))
2703 (while varlist
2704 (byte-compile-variable-ref 'byte-varbind (if (consp (car varlist))
2705 (car (car varlist))
2706 (car varlist)))
2707 (setq varlist (cdr varlist)))
2708 (byte-compile-body-do-effect (cdr (cdr form)))
2709 (byte-compile-out 'byte-unbind (length (car (cdr form))))))
2710
2711(defun byte-compile-let* (form)
2712 (let ((byte-compile-bound-variables byte-compile-bound-variables) ;new scope
2713 (varlist (copy-sequence (car (cdr form)))))
2714 (while varlist
2715 (if (atom (car varlist))
2716 (byte-compile-push-constant nil)
2717 (byte-compile-form (car (cdr (car varlist))))
2718 (setcar varlist (car (car varlist))))
2719 (byte-compile-variable-ref 'byte-varbind (car varlist))
2720 (setq varlist (cdr varlist)))
2721 (byte-compile-body-do-effect (cdr (cdr form)))
2722 (byte-compile-out 'byte-unbind (length (car (cdr form))))))
2723
2724
2725(byte-defop-compiler-1 /= byte-compile-negated)
2726(byte-defop-compiler-1 atom byte-compile-negated)
2727(byte-defop-compiler-1 nlistp byte-compile-negated)
2728
2729(put '/= 'byte-compile-negated-op '=)
2730(put 'atom 'byte-compile-negated-op 'consp)
2731(put 'nlistp 'byte-compile-negated-op 'listp)
2732
2733(defun byte-compile-negated (form)
2734 (byte-compile-form-do-effect (byte-compile-negation-optimizer form)))
2735
2736;; Even when optimization is off, /= is optimized to (not (= ...)).
2737(defun byte-compile-negation-optimizer (form)
2738 ;; an optimizer for forms where <form1> is less efficient than (not <form2>)
2739 (list 'not
2740 (cons (or (get (car form) 'byte-compile-negated-op)
2741 (error
52799cb8 2742 "Compiler error: `%s' has no `byte-compile-negated-op' property"
1c393159
JB
2743 (car form)))
2744 (cdr form))))
2745\f
2746;;; other tricky macro-like special-forms
2747
2748(byte-defop-compiler-1 catch)
2749(byte-defop-compiler-1 unwind-protect)
2750(byte-defop-compiler-1 condition-case)
2751(byte-defop-compiler-1 save-excursion)
2752(byte-defop-compiler-1 save-restriction)
2753(byte-defop-compiler-1 save-window-excursion)
2754(byte-defop-compiler-1 with-output-to-temp-buffer)
6e8d0db7 2755(byte-defop-compiler-1 track-mouse)
1c393159
JB
2756
2757(defun byte-compile-catch (form)
2758 (byte-compile-form (car (cdr form)))
2759 (byte-compile-push-constant
2760 (byte-compile-top-level (cons 'progn (cdr (cdr form))) for-effect))
2761 (byte-compile-out 'byte-catch 0))
2762
2763(defun byte-compile-unwind-protect (form)
2764 (byte-compile-push-constant
2765 (byte-compile-top-level-body (cdr (cdr form)) t))
2766 (byte-compile-out 'byte-unwind-protect 0)
2767 (byte-compile-form-do-effect (car (cdr form)))
2768 (byte-compile-out 'byte-unbind 1))
2769
6e8d0db7 2770(defun byte-compile-track-mouse (form)
d7846e08
RS
2771 (byte-compile-form
2772 (list
2773 'funcall
2774 (list 'quote
2775 (list 'lambda nil
2776 (cons 'track-mouse
2777 (byte-compile-top-level-body (cdr form))))))))
6e8d0db7 2778
1c393159
JB
2779(defun byte-compile-condition-case (form)
2780 (let* ((var (nth 1 form))
2781 (byte-compile-bound-variables
2782 (if var (cons var byte-compile-bound-variables)
2783 byte-compile-bound-variables)))
2784 (or (symbolp var)
2785 (byte-compile-warn
2786 "%s is not a variable-name or nil (in condition-case)" var))
2787 (byte-compile-push-constant var)
2788 (byte-compile-push-constant (byte-compile-top-level
2789 (nth 2 form) for-effect))
2790 (let ((clauses (cdr (cdr (cdr form))))
2791 compiled-clauses)
2792 (while clauses
e27c3564
JB
2793 (let* ((clause (car clauses))
2794 (condition (car clause)))
2abcddce
RS
2795 (cond ((not (or (symbolp condition)
2796 (and (listp condition)
2797 (let ((syms condition) (ok t))
2798 (while syms
2799 (if (not (symbolp (car syms)))
2800 (setq ok nil))
2801 (setq syms (cdr syms)))
2802 ok))))
e27c3564 2803 (byte-compile-warn
2abcddce 2804 "%s is not a condition name or list of such (in condition-case)"
e27c3564 2805 (prin1-to-string condition)))
2abcddce
RS
2806;; ((not (or (eq condition 't)
2807;; (and (stringp (get condition 'error-message))
2808;; (consp (get condition 'error-conditions)))))
2809;; (byte-compile-warn
2810;; "%s is not a known condition name (in condition-case)"
2811;; condition))
2812 )
1c393159 2813 (setq compiled-clauses
e27c3564 2814 (cons (cons condition
1c393159
JB
2815 (byte-compile-top-level-body
2816 (cdr clause) for-effect))
2817 compiled-clauses)))
2818 (setq clauses (cdr clauses)))
2819 (byte-compile-push-constant (nreverse compiled-clauses)))
2820 (byte-compile-out 'byte-condition-case 0)))
2821
2822
2823(defun byte-compile-save-excursion (form)
2824 (byte-compile-out 'byte-save-excursion 0)
2825 (byte-compile-body-do-effect (cdr form))
2826 (byte-compile-out 'byte-unbind 1))
2827
2828(defun byte-compile-save-restriction (form)
2829 (byte-compile-out 'byte-save-restriction 0)
2830 (byte-compile-body-do-effect (cdr form))
2831 (byte-compile-out 'byte-unbind 1))
2832
2833(defun byte-compile-save-window-excursion (form)
2834 (byte-compile-push-constant
2835 (byte-compile-top-level-body (cdr form) for-effect))
2836 (byte-compile-out 'byte-save-window-excursion 0))
2837
2838(defun byte-compile-with-output-to-temp-buffer (form)
2839 (byte-compile-form (car (cdr form)))
2840 (byte-compile-out 'byte-temp-output-buffer-setup 0)
2841 (byte-compile-body (cdr (cdr form)))
2842 (byte-compile-out 'byte-temp-output-buffer-show 0))
2843
2844\f
2845;;; top-level forms elsewhere
2846
2847(byte-defop-compiler-1 defun)
2848(byte-defop-compiler-1 defmacro)
2849(byte-defop-compiler-1 defvar)
2850(byte-defop-compiler-1 defconst byte-compile-defvar)
2851(byte-defop-compiler-1 autoload)
2852(byte-defop-compiler-1 lambda byte-compile-lambda-form)
5286a842 2853(byte-defop-compiler-1 defalias)
1c393159
JB
2854
2855(defun byte-compile-defun (form)
2856 ;; This is not used for file-level defuns with doc strings.
2857 (byte-compile-two-args ; Use this to avoid byte-compile-fset's warning.
2858 (list 'fset (list 'quote (nth 1 form))
2859 (byte-compile-byte-code-maker
2860 (byte-compile-lambda (cons 'lambda (cdr (cdr form)))))))
2861 (byte-compile-discard)
2862 (byte-compile-constant (nth 1 form)))
2863
2864(defun byte-compile-defmacro (form)
2865 ;; This is not used for file-level defmacros with doc strings.
2866 (byte-compile-body-do-effect
2867 (list (list 'fset (list 'quote (nth 1 form))
2868 (let ((code (byte-compile-byte-code-maker
2869 (byte-compile-lambda
2870 (cons 'lambda (cdr (cdr form)))))))
2871 (if (eq (car-safe code) 'make-byte-code)
2872 (list 'cons ''macro code)
2873 (list 'quote (cons 'macro (eval code))))))
2874 (list 'quote (nth 1 form)))))
2875
2876(defun byte-compile-defvar (form)
2877 ;; This is not used for file-level defvar/consts with doc strings.
2878 (let ((var (nth 1 form))
2879 (value (nth 2 form))
2880 (string (nth 3 form)))
2881 (if (memq 'free-vars byte-compile-warnings)
2882 (setq byte-compile-bound-variables
2883 (cons var byte-compile-bound-variables)))
2884 (byte-compile-body-do-effect
2885 (list (if (cdr (cdr form))
2886 (if (eq (car form) 'defconst)
2887 (list 'setq var value)
2888 (list 'or (list 'boundp (list 'quote var))
2889 (list 'setq var value))))
2890 (if string
2891 (list 'put (list 'quote var) ''variable-documentation string))
2892 (list 'quote var)))))
2893
2894(defun byte-compile-autoload (form)
2895 (and (byte-compile-constp (nth 1 form))
2896 (byte-compile-constp (nth 5 form))
2897 (eval (nth 5 form)) ; macro-p
2898 (not (fboundp (eval (nth 1 form))))
2899 (byte-compile-warn
2900 "The compiler ignores `autoload' except at top level. You should
2901 probably put the autoload of the macro `%s' at top-level."
2902 (eval (nth 1 form))))
2903 (byte-compile-normal-call form))
2904
2905;; Lambda's in valid places are handled as special cases by various code.
2906;; The ones that remain are errors.
2907(defun byte-compile-lambda-form (form)
2908 (error "`lambda' used as function name is invalid"))
2909
5286a842
RS
2910;; Compile normally, but deal with warnings for the function being defined.
2911(defun byte-compile-defalias (form)
2912 (if (and (consp (cdr form)) (consp (nth 1 form))
2913 (eq (car (nth 1 form)) 'quote)
2914 (consp (cdr (nth 1 form)))
2915 (symbolp (nth 1 (nth 1 form)))
2916 (consp (nthcdr 2 form))
2917 (consp (nth 2 form))
2918 (eq (car (nth 2 form)) 'quote)
2919 (consp (cdr (nth 2 form)))
2920 (symbolp (nth 1 (nth 2 form))))
2921 (progn
2922 (byte-compile-defalias-warn (nth 1 (nth 1 form))
2923 (nth 1 (nth 2 form)))
2924 (setq byte-compile-function-environment
2925 (cons (cons (nth 1 (nth 1 form))
2926 (nth 1 (nth 2 form)))
2927 byte-compile-function-environment))))
b3848c28 2928 (byte-compile-normal-call form))
5286a842
RS
2929
2930;; Turn off warnings about prior calls to the function being defalias'd.
2931;; This could be smarter and compare those calls with
2932;; the function it is being aliased to.
2933(defun byte-compile-defalias-warn (new alias)
2934 (let ((calls (assq new byte-compile-unresolved-functions)))
2935 (if calls
2936 (setq byte-compile-unresolved-functions
2937 (delq calls byte-compile-unresolved-functions)))))
1c393159
JB
2938\f
2939;;; tags
2940
2941;; Note: Most operations will strip off the 'TAG, but it speeds up
2942;; optimization to have the 'TAG as a part of the tag.
2943;; Tags will be (TAG . (tag-number . stack-depth)).
2944(defun byte-compile-make-tag ()
2945 (list 'TAG (setq byte-compile-tag-number (1+ byte-compile-tag-number))))
2946
2947
2948(defun byte-compile-out-tag (tag)
2949 (setq byte-compile-output (cons tag byte-compile-output))
2950 (if (cdr (cdr tag))
2951 (progn
2952 ;; ## remove this someday
2953 (and byte-compile-depth
2954 (not (= (cdr (cdr tag)) byte-compile-depth))
52799cb8 2955 (error "Compiler bug: depth conflict at tag %d" (car (cdr tag))))
1c393159
JB
2956 (setq byte-compile-depth (cdr (cdr tag))))
2957 (setcdr (cdr tag) byte-compile-depth)))
2958
2959(defun byte-compile-goto (opcode tag)
2960 (setq byte-compile-output (cons (cons opcode tag) byte-compile-output))
2961 (setcdr (cdr tag) (if (memq opcode byte-goto-always-pop-ops)
2962 (1- byte-compile-depth)
2963 byte-compile-depth))
2964 (setq byte-compile-depth (and (not (eq opcode 'byte-goto))
2965 (1- byte-compile-depth))))
2966
2967(defun byte-compile-out (opcode offset)
2968 (setq byte-compile-output (cons (cons opcode offset) byte-compile-output))
2969 (cond ((eq opcode 'byte-call)
2970 (setq byte-compile-depth (- byte-compile-depth offset)))
2971 ((eq opcode 'byte-return)
2972 ;; This is actually an unnecessary case, because there should be
2973 ;; no more opcodes behind byte-return.
2974 (setq byte-compile-depth nil))
2975 (t
2976 (setq byte-compile-depth (+ byte-compile-depth
2977 (or (aref byte-stack+-info
2978 (symbol-value opcode))
2979 (- (1- offset))))
2980 byte-compile-maxdepth (max byte-compile-depth
2981 byte-compile-maxdepth))))
52799cb8 2982 ;;(if (< byte-compile-depth 0) (error "Compiler error: stack underflow"))
1c393159
JB
2983 )
2984
2985\f
2986;;; call tree stuff
2987
2988(defun byte-compile-annotate-call-tree (form)
2989 (let (entry)
2990 ;; annotate the current call
2991 (if (setq entry (assq (car form) byte-compile-call-tree))
2992 (or (memq byte-compile-current-form (nth 1 entry)) ;callers
2993 (setcar (cdr entry)
2994 (cons byte-compile-current-form (nth 1 entry))))
2995 (setq byte-compile-call-tree
2996 (cons (list (car form) (list byte-compile-current-form) nil)
2997 byte-compile-call-tree)))
2998 ;; annotate the current function
2999 (if (setq entry (assq byte-compile-current-form byte-compile-call-tree))
3000 (or (memq (car form) (nth 2 entry)) ;called
3001 (setcar (cdr (cdr entry))
3002 (cons (car form) (nth 2 entry))))
3003 (setq byte-compile-call-tree
3004 (cons (list byte-compile-current-form nil (list (car form)))
3005 byte-compile-call-tree)))
3006 ))
3007
52799cb8
RS
3008;; Renamed from byte-compile-report-call-tree
3009;; to avoid interfering with completion of byte-compile-file.
fd5285f3 3010;;;###autoload
52799cb8
RS
3011(defun display-call-tree (&optional filename)
3012 "Display a call graph of a specified file.
3013This lists which functions have been called, what functions called
3014them, and what functions they call. The list includes all functions
3015whose definitions have been compiled in this Emacs session, as well as
3016all functions called by those functions.
1c393159 3017
52799cb8
RS
3018The call graph does not include macros, inline functions, or
3019primitives that the byte-code interpreter knows about directly \(eq,
3020cons, etc.\).
1c393159
JB
3021
3022The call tree also lists those functions which are not known to be called
52799cb8
RS
3023\(that is, to which no calls have been compiled\), and which cannot be
3024invoked interactively."
1c393159
JB
3025 (interactive)
3026 (message "Generating call tree...")
3027 (with-output-to-temp-buffer "*Call-Tree*"
3028 (set-buffer "*Call-Tree*")
3029 (erase-buffer)
3030 (message "Generating call tree (sorting on %s)..."
3031 byte-compile-call-tree-sort)
3032 (insert "Call tree for "
3033 (cond ((null byte-compile-current-file) (or filename "???"))
3034 ((stringp byte-compile-current-file)
3035 byte-compile-current-file)
3036 (t (buffer-name byte-compile-current-file)))
3037 " sorted on "
3038 (prin1-to-string byte-compile-call-tree-sort)
3039 ":\n\n")
3040 (if byte-compile-call-tree-sort
3041 (setq byte-compile-call-tree
3042 (sort byte-compile-call-tree
3043 (cond ((eq byte-compile-call-tree-sort 'callers)
3044 (function (lambda (x y) (< (length (nth 1 x))
3045 (length (nth 1 y))))))
3046 ((eq byte-compile-call-tree-sort 'calls)
3047 (function (lambda (x y) (< (length (nth 2 x))
3048 (length (nth 2 y))))))
3049 ((eq byte-compile-call-tree-sort 'calls+callers)
3050 (function (lambda (x y) (< (+ (length (nth 1 x))
3051 (length (nth 2 x)))
3052 (+ (length (nth 1 y))
3053 (length (nth 2 y)))))))
3054 ((eq byte-compile-call-tree-sort 'name)
3055 (function (lambda (x y) (string< (car x)
3056 (car y)))))
52799cb8 3057 (t (error "`byte-compile-call-tree-sort': `%s' - unknown sort mode"
1c393159
JB
3058 byte-compile-call-tree-sort))))))
3059 (message "Generating call tree...")
3060 (let ((rest byte-compile-call-tree)
3061 (b (current-buffer))
3062 f p
3063 callers calls)
3064 (while rest
3065 (prin1 (car (car rest)) b)
3066 (setq callers (nth 1 (car rest))
3067 calls (nth 2 (car rest)))
3068 (insert "\t"
3069 (cond ((not (fboundp (setq f (car (car rest)))))
3070 (if (null f)
3071 " <top level>";; shouldn't insert nil then, actually -sk
3072 " <not defined>"))
3073 ((subrp (setq f (symbol-function f)))
3074 " <subr>")
3075 ((symbolp f)
3076 (format " ==> %s" f))
ed015bdd 3077 ((byte-code-function-p f)
1c393159
JB
3078 "<compiled function>")
3079 ((not (consp f))
3080 "<malformed function>")
3081 ((eq 'macro (car f))
ed015bdd 3082 (if (or (byte-code-function-p (cdr f))
1c393159
JB
3083 (assq 'byte-code (cdr (cdr (cdr f)))))
3084 " <compiled macro>"
3085 " <macro>"))
3086 ((assq 'byte-code (cdr (cdr f)))
3087 "<compiled lambda>")
3088 ((eq 'lambda (car f))
3089 "<function>")
3090 (t "???"))
3091 (format " (%d callers + %d calls = %d)"
3092 ;; Does the optimizer eliminate common subexpressions?-sk
3093 (length callers)
3094 (length calls)
3095 (+ (length callers) (length calls)))
3096 "\n")
3097 (if callers
3098 (progn
3099 (insert " called by:\n")
3100 (setq p (point))
3101 (insert " " (if (car callers)
3102 (mapconcat 'symbol-name callers ", ")
3103 "<top level>"))
3104 (let ((fill-prefix " "))
3105 (fill-region-as-paragraph p (point)))))
3106 (if calls
3107 (progn
3108 (insert " calls:\n")
3109 (setq p (point))
3110 (insert " " (mapconcat 'symbol-name calls ", "))
3111 (let ((fill-prefix " "))
3112 (fill-region-as-paragraph p (point)))))
3113 (insert "\n")
3114 (setq rest (cdr rest)))
3115
3116 (message "Generating call tree...(finding uncalled functions...)")
3117 (setq rest byte-compile-call-tree)
3118 (let ((uncalled nil))
3119 (while rest
3120 (or (nth 1 (car rest))
3121 (null (setq f (car (car rest))))
3122 (byte-compile-fdefinition f t)
3123 (commandp (byte-compile-fdefinition f nil))
3124 (setq uncalled (cons f uncalled)))
3125 (setq rest (cdr rest)))
3126 (if uncalled
3127 (let ((fill-prefix " "))
3128 (insert "Noninteractive functions not known to be called:\n ")
3129 (setq p (point))
3130 (insert (mapconcat 'symbol-name (nreverse uncalled) ", "))
3131 (fill-region-as-paragraph p (point)))))
3132 )
3133 (message "Generating call tree...done.")
3134 ))
3135
3136\f
3137;;; by crl@newton.purdue.edu
3138;;; Only works noninteractively.
fd5285f3 3139;;;###autoload
1c393159 3140(defun batch-byte-compile ()
52799cb8
RS
3141 "Run `byte-compile-file' on the files remaining on the command line.
3142Use this from the command line, with `-batch';
3143it won't work in an interactive Emacs.
3144Each file is processed even if an error occurred previously.
1c393159
JB
3145For example, invoke \"emacs -batch -f batch-byte-compile $emacs/ ~/*.el\""
3146 ;; command-line-args-left is what is left of the command line (from startup.el)
3147 (defvar command-line-args-left) ;Avoid 'free variable' warning
3148 (if (not noninteractive)
52799cb8 3149 (error "`batch-byte-compile' is to be used only with -batch"))
1c393159
JB
3150 (let ((error nil))
3151 (while command-line-args-left
3152 (if (file-directory-p (expand-file-name (car command-line-args-left)))
3153 (let ((files (directory-files (car command-line-args-left)))
3154 source dest)
3155 (while files
52799cb8 3156 (if (and (string-match emacs-lisp-file-regexp (car files))
1c393159
JB
3157 (not (auto-save-file-name-p (car files)))
3158 (setq source (expand-file-name (car files)
3159 (car command-line-args-left)))
3160 (setq dest (byte-compile-dest-file source))
3161 (file-exists-p dest)
3162 (file-newer-than-file-p source dest))
3163 (if (null (batch-byte-compile-file source))
3164 (setq error t)))
3165 (setq files (cdr files))))
3166 (if (null (batch-byte-compile-file (car command-line-args-left)))
3167 (setq error t)))
3168 (setq command-line-args-left (cdr command-line-args-left)))
3169 (message "Done")
3170 (kill-emacs (if error 1 0))))
3171
3172(defun batch-byte-compile-file (file)
3173 (condition-case err
3174 (progn (byte-compile-file file) t)
3175 (error
3176 (message (if (cdr err)
3177 ">>Error occurred processing %s: %s (%s)"
3178 ">>Error occurred processing %s: %s")
3179 file
3180 (get (car err) 'error-message)
3181 (prin1-to-string (cdr err)))
3182 nil)))
3183
e9681c45 3184;;;###autoload
e27c3564
JB
3185(defun batch-byte-recompile-directory ()
3186 "Runs `byte-recompile-directory' on the dirs remaining on the command line.
79c6071d
RS
3187Must be used only with `-batch', and kills Emacs on completion.
3188For example, invoke `emacs -batch -f batch-byte-recompile-directory .'."
e27c3564
JB
3189 ;; command-line-args-left is what is left of the command line (startup.el)
3190 (defvar command-line-args-left) ;Avoid 'free variable' warning
3191 (if (not noninteractive)
3192 (error "batch-byte-recompile-directory is to be used only with -batch"))
3193 (or command-line-args-left
3194 (setq command-line-args-left '(".")))
3195 (while command-line-args-left
3196 (byte-recompile-directory (car command-line-args-left))
3197 (setq command-line-args-left (cdr command-line-args-left)))
3198 (kill-emacs 0))
3199
1c393159 3200
1c393159
JB
3201(make-obsolete 'dot 'point)
3202(make-obsolete 'dot-max 'point-max)
3203(make-obsolete 'dot-min 'point-min)
3204(make-obsolete 'dot-marker 'point-marker)
3205
52799cb8
RS
3206(make-obsolete 'buffer-flush-undo 'buffer-disable-undo)
3207(make-obsolete 'baud-rate "use the baud-rate variable instead")
ed015bdd 3208(make-obsolete 'compiled-function-p 'byte-code-function-p)
9e2b097b
JB
3209(make-obsolete-variable 'auto-fill-hook 'auto-fill-function)
3210(make-obsolete-variable 'blink-paren-hook 'blink-paren-function)
3211(make-obsolete-variable 'lisp-indent-hook 'lisp-indent-function)
3212(make-obsolete-variable 'temp-buffer-show-hook
3213 'temp-buffer-show-function)
3214(make-obsolete-variable 'inhibit-local-variables
3215 "use enable-local-variables (with the reversed sense.)")
79d52eea 3216(make-obsolete-variable 'unread-command-char
ed015bdd
JB
3217 "use unread-command-events instead. That variable is a list of events to reread, so it now uses nil to mean `no event', instead of -1.")
3218(make-obsolete-variable 'unread-command-event
3219 "use unread-command-events; this is now a list of events.")
f3341900 3220(make-obsolete-variable 'suspend-hooks 'suspend-hook)
ec9a76e3 3221(make-obsolete-variable 'comment-indent-hook 'comment-indent-function)
f3341900 3222(make-obsolete-variable 'meta-flag "Use the set-input-mode function instead.")
1c393159
JB
3223
3224(provide 'byte-compile)
200503bb 3225(provide 'bytecomp)
1c393159
JB
3226
3227\f
3228;;; report metering (see the hacks in bytecode.c)
3229
52799cb8
RS
3230(defun byte-compile-report-ops ()
3231 (defvar byte-code-meter)
3232 (with-output-to-temp-buffer "*Meter*"
3233 (set-buffer "*Meter*")
3234 (let ((i 0) n op off)
3235 (while (< i 256)
3236 (setq n (aref (aref byte-code-meter 0) i)
3237 off nil)
3238 (if t ;(not (zerop n))
3239 (progn
3240 (setq op i)
3241 (setq off nil)
3242 (cond ((< op byte-nth)
3243 (setq off (logand op 7))
3244 (setq op (logand op 248)))
3245 ((>= op byte-constant)
3246 (setq off (- op byte-constant)
3247 op byte-constant)))
3248 (setq op (aref byte-code-vector op))
3249 (insert (format "%-4d" i))
3250 (insert (symbol-name op))
3251 (if off (insert " [" (int-to-string off) "]"))
3252 (indent-to 40)
3253 (insert (int-to-string n) "\n")))
3254 (setq i (1+ i))))))
1c393159
JB
3255\f
3256;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when bytecomp compiles
3257;; itself, compile some of its most used recursive functions (at load time).
3258;;
3259(eval-when-compile
ed015bdd 3260 (or (byte-code-function-p (symbol-function 'byte-compile-form))
1c393159
JB
3261 (assq 'byte-code (symbol-function 'byte-compile-form))
3262 (let ((byte-optimize nil) ; do it fast
3263 (byte-compile-warnings nil))
3264 (mapcar '(lambda (x)
3265 (or noninteractive (message "compiling %s..." x))
3266 (byte-compile x)
3267 (or noninteractive (message "compiling %s...done" x)))
3268 '(byte-compile-normal-call
3269 byte-compile-form
3270 byte-compile-body
3271 ;; Inserted some more than necessary, to speed it up.
3272 byte-compile-top-level
3273 byte-compile-out-toplevel
3274 byte-compile-constant
3275 byte-compile-variable-ref))))
3276 nil)
fd5285f3
RS
3277
3278;;; bytecomp.el ends here