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