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