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