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