209
|
1 ;;; bytecomp.el --- compilation of Lisp code into byte code.
|
|
2
|
|
3 ;;; Copyright (C) 1985-1987, 1991-1994 Free Software Foundation, Inc.
|
|
4 ;;; Copyright (C) 1996 Ben Wing.
|
|
5
|
367
|
6 ;; Authors: Jamie Zawinski <jwz@jwz.org>
|
|
7 ;; Hallvard Furuseth <hbf@ulrik.uio.no>
|
|
8 ;; Ben Wing <ben@xemacs.org>
|
|
9 ;; Martin Buchholz <martin@xemacs.org>
|
|
10 ;; Richard Stallman <rms@gnu.org>
|
|
11 ;; Keywords: internal lisp
|
|
12
|
|
13 (defconst byte-compile-version (purecopy "2.27 XEmacs; 2000-09-12."))
|
209
|
14
|
|
15 ;; This file is part of XEmacs.
|
|
16
|
|
17 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
18 ;; under the terms of the GNU General Public License as published by
|
|
19 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
20 ;; any later version.
|
|
21
|
|
22 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
23 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
25 ;; General Public License for more details.
|
|
26
|
|
27 ;; You should have received a copy of the GNU General Public License
|
272
|
28 ;; along with XEmacs; see the file COPYING. If not, write to the
|
209
|
29 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
30 ;; Boston, MA 02111-1307, USA.
|
|
31
|
|
32 ;;; Synched up with: FSF 19.30.
|
|
33
|
|
34 ;;; Commentary:
|
|
35
|
284
|
36 ;; The Emacs Lisp byte compiler. This crunches lisp source into a
|
367
|
37 ;; sort of p-code (`bytecode') which takes up less space and can be
|
|
38 ;; interpreted faster. First, the source code forms are converted to
|
|
39 ;; an intermediate form, `lapcode' [`LAP' == `Lisp Assembly Program']
|
|
40 ;; which is much easier to manipulate than bytecode. Then the lapcode
|
|
41 ;; is converted to bytecode, which can be considered to be actual
|
|
42 ;; machine language. Optimizations can occur at either the source
|
|
43 ;; level or the lapcode level.
|
|
44
|
|
45 ;; The user entry points are byte-compile-file,
|
284
|
46 ;; byte-recompile-directory and byte-compile-buffer.
|
209
|
47
|
|
48 ;;; Code:
|
|
49
|
|
50 ;;; ========================================================================
|
|
51 ;;; Entry points:
|
|
52 ;;; byte-recompile-directory, byte-compile-file,
|
|
53 ;;; batch-byte-compile, batch-byte-recompile-directory,
|
|
54 ;;; byte-compile, compile-defun,
|
|
55 ;;; display-call-tree
|
|
56 ;;; RMS says:
|
|
57 ;;; (byte-compile-buffer and byte-compile-and-load-file were turned off
|
|
58 ;;; because they are not terribly useful and get in the way of completion.)
|
|
59 ;;; But I'm leaving them. --ben
|
|
60
|
|
61 ;;; This version of the byte compiler has the following improvements:
|
|
62 ;;; + optimization of compiled code:
|
|
63 ;;; - removal of unreachable code;
|
|
64 ;;; - removal of calls to side-effectless functions whose return-value
|
|
65 ;;; is unused;
|
|
66 ;;; - compile-time evaluation of safe constant forms, such as (consp nil)
|
|
67 ;;; and (ash 1 6);
|
|
68 ;;; - open-coding of literal lambdas;
|
|
69 ;;; - peephole optimization of emitted code;
|
|
70 ;;; - trivial functions are left uncompiled for speed.
|
|
71 ;;; + support for inline functions;
|
|
72 ;;; + compile-time evaluation of arbitrary expressions;
|
|
73 ;;; + compile-time warning messages for:
|
|
74 ;;; - functions being redefined with incompatible arglists;
|
|
75 ;;; - functions being redefined as macros, or vice-versa;
|
|
76 ;;; - functions or macros defined multiple times in the same file;
|
|
77 ;;; - functions being called with the incorrect number of arguments;
|
272
|
78 ;;; - functions being called which are not defined globally, in the
|
209
|
79 ;;; file, or as autoloads;
|
|
80 ;;; - assignment and reference of undeclared free variables;
|
|
81 ;;; - various syntax errors;
|
|
82 ;;; + correct compilation of nested defuns, defmacros, defvars and defsubsts;
|
|
83 ;;; + correct compilation of top-level uses of macros;
|
|
84 ;;; + the ability to generate a histogram of functions called.
|
|
85
|
|
86 ;;; User customization variables:
|
|
87 ;;;
|
|
88 ;;; byte-compile-verbose Whether to report the function currently being
|
|
89 ;;; compiled in the minibuffer;
|
272
|
90 ;;; byte-optimize Whether to do optimizations; this may be
|
209
|
91 ;;; t, nil, 'source, or 'byte;
|
272
|
92 ;;; byte-optimize-log Whether to report (in excruciating detail)
|
209
|
93 ;;; exactly which optimizations have been made.
|
|
94 ;;; This may be t, nil, 'source, or 'byte;
|
|
95 ;;; byte-compile-error-on-warn Whether to stop compilation when a warning is
|
|
96 ;;; produced;
|
|
97 ;;; byte-compile-delete-errors Whether the optimizer may delete calls or
|
|
98 ;;; variable references that are side-effect-free
|
|
99 ;;; except that they may return an error.
|
|
100 ;;; byte-compile-generate-call-tree Whether to generate a histogram of
|
272
|
101 ;;; function calls. This can be useful for
|
209
|
102 ;;; finding unused functions, as well as simple
|
|
103 ;;; performance metering.
|
|
104 ;;; byte-compile-warnings List of warnings to issue, or t. May contain
|
|
105 ;;; 'free-vars (references to variables not in the
|
|
106 ;;; current lexical scope)
|
|
107 ;;; 'unused-vars (non-global variables bound but
|
|
108 ;;; not referenced)
|
|
109 ;;; 'unresolved (calls to unknown functions)
|
|
110 ;;; 'callargs (lambda calls with args that don't
|
|
111 ;;; match the lambda's definition)
|
|
112 ;;; 'redefine (function cell redefined from
|
|
113 ;;; a macro to a lambda or vice versa,
|
|
114 ;;; or redefined to take other args)
|
|
115 ;;; 'obsolete (obsolete variables and functions)
|
|
116 ;;; 'pedantic (references to Emacs-compatible
|
|
117 ;;; symbols)
|
|
118 ;;; byte-compile-emacs19-compatibility Whether the compiler should
|
|
119 ;;; generate .elc files which can be loaded into
|
|
120 ;;; generic emacs 19.
|
|
121 ;;; emacs-lisp-file-regexp Regexp for the extension of source-files;
|
|
122 ;;; see also the function byte-compile-dest-file.
|
|
123 ;;; byte-compile-overwrite-file If nil, delete old .elc files before saving.
|
|
124 ;;;
|
|
125 ;;; Most of the above parameters can also be set on a file-by-file basis; see
|
|
126 ;;; the documentation of the `byte-compiler-options' macro.
|
|
127
|
|
128 ;;; New Features:
|
|
129 ;;;
|
|
130 ;;; o The form `defsubst' is just like `defun', except that the function
|
|
131 ;;; generated will be open-coded in compiled code which uses it. This
|
|
132 ;;; means that no function call will be generated, it will simply be
|
|
133 ;;; spliced in. Lisp functions calls are very slow, so this can be a
|
|
134 ;;; big win.
|
|
135 ;;;
|
|
136 ;;; You can generally accomplish the same thing with `defmacro', but in
|
|
137 ;;; that case, the defined procedure can't be used as an argument to
|
|
138 ;;; mapcar, etc.
|
|
139 ;;;
|
|
140 ;;; o You can make a given function be inline even if it has already been
|
|
141 ;;; defined with `defun' by using the `proclaim-inline' form like so:
|
|
142 ;;; (proclaim-inline my-function)
|
|
143 ;;; This is, in fact, exactly what `defsubst' does. To make a function no
|
|
144 ;;; longer be inline, you must use `proclaim-notinline'. Beware that if
|
272
|
145 ;;; you define a function with `defsubst' and later redefine it with
|
209
|
146 ;;; `defun', it will still be open-coded until you use proclaim-notinline.
|
|
147 ;;;
|
|
148 ;;; o You can also open-code one particular call to a function without
|
|
149 ;;; open-coding all calls. Use the 'inline' form to do this, like so:
|
|
150 ;;;
|
|
151 ;;; (inline (foo 1 2 3)) ;; `foo' will be open-coded
|
|
152 ;;; or...
|
272
|
153 ;;; (inline ;; `foo' and `baz' will be
|
209
|
154 ;;; (foo 1 2 3 (bar 5)) ;; open-coded, but `bar' will not.
|
|
155 ;;; (baz 0))
|
|
156 ;;;
|
|
157 ;;; o It is possible to open-code a function in the same file it is defined
|
|
158 ;;; in without having to load that file before compiling it. the
|
|
159 ;;; byte-compiler has been modified to remember function definitions in
|
|
160 ;;; the compilation environment in the same way that it remembers macro
|
|
161 ;;; definitions.
|
|
162 ;;;
|
|
163 ;;; o Forms like ((lambda ...) ...) are open-coded.
|
|
164 ;;;
|
|
165 ;;; o The form `eval-when-compile' is like progn, except that the body
|
|
166 ;;; is evaluated at compile-time. When it appears at top-level, this
|
|
167 ;;; is analogous to the Common Lisp idiom (eval-when (compile) ...).
|
|
168 ;;; When it does not appear at top-level, it is similar to the
|
|
169 ;;; Common Lisp #. reader macro (but not in interpreted code).
|
|
170 ;;;
|
|
171 ;;; o The form `eval-and-compile' is similar to eval-when-compile, but
|
|
172 ;;; the whole form is evalled both at compile-time and at run-time.
|
|
173 ;;;
|
|
174 ;;; o The command M-x byte-compile-and-load-file does what you'd think.
|
|
175 ;;;
|
|
176 ;;; o The command compile-defun is analogous to eval-defun.
|
|
177 ;;;
|
272
|
178 ;;; o If you run byte-compile-file on a filename which is visited in a
|
209
|
179 ;;; buffer, and that buffer is modified, you are asked whether you want
|
|
180 ;;; to save the buffer before compiling.
|
|
181 ;;;
|
|
182 ;;; o You can add this to /etc/magic to make file(1) recognise the files
|
|
183 ;;; generated by this compiler:
|
|
184 ;;;
|
|
185 ;;; 0 string ;ELC GNU Emacs Lisp compiled file,
|
|
186 ;;; >4 byte x version %d
|
|
187 ;;;
|
|
188 ;;; TO DO:
|
|
189 ;;;
|
|
190 ;;; o Should implement declarations and proclamations, notably special,
|
|
191 ;;; unspecial, and ignore. Do this in such a way as to not break cl.el.
|
|
192 ;;; o The bound-but-not-used warnings are not issued for variables whose
|
|
193 ;;; bindings were established in the arglist, due to the lack of an
|
|
194 ;;; ignore declaration. Once ignore exists, this should be turned on.
|
|
195 ;;; o Warn about functions and variables defined but not used?
|
|
196 ;;; Maybe add some kind of `export' declaration for this?
|
|
197 ;;; (With interactive functions being automatically exported?)
|
|
198 ;;; o Any reference to a variable, even one which is a no-op, will cause
|
|
199 ;;; the warning not to be given. Possibly we could use the for-effect
|
|
200 ;;; flag to determine when this reference is useless; possibly more
|
|
201 ;;; complex flow analysis would be necessary.
|
|
202 ;;; o If the optimizer deletes a variable reference, we might be left with
|
|
203 ;;; a bound-but-not-referenced warning. Generally this is ok, but not if
|
|
204 ;;; it's a synergistic result of macroexpansion. Need some way to note
|
|
205 ;;; that a varref is being optimized away? Of course it would be nice to
|
|
206 ;;; optimize away the binding too, someday, but it's unsafe today.
|
|
207 ;;; o (See byte-optimize.el for the optimization TODO list.)
|
|
208
|
|
209 (require 'backquote)
|
|
210
|
|
211 (or (fboundp 'defsubst)
|
|
212 ;; This really ought to be loaded already!
|
|
213 (load-library "bytecomp-runtime"))
|
|
214
|
|
215 (eval-when-compile
|
|
216 (defvar byte-compile-single-version nil
|
|
217 "If this is true, the choice of emacs version (v19 or v20) byte-codes will
|
|
218 be hard-coded into bytecomp when it compiles itself. If the compiler itself
|
|
219 is compiled with optimization, this causes a speedup.")
|
|
220
|
|
221 (cond (byte-compile-single-version
|
|
222 (defmacro byte-compile-single-version () t)
|
|
223 (defmacro byte-compile-version-cond (cond) (list 'quote (eval cond))))
|
|
224 (t
|
|
225 (defmacro byte-compile-single-version () nil)
|
|
226 (defmacro byte-compile-version-cond (cond) cond)))
|
|
227 )
|
|
228
|
|
229 (defvar emacs-lisp-file-regexp (if (eq system-type 'vax-vms)
|
|
230 (purecopy "\\.EL\\(;[0-9]+\\)?$")
|
|
231 (purecopy "\\.el$"))
|
|
232 "*Regexp which matches Emacs Lisp source files.
|
|
233 You may want to redefine `byte-compile-dest-file' if you change this.")
|
|
234
|
|
235 ;; This enables file name handlers such as jka-compr
|
|
236 ;; to remove parts of the file name that should not be copied
|
|
237 ;; through to the output file name.
|
|
238 (defun byte-compiler-base-file-name (filename)
|
|
239 (let ((handler (find-file-name-handler filename
|
|
240 'byte-compiler-base-file-name)))
|
|
241 (if handler
|
|
242 (funcall handler 'byte-compiler-base-file-name filename)
|
|
243 filename)))
|
|
244
|
|
245 (or (fboundp 'byte-compile-dest-file)
|
|
246 ;; The user may want to redefine this along with emacs-lisp-file-regexp,
|
|
247 ;; so only define it if it is undefined.
|
|
248 (defun byte-compile-dest-file (filename)
|
|
249 "Convert an Emacs Lisp source file name to a compiled file name."
|
|
250 (setq filename (byte-compiler-base-file-name filename))
|
|
251 (setq filename (file-name-sans-versions filename))
|
|
252 (cond ((eq system-type 'vax-vms)
|
|
253 (concat (substring filename 0 (string-match ";" filename)) "c"))
|
|
254 ((string-match emacs-lisp-file-regexp filename)
|
|
255 (concat (substring filename 0 (match-beginning 0)) ".elc"))
|
|
256 (t (concat filename ".elc")))))
|
|
257
|
|
258 ;; This can be the 'byte-compile property of any symbol.
|
|
259 (autoload 'byte-compile-inline-expand "byte-optimize")
|
|
260
|
|
261 ;; This is the entrypoint to the lapcode optimizer pass1.
|
|
262 (autoload 'byte-optimize-form "byte-optimize")
|
|
263 ;; This is the entrypoint to the lapcode optimizer pass2.
|
|
264 (autoload 'byte-optimize-lapcode "byte-optimize")
|
|
265 (autoload 'byte-compile-unfold-lambda "byte-optimize")
|
|
266
|
|
267 ;; This is the entry point to the decompiler, which is used by the
|
|
268 ;; disassembler. The disassembler just requires 'byte-compile, but
|
|
269 ;; that doesn't define this function, so this seems to be a reasonable
|
|
270 ;; thing to do.
|
|
271 (autoload 'byte-decompile-bytecode "byte-opt")
|
|
272
|
|
273 (defvar byte-compile-verbose
|
|
274 (and (not noninteractive) (> (device-baud-rate) search-slow-speed))
|
|
275 "*Non-nil means print messages describing progress of byte-compiler.")
|
|
276
|
|
277 (defvar byte-compile-emacs19-compatibility
|
|
278 (not (emacs-version>= 20))
|
|
279 "*Non-nil means generate output that can run in Emacs 19.")
|
|
280
|
272
|
281 (defvar byte-compile-print-gensym t
|
|
282 "*Non-nil means generate code that creates unique symbols at run-time.
|
|
283 This is achieved by printing uninterned symbols using the `#:SYMBOL'
|
|
284 notation, so that they will be read uninterned when run.
|
|
285
|
|
286 With this feature, code that uses uninterned symbols in macros will
|
|
287 not be runnable under pre-21.0 XEmacsen.
|
|
288
|
|
289 When `byte-compile-emacs19-compatibility' is non-nil, this variable is
|
|
290 ignored and considered to be nil.")
|
|
291
|
209
|
292 (defvar byte-optimize t
|
|
293 "*Enables optimization in the byte compiler.
|
|
294 nil means don't do any optimization.
|
|
295 t means do all optimizations.
|
|
296 `source' means do source-level optimizations only.
|
|
297 `byte' means do code-level optimizations only.")
|
|
298
|
|
299 (defvar byte-compile-delete-errors t
|
|
300 "*If non-nil, the optimizer may delete forms that may signal an error.
|
|
301 This includes variable references and calls to functions such as `car'.")
|
|
302
|
|
303 ;; XEmacs addition
|
|
304 (defvar byte-compile-new-bytecodes nil
|
|
305 "This is completely ignored. It is only around for backwards
|
|
306 compatibility.")
|
|
307
|
|
308
|
|
309 ;; FSF enables byte-compile-dynamic-docstrings but not byte-compile-dynamic
|
|
310 ;; by default. This would be a reasonable conservative approach except
|
|
311 ;; for the fact that if you enable either of these, you get incompatible
|
|
312 ;; byte code that can't be read by XEmacs 19.13 or before or FSF 19.28 or
|
|
313 ;; before.
|
|
314 ;;
|
|
315 ;; Therefore, neither is enabled for 19.14. Both are enabled for 20.0
|
|
316 ;; because we have no reason to be conservative about changing the
|
|
317 ;; way things work. (Ben)
|
|
318
|
|
319 ;; However, I don't think that defaulting byte-compile-dynamic to nil
|
|
320 ;; is a compatibility issue - rather it is a performance issue.
|
|
321 ;; Therefore I am setting byte-compile-dynamic back to nil. (mrb)
|
|
322
|
|
323 (defvar byte-compile-dynamic nil
|
|
324 "*If non-nil, compile function bodies so they load lazily.
|
|
325 They are hidden comments in the compiled file, and brought into core when the
|
|
326 function is called.
|
|
327
|
|
328 To enable this option, make it a file-local variable
|
|
329 in the source file you want it to apply to.
|
|
330 For example, add -*-byte-compile-dynamic: t;-*- on the first line.
|
|
331
|
|
332 When this option is true, if you load the compiled file and then move it,
|
|
333 the functions you loaded will not be able to run.")
|
|
334
|
|
335 (defvar byte-compile-dynamic-docstrings (emacs-version>= 20)
|
|
336 "*If non-nil, compile doc strings for lazy access.
|
|
337 We bury the doc strings of functions and variables
|
|
338 inside comments in the file, and bring them into core only when they
|
|
339 are actually needed.
|
|
340
|
|
341 When this option is true, if you load the compiled file and then move it,
|
|
342 you won't be able to find the documentation of anything in that file.
|
|
343
|
|
344 To disable this option for a certain file, make it a file-local variable
|
|
345 in the source file. For example, add this to the first line:
|
|
346 -*-byte-compile-dynamic-docstrings:nil;-*-
|
|
347 You can also set the variable globally.
|
|
348
|
|
349 This option is enabled by default because it reduces Emacs memory usage.")
|
|
350
|
|
351 (defvar byte-optimize-log nil
|
|
352 "*If true, the byte-compiler will log its optimizations into *Compile-Log*.
|
|
353 If this is 'source, then only source-level optimizations will be logged.
|
|
354 If it is 'byte, then only byte-level optimizations will be logged.")
|
|
355
|
|
356 (defvar byte-compile-error-on-warn nil
|
|
357 "*If true, the byte-compiler reports warnings with `error'.")
|
|
358
|
|
359 ;; byte-compile-warning-types in FSF.
|
|
360 (defvar byte-compile-default-warnings
|
|
361 '(redefine callargs free-vars unresolved unused-vars obsolete)
|
|
362 "*The warnings used when byte-compile-warnings is t.")
|
|
363
|
|
364 (defvar byte-compile-warnings t
|
|
365 "*List of warnings that the compiler should issue (t for the default set).
|
|
366 Elements of the list may be:
|
|
367
|
|
368 free-vars references to variables not in the current lexical scope.
|
|
369 unused-vars references to non-global variables bound but not referenced.
|
|
370 unresolved calls to unknown functions.
|
|
371 callargs lambda calls with args that don't match the definition.
|
|
372 redefine function cell redefined from a macro to a lambda or vice
|
|
373 versa, or redefined to take a different number of arguments.
|
|
374 obsolete use of an obsolete function or variable.
|
|
375 pedantic warn of use of compatible symbols.
|
|
376
|
|
377 The default set is specified by `byte-compile-default-warnings' and
|
|
378 normally encompasses all possible warnings.
|
|
379
|
|
380 See also the macro `byte-compiler-options'.")
|
|
381
|
|
382 (defvar byte-compile-generate-call-tree nil
|
|
383 "*Non-nil means collect call-graph information when compiling.
|
|
384 This records functions were called and from where.
|
|
385 If the value is t, compilation displays the call graph when it finishes.
|
|
386 If the value is neither t nor nil, compilation asks you whether to display
|
|
387 the graph.
|
|
388
|
|
389 The call tree only lists functions called, not macros used. Those functions
|
|
390 which the byte-code interpreter knows about directly (eq, cons, etc.) are
|
|
391 not reported.
|
|
392
|
|
393 The call tree also lists those functions which are not known to be called
|
|
394 \(that is, to which no calls have been compiled). Functions which can be
|
|
395 invoked interactively are excluded from this list.")
|
|
396
|
|
397 (defconst byte-compile-call-tree nil "Alist of functions and their call tree.
|
|
398 Each element looks like
|
|
399
|
|
400 \(FUNCTION CALLERS CALLS\)
|
|
401
|
|
402 where CALLERS is a list of functions that call FUNCTION, and CALLS
|
|
403 is a list of functions for which calls were generated while compiling
|
|
404 FUNCTION.")
|
|
405
|
|
406 (defvar byte-compile-call-tree-sort 'name
|
|
407 "*If non-nil, sort the call tree.
|
|
408 The values `name', `callers', `calls', `calls+callers'
|
|
409 specify different fields to sort on.")
|
|
410
|
|
411 (defvar byte-compile-overwrite-file t
|
|
412 "If nil, old .elc files are deleted before the new is saved, and .elc
|
|
413 files will have the same modes as the corresponding .el file. Otherwise,
|
|
414 existing .elc files will simply be overwritten, and the existing modes
|
272
|
415 will not be changed. If this variable is nil, then an .elc file which
|
209
|
416 is a symbolic link will be turned into a normal file, instead of the file
|
|
417 which the link points to being overwritten.")
|
|
418
|
|
419 (defvar byte-recompile-directory-ignore-errors-p nil
|
|
420 "If true, then `byte-recompile-directory' will continue compiling even
|
|
421 when an error occurs in a file. This is bound to t by
|
|
422 `batch-byte-recompile-directory'.")
|
|
423
|
|
424 (defvar byte-recompile-directory-recursively t
|
|
425 "*If true, then `byte-recompile-directory' will recurse on subdirectories.")
|
|
426
|
|
427 (defvar byte-compile-constants nil
|
|
428 "list of all constants encountered during compilation of this form")
|
|
429 (defvar byte-compile-variables nil
|
|
430 "list of all variables encountered during compilation of this form")
|
|
431 (defvar byte-compile-bound-variables nil
|
|
432 "Alist of variables bound in the context of the current form,
|
272
|
433 that is, the current lexical environment. This list lives partly
|
209
|
434 on the specbind stack. The cdr of each cell is an integer bitmask.")
|
|
435
|
|
436 (defconst byte-compile-referenced-bit 1)
|
|
437 (defconst byte-compile-assigned-bit 2)
|
|
438 (defconst byte-compile-arglist-bit 4)
|
|
439 (defconst byte-compile-global-bit 8)
|
|
440
|
|
441 (defvar byte-compile-free-references)
|
|
442 (defvar byte-compile-free-assignments)
|
|
443
|
|
444 (defvar byte-compiler-error-flag)
|
|
445
|
|
446 (defconst byte-compile-initial-macro-environment
|
|
447 (purecopy
|
|
448 '((byte-compiler-options . (lambda (&rest forms)
|
|
449 (apply 'byte-compiler-options-handler forms)))
|
|
450 (eval-when-compile . (lambda (&rest body)
|
|
451 (list 'quote (eval (byte-compile-top-level
|
|
452 (cons 'progn body))))))
|
|
453 (eval-and-compile . (lambda (&rest body)
|
|
454 (eval (cons 'progn body))
|
|
455 (cons 'progn body)))))
|
|
456 "The default macro-environment passed to macroexpand by the compiler.
|
|
457 Placing a macro here will cause a macro to have different semantics when
|
|
458 expanded by the compiler as when expanded by the interpreter.")
|
|
459
|
|
460 (defvar byte-compile-macro-environment byte-compile-initial-macro-environment
|
|
461 "Alist of macros defined in the file being compiled.
|
|
462 Each element looks like (MACRONAME . DEFINITION). It is
|
|
463 \(MACRONAME . nil) when a macro is redefined as a function.")
|
|
464
|
|
465 (defvar byte-compile-function-environment nil
|
|
466 "Alist of functions defined in the file being compiled.
|
|
467 This is so we can inline them when necessary.
|
|
468 Each element looks like (FUNCTIONNAME . DEFINITION). It is
|
|
469 \(FUNCTIONNAME . nil) when a function is redefined as a macro.")
|
|
470
|
|
471 (defvar byte-compile-autoload-environment nil
|
|
472 "Alist of functions and macros defined by autoload in the file being compiled.
|
|
473 This is so we can suppress warnings about calls to these functions, even though
|
|
474 they do not have `real' definitions.
|
|
475 Each element looks like (FUNCTIONNAME . CALL-TO-AUTOLOAD).")
|
|
476
|
|
477 (defvar byte-compile-unresolved-functions nil
|
|
478 "Alist of undefined functions to which calls have been compiled (used for
|
|
479 warnings when the function is later defined with incorrect args).")
|
|
480
|
|
481 (defvar byte-compile-file-domain) ; domain of file being compiled
|
|
482
|
|
483 (defvar byte-compile-tag-number 0)
|
|
484 (defvar byte-compile-output nil
|
|
485 "Alist describing contents to put in byte code string.
|
|
486 Each element is (INDEX . VALUE)")
|
|
487 (defvar byte-compile-depth 0 "Current depth of execution stack.")
|
|
488 (defvar byte-compile-maxdepth 0 "Maximum depth of execution stack.")
|
|
489
|
|
490
|
|
491 ;;; The byte codes; this information is duplicated in bytecode.c
|
|
492
|
|
493 (defconst byte-code-vector nil
|
|
494 "An array containing byte-code names indexed by byte-code values.")
|
|
495
|
|
496 (defconst byte-stack+-info nil
|
|
497 "An array with the stack adjustment for each byte-code.")
|
|
498
|
|
499 (defmacro byte-defop (opcode stack-adjust opname &optional docstring)
|
|
500 ;; This is a speed-hack for building the byte-code-vector at compile-time.
|
|
501 ;; We fill in the vector at macroexpand-time, and then after the last call
|
|
502 ;; to byte-defop, we write the vector out as a constant instead of writing
|
|
503 ;; out a bunch of calls to aset.
|
|
504 ;; Actually, we don't fill in the vector itself, because that could make
|
|
505 ;; it problematic to compile big changes to this compiler; we store the
|
|
506 ;; values on its plist, and remove them later in -extrude.
|
|
507 (let ((v1 (or (get 'byte-code-vector 'tmp-compile-time-value)
|
|
508 (put 'byte-code-vector 'tmp-compile-time-value
|
|
509 (make-vector 256 nil))))
|
|
510 (v2 (or (get 'byte-stack+-info 'tmp-compile-time-value)
|
|
511 (put 'byte-stack+-info 'tmp-compile-time-value
|
|
512 (make-vector 256 nil)))))
|
|
513 (aset v1 opcode opname)
|
|
514 (aset v2 opcode stack-adjust))
|
|
515 (if docstring
|
|
516 (list 'defconst opname opcode (concat "Byte code opcode " docstring "."))
|
|
517 (list 'defconst opname opcode)))
|
|
518
|
|
519 (defmacro byte-extrude-byte-code-vectors ()
|
|
520 (prog1 (list 'setq 'byte-code-vector
|
|
521 (get 'byte-code-vector 'tmp-compile-time-value)
|
|
522 'byte-stack+-info
|
|
523 (get 'byte-stack+-info 'tmp-compile-time-value))
|
|
524 (remprop 'byte-code-vector 'tmp-compile-time-value)
|
|
525 (remprop 'byte-stack+-info 'tmp-compile-time-value)))
|
|
526
|
|
527
|
|
528 ;; unused: 0-7
|
|
529
|
|
530 ;; These opcodes are special in that they pack their argument into the
|
|
531 ;; opcode word.
|
|
532 ;;
|
|
533 (byte-defop 8 1 byte-varref "for variable reference")
|
|
534 (byte-defop 16 -1 byte-varset "for setting a variable")
|
|
535 (byte-defop 24 -1 byte-varbind "for binding a variable")
|
|
536 (byte-defop 32 0 byte-call "for calling a function")
|
|
537 (byte-defop 40 0 byte-unbind "for unbinding special bindings")
|
|
538 ;; codes 8-47 are consumed by the preceding opcodes
|
|
539
|
|
540 ;; unused: 48-55
|
|
541
|
|
542 (byte-defop 56 -1 byte-nth)
|
|
543 (byte-defop 57 0 byte-symbolp)
|
|
544 (byte-defop 58 0 byte-consp)
|
|
545 (byte-defop 59 0 byte-stringp)
|
|
546 (byte-defop 60 0 byte-listp)
|
|
547 (byte-defop 61 -1 byte-old-eq)
|
|
548 (byte-defop 62 -1 byte-old-memq)
|
|
549 (byte-defop 63 0 byte-not)
|
|
550 (byte-defop 64 0 byte-car)
|
|
551 (byte-defop 65 0 byte-cdr)
|
|
552 (byte-defop 66 -1 byte-cons)
|
|
553 (byte-defop 67 0 byte-list1)
|
|
554 (byte-defop 68 -1 byte-list2)
|
|
555 (byte-defop 69 -2 byte-list3)
|
|
556 (byte-defop 70 -3 byte-list4)
|
|
557 (byte-defop 71 0 byte-length)
|
|
558 (byte-defop 72 -1 byte-aref)
|
|
559 (byte-defop 73 -2 byte-aset)
|
|
560 (byte-defop 74 0 byte-symbol-value)
|
|
561 (byte-defop 75 0 byte-symbol-function) ; this was commented out
|
|
562 (byte-defop 76 -1 byte-set)
|
|
563 (byte-defop 77 -1 byte-fset) ; this was commented out
|
|
564 (byte-defop 78 -1 byte-get)
|
|
565 (byte-defop 79 -2 byte-substring)
|
|
566 (byte-defop 80 -1 byte-concat2)
|
|
567 (byte-defop 81 -2 byte-concat3)
|
|
568 (byte-defop 82 -3 byte-concat4)
|
|
569 (byte-defop 83 0 byte-sub1)
|
|
570 (byte-defop 84 0 byte-add1)
|
|
571 (byte-defop 85 -1 byte-eqlsign)
|
|
572 (byte-defop 86 -1 byte-gtr)
|
|
573 (byte-defop 87 -1 byte-lss)
|
|
574 (byte-defop 88 -1 byte-leq)
|
|
575 (byte-defop 89 -1 byte-geq)
|
|
576 (byte-defop 90 -1 byte-diff)
|
|
577 (byte-defop 91 0 byte-negate)
|
|
578 (byte-defop 92 -1 byte-plus)
|
|
579 (byte-defop 93 -1 byte-max)
|
|
580 (byte-defop 94 -1 byte-min)
|
|
581 (byte-defop 95 -1 byte-mult)
|
|
582 (byte-defop 96 1 byte-point)
|
|
583 (byte-defop 97 -1 byte-eq) ; new as of v20
|
|
584 (byte-defop 98 0 byte-goto-char)
|
|
585 (byte-defop 99 0 byte-insert)
|
|
586 (byte-defop 100 1 byte-point-max)
|
|
587 (byte-defop 101 1 byte-point-min)
|
|
588 (byte-defop 102 0 byte-char-after)
|
|
589 (byte-defop 103 1 byte-following-char)
|
|
590 (byte-defop 104 1 byte-preceding-char)
|
|
591 (byte-defop 105 1 byte-current-column)
|
|
592 (byte-defop 106 0 byte-indent-to)
|
|
593 (byte-defop 107 -1 byte-equal) ; new as of v20
|
|
594 (byte-defop 108 1 byte-eolp)
|
|
595 (byte-defop 109 1 byte-eobp)
|
|
596 (byte-defop 110 1 byte-bolp)
|
|
597 (byte-defop 111 1 byte-bobp)
|
|
598 (byte-defop 112 1 byte-current-buffer)
|
|
599 (byte-defop 113 0 byte-set-buffer)
|
|
600 (byte-defop 114 0 byte-save-current-buffer
|
|
601 "To make a binding to record the current buffer.")
|
|
602 ;;(byte-defop 114 1 byte-read-char-OBSOLETE) ;obsolete as of v19
|
|
603 (byte-defop 115 -1 byte-memq) ; new as of v20
|
|
604 (byte-defop 116 1 byte-interactive-p)
|
|
605
|
|
606 (byte-defop 117 0 byte-forward-char)
|
|
607 (byte-defop 118 0 byte-forward-word)
|
|
608 (byte-defop 119 -1 byte-skip-chars-forward)
|
|
609 (byte-defop 120 -1 byte-skip-chars-backward)
|
|
610 (byte-defop 121 0 byte-forward-line)
|
|
611 (byte-defop 122 0 byte-char-syntax)
|
|
612 (byte-defop 123 -1 byte-buffer-substring)
|
|
613 (byte-defop 124 -1 byte-delete-region)
|
|
614 (byte-defop 125 -1 byte-narrow-to-region)
|
|
615 (byte-defop 126 1 byte-widen)
|
|
616 (byte-defop 127 0 byte-end-of-line)
|
|
617
|
|
618 ;; unused: 128
|
|
619
|
|
620 ;; These store their argument in the next two bytes
|
|
621 (byte-defop 129 1 byte-constant2
|
|
622 "for reference to a constant with vector index >= byte-constant-limit")
|
|
623 (byte-defop 130 0 byte-goto "for unconditional jump")
|
|
624 (byte-defop 131 -1 byte-goto-if-nil "to pop value and jump if it's nil")
|
|
625 (byte-defop 132 -1 byte-goto-if-not-nil
|
|
626 "to pop value and jump if it's not nil")
|
|
627 (byte-defop 133 -1 byte-goto-if-nil-else-pop
|
272
|
628 "to examine top-of-stack, jump and don't pop it if it's nil,
|
209
|
629 otherwise pop it")
|
|
630 (byte-defop 134 -1 byte-goto-if-not-nil-else-pop
|
272
|
631 "to examine top-of-stack, jump and don't pop it if it's non nil,
|
209
|
632 otherwise pop it")
|
|
633
|
|
634 (byte-defop 135 -1 byte-return "to pop a value and return it from `byte-code'")
|
|
635 (byte-defop 136 -1 byte-discard "to discard one value from stack")
|
|
636 (byte-defop 137 1 byte-dup "to duplicate the top of the stack")
|
|
637
|
|
638 (byte-defop 138 0 byte-save-excursion
|
|
639 "to make a binding to record the buffer, point and mark")
|
|
640 (byte-defop 139 0 byte-save-window-excursion
|
|
641 "to make a binding to record entire window configuration")
|
|
642 (byte-defop 140 0 byte-save-restriction
|
|
643 "to make a binding to record the current buffer clipping restrictions")
|
|
644 (byte-defop 141 -1 byte-catch
|
|
645 "for catch. Takes, on stack, the tag and an expression for the body")
|
|
646 (byte-defop 142 -1 byte-unwind-protect
|
|
647 "for unwind-protect. Takes, on stack, an expression for the unwind-action")
|
|
648
|
272
|
649 ;; For condition-case. Takes, on stack, the variable to bind,
|
209
|
650 ;; an expression for the body, and a list of clauses.
|
|
651 (byte-defop 143 -2 byte-condition-case)
|
|
652
|
|
653 ;; For entry to with-output-to-temp-buffer.
|
|
654 ;; Takes, on stack, the buffer name.
|
|
655 ;; Binds standard-output and does some other things.
|
|
656 ;; Returns with temp buffer on the stack in place of buffer name.
|
|
657 (byte-defop 144 0 byte-temp-output-buffer-setup)
|
|
658
|
|
659 ;; For exit from with-output-to-temp-buffer.
|
|
660 ;; Expects the temp buffer on the stack underneath value to return.
|
|
661 ;; Pops them both, then pushes the value back on.
|
|
662 ;; Unbinds standard-output and makes the temp buffer visible.
|
|
663 (byte-defop 145 -1 byte-temp-output-buffer-show)
|
|
664
|
|
665 ;; To unbind back to the beginning of this frame.
|
|
666 ;; Not used yet, but will be needed for tail-recursion elimination.
|
|
667 (byte-defop 146 0 byte-unbind-all)
|
|
668
|
|
669 (byte-defop 147 -2 byte-set-marker)
|
|
670 (byte-defop 148 0 byte-match-beginning)
|
|
671 (byte-defop 149 0 byte-match-end)
|
|
672 (byte-defop 150 0 byte-upcase)
|
|
673 (byte-defop 151 0 byte-downcase)
|
|
674 (byte-defop 152 -1 byte-string=)
|
|
675 (byte-defop 153 -1 byte-string<)
|
|
676 (byte-defop 154 -1 byte-old-equal)
|
|
677 (byte-defop 155 -1 byte-nthcdr)
|
|
678 (byte-defop 156 -1 byte-elt)
|
|
679 (byte-defop 157 -1 byte-old-member)
|
|
680 (byte-defop 158 -1 byte-old-assq)
|
|
681 (byte-defop 159 0 byte-nreverse)
|
|
682 (byte-defop 160 -1 byte-setcar)
|
|
683 (byte-defop 161 -1 byte-setcdr)
|
|
684 (byte-defop 162 0 byte-car-safe)
|
|
685 (byte-defop 163 0 byte-cdr-safe)
|
|
686 (byte-defop 164 -1 byte-nconc)
|
|
687 (byte-defop 165 -1 byte-quo)
|
|
688 (byte-defop 166 -1 byte-rem)
|
|
689 (byte-defop 167 0 byte-numberp)
|
|
690 (byte-defop 168 0 byte-integerp)
|
|
691
|
|
692 ;; unused: 169
|
|
693
|
|
694 ;; These are not present in FSF.
|
|
695 ;;
|
|
696 (byte-defop 170 0 byte-rel-goto)
|
|
697 (byte-defop 171 -1 byte-rel-goto-if-nil)
|
|
698 (byte-defop 172 -1 byte-rel-goto-if-not-nil)
|
|
699 (byte-defop 173 -1 byte-rel-goto-if-nil-else-pop)
|
|
700 (byte-defop 174 -1 byte-rel-goto-if-not-nil-else-pop)
|
|
701
|
|
702 (byte-defop 175 nil byte-listN)
|
|
703 (byte-defop 176 nil byte-concatN)
|
|
704 (byte-defop 177 nil byte-insertN)
|
|
705
|
|
706 ;; unused: 178-181
|
|
707
|
|
708 ;; these ops are new to v20
|
|
709 (byte-defop 182 -1 byte-member)
|
|
710 (byte-defop 183 -1 byte-assq)
|
|
711
|
|
712 ;; unused: 184-191
|
|
713
|
|
714 (byte-defop 192 1 byte-constant "for reference to a constant")
|
|
715 ;; codes 193-255 are consumed by byte-constant.
|
|
716 (defconst byte-constant-limit 64
|
|
717 "Exclusive maximum index usable in the `byte-constant' opcode.")
|
|
718
|
|
719 (defconst byte-goto-ops (purecopy
|
|
720 '(byte-goto byte-goto-if-nil byte-goto-if-not-nil
|
|
721 byte-goto-if-nil-else-pop
|
|
722 byte-goto-if-not-nil-else-pop))
|
|
723 "List of byte-codes whose offset is a pc.")
|
|
724
|
|
725 (defconst byte-goto-always-pop-ops
|
|
726 (purecopy '(byte-goto-if-nil byte-goto-if-not-nil)))
|
|
727
|
|
728 (defconst byte-rel-goto-ops
|
|
729 (purecopy '(byte-rel-goto byte-rel-goto-if-nil byte-rel-goto-if-not-nil
|
|
730 byte-rel-goto-if-nil-else-pop byte-rel-goto-if-not-nil-else-pop))
|
|
731 "byte-codes for relative jumps.")
|
|
732
|
|
733 (byte-extrude-byte-code-vectors)
|
|
734
|
|
735 ;;; lapcode generator
|
|
736 ;;;
|
|
737 ;;; the byte-compiler now does source -> lapcode -> bytecode instead of
|
|
738 ;;; source -> bytecode, because it's a lot easier to make optimizations
|
|
739 ;;; on lapcode than on bytecode.
|
|
740 ;;;
|
|
741 ;;; Elements of the lapcode list are of the form (<instruction> . <parameter>)
|
|
742 ;;; where instruction is a symbol naming a byte-code instruction,
|
|
743 ;;; and parameter is an argument to that instruction, if any.
|
|
744 ;;;
|
272
|
745 ;;; The instruction can be the pseudo-op TAG, which means that this position
|
209
|
746 ;;; in the instruction stream is a target of a goto. (car PARAMETER) will be
|
|
747 ;;; the PC for this location, and the whole instruction "(TAG pc)" will be the
|
|
748 ;;; parameter for some goto op.
|
|
749 ;;;
|
|
750 ;;; If the operation is varbind, varref, varset or push-constant, then the
|
|
751 ;;; parameter is (variable/constant . index_in_constant_vector).
|
|
752 ;;;
|
|
753 ;;; First, the source code is macroexpanded and optimized in various ways.
|
|
754 ;;; Then the resultant code is compiled into lapcode. Another set of
|
|
755 ;;; optimizations are then run over the lapcode. Then the variables and
|
|
756 ;;; constants referenced by the lapcode are collected and placed in the
|
|
757 ;;; constants-vector. (This happens now so that variables referenced by dead
|
|
758 ;;; code don't consume space.) And finally, the lapcode is transformed into
|
|
759 ;;; compacted byte-code.
|
|
760 ;;;
|
|
761 ;;; A distinction is made between variables and constants because the variable-
|
|
762 ;;; referencing instructions are more sensitive to the variables being near the
|
|
763 ;;; front of the constants-vector than the constant-referencing instructions.
|
|
764 ;;; Also, this lets us notice references to free variables.
|
|
765
|
|
766 (defun byte-compile-lapcode (lap)
|
|
767 "Turns lapcode into bytecode. The lapcode is destroyed."
|
|
768 ;; Lapcode modifications: changes the ID of a tag to be the tag's PC.
|
|
769 (let ((pc 0) ; Program counter
|
|
770 op off ; Operation & offset
|
|
771 (bytes '()) ; Put the output bytes here
|
|
772 (patchlist nil) ; List of tags and goto's to patch
|
|
773 rest rel tmp)
|
|
774 (while lap
|
|
775 (setq op (car (car lap))
|
|
776 off (cdr (car lap)))
|
|
777 (cond ((not (symbolp op))
|
|
778 (error "Non-symbolic opcode `%s'" op))
|
|
779 ((eq op 'TAG)
|
|
780 (setcar off pc)
|
|
781 (setq patchlist (cons off patchlist)))
|
|
782 ((memq op byte-goto-ops)
|
|
783 (setq pc (+ pc 3))
|
|
784 (setq bytes (cons (cons pc (cdr off))
|
|
785 (cons nil
|
|
786 (cons (symbol-value op) bytes))))
|
|
787 (setq patchlist (cons bytes patchlist)))
|
|
788 (t
|
|
789 (setq bytes
|
|
790 (cond ((cond ((consp off)
|
|
791 ;; Variable or constant reference
|
|
792 (setq off (cdr off))
|
|
793 (eq op 'byte-constant)))
|
|
794 (cond ((< off byte-constant-limit)
|
|
795 (setq pc (1+ pc))
|
|
796 (cons (+ byte-constant off) bytes))
|
|
797 (t
|
|
798 (setq pc (+ 3 pc))
|
|
799 (cons (lsh off -8)
|
|
800 (cons (logand off 255)
|
|
801 (cons byte-constant2 bytes))))))
|
|
802 ((and (<= byte-listN (symbol-value op))
|
|
803 (<= (symbol-value op) byte-insertN))
|
|
804 (setq pc (+ 2 pc))
|
|
805 (cons off (cons (symbol-value op) bytes)))
|
|
806 ((< off 6)
|
|
807 (setq pc (1+ pc))
|
|
808 (cons (+ (symbol-value op) off) bytes))
|
|
809 ((< off 256)
|
|
810 (setq pc (+ 2 pc))
|
|
811 (cons off (cons (+ (symbol-value op) 6) bytes)))
|
|
812 (t
|
|
813 (setq pc (+ 3 pc))
|
|
814 (cons (lsh off -8)
|
|
815 (cons (logand off 255)
|
|
816 (cons (+ (symbol-value op) 7)
|
|
817 bytes))))))))
|
|
818 (setq lap (cdr lap)))
|
|
819 ;;(if (not (= pc (length bytes)))
|
|
820 ;; (error "Compiler error: pc mismatch - %s %s" pc (length bytes)))
|
|
821 (cond (t ;; starting with Emacs 19.
|
|
822 ;; Make relative jumps
|
|
823 (setq patchlist (nreverse patchlist))
|
|
824 (while (progn
|
|
825 (setq off 0) ; PC change because of deleted bytes
|
|
826 (setq rest patchlist)
|
|
827 (while rest
|
|
828 (setq tmp (car rest))
|
|
829 (and (consp (car tmp)) ; Jump
|
|
830 (prog1 (null (nth 1 tmp)) ; Absolute jump
|
|
831 (setq tmp (car tmp)))
|
|
832 (progn
|
|
833 (setq rel (- (car (cdr tmp)) (car tmp)))
|
|
834 (and (<= -129 rel) (< rel 128)))
|
|
835 (progn
|
|
836 ;; Convert to relative jump.
|
|
837 (setcdr (car rest) (cdr (cdr (car rest))))
|
|
838 (setcar (cdr (car rest))
|
|
839 (+ (car (cdr (car rest)))
|
|
840 (- byte-rel-goto byte-goto)))
|
|
841 (setq off (1- off))))
|
|
842 (setcar tmp (+ (car tmp) off)) ; Adjust PC
|
|
843 (setq rest (cdr rest)))
|
|
844 ;; If optimizing, repeat until no change.
|
|
845 (and byte-optimize
|
|
846 (not (zerop off)))))))
|
|
847 ;; Patch PC into jumps
|
|
848 (let (bytes)
|
|
849 (while patchlist
|
|
850 (setq bytes (car patchlist))
|
|
851 (cond ((atom (car bytes))) ; Tag
|
|
852 ((nth 1 bytes) ; Relative jump
|
|
853 (setcar bytes (+ (- (car (cdr (car bytes))) (car (car bytes)))
|
|
854 128)))
|
|
855 (t ; Absolute jump
|
|
856 (setq pc (car (cdr (car bytes)))) ; Pick PC from tag
|
|
857 (setcar (cdr bytes) (logand pc 255))
|
|
858 (setcar bytes (lsh pc -8))))
|
|
859 (setq patchlist (cdr patchlist))))
|
|
860 (concat (nreverse bytes))))
|
|
861
|
|
862
|
|
863 ;;; byte compiler messages
|
|
864
|
|
865 (defvar byte-compile-current-form nil)
|
|
866 (defvar byte-compile-current-file nil)
|
|
867 (defvar byte-compile-dest-file nil)
|
|
868
|
|
869 (defmacro byte-compile-log (format-string &rest args)
|
|
870 (list 'and
|
|
871 'byte-optimize
|
|
872 '(memq byte-optimize-log '(t source))
|
|
873 (list 'let '((print-escape-newlines t)
|
|
874 (print-level 4)
|
|
875 (print-length 4))
|
|
876 (list 'byte-compile-log-1
|
|
877 (cons 'format
|
|
878 (cons format-string
|
|
879 (mapcar
|
|
880 '(lambda (x)
|
|
881 (if (symbolp x) (list 'prin1-to-string x) x))
|
|
882 args)))))))
|
|
883
|
|
884 (defconst byte-compile-last-warned-form nil)
|
|
885
|
|
886 ;; Log a message STRING in *Compile-Log*.
|
|
887 ;; Also log the current function and file if not already done.
|
|
888 (defun byte-compile-log-1 (string &optional fill)
|
|
889 (let ((this-form (or byte-compile-current-form "toplevel forms")))
|
|
890 (cond
|
|
891 (noninteractive
|
|
892 (if (or byte-compile-current-file
|
|
893 (and byte-compile-last-warned-form
|
|
894 (not (eq this-form byte-compile-last-warned-form))))
|
|
895 (message
|
|
896 (format "While compiling %s%s:"
|
|
897 this-form
|
|
898 (if byte-compile-current-file
|
|
899 (if (stringp byte-compile-current-file)
|
|
900 (concat " in file " byte-compile-current-file)
|
|
901 (concat " in buffer "
|
|
902 (buffer-name byte-compile-current-file)))
|
|
903 ""))))
|
|
904 (message " %s" string))
|
|
905 (t
|
|
906 (save-excursion
|
|
907 (set-buffer (get-buffer-create "*Compile-Log*"))
|
|
908 (goto-char (point-max))
|
|
909 (cond ((or byte-compile-current-file
|
|
910 (and byte-compile-last-warned-form
|
|
911 (not (eq this-form byte-compile-last-warned-form))))
|
|
912 (if byte-compile-current-file
|
|
913 (insert "\n\^L\n" (current-time-string) "\n"))
|
|
914 (insert "While compiling "
|
|
915 (if (stringp this-form) this-form
|
|
916 (format "%s" this-form)))
|
|
917 (if byte-compile-current-file
|
|
918 (if (stringp byte-compile-current-file)
|
|
919 (insert " in file " byte-compile-current-file)
|
|
920 (insert " in buffer "
|
|
921 (buffer-name byte-compile-current-file))))
|
|
922 (insert ":\n")))
|
|
923 (insert " " string "\n")
|
|
924 (if (and fill (not (string-match "\n" string)))
|
|
925 (let ((fill-prefix " ")
|
|
926 (fill-column 78))
|
|
927 (fill-paragraph nil)))
|
|
928 )))
|
|
929 (setq byte-compile-current-file nil
|
|
930 byte-compile-last-warned-form this-form)))
|
|
931
|
|
932 ;; Log the start of a file in *Compile-Log*, and mark it as done.
|
|
933 ;; But do nothing in batch mode.
|
|
934 (defun byte-compile-log-file ()
|
|
935 (and byte-compile-current-file (not noninteractive)
|
|
936 (save-excursion
|
|
937 (set-buffer (get-buffer-create "*Compile-Log*"))
|
|
938 (goto-char (point-max))
|
|
939 (insert "\n\^L\nCompiling "
|
|
940 (if (stringp byte-compile-current-file)
|
|
941 (concat "file " byte-compile-current-file)
|
|
942 (concat "buffer " (buffer-name byte-compile-current-file)))
|
|
943 " at " (current-time-string) "\n")
|
|
944 (setq byte-compile-current-file nil))))
|
|
945
|
|
946 (defun byte-compile-warn (format &rest args)
|
|
947 (setq format (apply 'format format args))
|
|
948 (if byte-compile-error-on-warn
|
|
949 (error "%s" format) ; byte-compile-file catches and logs it
|
|
950 (byte-compile-log-1 (concat "** " format) t)
|
|
951 ;;; RMS says:
|
|
952 ;;; It is useless to flash warnings too fast to be read.
|
|
953 ;;; Besides, they will all be shown at the end.
|
|
954 ;;; and comments out the next two lines.
|
|
955 (or noninteractive ; already written on stdout.
|
|
956 (message "Warning: %s" format))))
|
|
957
|
|
958 ;;; This function should be used to report errors that have halted
|
|
959 ;;; compilation of the current file.
|
|
960 (defun byte-compile-report-error (error-info)
|
|
961 (setq byte-compiler-error-flag t)
|
|
962 (byte-compile-log-1
|
|
963 (concat "!! "
|
|
964 (format (if (cdr error-info) "%s (%s)" "%s")
|
|
965 (get (car error-info) 'error-message)
|
|
966 (prin1-to-string (cdr error-info))))))
|
|
967
|
|
968 ;;; Used by make-obsolete.
|
|
969 (defun byte-compile-obsolete (form)
|
|
970 (let ((new (get (car form) 'byte-obsolete-info)))
|
|
971 (if (memq 'obsolete byte-compile-warnings)
|
|
972 (byte-compile-warn "%s is an obsolete function; %s" (car form)
|
|
973 (if (stringp (car new))
|
|
974 (car new)
|
|
975 (format "use %s instead." (car new)))))
|
|
976 (funcall (or (cdr new) 'byte-compile-normal-call) form)))
|
|
977
|
|
978 ;;; Used by make-obsolete.
|
|
979 (defun byte-compile-compatible (form)
|
|
980 (let ((new (get (car form) 'byte-compatible-info)))
|
|
981 (if (memq 'pedantic byte-compile-warnings)
|
|
982 (byte-compile-warn "%s is provided for compatibility; %s" (car form)
|
|
983 (if (stringp (car new))
|
|
984 (car new)
|
|
985 (format "use %s instead." (car new)))))
|
|
986 (funcall (or (cdr new) 'byte-compile-normal-call) form)))
|
|
987
|
|
988 ;; Compiler options
|
|
989
|
|
990 (defconst byte-compiler-legal-options
|
|
991 '((optimize byte-optimize (t nil source byte) val)
|
|
992 (file-format byte-compile-emacs19-compatibility (emacs19 emacs20)
|
|
993 (eq val 'emacs19))
|
|
994 (delete-errors byte-compile-delete-errors (t nil) val)
|
|
995 (verbose byte-compile-verbose (t nil) val)
|
|
996 (new-bytecodes byte-compile-new-bytecodes (t nil) val)
|
|
997 (warnings byte-compile-warnings
|
|
998 ((callargs redefine free-vars unused-vars unresolved))
|
|
999 val)))
|
|
1000
|
|
1001 ;; XEmacs addition
|
|
1002 (defconst byte-compiler-obsolete-options
|
|
1003 '((new-bytecodes t)))
|
|
1004
|
|
1005 ;; Inhibit v19/v20 selectors if the version is hardcoded.
|
272
|
1006 ;; #### This should print a warning if the user tries to change something
|
209
|
1007 ;; than can't be changed because the running compiler doesn't support it.
|
|
1008 (cond
|
|
1009 ((byte-compile-single-version)
|
|
1010 (setcar (cdr (cdr (assq 'file-format byte-compiler-legal-options)))
|
|
1011 (if (byte-compile-version-cond byte-compile-emacs19-compatibility)
|
|
1012 '(emacs19) '(emacs20)))))
|
|
1013
|
|
1014 ;; now we can copy it.
|
|
1015 (setq byte-compiler-legal-options (purecopy byte-compiler-legal-options))
|
|
1016
|
|
1017 (defun byte-compiler-options-handler (&rest args)
|
|
1018 (let (key val desc choices)
|
|
1019 (while args
|
|
1020 (if (or (atom (car args)) (nthcdr 2 (car args)) (null (cdr (car args))))
|
|
1021 (error "malformed byte-compiler-option %s" (car args)))
|
|
1022 (setq key (car (car args))
|
|
1023 val (car (cdr (car args)))
|
|
1024 desc (assq key byte-compiler-legal-options))
|
|
1025 (or desc
|
|
1026 (error "unknown byte-compiler option %s" key))
|
|
1027 (if (assq key byte-compiler-obsolete-options)
|
|
1028 (byte-compile-warn "%s is an obsolete byte-compiler option." key))
|
|
1029 (setq choices (nth 2 desc))
|
|
1030 (if (consp (car choices))
|
|
1031 (let* (this
|
|
1032 (handler 'cons)
|
|
1033 (var (nth 1 desc))
|
|
1034 (ret (and (memq (car val) '(+ -))
|
|
1035 (copy-sequence (if (eq t (symbol-value var))
|
|
1036 (car choices)
|
|
1037 (symbol-value var))))))
|
|
1038 (setq choices (car choices))
|
|
1039 (while val
|
|
1040 (setq this (car val))
|
|
1041 (cond ((memq this choices)
|
|
1042 (setq ret (funcall handler this ret)))
|
|
1043 ((eq this '+) (setq handler 'cons))
|
|
1044 ((eq this '-) (setq handler 'delq))
|
|
1045 ((error "%s only accepts %s." key choices)))
|
|
1046 (setq val (cdr val)))
|
|
1047 (set (nth 1 desc) ret))
|
|
1048 (or (memq val choices)
|
|
1049 (error "%s must be one of %s." key choices))
|
|
1050 (set (nth 1 desc) (eval (nth 3 desc))))
|
|
1051 (setq args (cdr args)))
|
|
1052 nil))
|
|
1053
|
|
1054 ;;; sanity-checking arglists
|
|
1055
|
|
1056 (defun byte-compile-fdefinition (name macro-p)
|
|
1057 (let* ((list (if (memq macro-p '(nil subr))
|
|
1058 byte-compile-function-environment
|
|
1059 byte-compile-macro-environment))
|
|
1060 (env (cdr (assq name list))))
|
|
1061 (or env
|
|
1062 (let ((fn name))
|
|
1063 (while (and (symbolp fn)
|
|
1064 (fboundp fn)
|
|
1065 (or (symbolp (symbol-function fn))
|
|
1066 (consp (symbol-function fn))
|
|
1067 (and (not macro-p)
|
|
1068 (compiled-function-p (symbol-function fn)))
|
|
1069 (and (eq macro-p 'subr) (subrp fn))))
|
|
1070 (setq fn (symbol-function fn)))
|
|
1071 (if (or (and (not macro-p) (compiled-function-p fn))
|
|
1072 (and (eq macro-p 'subr) (subrp fn)))
|
|
1073 fn
|
|
1074 (and (consp fn)
|
|
1075 (not (eq macro-p 'subr))
|
|
1076 (if (eq 'macro (car fn))
|
|
1077 (cdr fn)
|
|
1078 (if macro-p
|
|
1079 nil
|
|
1080 (if (eq 'autoload (car fn))
|
|
1081 nil
|
|
1082 fn)))))))))
|
|
1083
|
|
1084 (defun byte-compile-arglist-signature (arglist)
|
|
1085 (let ((args 0)
|
|
1086 opts
|
|
1087 restp)
|
|
1088 (while arglist
|
|
1089 (cond ((eq (car arglist) '&optional)
|
|
1090 (or opts (setq opts 0)))
|
|
1091 ((eq (car arglist) '&rest)
|
|
1092 (if (cdr arglist)
|
|
1093 (setq restp t
|
|
1094 arglist nil)))
|
|
1095 (t
|
|
1096 (if opts
|
|
1097 (setq opts (1+ opts))
|
|
1098 (setq args (1+ args)))))
|
|
1099 (setq arglist (cdr arglist)))
|
|
1100 (cons args (if restp nil (if opts (+ args opts) args)))))
|
|
1101
|
|
1102
|
|
1103 (defun byte-compile-arglist-signatures-congruent-p (old new)
|
|
1104 (not (or
|
|
1105 (> (car new) (car old)) ; requires more args now
|
|
1106 (and (null (cdr old)) ; tooks rest-args, doesn't any more
|
|
1107 (cdr new))
|
|
1108 (and (cdr new) (cdr old) ; can't take as many args now
|
|
1109 (< (cdr new) (cdr old)))
|
|
1110 )))
|
|
1111
|
|
1112 (defun byte-compile-arglist-signature-string (signature)
|
|
1113 (cond ((null (cdr signature))
|
|
1114 (format "%d+" (car signature)))
|
|
1115 ((= (car signature) (cdr signature))
|
|
1116 (format "%d" (car signature)))
|
|
1117 (t (format "%d-%d" (car signature) (cdr signature)))))
|
|
1118
|
|
1119
|
|
1120 ;; Warn if the form is calling a function with the wrong number of arguments.
|
|
1121 (defun byte-compile-callargs-warn (form)
|
|
1122 (let* ((def (or (byte-compile-fdefinition (car form) nil)
|
|
1123 (byte-compile-fdefinition (car form) t)))
|
|
1124 (sig (and def (byte-compile-arglist-signature
|
|
1125 (if (eq 'lambda (car-safe def))
|
|
1126 (nth 1 def)
|
|
1127 (if (compiled-function-p def)
|
|
1128 (compiled-function-arglist def)
|
|
1129 '(&rest def))))))
|
|
1130 (ncall (length (cdr form))))
|
|
1131 (if (and (null def)
|
|
1132 (fboundp 'subr-min-args)
|
|
1133 (setq def (byte-compile-fdefinition (car form) 'subr)))
|
|
1134 (setq sig (cons (subr-min-args def) (subr-max-args def))))
|
|
1135 (if sig
|
|
1136 (if (or (< ncall (car sig))
|
|
1137 (and (cdr sig) (> ncall (cdr sig))))
|
|
1138 (byte-compile-warn
|
|
1139 "%s called with %d argument%s, but %s %s"
|
|
1140 (car form) ncall
|
|
1141 (if (= 1 ncall) "" "s")
|
|
1142 (if (< ncall (car sig))
|
|
1143 "requires"
|
|
1144 "accepts only")
|
|
1145 (byte-compile-arglist-signature-string sig)))
|
|
1146 (or (fboundp (car form)) ; might be a subr or autoload.
|
|
1147 ;; ## this doesn't work with recursion.
|
|
1148 (eq (car form) byte-compile-current-form)
|
|
1149 ;; It's a currently-undefined function.
|
|
1150 ;; Remember number of args in call.
|
|
1151 (let ((cons (assq (car form) byte-compile-unresolved-functions))
|
|
1152 (n (length (cdr form))))
|
|
1153 (if cons
|
|
1154 (or (memq n (cdr cons))
|
|
1155 (setcdr cons (cons n (cdr cons))))
|
|
1156 (setq byte-compile-unresolved-functions
|
|
1157 (cons (list (car form) n)
|
|
1158 byte-compile-unresolved-functions))))))))
|
|
1159
|
|
1160 ;; Warn if the function or macro is being redefined with a different
|
|
1161 ;; number of arguments.
|
|
1162 (defun byte-compile-arglist-warn (form macrop)
|
|
1163 (let ((old (byte-compile-fdefinition (nth 1 form) macrop)))
|
|
1164 (if old
|
|
1165 (let ((sig1 (byte-compile-arglist-signature
|
|
1166 (if (eq 'lambda (car-safe old))
|
|
1167 (nth 1 old)
|
|
1168 (if (compiled-function-p old)
|
|
1169 (compiled-function-arglist old)
|
|
1170 '(&rest def)))))
|
|
1171 (sig2 (byte-compile-arglist-signature (nth 2 form))))
|
|
1172 (or (byte-compile-arglist-signatures-congruent-p sig1 sig2)
|
|
1173 (byte-compile-warn "%s %s used to take %s %s, now takes %s"
|
|
1174 (if (eq (car form) 'defun) "function" "macro")
|
|
1175 (nth 1 form)
|
|
1176 (byte-compile-arglist-signature-string sig1)
|
|
1177 (if (equal sig1 '(1 . 1)) "argument" "arguments")
|
|
1178 (byte-compile-arglist-signature-string sig2))))
|
|
1179 ;; This is the first definition. See if previous calls are compatible.
|
|
1180 (let ((calls (assq (nth 1 form) byte-compile-unresolved-functions))
|
|
1181 nums sig min max)
|
|
1182 (if calls
|
|
1183 (progn
|
|
1184 (setq sig (byte-compile-arglist-signature (nth 2 form))
|
|
1185 nums (sort (copy-sequence (cdr calls)) (function <))
|
|
1186 min (car nums)
|
|
1187 max (car (nreverse nums)))
|
|
1188 (if (or (< min (car sig))
|
|
1189 (and (cdr sig) (> max (cdr sig))))
|
|
1190 (byte-compile-warn
|
|
1191 "%s being defined to take %s%s, but was previously called with %s"
|
|
1192 (nth 1 form)
|
|
1193 (byte-compile-arglist-signature-string sig)
|
|
1194 (if (equal sig '(1 . 1)) " arg" " args")
|
|
1195 (byte-compile-arglist-signature-string (cons min max))))
|
272
|
1196
|
209
|
1197 (setq byte-compile-unresolved-functions
|
|
1198 (delq calls byte-compile-unresolved-functions)))))
|
|
1199 )))
|
|
1200
|
272
|
1201 ;; If we have compiled any calls to functions which are not known to be
|
209
|
1202 ;; defined, issue a warning enumerating them.
|
|
1203 ;; `unresolved' in the list `byte-compile-warnings' disables this.
|
|
1204 (defun byte-compile-warn-about-unresolved-functions (&optional msg)
|
|
1205 (if (memq 'unresolved byte-compile-warnings)
|
|
1206 (let ((byte-compile-current-form (or msg "the end of the data")))
|
|
1207 ;; First delete the autoloads from the list.
|
|
1208 (if byte-compile-autoload-environment
|
|
1209 (let ((rest byte-compile-unresolved-functions))
|
|
1210 (while rest
|
|
1211 (if (assq (car (car rest)) byte-compile-autoload-environment)
|
|
1212 (setq byte-compile-unresolved-functions
|
|
1213 (delq (car rest) byte-compile-unresolved-functions)))
|
|
1214 (setq rest (cdr rest)))))
|
|
1215 ;; Now warn.
|
|
1216 (if (cdr byte-compile-unresolved-functions)
|
|
1217 (let* ((str "The following functions are not known to be defined: ")
|
|
1218 (L (+ (length str) 5))
|
|
1219 (rest (reverse byte-compile-unresolved-functions))
|
|
1220 s)
|
|
1221 (while rest
|
|
1222 (setq s (symbol-name (car (car rest)))
|
|
1223 L (+ L (length s) 2)
|
|
1224 rest (cdr rest))
|
|
1225 (if (<= L (1- fill-column))
|
|
1226 (setq str (concat str " " s (and rest ",")))
|
|
1227 (setq str (concat str "\n " s (and rest ","))
|
|
1228 L (+ (length s) 4))))
|
|
1229 (byte-compile-warn "%s" str))
|
|
1230 (if byte-compile-unresolved-functions
|
|
1231 (byte-compile-warn "the function %s is not known to be defined."
|
|
1232 (car (car byte-compile-unresolved-functions)))))))
|
|
1233 nil)
|
|
1234
|
|
1235 (defun byte-compile-defvar-p (var)
|
|
1236 ;; Whether the byte compiler thinks that nonexical references to this
|
|
1237 ;; variable are ok.
|
|
1238 (or (globally-boundp var)
|
|
1239 (let ((rest byte-compile-bound-variables))
|
|
1240 (while (and rest var)
|
|
1241 (if (and (eq var (car-safe (car rest)))
|
|
1242 (not (= 0 (logand (cdr (car rest))
|
|
1243 byte-compile-global-bit))))
|
|
1244 (setq var nil))
|
|
1245 (setq rest (cdr rest)))
|
|
1246 ;; if var is nil at this point, it's a defvar in this file.
|
|
1247 (not var))))
|
|
1248
|
|
1249
|
|
1250 ;;; If we have compiled bindings of variables which have no referents, warn.
|
|
1251 (defun byte-compile-warn-about-unused-variables ()
|
|
1252 (let ((rest byte-compile-bound-variables)
|
|
1253 (unreferenced '())
|
|
1254 cell)
|
|
1255 (while (and rest
|
|
1256 ;; only warn about variables whose lifetime is now ending,
|
|
1257 ;; that is, variables from the lexical scope that is now
|
|
1258 ;; terminating. (Think nested lets.)
|
|
1259 (not (eq (car rest) 'new-scope)))
|
|
1260 (setq cell (car rest))
|
|
1261 (if (and (= 0 (logand byte-compile-referenced-bit (cdr cell)))
|
|
1262 ;; Don't warn about declared-but-unused arguments,
|
|
1263 ;; for two reasons: first, the arglist structure
|
|
1264 ;; might be imposed by external forces, and we don't
|
|
1265 ;; have (declare (ignore x)) yet; and second, inline
|
|
1266 ;; expansion produces forms like
|
|
1267 ;; ((lambda (arg) (byte-code "..." [arg])) x)
|
|
1268 ;; which we can't (ok, well, don't) recognise as
|
|
1269 ;; containing a reference to arg, so every inline
|
|
1270 ;; expansion would generate a warning. (If we had
|
|
1271 ;; `ignore' then inline expansion could emit an
|
|
1272 ;; ignore declaration.)
|
|
1273 (= 0 (logand byte-compile-arglist-bit (cdr cell)))
|
|
1274 ;; Don't warn about defvars because this is a
|
|
1275 ;; legitimate special binding.
|
|
1276 (not (byte-compile-defvar-p (car cell))))
|
|
1277 (setq unreferenced (cons (car cell) unreferenced)))
|
|
1278 (setq rest (cdr rest)))
|
|
1279 (setq unreferenced (nreverse unreferenced))
|
|
1280 (while unreferenced
|
|
1281 (byte-compile-warn
|
|
1282 (format "variable %s bound but not referenced" (car unreferenced)))
|
|
1283 (setq unreferenced (cdr unreferenced)))))
|
|
1284
|
|
1285
|
|
1286 (defmacro byte-compile-constp (form)
|
|
1287 ;; Returns non-nil if FORM is a constant.
|
|
1288 (` (cond ((consp (, form)) (eq (car (, form)) 'quote))
|
|
1289 ((not (symbolp (, form))))
|
|
1290 ((keywordp (, form)))
|
|
1291 ((memq (, form) '(nil t))))))
|
|
1292
|
|
1293 (defmacro byte-compile-close-variables (&rest body)
|
272
|
1294 `(let
|
|
1295 (;;
|
|
1296 ;; Close over these variables to encapsulate the
|
|
1297 ;; compilation state
|
|
1298 ;;
|
|
1299 (byte-compile-macro-environment
|
|
1300 ;; Copy it because the compiler may patch into the
|
|
1301 ;; macroenvironment.
|
|
1302 (copy-alist byte-compile-initial-macro-environment))
|
|
1303 (byte-compile-function-environment nil)
|
|
1304 (byte-compile-autoload-environment nil)
|
|
1305 (byte-compile-unresolved-functions nil)
|
|
1306 (byte-compile-bound-variables nil)
|
|
1307 (byte-compile-free-references nil)
|
|
1308 (byte-compile-free-assignments nil)
|
|
1309 ;;
|
|
1310 ;; Close over these variables so that `byte-compiler-options'
|
|
1311 ;; can change them on a per-file basis.
|
|
1312 ;;
|
|
1313 (byte-compile-verbose byte-compile-verbose)
|
|
1314 (byte-optimize byte-optimize)
|
|
1315 (byte-compile-emacs19-compatibility
|
|
1316 byte-compile-emacs19-compatibility)
|
|
1317 (byte-compile-dynamic byte-compile-dynamic)
|
|
1318 (byte-compile-dynamic-docstrings
|
|
1319 byte-compile-dynamic-docstrings)
|
|
1320 (byte-compile-warnings (if (eq byte-compile-warnings t)
|
|
1321 byte-compile-default-warnings
|
|
1322 byte-compile-warnings))
|
|
1323 (byte-compile-file-domain nil)
|
|
1324 )
|
|
1325 (prog1
|
|
1326 (progn ,@body)
|
|
1327 (if (memq 'unused-vars byte-compile-warnings)
|
|
1328 ;; done compiling in this scope, warn now.
|
|
1329 (byte-compile-warn-about-unused-variables)))))
|
209
|
1330
|
|
1331
|
|
1332 (defvar byte-compile-warnings-point-max nil)
|
|
1333 (defmacro displaying-byte-compile-warnings (&rest body)
|
272
|
1334 `(let ((byte-compile-warnings-point-max byte-compile-warnings-point-max))
|
209
|
1335 ;; Log the file name.
|
272
|
1336 (byte-compile-log-file)
|
209
|
1337 ;; Record how much is logged now.
|
|
1338 ;; We will display the log buffer if anything more is logged
|
|
1339 ;; before the end of BODY.
|
272
|
1340 (or byte-compile-warnings-point-max
|
|
1341 (save-excursion
|
|
1342 (set-buffer (get-buffer-create "*Compile-Log*"))
|
|
1343 (setq byte-compile-warnings-point-max (point-max))))
|
|
1344 (unwind-protect
|
|
1345 (condition-case error-info
|
|
1346 (progn ,@body)
|
|
1347 (error
|
|
1348 (byte-compile-report-error error-info)))
|
|
1349 (save-excursion
|
|
1350 ;; If there were compilation warnings, display them.
|
|
1351 (set-buffer "*Compile-Log*")
|
|
1352 (if (= byte-compile-warnings-point-max (point-max))
|
|
1353 nil
|
|
1354 (if temp-buffer-show-function
|
|
1355 (let ((show-buffer (get-buffer-create "*Compile-Log-Show*")))
|
|
1356 (save-excursion
|
|
1357 (set-buffer show-buffer)
|
|
1358 (setq buffer-read-only nil)
|
|
1359 (erase-buffer))
|
|
1360 (copy-to-buffer show-buffer
|
|
1361 (save-excursion
|
|
1362 (goto-char byte-compile-warnings-point-max)
|
|
1363 (forward-line -1)
|
|
1364 (point))
|
|
1365 (point-max))
|
|
1366 (funcall temp-buffer-show-function show-buffer))
|
259
|
1367 (select-window
|
|
1368 (prog1 (selected-window)
|
|
1369 (select-window (display-buffer (current-buffer)))
|
|
1370 (goto-char byte-compile-warnings-point-max)
|
|
1371 (recenter 1)))))))))
|
209
|
1372
|
|
1373
|
|
1374 ;;;###autoload
|
|
1375 (defun byte-force-recompile (directory)
|
|
1376 "Recompile every `.el' file in DIRECTORY that already has a `.elc' file.
|
|
1377 Files in subdirectories of DIRECTORY are processed also."
|
|
1378 (interactive "DByte force recompile (directory): ")
|
|
1379 (byte-recompile-directory directory nil t))
|
|
1380
|
|
1381 ;;;###autoload
|
|
1382 (defun byte-recompile-directory (directory &optional arg norecursion force)
|
|
1383 "Recompile every `.el' file in DIRECTORY that needs recompilation.
|
|
1384 This is if a `.elc' file exists but is older than the `.el' file.
|
|
1385 Files in subdirectories of DIRECTORY are processed also unless argument
|
|
1386 NORECURSION is non-nil.
|
|
1387
|
|
1388 If the `.elc' file does not exist, normally the `.el' file is *not* compiled.
|
|
1389 But a prefix argument (optional second arg) means ask user,
|
|
1390 for each such `.el' file, whether to compile it. Prefix argument 0 means
|
|
1391 don't ask and compile the file anyway.
|
|
1392
|
|
1393 A nonzero prefix argument also means ask about each subdirectory.
|
|
1394
|
|
1395 If the fourth argument FORCE is non-nil,
|
|
1396 recompile every `.el' file that already has a `.elc' file."
|
|
1397 (interactive "DByte recompile directory: \nP")
|
|
1398 (if arg
|
|
1399 (setq arg (prefix-numeric-value arg)))
|
|
1400 (if noninteractive
|
|
1401 nil
|
|
1402 (save-some-buffers)
|
|
1403 (redraw-modeline))
|
|
1404 (let ((directories (list (expand-file-name directory)))
|
|
1405 (file-count 0)
|
|
1406 (dir-count 0)
|
|
1407 last-dir)
|
|
1408 (displaying-byte-compile-warnings
|
|
1409 (while directories
|
|
1410 (setq directory (file-name-as-directory (car directories)))
|
|
1411 (or noninteractive (message "Checking %s..." directory))
|
|
1412 (let ((files (directory-files directory))
|
|
1413 source dest)
|
|
1414 (while files
|
|
1415 (setq source (expand-file-name (car files) directory))
|
|
1416 (if (and (not (member (car files) '("." ".." "RCS" "CVS" "SCCS")))
|
|
1417 ;; Stay away from directory back-links, etc:
|
|
1418 (not (file-symlink-p source))
|
|
1419 (file-directory-p source)
|
|
1420 byte-recompile-directory-recursively)
|
|
1421 ;; This file is a subdirectory. Handle them differently.
|
|
1422 (if (or (null arg)
|
|
1423 (eq arg 0)
|
|
1424 (y-or-n-p (concat "Check " source "? ")))
|
|
1425 (setq directories
|
|
1426 (nconc directories (list source))))
|
|
1427 ;; It is an ordinary file. Decide whether to compile it.
|
|
1428 (if (and (string-match emacs-lisp-file-regexp source)
|
|
1429 (not (auto-save-file-name-p source))
|
|
1430 (setq dest (byte-compile-dest-file source))
|
|
1431 (if (file-exists-p dest)
|
|
1432 ;; File was already compiled.
|
|
1433 (or force (file-newer-than-file-p source dest))
|
|
1434 ;; No compiled file exists yet.
|
|
1435 (and arg
|
|
1436 (or (eq 0 arg)
|
|
1437 (y-or-n-p (concat "Compile " source "? "))))))
|
|
1438 (progn ;(if (and noninteractive (not byte-compile-verbose))
|
|
1439 ; (message "Compiling %s..." source))
|
|
1440 ; we do this in byte-compile-file.
|
|
1441 (if byte-recompile-directory-ignore-errors-p
|
|
1442 (batch-byte-compile-1 source)
|
|
1443 (byte-compile-file source))
|
|
1444 (or noninteractive
|
|
1445 (message "Checking %s..." directory))
|
|
1446 (setq file-count (1+ file-count))
|
|
1447 (if (not (eq last-dir directory))
|
|
1448 (setq last-dir directory
|
|
1449 dir-count (1+ dir-count)))
|
|
1450 )))
|
|
1451 (setq files (cdr files))))
|
|
1452 (setq directories (cdr directories))))
|
|
1453 (message "Done (Total of %d file%s compiled%s)"
|
|
1454 file-count (if (= file-count 1) "" "s")
|
|
1455 (if (> dir-count 1) (format " in %d directories" dir-count) ""))))
|
|
1456
|
|
1457 ;;;###autoload
|
|
1458 (defun byte-recompile-file (filename &optional force)
|
|
1459 "Recompile a file of Lisp code named FILENAME if it needs recompilation.
|
|
1460 This is if the `.elc' file exists but is older than the `.el' file.
|
|
1461
|
|
1462 If the `.elc' file does not exist, normally the `.el' file is *not*
|
|
1463 compiled. But a prefix argument (optional second arg) means ask user
|
|
1464 whether to compile it. Prefix argument 0 don't ask and recompile anyway."
|
|
1465 (interactive "fByte recompile file: \nP")
|
|
1466 (let ((dest))
|
|
1467 (if (and (string-match emacs-lisp-file-regexp filename)
|
|
1468 (not (auto-save-file-name-p filename))
|
|
1469 (setq dest (byte-compile-dest-file filename))
|
|
1470 (if (file-exists-p dest)
|
|
1471 (file-newer-than-file-p filename dest)
|
|
1472 (and force
|
|
1473 (or (eq 0 force)
|
|
1474 (y-or-n-p (concat "Compile " filename "? "))))))
|
272
|
1475 (byte-compile-file filename))))
|
209
|
1476
|
|
1477 (defvar kanji-flag nil)
|
|
1478
|
|
1479 ;;;###autoload
|
|
1480 (defun byte-compile-file (filename &optional load)
|
|
1481 "Compile a file of Lisp code named FILENAME into a file of byte code.
|
|
1482 The output file's name is made by appending `c' to the end of FILENAME.
|
|
1483 With prefix arg (noninteractively: 2nd arg), load the file after compiling."
|
|
1484 ;; (interactive "fByte compile file: \nP")
|
|
1485 (interactive
|
|
1486 (let ((file buffer-file-name)
|
|
1487 (file-name nil)
|
|
1488 (file-dir nil))
|
|
1489 (and file
|
|
1490 (eq (cdr (assq 'major-mode (buffer-local-variables)))
|
|
1491 'emacs-lisp-mode)
|
|
1492 (setq file-name (file-name-nondirectory file)
|
|
1493 file-dir (file-name-directory file)))
|
|
1494 (list (read-file-name (if current-prefix-arg
|
|
1495 "Byte compile and load file: "
|
|
1496 "Byte compile file: ")
|
|
1497 file-dir nil nil file-name)
|
|
1498 current-prefix-arg)))
|
|
1499 ;; Expand now so we get the current buffer's defaults
|
|
1500 (setq filename (expand-file-name filename))
|
|
1501
|
|
1502 ;; If we're compiling a file that's in a buffer and is modified, offer
|
|
1503 ;; to save it first.
|
|
1504 (or noninteractive
|
|
1505 (let ((b (get-file-buffer (expand-file-name filename))))
|
|
1506 (if (and b (buffer-modified-p b)
|
|
1507 (y-or-n-p (format "save buffer %s first? " (buffer-name b))))
|
|
1508 (save-excursion (set-buffer b) (save-buffer)))))
|
|
1509
|
|
1510 (if (or noninteractive byte-compile-verbose) ; XEmacs change
|
|
1511 (message "Compiling %s..." filename))
|
|
1512 (let (;;(byte-compile-current-file (file-name-nondirectory filename))
|
|
1513 (byte-compile-current-file filename)
|
|
1514 (debug-issue-ebola-notices 0) ; Hack -slb
|
|
1515 target-file input-buffer output-buffer
|
|
1516 byte-compile-dest-file)
|
|
1517 (setq target-file (byte-compile-dest-file filename))
|
|
1518 (setq byte-compile-dest-file target-file)
|
|
1519 (save-excursion
|
|
1520 (setq input-buffer (get-buffer-create " *Compiler Input*"))
|
|
1521 (set-buffer input-buffer)
|
|
1522 (erase-buffer)
|
|
1523 (insert-file-contents filename)
|
|
1524 ;; Run hooks including the uncompression hook.
|
|
1525 ;; If they change the file name, then change it for the output also.
|
|
1526 (let ((buffer-file-name filename)
|
|
1527 (default-major-mode 'emacs-lisp-mode)
|
|
1528 (enable-local-eval nil))
|
|
1529 (normal-mode)
|
|
1530 (setq filename buffer-file-name)))
|
|
1531 (setq byte-compiler-error-flag nil)
|
|
1532 ;; It is important that input-buffer not be current at this call,
|
|
1533 ;; so that the value of point set in input-buffer
|
|
1534 ;; within byte-compile-from-buffer lingers in that buffer.
|
|
1535 (setq output-buffer (byte-compile-from-buffer input-buffer filename))
|
|
1536 (if byte-compiler-error-flag
|
|
1537 nil
|
|
1538 (if byte-compile-verbose
|
|
1539 (message "Compiling %s...done" filename))
|
|
1540 (kill-buffer input-buffer)
|
|
1541 (save-excursion
|
|
1542 (set-buffer output-buffer)
|
|
1543 (goto-char (point-max))
|
|
1544 (insert "\n") ; aaah, unix.
|
|
1545 (let ((vms-stmlf-recfm t))
|
|
1546 (setq target-file (byte-compile-dest-file filename))
|
|
1547 (or byte-compile-overwrite-file
|
|
1548 (condition-case ()
|
|
1549 (delete-file target-file)
|
|
1550 (error nil)))
|
|
1551 (if (file-writable-p target-file)
|
|
1552 (let ((kanji-flag nil)) ; for nemacs, from Nakagawa Takayuki
|
|
1553 (if (or (eq system-type 'ms-dos) (eq system-type 'windows-nt))
|
|
1554 (setq buffer-file-type t))
|
|
1555 (write-region 1 (point-max) target-file))
|
|
1556 ;; This is just to give a better error message than write-region
|
|
1557 (signal 'file-error
|
|
1558 (list "Opening output file"
|
|
1559 (if (file-exists-p target-file)
|
|
1560 "cannot overwrite file"
|
|
1561 "directory not writable or nonexistent")
|
|
1562 target-file)))
|
|
1563 (or byte-compile-overwrite-file
|
|
1564 (condition-case ()
|
|
1565 (set-file-modes target-file (file-modes filename))
|
|
1566 (error nil))))
|
|
1567 (kill-buffer (current-buffer)))
|
|
1568 (if (and byte-compile-generate-call-tree
|
|
1569 (or (eq t byte-compile-generate-call-tree)
|
|
1570 (y-or-n-p (format "Report call tree for %s? " filename))))
|
|
1571 (save-excursion
|
|
1572 (display-call-tree filename)))
|
|
1573 (if load
|
|
1574 (load target-file))
|
|
1575 t)))
|
|
1576
|
|
1577 ;; RMS comments the next two out.
|
284
|
1578
|
|
1579 ;;;###autoload
|
209
|
1580 (defun byte-compile-and-load-file (&optional filename)
|
|
1581 "Compile a file of Lisp code named FILENAME into a file of byte code,
|
272
|
1582 and then load it. The output file's name is made by appending \"c\" to
|
209
|
1583 the end of FILENAME."
|
|
1584 (interactive)
|
|
1585 (if filename ; I don't get it, (interactive-p) doesn't always work
|
|
1586 (byte-compile-file filename t)
|
|
1587 (let ((current-prefix-arg '(4)))
|
|
1588 (call-interactively 'byte-compile-file))))
|
|
1589
|
284
|
1590 ;;;###autoload
|
209
|
1591 (defun byte-compile-buffer (&optional buffer)
|
|
1592 "Byte-compile and evaluate contents of BUFFER (default: the current buffer)."
|
|
1593 (interactive "bByte compile buffer: ")
|
|
1594 (setq buffer (if buffer (get-buffer buffer) (current-buffer)))
|
284
|
1595 (message "Compiling %s..." buffer)
|
209
|
1596 (let* ((filename (or (buffer-file-name buffer)
|
284
|
1597 (prin1-to-string buffer)))
|
|
1598 (byte-compile-current-file buffer))
|
209
|
1599 (byte-compile-from-buffer buffer filename t))
|
284
|
1600 (message "Compiling %s...done" buffer)
|
209
|
1601 t)
|
|
1602
|
|
1603 ;;; compiling a single function
|
|
1604 ;;;###autoload
|
|
1605 (defun compile-defun (&optional arg)
|
|
1606 "Compile and evaluate the current top-level form.
|
|
1607 Print the result in the minibuffer.
|
|
1608 With argument, insert value in current buffer after the form."
|
|
1609 (interactive "P")
|
|
1610 (save-excursion
|
|
1611 (end-of-defun)
|
|
1612 (beginning-of-defun)
|
|
1613 (let* ((byte-compile-current-file (buffer-file-name))
|
|
1614 (load-file-name (buffer-file-name))
|
|
1615 (byte-compile-last-warned-form 'nothing)
|
|
1616 (value (eval (displaying-byte-compile-warnings
|
|
1617 (byte-compile-sexp (read (current-buffer))
|
|
1618 "toplevel forms")))))
|
|
1619 (cond (arg
|
|
1620 (message "Compiling from buffer... done.")
|
|
1621 (prin1 value (current-buffer))
|
|
1622 (insert "\n"))
|
|
1623 ((message "%s" (prin1-to-string value)))))))
|
|
1624
|
|
1625 (defvar byte-compile-inbuffer)
|
|
1626 (defvar byte-compile-outbuffer)
|
|
1627
|
|
1628 (defun byte-compile-from-buffer (byte-compile-inbuffer filename &optional eval)
|
|
1629 ;; buffer --> output-buffer, or buffer --> eval form, return nil
|
|
1630 (let (byte-compile-outbuffer
|
|
1631 ;; Prevent truncation of flonums and lists as we read and print them
|
|
1632 (float-output-format nil)
|
|
1633 (case-fold-search nil)
|
|
1634 (print-length nil)
|
|
1635 (print-level nil)
|
|
1636 ;; Simulate entry to byte-compile-top-level
|
|
1637 (byte-compile-constants nil)
|
|
1638 (byte-compile-variables nil)
|
|
1639 (byte-compile-tag-number 0)
|
|
1640 (byte-compile-depth 0)
|
|
1641 (byte-compile-maxdepth 0)
|
|
1642 (byte-compile-output nil)
|
|
1643 ;; #### This is bound in b-c-close-variables.
|
|
1644 ;; (byte-compile-warnings (if (eq byte-compile-warnings t)
|
|
1645 ;; byte-compile-warning-types
|
|
1646 ;; byte-compile-warnings))
|
|
1647 )
|
|
1648 (byte-compile-close-variables
|
|
1649 (save-excursion
|
|
1650 (setq byte-compile-outbuffer
|
|
1651 (set-buffer (get-buffer-create " *Compiler Output*")))
|
|
1652 (erase-buffer)
|
|
1653 ;; (emacs-lisp-mode)
|
|
1654 (setq case-fold-search nil)
|
|
1655 (and filename
|
|
1656 (not eval)
|
|
1657 (byte-compile-insert-header filename
|
|
1658 byte-compile-inbuffer
|
|
1659 byte-compile-outbuffer))
|
|
1660
|
|
1661 ;; This is a kludge. Some operating systems (OS/2, DOS) need to
|
|
1662 ;; write files containing binary information specially.
|
|
1663 ;; Under most circumstances, such files will be in binary
|
|
1664 ;; overwrite mode, so those OS's use that flag to guess how
|
|
1665 ;; they should write their data. Advise them that .elc files
|
|
1666 ;; need to be written carefully.
|
|
1667 (setq overwrite-mode 'overwrite-mode-binary))
|
|
1668 (displaying-byte-compile-warnings
|
|
1669 (save-excursion
|
|
1670 (set-buffer byte-compile-inbuffer)
|
|
1671 (goto-char 1)
|
|
1672
|
|
1673 ;; Compile the forms from the input buffer.
|
|
1674 (while (progn
|
|
1675 (while (progn (skip-chars-forward " \t\n\^l")
|
|
1676 (looking-at ";"))
|
|
1677 (forward-line 1))
|
|
1678 (not (eobp)))
|
|
1679 (byte-compile-file-form (read byte-compile-inbuffer)))
|
|
1680
|
|
1681 ;; Compile pending forms at end of file.
|
|
1682 (byte-compile-flush-pending)
|
|
1683 (byte-compile-warn-about-unresolved-functions)
|
272
|
1684 ;; Should we always do this? When calling multiple files, it
|
209
|
1685 ;; would be useful to delay this warning until all have
|
|
1686 ;; been compiled.
|
|
1687 (setq byte-compile-unresolved-functions nil)))
|
|
1688 (save-excursion
|
|
1689 (set-buffer byte-compile-outbuffer)
|
|
1690 (goto-char (point-min))))
|
|
1691 (if (not eval)
|
|
1692 byte-compile-outbuffer
|
|
1693 (let (form)
|
|
1694 (while (condition-case nil
|
|
1695 (progn (setq form (read byte-compile-outbuffer))
|
|
1696 t)
|
|
1697 (end-of-file nil))
|
|
1698 (eval form)))
|
|
1699 (kill-buffer byte-compile-outbuffer)
|
|
1700 nil)))
|
|
1701
|
|
1702 (defun byte-compile-insert-header (filename byte-compile-inbuffer
|
|
1703 byte-compile-outbuffer)
|
|
1704 (set-buffer byte-compile-inbuffer)
|
|
1705 (let ((dynamic-docstrings byte-compile-dynamic-docstrings))
|
|
1706 (set-buffer byte-compile-outbuffer)
|
|
1707 (goto-char 1)
|
|
1708 ;;
|
|
1709 ;; The magic number of .elc files is ";ELC", or 0x3B454C43. After that is
|
|
1710 ;; the file-format version number (19 or 20) as a byte, followed by some
|
|
1711 ;; nulls. The primary motivation for doing this is to get some binary
|
|
1712 ;; characters up in the first line of the file so that `diff' will simply
|
|
1713 ;; say "Binary files differ" instead of actually doing a diff of two .elc
|
|
1714 ;; files. An extra benefit is that you can add this to /etc/magic:
|
|
1715 ;;
|
|
1716 ;; 0 string ;ELC GNU Emacs Lisp compiled file,
|
|
1717 ;; >4 byte x version %d
|
|
1718 ;;
|
|
1719 (insert
|
|
1720 ";ELC"
|
|
1721 (if (byte-compile-version-cond byte-compile-emacs19-compatibility) 19 20)
|
|
1722 "\000\000\000\n"
|
|
1723 )
|
|
1724 (insert ";;; compiled by "
|
|
1725 (or (and (boundp 'user-mail-address) user-mail-address)
|
|
1726 (concat (user-login-name) "@" (system-name)))
|
|
1727 " on "
|
|
1728 (current-time-string) "\n;;; from file " filename "\n")
|
|
1729 (insert ";;; emacs version " emacs-version ".\n")
|
|
1730 (insert ";;; bytecomp version " byte-compile-version "\n;;; "
|
|
1731 (cond
|
|
1732 ((eq byte-optimize 'source) "source-level optimization only")
|
|
1733 ((eq byte-optimize 'byte) "byte-level optimization only")
|
|
1734 (byte-optimize "optimization is on")
|
|
1735 (t "optimization is off"))
|
|
1736 (if (byte-compile-version-cond byte-compile-emacs19-compatibility)
|
|
1737 "; compiled with Emacs 19 compatibility.\n"
|
|
1738 ".\n"))
|
|
1739 (if (not (byte-compile-version-cond byte-compile-emacs19-compatibility))
|
|
1740 (insert ";;; this file uses opcodes which do not exist in Emacs 19.\n"
|
|
1741 ;; Have to check if emacs-version is bound so that this works
|
|
1742 ;; in files loaded early in loadup.el.
|
|
1743 "\n(if (and (boundp 'emacs-version)\n"
|
|
1744 "\t (or (and (boundp 'epoch::version) epoch::version)\n"
|
|
1745 "\t (string-lessp emacs-version \"20\")))\n"
|
|
1746 " (error \"`"
|
|
1747 ;; prin1-to-string is used to quote backslashes.
|
|
1748 (substring (prin1-to-string (file-name-nondirectory filename))
|
|
1749 1 -1)
|
|
1750 "' was compiled for Emacs 20\"))\n\n"))
|
|
1751 (insert "(or (boundp 'current-load-list) (setq current-load-list nil))\n"
|
|
1752 "\n")
|
|
1753 (if (and (byte-compile-version-cond byte-compile-emacs19-compatibility)
|
|
1754 dynamic-docstrings)
|
|
1755 (insert ";;; this file uses opcodes which do not exist prior to\n"
|
|
1756 ";;; XEmacs 19.14/GNU Emacs 19.29 or later."
|
|
1757 ;; Have to check if emacs-version is bound so that this works
|
|
1758 ;; in files loaded early in loadup.el.
|
|
1759 "\n(if (and (boundp 'emacs-version)\n"
|
|
1760 "\t (or (and (boundp 'epoch::version) epoch::version)\n"
|
|
1761 "\t (and (not (string-match \"XEmacs\" emacs-version))\n"
|
|
1762 "\t (string-lessp emacs-version \"19.29\"))\n"
|
|
1763 "\t (string-lessp emacs-version \"19.14\")))\n"
|
|
1764 " (error \"`"
|
|
1765 ;; prin1-to-string is used to quote backslashes.
|
|
1766 (substring (prin1-to-string (file-name-nondirectory filename))
|
|
1767 1 -1)
|
|
1768 "' was compiled for XEmacs 19.14/Emacs 19.29 or later\"))\n\n"
|
|
1769 )
|
|
1770 ))
|
|
1771
|
|
1772 ;; back in the inbuffer; determine and set the coding system for the .elc
|
|
1773 ;; file if under Mule. If there are any extended characters in the
|
|
1774 ;; input file, use `escape-quoted' to make sure that both binary and
|
|
1775 ;; extended characters are output properly and distinguished properly.
|
|
1776 ;; Otherwise, use `no-conversion' for maximum portability with non-Mule
|
|
1777 ;; Emacsen.
|
|
1778 (if (featurep 'mule)
|
|
1779 (if (save-excursion
|
|
1780 (set-buffer byte-compile-inbuffer)
|
|
1781 (goto-char (point-min))
|
|
1782 ;; mrb- There must be a better way than skip-chars-forward
|
|
1783 (skip-chars-forward (concat (char-to-string 0) "-"
|
|
1784 (char-to-string 255)))
|
|
1785 (eq (point) (point-max)))
|
|
1786 (setq buffer-file-coding-system 'no-conversion)
|
|
1787 (insert "(require 'mule)\n;;;###coding system: escape-quoted\n")
|
|
1788 (setq buffer-file-coding-system 'escape-quoted)
|
|
1789 ;; Lazy loading not yet implemented for MULE files
|
|
1790 ;; mrb - Fix this someday.
|
|
1791 (save-excursion
|
|
1792 (set-buffer byte-compile-inbuffer)
|
272
|
1793 (setq byte-compile-dynamic nil
|
209
|
1794 byte-compile-dynamic-docstrings nil))
|
|
1795 ;;(external-debugging-output (prin1-to-string (buffer-local-variables))))
|
|
1796 ))
|
|
1797 )
|
|
1798
|
|
1799
|
|
1800 (defun byte-compile-output-file-form (form)
|
|
1801 ;; writes the given form to the output buffer, being careful of docstrings
|
|
1802 ;; in defun, defmacro, defvar, defconst and autoload because make-docfile is
|
|
1803 ;; so amazingly stupid.
|
|
1804 ;; defalias calls are output directly by byte-compile-file-form-defmumble;
|
|
1805 ;; it does not pay to first build the defalias in defmumble and then parse
|
|
1806 ;; it here.
|
|
1807 (if (and (memq (car-safe form) '(defun defmacro defvar defconst autoload))
|
|
1808 (stringp (nth 3 form)))
|
|
1809 (byte-compile-output-docform nil nil '("\n(" 3 ")") form nil
|
|
1810 (eq (car form) 'autoload))
|
|
1811 (let ((print-escape-newlines t)
|
|
1812 (print-length nil)
|
|
1813 (print-level nil)
|
|
1814 (print-readably t) ; print #[] for bytecode, 'x for (quote x)
|
272
|
1815 (print-gensym (if (and byte-compile-print-gensym
|
|
1816 (not byte-compile-emacs19-compatibility))
|
|
1817 t nil)))
|
209
|
1818 (princ "\n" byte-compile-outbuffer)
|
|
1819 (prin1 form byte-compile-outbuffer)
|
|
1820 nil)))
|
|
1821
|
|
1822 (defun byte-compile-output-docform (preface name info form specindex quoted)
|
|
1823 "Print a form with a doc string. INFO is (prefix doc-index postfix).
|
|
1824 If PREFACE and NAME are non-nil, print them too,
|
|
1825 before INFO and the FORM but after the doc string itself.
|
|
1826 If SPECINDEX is non-nil, it is the index in FORM
|
|
1827 of the function bytecode string. In that case,
|
|
1828 we output that argument and the following argument (the constants vector)
|
|
1829 together, for lazy loading.
|
|
1830 QUOTED says that we have to put a quote before the
|
|
1831 list that represents a doc string reference.
|
|
1832 `autoload' needs that."
|
|
1833 ;; We need to examine byte-compile-dynamic-docstrings
|
|
1834 ;; in the input buffer (now current), not in the output buffer.
|
|
1835 (let ((dynamic-docstrings byte-compile-dynamic-docstrings))
|
|
1836 (set-buffer
|
|
1837 (prog1 (current-buffer)
|
|
1838 (set-buffer byte-compile-outbuffer)
|
|
1839 (let (position)
|
|
1840
|
|
1841 ;; Insert the doc string, and make it a comment with #@LENGTH.
|
|
1842 (and (>= (nth 1 info) 0)
|
|
1843 dynamic-docstrings
|
|
1844 (progn
|
|
1845 ;; Make the doc string start at beginning of line
|
|
1846 ;; for make-docfile's sake.
|
|
1847 (insert "\n")
|
|
1848 (setq position
|
|
1849 (byte-compile-output-as-comment
|
|
1850 (nth (nth 1 info) form) nil))
|
|
1851 ;; If the doc string starts with * (a user variable),
|
|
1852 ;; negate POSITION.
|
|
1853 (if (and (stringp (nth (nth 1 info) form))
|
|
1854 (> (length (nth (nth 1 info) form)) 0)
|
|
1855 (char= (aref (nth (nth 1 info) form) 0) ?*))
|
|
1856 (setq position (- position)))))
|
|
1857
|
|
1858 (if preface
|
|
1859 (progn
|
|
1860 (insert preface)
|
|
1861 (prin1 name byte-compile-outbuffer)))
|
|
1862 (insert (car info))
|
|
1863 (let ((print-escape-newlines t)
|
|
1864 (print-readably t) ; print #[] for bytecode, 'x for (quote x)
|
245
|
1865 ;; Use a cons cell to say that we want
|
|
1866 ;; print-gensym-alist not to be cleared between calls
|
|
1867 ;; to print functions.
|
272
|
1868 (print-gensym (if (and byte-compile-print-gensym
|
|
1869 (not byte-compile-emacs19-compatibility))
|
|
1870 '(t) nil))
|
245
|
1871 print-gensym-alist
|
209
|
1872 (index 0))
|
|
1873 (prin1 (car form) byte-compile-outbuffer)
|
|
1874 (while (setq form (cdr form))
|
|
1875 (setq index (1+ index))
|
|
1876 (insert " ")
|
|
1877 (cond ((and (numberp specindex) (= index specindex))
|
|
1878 (let ((position
|
|
1879 (byte-compile-output-as-comment
|
|
1880 (cons (car form) (nth 1 form))
|
|
1881 t)))
|
|
1882 (princ (format "(#$ . %d) nil" position)
|
|
1883 byte-compile-outbuffer)
|
|
1884 (setq form (cdr form))
|
|
1885 (setq index (1+ index))))
|
|
1886 ((= index (nth 1 info))
|
|
1887 (if position
|
|
1888 (princ (format (if quoted "'(#$ . %d)" "(#$ . %d)")
|
|
1889 position)
|
|
1890 byte-compile-outbuffer)
|
|
1891 (let ((print-escape-newlines nil))
|
|
1892 (goto-char (prog1 (1+ (point))
|
|
1893 (prin1 (car form)
|
|
1894 byte-compile-outbuffer)))
|
|
1895 (insert "\\\n")
|
|
1896 (goto-char (point-max)))))
|
|
1897 (t
|
|
1898 (prin1 (car form) byte-compile-outbuffer)))))
|
|
1899 (insert (nth 2 info))))))
|
|
1900 nil)
|
|
1901
|
|
1902 (defvar for-effect) ; ## Kludge! This should be an arg, not a special.
|
|
1903
|
|
1904 (defun byte-compile-keep-pending (form &optional handler)
|
|
1905 (if (memq byte-optimize '(t source))
|
|
1906 (setq form (byte-optimize-form form t)))
|
|
1907 (if handler
|
|
1908 (let ((for-effect t))
|
|
1909 ;; To avoid consing up monstrously large forms at load time, we split
|
|
1910 ;; the output regularly.
|
|
1911 (and (memq (car-safe form) '(fset defalias define-function))
|
|
1912 (nthcdr 300 byte-compile-output)
|
|
1913 (byte-compile-flush-pending))
|
|
1914 (funcall handler form)
|
|
1915 (if for-effect
|
|
1916 (byte-compile-discard)))
|
|
1917 (byte-compile-form form t))
|
|
1918 nil)
|
|
1919
|
|
1920 (defun byte-compile-flush-pending ()
|
|
1921 (if byte-compile-output
|
|
1922 (let ((form (byte-compile-out-toplevel t 'file)))
|
|
1923 (cond ((eq (car-safe form) 'progn)
|
|
1924 (mapcar 'byte-compile-output-file-form (cdr form)))
|
|
1925 (form
|
|
1926 (byte-compile-output-file-form form)))
|
|
1927 (setq byte-compile-constants nil
|
|
1928 byte-compile-variables nil
|
|
1929 byte-compile-depth 0
|
|
1930 byte-compile-maxdepth 0
|
|
1931 byte-compile-output nil))))
|
|
1932
|
|
1933 (defun byte-compile-file-form (form)
|
|
1934 (let ((byte-compile-current-form nil) ; close over this for warnings.
|
|
1935 handler)
|
|
1936 (cond
|
|
1937 ((not (consp form))
|
|
1938 (byte-compile-keep-pending form))
|
|
1939 ((and (symbolp (car form))
|
|
1940 (setq handler (get (car form) 'byte-hunk-handler)))
|
|
1941 (cond ((setq form (funcall handler form))
|
|
1942 (byte-compile-flush-pending)
|
|
1943 (byte-compile-output-file-form form))))
|
|
1944 ((eq form (setq form (macroexpand form byte-compile-macro-environment)))
|
|
1945 (byte-compile-keep-pending form))
|
|
1946 (t
|
|
1947 (byte-compile-file-form form)))))
|
|
1948
|
|
1949 ;; Functions and variables with doc strings must be output separately,
|
|
1950 ;; so make-docfile can recognise them. Most other things can be output
|
|
1951 ;; as byte-code.
|
|
1952
|
|
1953 (put 'defsubst 'byte-hunk-handler 'byte-compile-file-form-defsubst)
|
|
1954 (defun byte-compile-file-form-defsubst (form)
|
|
1955 (cond ((assq (nth 1 form) byte-compile-unresolved-functions)
|
|
1956 (setq byte-compile-current-form (nth 1 form))
|
|
1957 (byte-compile-warn "defsubst %s was used before it was defined"
|
|
1958 (nth 1 form))))
|
|
1959 (byte-compile-file-form
|
|
1960 (macroexpand form byte-compile-macro-environment))
|
|
1961 ;; Return nil so the form is not output twice.
|
|
1962 nil)
|
|
1963
|
|
1964 (put 'autoload 'byte-hunk-handler 'byte-compile-file-form-autoload)
|
|
1965 (defun byte-compile-file-form-autoload (form)
|
|
1966 ;;
|
|
1967 ;; If this is an autoload of a macro, and all arguments are constants (that
|
|
1968 ;; is, there is no hairy computation going on here) then evaluate the form
|
|
1969 ;; at compile-time. This is so that we can make use of macros which we
|
|
1970 ;; have autoloaded from the file being compiled. Normal function autoloads
|
|
1971 ;; are not automatically evaluated at compile time, because there's not
|
|
1972 ;; much point to it (so why bother cluttering up the compile-time namespace.)
|
|
1973 ;;
|
|
1974 ;; If this is an autoload of a function, then record its definition in the
|
|
1975 ;; byte-compile-autoload-environment to suppress any `not known to be
|
|
1976 ;; defined' warnings at the end of this file (this only matters for
|
|
1977 ;; functions which are autoloaded and compiled in the same file, if the
|
|
1978 ;; autoload already exists in the compilation environment, we wouldn't have
|
|
1979 ;; warned anyway.)
|
|
1980 ;;
|
|
1981 (let* ((name (if (byte-compile-constp (nth 1 form))
|
|
1982 (eval (nth 1 form))))
|
|
1983 ;; In v19, the 5th arg to autoload can be t, nil, 'macro, or 'keymap.
|
|
1984 (macrop (and (byte-compile-constp (nth 5 form))
|
|
1985 (memq (eval (nth 5 form)) '(t macro))))
|
|
1986 ;; (functionp (and (byte-compile-constp (nth 5 form))
|
|
1987 ;; (eq 'nil (eval (nth 5 form)))))
|
|
1988 )
|
|
1989 (if (and macrop
|
|
1990 (let ((form form))
|
|
1991 ;; all forms are constant
|
|
1992 (while (if (setq form (cdr form))
|
|
1993 (byte-compile-constp (car form))))
|
|
1994 (null form)))
|
|
1995 ;; eval the macro autoload into the compilation enviroment
|
|
1996 (eval form))
|
|
1997
|
|
1998 (if name
|
|
1999 (let ((old (assq name byte-compile-autoload-environment)))
|
|
2000 (cond (old
|
|
2001 (if (memq 'redefine byte-compile-warnings)
|
|
2002 (byte-compile-warn "multiple autoloads for %s" name))
|
|
2003 (setcdr old form))
|
|
2004 (t
|
|
2005 ;; We only use the names in the autoload environment, but
|
|
2006 ;; it might be useful to have the bodies some day.
|
|
2007 (setq byte-compile-autoload-environment
|
|
2008 (cons (cons name form)
|
|
2009 byte-compile-autoload-environment)))))))
|
272
|
2010 ;;
|
209
|
2011 ;; Now output the form.
|
|
2012 (if (stringp (nth 3 form))
|
|
2013 form
|
|
2014 ;; No doc string, so we can compile this as a normal form.
|
|
2015 (byte-compile-keep-pending form 'byte-compile-normal-call)))
|
|
2016
|
367
|
2017 (put 'defvar 'byte-hunk-handler 'byte-compile-file-form-defvar-or-defconst)
|
|
2018 (put 'defconst 'byte-hunk-handler 'byte-compile-file-form-defvar-or-defconst)
|
|
2019 (defun byte-compile-file-form-defvar-or-defconst (form)
|
|
2020 ;; (defvar|defconst VAR [VALUE [DOCSTRING]])
|
209
|
2021 (if (> (length form) 4)
|
367
|
2022 (byte-compile-warn
|
|
2023 "%s %s called with %d arguments, but accepts only %s"
|
|
2024 (car form) (nth 1 form) (length (cdr form)) 3))
|
209
|
2025 (if (and (> (length form) 3) (not (stringp (nth 3 form))))
|
|
2026 (byte-compile-warn "Third arg to %s %s is not a string: %s"
|
|
2027 (car form) (nth 1 form) (nth 3 form)))
|
|
2028 (if (null (nth 3 form))
|
|
2029 ;; Since there is no doc string, we can compile this as a normal form,
|
|
2030 ;; and not do a file-boundary.
|
|
2031 (byte-compile-keep-pending form)
|
|
2032 (if (memq 'free-vars byte-compile-warnings)
|
|
2033 (setq byte-compile-bound-variables
|
|
2034 (cons (cons (nth 1 form) byte-compile-global-bit)
|
|
2035 byte-compile-bound-variables)))
|
|
2036 (cond ((consp (nth 2 form))
|
|
2037 (setq form (copy-sequence form))
|
|
2038 (setcar (cdr (cdr form))
|
|
2039 (byte-compile-top-level (nth 2 form) nil 'file))))
|
|
2040
|
|
2041 ;; The following turns out not to be necessary, since we emit a call to
|
|
2042 ;; defvar, which can hack Vfile_domain by itself!
|
|
2043 ;;
|
|
2044 ;; If a file domain has been set, emit (put 'VAR 'variable-domain ...)
|
|
2045 ;; after this defvar.
|
|
2046 ; (if byte-compile-file-domain
|
|
2047 ; (progn
|
|
2048 ; ;; Actually, this will emit the (put ...) before the (defvar ...)
|
|
2049 ; ;; but I don't think that can matter in this case.
|
|
2050 ; (byte-compile-keep-pending
|
|
2051 ; (list 'put (list 'quote (nth 1 form)) ''variable-domain
|
|
2052 ; (list 'quote byte-compile-file-domain)))))
|
|
2053 form))
|
|
2054
|
|
2055 (put 'require 'byte-hunk-handler 'byte-compile-file-form-eval-boundary)
|
|
2056 (defun byte-compile-file-form-eval-boundary (form)
|
|
2057 (eval form)
|
|
2058 (byte-compile-keep-pending form 'byte-compile-normal-call))
|
|
2059
|
|
2060 (put 'progn 'byte-hunk-handler 'byte-compile-file-form-progn)
|
|
2061 (put 'prog1 'byte-hunk-handler 'byte-compile-file-form-progn)
|
|
2062 (put 'prog2 'byte-hunk-handler 'byte-compile-file-form-progn)
|
|
2063 (defun byte-compile-file-form-progn (form)
|
|
2064 (mapcar 'byte-compile-file-form (cdr form))
|
|
2065 ;; Return nil so the forms are not output twice.
|
|
2066 nil)
|
|
2067
|
|
2068 ;; This handler is not necessary, but it makes the output from dont-compile
|
|
2069 ;; and similar macros cleaner.
|
|
2070 (put 'eval 'byte-hunk-handler 'byte-compile-file-form-eval)
|
|
2071 (defun byte-compile-file-form-eval (form)
|
|
2072 (if (eq (car-safe (nth 1 form)) 'quote)
|
|
2073 (nth 1 (nth 1 form))
|
|
2074 (byte-compile-keep-pending form)))
|
|
2075
|
|
2076 (put 'defun 'byte-hunk-handler 'byte-compile-file-form-defun)
|
|
2077 (defun byte-compile-file-form-defun (form)
|
|
2078 (byte-compile-file-form-defmumble form nil))
|
|
2079
|
|
2080 (put 'defmacro 'byte-hunk-handler 'byte-compile-file-form-defmacro)
|
|
2081 (defun byte-compile-file-form-defmacro (form)
|
|
2082 (byte-compile-file-form-defmumble form t))
|
|
2083
|
|
2084 (defun byte-compile-compiled-obj-to-list (obj)
|
|
2085 ;; #### this is fairly disgusting. Rewrite the code instead
|
|
2086 ;; so that it doesn't create compiled objects in the first place!
|
|
2087 ;; Much better than creating them and then "uncreating" them
|
|
2088 ;; like this.
|
|
2089 (read (concat "("
|
245
|
2090 (substring (let ((print-readably t)
|
|
2091 (print-gensym
|
272
|
2092 (if (and byte-compile-print-gensym
|
|
2093 (not byte-compile-emacs19-compatibility))
|
|
2094 '(t) nil))
|
245
|
2095 (print-gensym-alist nil))
|
209
|
2096 (prin1-to-string obj))
|
|
2097 2 -1)
|
|
2098 ")")))
|
|
2099
|
|
2100 (defun byte-compile-file-form-defmumble (form macrop)
|
|
2101 (let* ((name (car (cdr form)))
|
|
2102 (this-kind (if macrop 'byte-compile-macro-environment
|
|
2103 'byte-compile-function-environment))
|
|
2104 (that-kind (if macrop 'byte-compile-function-environment
|
|
2105 'byte-compile-macro-environment))
|
|
2106 (this-one (assq name (symbol-value this-kind)))
|
|
2107 (that-one (assq name (symbol-value that-kind)))
|
|
2108 (byte-compile-free-references nil)
|
|
2109 (byte-compile-free-assignments nil))
|
|
2110
|
|
2111 ;; When a function or macro is defined, add it to the call tree so that
|
|
2112 ;; we can tell when functions are not used.
|
|
2113 (if byte-compile-generate-call-tree
|
|
2114 (or (assq name byte-compile-call-tree)
|
|
2115 (setq byte-compile-call-tree
|
|
2116 (cons (list name nil nil) byte-compile-call-tree))))
|
|
2117
|
|
2118 (setq byte-compile-current-form name) ; for warnings
|
|
2119 (if (memq 'redefine byte-compile-warnings)
|
|
2120 (byte-compile-arglist-warn form macrop))
|
|
2121 (if byte-compile-verbose
|
|
2122 (message "Compiling %s... (%s)"
|
|
2123 ;; #### filename used free
|
|
2124 (if filename (file-name-nondirectory filename) "")
|
|
2125 (nth 1 form)))
|
|
2126 (cond (that-one
|
|
2127 (if (and (memq 'redefine byte-compile-warnings)
|
|
2128 ;; hack hack: don't warn when compiling the stubs in
|
|
2129 ;; bytecomp-runtime...
|
|
2130 (not (assq (nth 1 form)
|
|
2131 byte-compile-initial-macro-environment)))
|
|
2132 (byte-compile-warn
|
|
2133 "%s defined multiple times, as both function and macro"
|
|
2134 (nth 1 form)))
|
|
2135 (setcdr that-one nil))
|
|
2136 (this-one
|
|
2137 (if (and (memq 'redefine byte-compile-warnings)
|
|
2138 ;; hack: don't warn when compiling the magic internal
|
|
2139 ;; byte-compiler macros in bytecomp-runtime.el...
|
|
2140 (not (assq (nth 1 form)
|
|
2141 byte-compile-initial-macro-environment)))
|
|
2142 (byte-compile-warn "%s %s defined multiple times in this file"
|
|
2143 (if macrop "macro" "function")
|
|
2144 (nth 1 form))))
|
|
2145 ((and (fboundp name)
|
|
2146 (or (subrp (symbol-function name))
|
|
2147 (eq (car-safe (symbol-function name))
|
|
2148 (if macrop 'lambda 'macro))))
|
|
2149 (if (memq 'redefine byte-compile-warnings)
|
|
2150 (byte-compile-warn "%s %s being redefined as a %s"
|
|
2151 (if (subrp (symbol-function name))
|
|
2152 "subr"
|
|
2153 (if macrop "function" "macro"))
|
|
2154 (nth 1 form)
|
|
2155 (if macrop "macro" "function")))
|
|
2156 ;; shadow existing definition
|
|
2157 (set this-kind
|
|
2158 (cons (cons name nil) (symbol-value this-kind))))
|
|
2159 )
|
|
2160 (let ((body (nthcdr 3 form)))
|
|
2161 (if (and (stringp (car body))
|
|
2162 (symbolp (car-safe (cdr-safe body)))
|
|
2163 (car-safe (cdr-safe body))
|
|
2164 (stringp (car-safe (cdr-safe (cdr-safe body)))))
|
|
2165 (byte-compile-warn "Probable `\"' without `\\' in doc string of %s"
|
|
2166 (nth 1 form))))
|
|
2167 (let* ((new-one (byte-compile-lambda (cons 'lambda (nthcdr 2 form))))
|
|
2168 (code (byte-compile-byte-code-maker new-one)))
|
|
2169 (if this-one
|
|
2170 (setcdr this-one new-one)
|
|
2171 (set this-kind
|
|
2172 (cons (cons name new-one) (symbol-value this-kind))))
|
|
2173 (if (and (stringp (nth 3 form))
|
|
2174 (eq 'quote (car-safe code))
|
|
2175 (eq 'lambda (car-safe (nth 1 code))))
|
|
2176 (cons (car form)
|
|
2177 (cons name (cdr (nth 1 code))))
|
|
2178 (byte-compile-flush-pending)
|
|
2179 (if (not (stringp (nth 3 form)))
|
|
2180 ;; No doc string. Provide -1 as the "doc string index"
|
|
2181 ;; so that no element will be treated as a doc string.
|
|
2182 (byte-compile-output-docform
|
|
2183 "\n(defalias '"
|
|
2184 name
|
|
2185 (cond ((atom code)
|
|
2186 (if macrop '(" '(macro . #[" -1 "])") '(" #[" -1 "]")))
|
|
2187 ((eq (car code) 'quote)
|
|
2188 (setq code new-one)
|
|
2189 (if macrop '(" '(macro " -1 ")") '(" '(" -1 ")")))
|
|
2190 ((if macrop '(" (cons 'macro (" -1 "))") '(" (" -1 ")"))))
|
|
2191 ;; FSF just calls `(append code nil)' here but that relies
|
|
2192 ;; on horrible C kludges in concat() that accept byte-
|
|
2193 ;; compiled objects and pretend they're vectors.
|
|
2194 (if (compiled-function-p code)
|
|
2195 (byte-compile-compiled-obj-to-list code)
|
|
2196 (append code nil))
|
|
2197 (and (atom code) byte-compile-dynamic
|
|
2198 1)
|
|
2199 nil)
|
|
2200 ;; Output the form by hand, that's much simpler than having
|
|
2201 ;; b-c-output-file-form analyze the defalias.
|
|
2202 (byte-compile-output-docform
|
|
2203 "\n(defalias '"
|
|
2204 name
|
|
2205 (cond ((atom code) ; compiled-function-p
|
|
2206 (if macrop '(" '(macro . #[" 4 "])") '(" #[" 4 "]")))
|
|
2207 ((eq (car code) 'quote)
|
|
2208 (setq code new-one)
|
|
2209 (if macrop '(" '(macro " 2 ")") '(" '(" 2 ")")))
|
|
2210 ((if macrop '(" (cons 'macro (" 5 "))") '(" (" 5 ")"))))
|
272
|
2211 ;; The result of byte-compile-byte-code-maker is either a
|
209
|
2212 ;; compiled-function object, or a list of some kind. If it's
|
|
2213 ;; not a cons, we must coerce it into a list of the elements
|
|
2214 ;; to be printed to the file.
|
|
2215 (if (consp code)
|
|
2216 code
|
|
2217 (nconc (list
|
|
2218 (compiled-function-arglist code)
|
|
2219 (compiled-function-instructions code)
|
|
2220 (compiled-function-constants code)
|
|
2221 (compiled-function-stack-depth code))
|
|
2222 (let ((doc (documentation code t)))
|
|
2223 (if doc (list doc)))
|
|
2224 (if (commandp code)
|
|
2225 (list (nth 1 (compiled-function-interactive code))))))
|
|
2226 (and (atom code) byte-compile-dynamic
|
|
2227 1)
|
|
2228 nil))
|
|
2229 (princ ")" byte-compile-outbuffer)
|
|
2230 nil))))
|
|
2231
|
|
2232 ;; Print Lisp object EXP in the output file, inside a comment,
|
|
2233 ;; and return the file position it will have.
|
|
2234 ;; If QUOTED is non-nil, print with quoting; otherwise, print without quoting.
|
|
2235 (defun byte-compile-output-as-comment (exp quoted)
|
|
2236 (let ((position (point)))
|
|
2237 (set-buffer
|
|
2238 (prog1 (current-buffer)
|
|
2239 (set-buffer byte-compile-outbuffer)
|
|
2240
|
|
2241 ;; Insert EXP, and make it a comment with #@LENGTH.
|
|
2242 (insert " ")
|
|
2243 (if quoted
|
|
2244 (prin1 exp byte-compile-outbuffer)
|
|
2245 (princ exp byte-compile-outbuffer))
|
|
2246 (goto-char position)
|
|
2247 ;; Quote certain special characters as needed.
|
|
2248 ;; get_doc_string in doc.c does the unquoting.
|
|
2249 (while (search-forward "\^A" nil t)
|
|
2250 (replace-match "\^A\^A" t t))
|
|
2251 (goto-char position)
|
|
2252 (while (search-forward "\000" nil t)
|
|
2253 (replace-match "\^A0" t t))
|
|
2254 (goto-char position)
|
|
2255 (while (search-forward "\037" nil t)
|
|
2256 (replace-match "\^A_" t t))
|
|
2257 (goto-char (point-max))
|
|
2258 (insert "\037")
|
|
2259 (goto-char position)
|
|
2260 (insert "#@" (format "%d" (- (point-max) position)))
|
|
2261
|
|
2262 ;; Save the file position of the object.
|
|
2263 ;; Note we should add 1 to skip the space
|
|
2264 ;; that we inserted before the actual doc string,
|
|
2265 ;; and subtract 1 to convert from an 1-origin Emacs position
|
|
2266 ;; to a file position; they cancel.
|
|
2267 (setq position (point))
|
|
2268 (goto-char (point-max))))
|
|
2269 position))
|
|
2270
|
|
2271
|
|
2272
|
|
2273 ;; The `domain' declaration. This is legal only at top-level in a file, and
|
|
2274 ;; should generally be the first form in the file. It is not legal inside
|
|
2275 ;; function bodies.
|
|
2276
|
|
2277 (put 'domain 'byte-hunk-handler 'byte-compile-file-form-domain)
|
|
2278 (defun byte-compile-file-form-domain (form)
|
|
2279 (if (not (null (cdr (cdr form))))
|
|
2280 (byte-compile-warn "domain used with too many arguments: %s" form))
|
|
2281 (let ((domain (nth 1 form)))
|
|
2282 (or (null domain)
|
|
2283 (stringp domain)
|
|
2284 (progn
|
|
2285 (byte-compile-warn
|
|
2286 "argument to `domain' declaration must be a literal string: %s"
|
|
2287 form)
|
|
2288 (setq domain nil)))
|
|
2289 (setq byte-compile-file-domain domain))
|
|
2290 (byte-compile-keep-pending form 'byte-compile-normal-call))
|
|
2291
|
|
2292 (defun byte-compile-domain (form)
|
|
2293 (byte-compile-warn "The `domain' declaration is legal only at top-level: %s"
|
|
2294 (let ((print-escape-newlines t)
|
|
2295 (print-level 4)
|
|
2296 (print-length 4))
|
|
2297 (prin1-to-string form)))
|
|
2298 (byte-compile-normal-call
|
|
2299 (list 'signal ''error
|
|
2300 (list 'quote (list "`domain' used inside a function" form)))))
|
|
2301
|
|
2302 ;; This is part of bytecomp.el in 19.35:
|
|
2303 (put 'custom-declare-variable 'byte-hunk-handler
|
|
2304 'byte-compile-file-form-custom-declare-variable)
|
|
2305 (defun byte-compile-file-form-custom-declare-variable (form)
|
|
2306 (if (memq 'free-vars byte-compile-warnings)
|
|
2307 (setq byte-compile-bound-variables
|
|
2308 (cons (cons (nth 1 (nth 1 form))
|
|
2309 byte-compile-global-bit)
|
|
2310 byte-compile-bound-variables)))
|
|
2311 form)
|
|
2312
|
|
2313
|
|
2314 ;;;###autoload
|
|
2315 (defun byte-compile (form)
|
|
2316 "If FORM is a symbol, byte-compile its function definition.
|
|
2317 If FORM is a lambda or a macro, byte-compile it as a function."
|
|
2318 (displaying-byte-compile-warnings
|
|
2319 (byte-compile-close-variables
|
|
2320 (let* ((fun (if (symbolp form)
|
|
2321 (and (fboundp form) (symbol-function form))
|
|
2322 form))
|
|
2323 (macro (eq (car-safe fun) 'macro)))
|
|
2324 (if macro
|
|
2325 (setq fun (cdr fun)))
|
|
2326 (cond ((eq (car-safe fun) 'lambda)
|
|
2327 (setq fun (if macro
|
|
2328 (cons 'macro (byte-compile-lambda fun))
|
|
2329 (byte-compile-lambda fun)))
|
|
2330 (if (symbolp form)
|
|
2331 (defalias form fun)
|
|
2332 fun)))))))
|
|
2333
|
|
2334 ;;;###autoload
|
|
2335 (defun byte-compile-sexp (sexp &optional msg)
|
|
2336 "Compile and return SEXP."
|
|
2337 (displaying-byte-compile-warnings
|
|
2338 (byte-compile-close-variables
|
|
2339 (prog1
|
|
2340 (byte-compile-top-level sexp)
|
|
2341 (byte-compile-warn-about-unresolved-functions msg)))))
|
|
2342
|
|
2343 ;; Given a function made by byte-compile-lambda, make a form which produces it.
|
|
2344 (defun byte-compile-byte-code-maker (fun)
|
|
2345 (cond
|
|
2346 ;; ## atom is faster than compiled-func-p.
|
|
2347 ((atom fun) ; compiled-function-p
|
|
2348 fun)
|
|
2349 ;; b-c-lambda didn't produce a compiled-function, so it must be a trivial
|
|
2350 ;; function.
|
|
2351 ((let (tmp)
|
|
2352 (if (and (setq tmp (assq 'byte-code (cdr-safe (cdr fun))))
|
|
2353 (null (cdr (memq tmp fun))))
|
|
2354 ;; Generate a make-byte-code call.
|
|
2355 (let* ((interactive (assq 'interactive (cdr (cdr fun)))))
|
|
2356 (nconc (list 'make-byte-code
|
|
2357 (list 'quote (nth 1 fun)) ;arglist
|
|
2358 (nth 1 tmp) ;bytes
|
|
2359 (nth 2 tmp) ;consts
|
|
2360 (nth 3 tmp)) ;depth
|
|
2361 (cond ((stringp (nth 2 fun))
|
|
2362 (list (nth 2 fun))) ;doc
|
|
2363 (interactive
|
|
2364 (list nil)))
|
|
2365 (cond (interactive
|
|
2366 (list (if (or (null (nth 1 interactive))
|
|
2367 (stringp (nth 1 interactive)))
|
|
2368 (nth 1 interactive)
|
|
2369 ;; Interactive spec is a list or a variable
|
|
2370 ;; (if it is correct).
|
|
2371 (list 'quote (nth 1 interactive))))))))
|
|
2372 ;; a non-compiled function (probably trivial)
|
|
2373 (list 'quote fun))))))
|
|
2374
|
|
2375 ;; Byte-compile a lambda-expression and return a valid function.
|
|
2376 ;; The value is usually a compiled function but may be the original
|
|
2377 ;; lambda-expression.
|
|
2378 (defun byte-compile-lambda (fun)
|
|
2379 (or (eq 'lambda (car-safe fun))
|
|
2380 (error "not a lambda -- %s" (prin1-to-string fun)))
|
|
2381 (let* ((arglist (nth 1 fun))
|
|
2382 (byte-compile-bound-variables
|
|
2383 (let ((new-bindings
|
|
2384 (mapcar (function (lambda (x)
|
|
2385 (cons x byte-compile-arglist-bit)))
|
|
2386 (and (memq 'free-vars byte-compile-warnings)
|
|
2387 (delq '&rest (delq '&optional
|
|
2388 (copy-sequence arglist)))))))
|
|
2389 (nconc new-bindings
|
|
2390 (cons 'new-scope byte-compile-bound-variables))))
|
|
2391 (body (cdr (cdr fun)))
|
|
2392 (doc (if (stringp (car body))
|
|
2393 (prog1 (car body)
|
|
2394 (setq body (cdr body)))))
|
|
2395 (int (assq 'interactive body)))
|
|
2396 (let ((rest arglist))
|
|
2397 (while rest
|
|
2398 (cond ((not (symbolp (car rest)))
|
|
2399 (byte-compile-warn "non-symbol in arglist: %s"
|
|
2400 (prin1-to-string (car rest))))
|
|
2401 ((memq (car rest) '(t nil))
|
|
2402 (byte-compile-warn "constant in arglist: %s" (car rest)))
|
|
2403 ((and (char= ?\& (aref (symbol-name (car rest)) 0))
|
|
2404 (not (memq (car rest) '(&optional &rest))))
|
|
2405 (byte-compile-warn "unrecognised `&' keyword in arglist: %s"
|
|
2406 (car rest))))
|
|
2407 (setq rest (cdr rest))))
|
|
2408 (cond (int
|
|
2409 ;; Skip (interactive) if it is in front (the most usual location).
|
|
2410 (if (eq int (car body))
|
|
2411 (setq body (cdr body)))
|
|
2412 (cond ((consp (cdr int))
|
|
2413 (if (cdr (cdr int))
|
|
2414 (byte-compile-warn "malformed interactive spec: %s"
|
|
2415 (prin1-to-string int)))
|
|
2416 ;; If the interactive spec is a call to `list',
|
|
2417 ;; don't compile it, because `call-interactively'
|
|
2418 ;; looks at the args of `list'.
|
|
2419 (let ((form (nth 1 int)))
|
|
2420 (while (or (eq (car-safe form) 'let)
|
|
2421 (eq (car-safe form) 'let*)
|
|
2422 (eq (car-safe form) 'save-excursion))
|
|
2423 (while (consp (cdr form))
|
|
2424 (setq form (cdr form)))
|
|
2425 (setq form (car form)))
|
|
2426 (or (eq (car-safe form) 'list)
|
|
2427 (setq int (list 'interactive
|
|
2428 (byte-compile-top-level (nth 1 int)))))))
|
|
2429 ((cdr int)
|
|
2430 (byte-compile-warn "malformed interactive spec: %s"
|
|
2431 (prin1-to-string int))))))
|
|
2432 (let ((compiled (byte-compile-top-level (cons 'progn body) nil 'lambda)))
|
|
2433 (if (memq 'unused-vars byte-compile-warnings)
|
|
2434 ;; done compiling in this scope, warn now.
|
|
2435 (byte-compile-warn-about-unused-variables))
|
|
2436 (if (eq 'byte-code (car-safe compiled))
|
|
2437 (apply 'make-byte-code
|
|
2438 (append (list arglist)
|
|
2439 ;; byte-string, constants-vector, stack depth
|
|
2440 (cdr compiled)
|
|
2441 ;; optionally, the doc string.
|
|
2442 (if (or doc int)
|
|
2443 (list doc))
|
|
2444 ;; optionally, the interactive spec.
|
|
2445 (if int
|
|
2446 (list (nth 1 int)))))
|
|
2447 (setq compiled
|
|
2448 (nconc (if int (list int))
|
|
2449 (cond ((eq (car-safe compiled) 'progn) (cdr compiled))
|
|
2450 (compiled (list compiled)))))
|
|
2451 (nconc (list 'lambda arglist)
|
|
2452 (if (or doc (stringp (car compiled)))
|
|
2453 (cons doc (cond (compiled)
|
|
2454 (body (list nil))))
|
|
2455 compiled))))))
|
|
2456
|
|
2457 (defun byte-compile-constants-vector ()
|
|
2458 ;; Builds the constants-vector from the current variables and constants.
|
|
2459 ;; This modifies the constants from (const . nil) to (const . offset).
|
|
2460 ;; To keep the byte-codes to look up the vector as short as possible:
|
|
2461 ;; First 6 elements are vars, as there are one-byte varref codes for those.
|
|
2462 ;; Next up to byte-constant-limit are constants, still with one-byte codes.
|
|
2463 ;; Next variables again, to get 2-byte codes for variable lookup.
|
|
2464 ;; The rest of the constants and variables need 3-byte byte-codes.
|
|
2465 (let* ((i -1)
|
|
2466 (rest (nreverse byte-compile-variables)) ; nreverse because the first
|
|
2467 (other (nreverse byte-compile-constants)) ; vars often are used most.
|
|
2468 ret tmp
|
|
2469 (limits '(5 ; Use the 1-byte varref codes,
|
|
2470 63 ; 1-constlim ; 1-byte byte-constant codes,
|
|
2471 255 ; 2-byte varref codes,
|
|
2472 65535)) ; 3-byte codes for the rest.
|
|
2473 limit)
|
|
2474 (while (or rest other)
|
|
2475 (setq limit (car limits))
|
|
2476 (while (and rest (not (eq i limit)))
|
|
2477 (if (setq tmp (assq (car (car rest)) ret))
|
|
2478 (setcdr (car rest) (cdr tmp))
|
|
2479 (setcdr (car rest) (setq i (1+ i)))
|
|
2480 (setq ret (cons (car rest) ret)))
|
|
2481 (setq rest (cdr rest)))
|
|
2482 (setq limits (cdr limits)
|
|
2483 rest (prog1 other
|
|
2484 (setq other rest))))
|
|
2485 (apply 'vector (nreverse (mapcar 'car ret)))))
|
|
2486
|
|
2487 ;; Given an expression FORM, compile it and return an equivalent byte-code
|
|
2488 ;; expression (a call to the function byte-code).
|
|
2489 (defun byte-compile-top-level (form &optional for-effect output-type)
|
|
2490 ;; OUTPUT-TYPE advises about how form is expected to be used:
|
|
2491 ;; 'eval or nil -> a single form,
|
|
2492 ;; 'progn or t -> a list of forms,
|
|
2493 ;; 'lambda -> body of a lambda,
|
|
2494 ;; 'file -> used at file-level.
|
|
2495 (let ((byte-compile-constants nil)
|
|
2496 (byte-compile-variables nil)
|
|
2497 (byte-compile-tag-number 0)
|
|
2498 (byte-compile-depth 0)
|
|
2499 (byte-compile-maxdepth 0)
|
|
2500 (byte-compile-output nil))
|
|
2501 (if (memq byte-optimize '(t source))
|
|
2502 (setq form (byte-optimize-form form for-effect)))
|
|
2503 (while (and (eq (car-safe form) 'progn) (null (cdr (cdr form))))
|
|
2504 (setq form (nth 1 form)))
|
|
2505 (if (and (eq 'byte-code (car-safe form))
|
|
2506 (not (memq byte-optimize '(t byte)))
|
|
2507 (stringp (nth 1 form))
|
|
2508 (vectorp (nth 2 form))
|
|
2509 (natnump (nth 3 form)))
|
|
2510 form
|
|
2511 (byte-compile-form form for-effect)
|
|
2512 (byte-compile-out-toplevel for-effect output-type))))
|
|
2513
|
|
2514 (defun byte-compile-out-toplevel (&optional for-effect output-type)
|
|
2515 (if for-effect
|
|
2516 ;; The stack is empty. Push a value to be returned from (byte-code ..).
|
|
2517 (if (eq (car (car byte-compile-output)) 'byte-discard)
|
|
2518 (setq byte-compile-output (cdr byte-compile-output))
|
|
2519 (byte-compile-push-constant
|
|
2520 ;; Push any constant - preferably one which already is used, and
|
|
2521 ;; a number or symbol - ie not some big sequence. The return value
|
|
2522 ;; isn't returned, but it would be a shame if some textually large
|
|
2523 ;; constant was not optimized away because we chose to return it.
|
|
2524 (and (not (assq nil byte-compile-constants)) ; Nil is often there.
|
|
2525 (let ((tmp (reverse byte-compile-constants)))
|
|
2526 (while (and tmp (not (or (symbolp (car (car tmp)))
|
|
2527 (numberp (car (car tmp))))))
|
|
2528 (setq tmp (cdr tmp)))
|
|
2529 (car (car tmp)))))))
|
|
2530 (byte-compile-out 'byte-return 0)
|
|
2531 (setq byte-compile-output (nreverse byte-compile-output))
|
|
2532 (if (memq byte-optimize '(t byte))
|
|
2533 (setq byte-compile-output
|
|
2534 (byte-optimize-lapcode byte-compile-output for-effect)))
|
272
|
2535
|
209
|
2536 ;; Decompile trivial functions:
|
|
2537 ;; only constants and variables, or a single funcall except in lambdas.
|
|
2538 ;; Except for Lisp_Compiled objects, forms like (foo "hi")
|
|
2539 ;; are still quicker than (byte-code "..." [foo "hi"] 2).
|
|
2540 ;; Note that even (quote foo) must be parsed just as any subr by the
|
|
2541 ;; interpreter, so quote should be compiled into byte-code in some contexts.
|
|
2542 ;; What to leave uncompiled:
|
|
2543 ;; lambda -> never. we used to leave it uncompiled if the body was
|
|
2544 ;; a single atom, but that causes confusion if the docstring
|
|
2545 ;; uses the (file . pos) syntax. Besides, now that we have
|
|
2546 ;; the Lisp_Compiled type, the compiled form is faster.
|
|
2547 ;; eval -> atom, quote or (function atom atom atom)
|
|
2548 ;; progn -> as <<same-as-eval>> or (progn <<same-as-eval>> atom)
|
|
2549 ;; file -> as progn, but takes both quotes and atoms, and longer forms.
|
|
2550 (let (rest
|
|
2551 (maycall (not (eq output-type 'lambda))) ; t if we may make a funcall.
|
|
2552 tmp body)
|
|
2553 (cond
|
|
2554 ;; #### This should be split out into byte-compile-nontrivial-function-p.
|
|
2555 ((or (eq output-type 'lambda)
|
|
2556 (nthcdr (if (eq output-type 'file) 50 8) byte-compile-output)
|
|
2557 (assq 'TAG byte-compile-output) ; Not necessary, but speeds up a bit.
|
|
2558 (not (setq tmp (assq 'byte-return byte-compile-output)))
|
|
2559 (progn
|
|
2560 (setq rest (nreverse
|
|
2561 (cdr (memq tmp (reverse byte-compile-output)))))
|
|
2562 (while (cond
|
|
2563 ((memq (car (car rest)) '(byte-varref byte-constant))
|
|
2564 (setq tmp (car (cdr (car rest))))
|
|
2565 (if (if (eq (car (car rest)) 'byte-constant)
|
|
2566 (or (consp tmp)
|
|
2567 (and (symbolp tmp)
|
|
2568 (not (keywordp tmp))
|
|
2569 (not (memq tmp '(nil t))))))
|
|
2570 (if maycall
|
|
2571 (setq body (cons (list 'quote tmp) body)))
|
|
2572 (setq body (cons tmp body))))
|
|
2573 ((and maycall
|
|
2574 ;; Allow a funcall if at most one atom follows it.
|
|
2575 (null (nthcdr 3 rest))
|
|
2576 (setq tmp
|
|
2577 ;; XEmacs change for rms funs
|
|
2578 (or (and
|
|
2579 (byte-compile-version-cond
|
|
2580 byte-compile-emacs19-compatibility)
|
|
2581 (get (car (car rest))
|
|
2582 'byte-opcode19-invert))
|
|
2583 (get (car (car rest))
|
|
2584 'byte-opcode-invert)))
|
|
2585 (or (null (cdr rest))
|
|
2586 (and (memq output-type '(file progn t))
|
|
2587 (cdr (cdr rest))
|
|
2588 (eq (car (nth 1 rest)) 'byte-discard)
|
|
2589 (progn (setq rest (cdr rest)) t))))
|
|
2590 (setq maycall nil) ; Only allow one real function call.
|
|
2591 (setq body (nreverse body))
|
|
2592 (setq body (list
|
|
2593 (if (and (eq tmp 'funcall)
|
|
2594 (eq (car-safe (car body)) 'quote))
|
|
2595 (cons (nth 1 (car body)) (cdr body))
|
|
2596 (cons tmp body))))
|
|
2597 (or (eq output-type 'file)
|
|
2598 (not (delq nil (mapcar 'consp (cdr (car body))))))))
|
|
2599 (setq rest (cdr rest)))
|
|
2600 rest))
|
|
2601 (let ((byte-compile-vector (byte-compile-constants-vector)))
|
|
2602 (list 'byte-code (byte-compile-lapcode byte-compile-output)
|
|
2603 byte-compile-vector byte-compile-maxdepth)))
|
|
2604 ;; it's a trivial function
|
|
2605 ((cdr body) (cons 'progn (nreverse body)))
|
|
2606 ((car body)))))
|
|
2607
|
|
2608 ;; Given BODY, compile it and return a new body.
|
|
2609 (defun byte-compile-top-level-body (body &optional for-effect)
|
|
2610 (setq body (byte-compile-top-level (cons 'progn body) for-effect t))
|
|
2611 (cond ((eq (car-safe body) 'progn)
|
|
2612 (cdr body))
|
|
2613 (body
|
|
2614 (list body))))
|
|
2615
|
272
|
2616 ;; This is the recursive entry point for compiling each subform of an
|
209
|
2617 ;; expression.
|
|
2618 ;; If for-effect is non-nil, byte-compile-form will output a byte-discard
|
|
2619 ;; before terminating (ie no value will be left on the stack).
|
|
2620 ;; A byte-compile handler may, when for-effect is non-nil, choose output code
|
|
2621 ;; which does not leave a value on the stack, and then set for-effect to nil
|
|
2622 ;; (to prevent byte-compile-form from outputting the byte-discard).
|
|
2623 ;; If a handler wants to call another handler, it should do so via
|
|
2624 ;; byte-compile-form, or take extreme care to handle for-effect correctly.
|
|
2625 ;; (Use byte-compile-form-do-effect to reset the for-effect flag too.)
|
|
2626 ;;
|
|
2627 (defun byte-compile-form (form &optional for-effect)
|
|
2628 (setq form (macroexpand form byte-compile-macro-environment))
|
|
2629 (cond ((not (consp form))
|
|
2630 ;; XEmacs addition: keywordp
|
|
2631 (cond ((or (not (symbolp form)) (keywordp form) (memq form '(nil t)))
|
|
2632 (byte-compile-constant form))
|
|
2633 ((and for-effect byte-compile-delete-errors)
|
|
2634 (setq for-effect nil))
|
|
2635 (t (byte-compile-variable-ref 'byte-varref form))))
|
|
2636 ((symbolp (car form))
|
|
2637 (let* ((fn (car form))
|
|
2638 (handler (get fn 'byte-compile)))
|
|
2639 (if (memq fn '(t nil))
|
|
2640 (byte-compile-warn "%s called as a function" fn))
|
|
2641 (if (and handler
|
|
2642 (or (not (byte-compile-version-cond
|
|
2643 byte-compile-emacs19-compatibility))
|
|
2644 (not (get (get fn 'byte-opcode) 'emacs20-opcode))))
|
|
2645 (funcall handler form)
|
|
2646 (if (memq 'callargs byte-compile-warnings)
|
|
2647 (byte-compile-callargs-warn form))
|
|
2648 (byte-compile-normal-call form))))
|
|
2649 ((and (or (compiled-function-p (car form))
|
|
2650 (eq (car-safe (car form)) 'lambda))
|
|
2651 ;; if the form comes out the same way it went in, that's
|
|
2652 ;; because it was malformed, and we couldn't unfold it.
|
|
2653 (not (eq form (setq form (byte-compile-unfold-lambda form)))))
|
|
2654 (byte-compile-form form for-effect)
|
|
2655 (setq for-effect nil))
|
|
2656 ((byte-compile-normal-call form)))
|
|
2657 (if for-effect
|
|
2658 (byte-compile-discard)))
|
|
2659
|
|
2660 (defun byte-compile-normal-call (form)
|
|
2661 (if byte-compile-generate-call-tree
|
|
2662 (byte-compile-annotate-call-tree form))
|
|
2663 (byte-compile-push-constant (car form))
|
|
2664 (mapcar 'byte-compile-form (cdr form)) ; wasteful, but faster.
|
|
2665 (byte-compile-out 'byte-call (length (cdr form))))
|
|
2666
|
|
2667 ;; kludge added to XEmacs to work around the bogosities of a nonlexical lisp.
|
|
2668 (or (fboundp 'globally-boundp) (fset 'globally-boundp 'boundp))
|
|
2669
|
|
2670 (defun byte-compile-variable-ref (base-op var &optional varbind-flags)
|
|
2671 (if (or (not (symbolp var)) (keywordp var) (memq var '(nil t)))
|
|
2672 (byte-compile-warn (if (eq base-op 'byte-varbind)
|
|
2673 "Attempt to let-bind %s %s"
|
|
2674 "Variable reference to %s %s")
|
|
2675 (if (symbolp var) "constant" "nonvariable")
|
|
2676 (prin1-to-string var))
|
|
2677 (if (and (get var 'byte-obsolete-variable)
|
|
2678 (memq 'obsolete byte-compile-warnings))
|
|
2679 (let ((ob (get var 'byte-obsolete-variable)))
|
|
2680 (byte-compile-warn "%s is an obsolete variable; %s" var
|
|
2681 (if (stringp ob)
|
|
2682 ob
|
|
2683 (format "use %s instead." ob)))))
|
|
2684 (if (and (get var 'byte-compatible-variable)
|
|
2685 (memq 'pedantic byte-compile-warnings))
|
|
2686 (let ((ob (get var 'byte-compatible-variable)))
|
|
2687 (byte-compile-warn "%s is provided for compatibility; %s" var
|
|
2688 (if (stringp ob)
|
|
2689 ob
|
|
2690 (format "use %s instead." ob)))))
|
|
2691 (if (memq 'free-vars byte-compile-warnings)
|
|
2692 (if (eq base-op 'byte-varbind)
|
|
2693 (setq byte-compile-bound-variables
|
|
2694 (cons (cons var (or varbind-flags 0))
|
|
2695 byte-compile-bound-variables))
|
|
2696 (or (globally-boundp var)
|
|
2697 (let ((cell (assq var byte-compile-bound-variables)))
|
|
2698 (if cell (setcdr cell
|
|
2699 (logior (cdr cell)
|
|
2700 (if (eq base-op 'byte-varset)
|
|
2701 byte-compile-assigned-bit
|
|
2702 byte-compile-referenced-bit)))))
|
|
2703 (if (eq base-op 'byte-varset)
|
|
2704 (or (memq var byte-compile-free-assignments)
|
|
2705 (progn
|
|
2706 (byte-compile-warn "assignment to free variable %s"
|
|
2707 var)
|
|
2708 (setq byte-compile-free-assignments
|
|
2709 (cons var byte-compile-free-assignments))))
|
|
2710 (or (memq var byte-compile-free-references)
|
|
2711 (progn
|
|
2712 (byte-compile-warn "reference to free variable %s" var)
|
|
2713 (setq byte-compile-free-references
|
|
2714 (cons var byte-compile-free-references)))))))))
|
|
2715 (let ((tmp (assq var byte-compile-variables)))
|
|
2716 (or tmp
|
|
2717 (setq tmp (list var)
|
|
2718 byte-compile-variables (cons tmp byte-compile-variables)))
|
|
2719 (byte-compile-out base-op tmp)))
|
|
2720
|
|
2721 (defmacro byte-compile-get-constant (const)
|
|
2722 (` (or (if (stringp (, const))
|
|
2723 (assoc (, const) byte-compile-constants)
|
|
2724 (assq (, const) byte-compile-constants))
|
|
2725 (car (setq byte-compile-constants
|
|
2726 (cons (list (, const)) byte-compile-constants))))))
|
|
2727
|
|
2728 ;; Use this when the value of a form is a constant. This obeys for-effect.
|
|
2729 (defun byte-compile-constant (const)
|
|
2730 (if for-effect
|
|
2731 (setq for-effect nil)
|
|
2732 (byte-compile-out 'byte-constant (byte-compile-get-constant const))))
|
|
2733
|
|
2734 ;; Use this for a constant that is not the value of its containing form.
|
|
2735 ;; This ignores for-effect.
|
|
2736 (defun byte-compile-push-constant (const)
|
|
2737 (let ((for-effect nil))
|
|
2738 (inline (byte-compile-constant const))))
|
|
2739
|
|
2740
|
|
2741 ;; Compile those primitive ordinary functions
|
|
2742 ;; which have special byte codes just for speed.
|
|
2743
|
|
2744 (defmacro byte-defop-compiler (function &optional compile-handler)
|
|
2745 ;; add a compiler-form for FUNCTION.
|
|
2746 ;; If function is a symbol, then the variable "byte-SYMBOL" must name
|
|
2747 ;; the opcode to be used. If function is a list, the first element
|
|
2748 ;; is the function and the second element is the bytecode-symbol.
|
|
2749 ;; COMPILE-HANDLER is the function to use to compile this byte-op, or
|
|
2750 ;; may be the abbreviations 0, 1, 2, 3, 0-1, 1-2, 2-3, 0+1, 1+1, 2+1,
|
|
2751 ;; 0-1+1, 1-2+1, 2-3+1, 0+2, or 1+2. If it is nil, then the handler is
|
|
2752 ;; "byte-compile-SYMBOL."
|
|
2753 (let (opcode)
|
|
2754 (if (symbolp function)
|
|
2755 (setq opcode (intern (concat "byte-" (symbol-name function))))
|
|
2756 (setq opcode (car (cdr function))
|
|
2757 function (car function)))
|
|
2758 (let ((fnform
|
|
2759 (list 'put (list 'quote function) ''byte-compile
|
|
2760 (list 'quote
|
|
2761 (or (cdr (assq compile-handler
|
|
2762 '((0 . byte-compile-no-args)
|
|
2763 (1 . byte-compile-one-arg)
|
|
2764 (2 . byte-compile-two-args)
|
|
2765 (3 . byte-compile-three-args)
|
|
2766 (0-1 . byte-compile-zero-or-one-arg)
|
|
2767 (1-2 . byte-compile-one-or-two-args)
|
|
2768 (2-3 . byte-compile-two-or-three-args)
|
|
2769 (0+1 . byte-compile-no-args-with-one-extra)
|
|
2770 (1+1 . byte-compile-one-arg-with-one-extra)
|
|
2771 (2+1 . byte-compile-two-args-with-one-extra)
|
|
2772 (0-1+1 . byte-compile-zero-or-one-arg-with-one-extra)
|
|
2773 (1-2+1 . byte-compile-one-or-two-args-with-one-extra)
|
|
2774 (2-3+1 . byte-compile-two-or-three-args-with-one-extra)
|
|
2775 (0+2 . byte-compile-no-args-with-two-extra)
|
|
2776 (1+2 . byte-compile-one-arg-with-two-extra)
|
272
|
2777
|
209
|
2778 )))
|
|
2779 compile-handler
|
|
2780 (intern (concat "byte-compile-"
|
|
2781 (symbol-name function))))))))
|
|
2782 (if opcode
|
|
2783 (list 'progn fnform
|
|
2784 (list 'put (list 'quote function)
|
|
2785 ''byte-opcode (list 'quote opcode))
|
|
2786 (list 'put (list 'quote opcode)
|
|
2787 ''byte-opcode-invert (list 'quote function)))
|
|
2788 fnform))))
|
|
2789
|
|
2790 (defmacro byte-defop-compiler20 (function &optional compile-handler)
|
|
2791 ;; Just like byte-defop-compiler, but defines an opcode that will only
|
|
2792 ;; be used when byte-compile-emacs19-compatibility is false.
|
|
2793 (if (and (byte-compile-single-version)
|
|
2794 byte-compile-emacs19-compatibility)
|
|
2795 ;; #### instead of doing nothing, this should do some remprops,
|
|
2796 ;; #### to protect against the case where a single-version compiler
|
|
2797 ;; #### is loaded into a world that has contained a multi-version one.
|
|
2798 nil
|
|
2799 (list 'progn
|
|
2800 (list 'put
|
|
2801 (list 'quote
|
|
2802 (or (car (cdr-safe function))
|
|
2803 (intern (concat "byte-"
|
|
2804 (symbol-name (or (car-safe function) function))))))
|
|
2805 ''emacs20-opcode t)
|
|
2806 (list 'byte-defop-compiler function compile-handler))))
|
|
2807
|
|
2808 ;; XEmacs addition:
|
|
2809 (defmacro byte-defop-compiler-rmsfun (function &optional compile-handler)
|
|
2810 ;; for functions like `eq' that compile into different opcodes depending
|
|
2811 ;; on the Emacs version: byte-old-eq for v19, byte-eq for v20.
|
|
2812 (let ((opcode (intern (concat "byte-" (symbol-name function))))
|
|
2813 (opcode19 (intern (concat "byte-old-" (symbol-name function))))
|
|
2814 (fnform
|
|
2815 (list 'put (list 'quote function) ''byte-compile
|
|
2816 (list 'quote
|
|
2817 (or (cdr (assq compile-handler
|
|
2818 '((2 . byte-compile-two-args-19->20)
|
|
2819 )))
|
|
2820 compile-handler
|
|
2821 (intern (concat "byte-compile-"
|
|
2822 (symbol-name function))))))))
|
|
2823 (list 'progn fnform
|
|
2824 (list 'put (list 'quote function)
|
|
2825 ''byte-opcode (list 'quote opcode))
|
|
2826 (list 'put (list 'quote function)
|
|
2827 ''byte-opcode19 (list 'quote opcode19))
|
|
2828 (list 'put (list 'quote opcode)
|
|
2829 ''byte-opcode-invert (list 'quote function))
|
|
2830 (list 'put (list 'quote opcode19)
|
|
2831 ''byte-opcode19-invert (list 'quote function)))))
|
|
2832
|
|
2833 (defmacro byte-defop-compiler-1 (function &optional compile-handler)
|
|
2834 (list 'byte-defop-compiler (list function nil) compile-handler))
|
|
2835
|
|
2836
|
|
2837 (put 'byte-call 'byte-opcode-invert 'funcall)
|
|
2838 (put 'byte-list1 'byte-opcode-invert 'list)
|
|
2839 (put 'byte-list2 'byte-opcode-invert 'list)
|
|
2840 (put 'byte-list3 'byte-opcode-invert 'list)
|
|
2841 (put 'byte-list4 'byte-opcode-invert 'list)
|
|
2842 (put 'byte-listN 'byte-opcode-invert 'list)
|
|
2843 (put 'byte-concat2 'byte-opcode-invert 'concat)
|
|
2844 (put 'byte-concat3 'byte-opcode-invert 'concat)
|
|
2845 (put 'byte-concat4 'byte-opcode-invert 'concat)
|
|
2846 (put 'byte-concatN 'byte-opcode-invert 'concat)
|
|
2847 (put 'byte-insertN 'byte-opcode-invert 'insert)
|
|
2848
|
|
2849 (byte-defop-compiler (dot byte-point) 0+1)
|
|
2850 (byte-defop-compiler (dot-max byte-point-max) 0+1)
|
|
2851 (byte-defop-compiler (dot-min byte-point-min) 0+1)
|
|
2852 (byte-defop-compiler point 0+1)
|
|
2853 (byte-defop-compiler-rmsfun eq 2)
|
|
2854 (byte-defop-compiler point-max 0+1)
|
|
2855 (byte-defop-compiler point-min 0+1)
|
|
2856 (byte-defop-compiler following-char 0+1)
|
|
2857 (byte-defop-compiler preceding-char 0+1)
|
|
2858 (byte-defop-compiler current-column 0+1)
|
|
2859 ;; FSF has special function here; generalized here by the 1+2 stuff.
|
|
2860 (byte-defop-compiler (indent-to-column byte-indent-to) 1+2)
|
|
2861 (byte-defop-compiler indent-to 1+2)
|
|
2862 (byte-defop-compiler-rmsfun equal 2)
|
|
2863 (byte-defop-compiler eolp 0+1)
|
|
2864 (byte-defop-compiler eobp 0+1)
|
|
2865 (byte-defop-compiler bolp 0+1)
|
|
2866 (byte-defop-compiler bobp 0+1)
|
|
2867 (byte-defop-compiler current-buffer 0)
|
|
2868 ;;(byte-defop-compiler read-char 0) ;; obsolete
|
|
2869 (byte-defop-compiler-rmsfun memq 2)
|
|
2870 (byte-defop-compiler interactive-p 0)
|
|
2871 (byte-defop-compiler widen 0+1)
|
|
2872 (byte-defop-compiler end-of-line 0-1+1)
|
|
2873 (byte-defop-compiler forward-char 0-1+1)
|
|
2874 (byte-defop-compiler forward-line 0-1+1)
|
|
2875 (byte-defop-compiler symbolp 1)
|
|
2876 (byte-defop-compiler consp 1)
|
|
2877 (byte-defop-compiler stringp 1)
|
|
2878 (byte-defop-compiler listp 1)
|
|
2879 (byte-defop-compiler not 1)
|
|
2880 (byte-defop-compiler (null byte-not) 1)
|
|
2881 (byte-defop-compiler car 1)
|
|
2882 (byte-defop-compiler cdr 1)
|
|
2883 (byte-defop-compiler length 1)
|
|
2884 (byte-defop-compiler symbol-value 1)
|
|
2885 (byte-defop-compiler symbol-function 1)
|
|
2886 (byte-defop-compiler (1+ byte-add1) 1)
|
|
2887 (byte-defop-compiler (1- byte-sub1) 1)
|
|
2888 (byte-defop-compiler goto-char 1+1)
|
|
2889 (byte-defop-compiler char-after 0-1+1)
|
|
2890 (byte-defop-compiler set-buffer 1)
|
|
2891 ;;(byte-defop-compiler set-mark 1) ;; obsolete
|
|
2892 (byte-defop-compiler forward-word 1+1)
|
|
2893 (byte-defop-compiler char-syntax 1+1)
|
|
2894 (byte-defop-compiler nreverse 1)
|
|
2895 (byte-defop-compiler car-safe 1)
|
|
2896 (byte-defop-compiler cdr-safe 1)
|
|
2897 (byte-defop-compiler numberp 1)
|
|
2898 (byte-defop-compiler integerp 1)
|
|
2899 (byte-defop-compiler skip-chars-forward 1-2+1)
|
|
2900 (byte-defop-compiler skip-chars-backward 1-2+1)
|
|
2901 (byte-defop-compiler (eql byte-eq) 2)
|
|
2902 (byte-defop-compiler20 old-eq 2)
|
|
2903 (byte-defop-compiler20 old-memq 2)
|
|
2904 (byte-defop-compiler cons 2)
|
|
2905 (byte-defop-compiler aref 2)
|
280
|
2906 (byte-defop-compiler (= byte-eqlsign) byte-compile-one-or-more-args)
|
|
2907 (byte-defop-compiler (< byte-lss) byte-compile-one-or-more-args)
|
|
2908 (byte-defop-compiler (> byte-gtr) byte-compile-one-or-more-args)
|
|
2909 (byte-defop-compiler (<= byte-leq) byte-compile-one-or-more-args)
|
|
2910 (byte-defop-compiler (>= byte-geq) byte-compile-one-or-more-args)
|
|
2911 (byte-defop-compiler /= byte-compile-/=)
|
209
|
2912 (byte-defop-compiler get 2+1)
|
|
2913 (byte-defop-compiler nth 2)
|
|
2914 (byte-defop-compiler substring 2-3)
|
|
2915 (byte-defop-compiler (move-marker byte-set-marker) 2-3)
|
|
2916 (byte-defop-compiler set-marker 2-3)
|
|
2917 (byte-defop-compiler match-beginning 1)
|
|
2918 (byte-defop-compiler match-end 1)
|
|
2919 (byte-defop-compiler upcase 1+1)
|
|
2920 (byte-defop-compiler downcase 1+1)
|
|
2921 (byte-defop-compiler string= 2)
|
|
2922 (byte-defop-compiler string< 2)
|
|
2923 (byte-defop-compiler (string-equal byte-string=) 2)
|
|
2924 (byte-defop-compiler (string-lessp byte-string<) 2)
|
|
2925 (byte-defop-compiler20 old-equal 2)
|
|
2926 (byte-defop-compiler nthcdr 2)
|
|
2927 (byte-defop-compiler elt 2)
|
|
2928 (byte-defop-compiler20 old-member 2)
|
|
2929 (byte-defop-compiler20 old-assq 2)
|
|
2930 (byte-defop-compiler (rplaca byte-setcar) 2)
|
|
2931 (byte-defop-compiler (rplacd byte-setcdr) 2)
|
|
2932 (byte-defop-compiler setcar 2)
|
|
2933 (byte-defop-compiler setcdr 2)
|
|
2934 ;; buffer-substring now has its own function. This used to be
|
|
2935 ;; 2+1, but now all args are optional.
|
|
2936 (byte-defop-compiler buffer-substring)
|
|
2937 (byte-defop-compiler delete-region 2+1)
|
|
2938 (byte-defop-compiler narrow-to-region 2+1)
|
|
2939 (byte-defop-compiler (% byte-rem) 2)
|
|
2940 (byte-defop-compiler aset 3)
|
|
2941
|
|
2942 (byte-defop-compiler-rmsfun member 2)
|
|
2943 (byte-defop-compiler-rmsfun assq 2)
|
|
2944
|
|
2945 (byte-defop-compiler max byte-compile-associative)
|
|
2946 (byte-defop-compiler min byte-compile-associative)
|
|
2947 (byte-defop-compiler (+ byte-plus) byte-compile-associative)
|
|
2948 (byte-defop-compiler (* byte-mult) byte-compile-associative)
|
|
2949
|
|
2950 ;;####(byte-defop-compiler move-to-column 1)
|
|
2951 (byte-defop-compiler-1 interactive byte-compile-noop)
|
|
2952 (byte-defop-compiler-1 domain byte-compile-domain)
|
|
2953
|
|
2954 ;; As of GNU Emacs 19.18 and Lucid Emacs 19.8, mod and % are different: `%'
|
|
2955 ;; means integral remainder and may have a negative result; `mod' is always
|
|
2956 ;; positive, and accepts floating point args. All code which uses `mod' and
|
|
2957 ;; requires the new interpretation must be compiled with bytecomp version 2.18
|
|
2958 ;; or newer, or the emitted code will run the byte-code for `%' instead of an
|
|
2959 ;; actual call to `mod'. So be careful of compiling new code with an old
|
272
|
2960 ;; compiler. Note also that `%' is more efficient than `mod' because the
|
209
|
2961 ;; former is byte-coded and the latter is not.
|
|
2962 ;;(byte-defop-compiler (mod byte-rem) 2)
|
|
2963
|
|
2964
|
|
2965 (defun byte-compile-subr-wrong-args (form n)
|
|
2966 (byte-compile-warn "%s called with %d arg%s, but requires %s"
|
|
2967 (car form) (length (cdr form))
|
|
2968 (if (= 1 (length (cdr form))) "" "s") n)
|
|
2969 ;; get run-time wrong-number-of-args error.
|
|
2970 (byte-compile-normal-call form))
|
|
2971
|
|
2972 (defun byte-compile-no-args (form)
|
|
2973 (if (not (= (length form) 1))
|
|
2974 (byte-compile-subr-wrong-args form "none")
|
|
2975 (byte-compile-out (get (car form) 'byte-opcode) 0)))
|
|
2976
|
|
2977 (defun byte-compile-one-arg (form)
|
|
2978 (if (not (= (length form) 2))
|
|
2979 (byte-compile-subr-wrong-args form 1)
|
|
2980 (byte-compile-form (car (cdr form))) ;; Push the argument
|
|
2981 (byte-compile-out (get (car form) 'byte-opcode) 0)))
|
|
2982
|
|
2983 (defun byte-compile-two-args (form)
|
|
2984 (if (not (= (length form) 3))
|
|
2985 (byte-compile-subr-wrong-args form 2)
|
|
2986 (byte-compile-form (car (cdr form))) ;; Push the arguments
|
|
2987 (byte-compile-form (nth 2 form))
|
|
2988 (byte-compile-out (get (car form) 'byte-opcode) 0)))
|
|
2989
|
|
2990 (defun byte-compile-three-args (form)
|
|
2991 (if (not (= (length form) 4))
|
|
2992 (byte-compile-subr-wrong-args form 3)
|
|
2993 (byte-compile-form (car (cdr form))) ;; Push the arguments
|
|
2994 (byte-compile-form (nth 2 form))
|
|
2995 (byte-compile-form (nth 3 form))
|
|
2996 (byte-compile-out (get (car form) 'byte-opcode) 0)))
|
|
2997
|
|
2998 (defun byte-compile-zero-or-one-arg (form)
|
|
2999 (let ((len (length form)))
|
|
3000 (cond ((= len 1) (byte-compile-one-arg (append form '(nil))))
|
|
3001 ((= len 2) (byte-compile-one-arg form))
|
|
3002 (t (byte-compile-subr-wrong-args form "0-1")))))
|
|
3003
|
|
3004 (defun byte-compile-one-or-two-args (form)
|
|
3005 (let ((len (length form)))
|
|
3006 (cond ((= len 2) (byte-compile-two-args (append form '(nil))))
|
|
3007 ((= len 3) (byte-compile-two-args form))
|
|
3008 (t (byte-compile-subr-wrong-args form "1-2")))))
|
|
3009
|
|
3010 (defun byte-compile-two-or-three-args (form)
|
|
3011 (let ((len (length form)))
|
|
3012 (cond ((= len 3) (byte-compile-three-args (append form '(nil))))
|
|
3013 ((= len 4) (byte-compile-three-args form))
|
|
3014 (t (byte-compile-subr-wrong-args form "2-3")))))
|
|
3015
|
298
|
3016 ;; from Ben Wing <ben@xemacs.org>: some inlined functions have extra
|
209
|
3017 ;; optional args added to them in XEmacs 19.12. Changing the byte
|
|
3018 ;; interpreter to deal with these args would be wrong and cause
|
|
3019 ;; incompatibility, so we generate non-inlined calls for those cases.
|
|
3020 ;; Without the following functions, spurious warnings will be generated;
|
|
3021 ;; however, they would still compile correctly because
|
|
3022 ;; `byte-compile-subr-wrong-args' also converts the call to non-inlined.
|
|
3023
|
|
3024 (defun byte-compile-no-args-with-one-extra (form)
|
|
3025 (let ((len (length form)))
|
|
3026 (cond ((= len 1) (byte-compile-no-args form))
|
|
3027 ((= len 2) (byte-compile-normal-call form))
|
|
3028 (t (byte-compile-subr-wrong-args form "0-1")))))
|
|
3029
|
|
3030 (defun byte-compile-one-arg-with-one-extra (form)
|
|
3031 (let ((len (length form)))
|
|
3032 (cond ((= len 2) (byte-compile-one-arg form))
|
|
3033 ((= len 3) (byte-compile-normal-call form))
|
|
3034 (t (byte-compile-subr-wrong-args form "1-2")))))
|
|
3035
|
|
3036 (defun byte-compile-two-args-with-one-extra (form)
|
|
3037 (let ((len (length form)))
|
|
3038 (cond ((= len 3) (byte-compile-two-args form))
|
|
3039 ((= len 4) (byte-compile-normal-call form))
|
|
3040 (t (byte-compile-subr-wrong-args form "2-3")))))
|
|
3041
|
|
3042 (defun byte-compile-zero-or-one-arg-with-one-extra (form)
|
|
3043 (let ((len (length form)))
|
|
3044 (cond ((= len 1) (byte-compile-one-arg (append form '(nil))))
|
|
3045 ((= len 2) (byte-compile-one-arg form))
|
|
3046 ((= len 3) (byte-compile-normal-call form))
|
|
3047 (t (byte-compile-subr-wrong-args form "0-2")))))
|
272
|
3048
|
209
|
3049 (defun byte-compile-one-or-two-args-with-one-extra (form)
|
|
3050 (let ((len (length form)))
|
|
3051 (cond ((= len 2) (byte-compile-two-args (append form '(nil))))
|
|
3052 ((= len 3) (byte-compile-two-args form))
|
|
3053 ((= len 4) (byte-compile-normal-call form))
|
|
3054 (t (byte-compile-subr-wrong-args form "1-3")))))
|
|
3055
|
|
3056 (defun byte-compile-two-or-three-args-with-one-extra (form)
|
|
3057 (let ((len (length form)))
|
|
3058 (cond ((= len 3) (byte-compile-three-args (append form '(nil))))
|
|
3059 ((= len 4) (byte-compile-three-args form))
|
|
3060 ((= len 5) (byte-compile-normal-call form))
|
|
3061 (t (byte-compile-subr-wrong-args form "2-4")))))
|
|
3062
|
|
3063 (defun byte-compile-no-args-with-two-extra (form)
|
|
3064 (let ((len (length form)))
|
|
3065 (cond ((= len 1) (byte-compile-no-args form))
|
|
3066 ((or (= len 2) (= len 3)) (byte-compile-normal-call form))
|
|
3067 (t (byte-compile-subr-wrong-args form "0-2")))))
|
|
3068
|
|
3069 (defun byte-compile-one-arg-with-two-extra (form)
|
|
3070 (let ((len (length form)))
|
|
3071 (cond ((= len 2) (byte-compile-one-arg form))
|
|
3072 ((or (= len 3) (= len 4)) (byte-compile-normal-call form))
|
|
3073 (t (byte-compile-subr-wrong-args form "1-3")))))
|
|
3074
|
|
3075 ;; XEmacs: used for functions that have a different opcode in v19 than v20.
|
|
3076 ;; this includes `eq', `equal', and other old-ified functions.
|
|
3077 (defun byte-compile-two-args-19->20 (form)
|
|
3078 (if (not (= (length form) 3))
|
|
3079 (byte-compile-subr-wrong-args form 2)
|
|
3080 (byte-compile-form (car (cdr form))) ;; Push the arguments
|
|
3081 (byte-compile-form (nth 2 form))
|
|
3082 (if (byte-compile-version-cond byte-compile-emacs19-compatibility)
|
|
3083 (byte-compile-out (get (car form) 'byte-opcode19) 0)
|
|
3084 (byte-compile-out (get (car form) 'byte-opcode) 0))))
|
|
3085
|
|
3086 (defun byte-compile-noop (form)
|
|
3087 (byte-compile-constant nil))
|
|
3088
|
|
3089 (defun byte-compile-discard ()
|
|
3090 (byte-compile-out 'byte-discard 0))
|
|
3091
|
|
3092
|
|
3093 ;; Compile a function that accepts one or more args and is right-associative.
|
|
3094 ;; We do it by left-associativity so that the operations
|
|
3095 ;; are done in the same order as in interpreted code.
|
|
3096 (defun byte-compile-associative (form)
|
|
3097 (if (cdr form)
|
|
3098 (let ((opcode (get (car form) 'byte-opcode))
|
|
3099 (args (copy-sequence (cdr form))))
|
|
3100 (byte-compile-form (car args))
|
|
3101 (setq args (cdr args))
|
|
3102 (while args
|
|
3103 (byte-compile-form (car args))
|
|
3104 (byte-compile-out opcode 0)
|
|
3105 (setq args (cdr args))))
|
|
3106 (byte-compile-constant (eval form))))
|
|
3107
|
|
3108
|
|
3109 ;; more complicated compiler macros
|
|
3110
|
|
3111 (byte-defop-compiler list)
|
|
3112 (byte-defop-compiler concat)
|
|
3113 (byte-defop-compiler fset)
|
|
3114 (byte-defop-compiler insert)
|
|
3115 (byte-defop-compiler-1 function byte-compile-function-form)
|
|
3116 (byte-defop-compiler-1 - byte-compile-minus)
|
|
3117 (byte-defop-compiler (/ byte-quo) byte-compile-quo)
|
|
3118 (byte-defop-compiler nconc)
|
|
3119 (byte-defop-compiler-1 beginning-of-line)
|
|
3120
|
280
|
3121 (defun byte-compile-one-or-more-args (form)
|
|
3122 (let ((len (length form)))
|
|
3123 (cond ((= len 1) (byte-compile-subr-wrong-args form "1 or more"))
|
|
3124 ((= len 2) (byte-compile-constant t))
|
|
3125 ((= len 3) (byte-compile-two-args form))
|
|
3126 (t (byte-compile-normal-call form)))))
|
|
3127
|
|
3128 (defun byte-compile-/= (form)
|
|
3129 (let ((len (length form)))
|
|
3130 (cond ((= len 1) (byte-compile-subr-wrong-args form "1 or more"))
|
|
3131 ((= len 2) (byte-compile-constant t))
|
|
3132 ;; optimize (/= X Y) to (not (= X Y))
|
|
3133 ((= len 3) (byte-compile-form-do-effect `(not (= ,@(cdr form)))))
|
|
3134 (t (byte-compile-normal-call form)))))
|
|
3135
|
209
|
3136 (defun byte-compile-buffer-substring (form)
|
|
3137 (let ((len (length form)))
|
|
3138 ;; buffer-substring used to take exactly two args, but now takes 0-3.
|
|
3139 ;; convert 0-2 to two args and use special bytecode operand.
|
|
3140 ;; convert 3 args to a normal call.
|
355
|
3141 (cond ((= len 1) (byte-compile-two-args (append form '(nil nil))))
|
|
3142 ((= len 2) (byte-compile-two-args (append form '(nil))))
|
|
3143 ((= len 3) (byte-compile-two-args form))
|
209
|
3144 ((= len 4) (byte-compile-normal-call form))
|
|
3145 (t (byte-compile-subr-wrong-args form "0-3")))))
|
|
3146
|
|
3147 (defun byte-compile-list (form)
|
|
3148 (let ((count (length (cdr form))))
|
|
3149 (cond ((= count 0)
|
|
3150 (byte-compile-constant nil))
|
|
3151 ((< count 5)
|
|
3152 (mapcar 'byte-compile-form (cdr form))
|
|
3153 (byte-compile-out
|
|
3154 (aref [byte-list1 byte-list2 byte-list3 byte-list4] (1- count)) 0))
|
|
3155 ((< count 256)
|
|
3156 (mapcar 'byte-compile-form (cdr form))
|
|
3157 (byte-compile-out 'byte-listN count))
|
|
3158 (t (byte-compile-normal-call form)))))
|
|
3159
|
|
3160 (defun byte-compile-concat (form)
|
|
3161 (let ((count (length (cdr form))))
|
|
3162 (cond ((and (< 1 count) (< count 5))
|
|
3163 (mapcar 'byte-compile-form (cdr form))
|
|
3164 (byte-compile-out
|
|
3165 (aref [byte-concat2 byte-concat3 byte-concat4] (- count 2))
|
|
3166 0))
|
|
3167 ;; Concat of one arg is not a no-op if arg is not a string.
|
|
3168 ((= count 0)
|
|
3169 (byte-compile-form ""))
|
|
3170 ((< count 256)
|
|
3171 (mapcar 'byte-compile-form (cdr form))
|
|
3172 (byte-compile-out 'byte-concatN count))
|
|
3173 ((byte-compile-normal-call form)))))
|
|
3174
|
|
3175 (defun byte-compile-minus (form)
|
|
3176 (if (null (setq form (cdr form)))
|
|
3177 (byte-compile-constant 0)
|
|
3178 (byte-compile-form (car form))
|
|
3179 (if (cdr form)
|
|
3180 (while (setq form (cdr form))
|
|
3181 (byte-compile-form (car form))
|
|
3182 (byte-compile-out 'byte-diff 0))
|
|
3183 (byte-compile-out 'byte-negate 0))))
|
|
3184
|
|
3185 (defun byte-compile-quo (form)
|
|
3186 (let ((len (length form)))
|
|
3187 (cond ((<= len 2)
|
|
3188 (byte-compile-subr-wrong-args form "2 or more"))
|
|
3189 (t
|
|
3190 (byte-compile-form (car (setq form (cdr form))))
|
|
3191 (while (setq form (cdr form))
|
|
3192 (byte-compile-form (car form))
|
|
3193 (byte-compile-out 'byte-quo 0))))))
|
|
3194
|
|
3195 (defun byte-compile-nconc (form)
|
|
3196 (let ((len (length form)))
|
|
3197 (cond ((= len 1)
|
|
3198 (byte-compile-constant nil))
|
|
3199 ((= len 2)
|
|
3200 ;; nconc of one arg is a noop, even if that arg isn't a list.
|
|
3201 (byte-compile-form (nth 1 form)))
|
|
3202 (t
|
|
3203 (byte-compile-form (car (setq form (cdr form))))
|
|
3204 (while (setq form (cdr form))
|
|
3205 (byte-compile-form (car form))
|
|
3206 (byte-compile-out 'byte-nconc 0))))))
|
|
3207
|
|
3208 (defun byte-compile-fset (form)
|
|
3209 ;; warn about forms like (fset 'foo '(lambda () ...))
|
|
3210 ;; (where the lambda expression is non-trivial...)
|
|
3211 ;; Except don't warn if the first argument is 'make-byte-code, because
|
|
3212 ;; I'm sick of getting mail asking me whether that warning is a problem.
|
|
3213 (let ((fn (nth 2 form))
|
|
3214 body)
|
|
3215 (if (and (eq (car-safe fn) 'quote)
|
|
3216 (eq (car-safe (setq fn (nth 1 fn))) 'lambda)
|
|
3217 (not (eq (car-safe (cdr-safe (nth 1 form))) 'make-byte-code)))
|
|
3218 (progn
|
|
3219 (setq body (cdr (cdr fn)))
|
|
3220 (if (stringp (car body)) (setq body (cdr body)))
|
|
3221 (if (eq 'interactive (car-safe (car body))) (setq body (cdr body)))
|
|
3222 (if (and (consp (car body))
|
|
3223 (not (eq 'byte-code (car (car body)))))
|
|
3224 (byte-compile-warn
|
|
3225 "A quoted lambda form is the second argument of fset. This is probably
|
|
3226 not what you want, as that lambda cannot be compiled. Consider using
|
|
3227 the syntax (function (lambda (...) ...)) instead.")))))
|
|
3228 (byte-compile-two-args form))
|
|
3229
|
|
3230 (defun byte-compile-funarg (form)
|
|
3231 ;; (mapcar '(lambda (x) ..) ..) ==> (mapcar (function (lambda (x) ..)) ..)
|
|
3232 ;; for cases where it's guaranteed that first arg will be used as a lambda.
|
|
3233 (byte-compile-normal-call
|
|
3234 (let ((fn (nth 1 form)))
|
|
3235 (if (and (eq (car-safe fn) 'quote)
|
|
3236 (eq (car-safe (nth 1 fn)) 'lambda))
|
|
3237 (cons (car form)
|
|
3238 (cons (cons 'function (cdr fn))
|
|
3239 (cdr (cdr form))))
|
|
3240 form))))
|
|
3241
|
|
3242 ;; (function foo) must compile like 'foo, not like (symbol-function 'foo).
|
|
3243 ;; Otherwise it will be incompatible with the interpreter,
|
|
3244 ;; and (funcall (function foo)) will lose with autoloads.
|
|
3245
|
|
3246 (defun byte-compile-function-form (form)
|
|
3247 (byte-compile-constant
|
|
3248 (cond ((symbolp (nth 1 form))
|
|
3249 (nth 1 form))
|
|
3250 ((byte-compile-lambda (nth 1 form))))))
|
|
3251
|
|
3252 (defun byte-compile-insert (form)
|
|
3253 (cond ((null (cdr form))
|
|
3254 (byte-compile-constant nil))
|
|
3255 ((<= (length form) 256)
|
|
3256 (mapcar 'byte-compile-form (cdr form))
|
|
3257 (if (cdr (cdr form))
|
|
3258 (byte-compile-out 'byte-insertN (length (cdr form)))
|
|
3259 (byte-compile-out 'byte-insert 0)))
|
|
3260 ((memq t (mapcar 'consp (cdr (cdr form))))
|
|
3261 (byte-compile-normal-call form))
|
|
3262 ;; We can split it; there is no function call after inserting 1st arg.
|
|
3263 (t
|
|
3264 (while (setq form (cdr form))
|
|
3265 (byte-compile-form (car form))
|
|
3266 (byte-compile-out 'byte-insert 0)
|
|
3267 (if (cdr form)
|
|
3268 (byte-compile-discard))))))
|
|
3269
|
|
3270 ;; alas, the old (pre-19.12, and all existing versions of FSFmacs 19)
|
|
3271 ;; byte compiler will generate incorrect code for
|
|
3272 ;; (beginning-of-line nil buffer) because it buggily doesn't
|
|
3273 ;; check the number of arguments passed to beginning-of-line.
|
|
3274
|
|
3275 (defun byte-compile-beginning-of-line (form)
|
|
3276 (let ((len (length form)))
|
|
3277 (cond ((> len 3)
|
|
3278 (byte-compile-subr-wrong-args form "0-2"))
|
|
3279 ((or (= len 3) (not (byte-compile-constp (nth 1 form))))
|
|
3280 (byte-compile-normal-call form))
|
|
3281 (t
|
|
3282 (byte-compile-form
|
|
3283 (list 'forward-line
|
|
3284 (if (integerp (setq form (or (eval (nth 1 form)) 1)))
|
|
3285 (1- form)
|
|
3286 (byte-compile-warn
|
|
3287 "Non-numeric arg to beginning-of-line: %s" form)
|
|
3288 (list '1- (list 'quote form))))
|
|
3289 t)
|
|
3290 (byte-compile-constant nil)))))
|
|
3291
|
|
3292
|
|
3293 (byte-defop-compiler set)
|
|
3294 (byte-defop-compiler-1 setq)
|
|
3295 (byte-defop-compiler-1 set-default)
|
|
3296 (byte-defop-compiler-1 setq-default)
|
|
3297
|
|
3298 (byte-defop-compiler-1 quote)
|
|
3299 (byte-defop-compiler-1 quote-form)
|
|
3300
|
|
3301 (defun byte-compile-setq (form)
|
|
3302 (let ((args (cdr form)))
|
|
3303 (if args
|
|
3304 (while args
|
|
3305 (byte-compile-form (car (cdr args)))
|
|
3306 (or for-effect (cdr (cdr args))
|
|
3307 (byte-compile-out 'byte-dup 0))
|
|
3308 (byte-compile-variable-ref 'byte-varset (car args))
|
|
3309 (setq args (cdr (cdr args))))
|
|
3310 ;; (setq), with no arguments.
|
|
3311 (byte-compile-form nil for-effect))
|
|
3312 (setq for-effect nil)))
|
|
3313
|
|
3314 (defun byte-compile-set (form)
|
|
3315 ;; Compile (set 'foo x) as (setq foo x) for trivially better code and so
|
|
3316 ;; that we get applicable warnings. Compile everything else (including
|
|
3317 ;; malformed calls) like a normal 2-arg byte-coded function.
|
|
3318 (if (or (not (eq (car-safe (nth 1 form)) 'quote))
|
|
3319 (not (= (length form) 3))
|
|
3320 (not (= (length (nth 1 form)) 2)))
|
|
3321 (byte-compile-two-args form)
|
|
3322 (byte-compile-setq (list 'setq (nth 1 (nth 1 form)) (nth 2 form)))))
|
|
3323
|
|
3324 (defun byte-compile-setq-default (form)
|
|
3325 (let ((rest (cdr form)))
|
|
3326 ;; emit multiple calls to set-default if necessary
|
|
3327 (while rest
|
|
3328 (byte-compile-form
|
|
3329 (list 'set-default (list 'quote (car rest)) (car (cdr rest)))
|
|
3330 (not (null (cdr (cdr rest)))))
|
|
3331 (setq rest (cdr (cdr rest))))))
|
|
3332
|
|
3333 (defun byte-compile-set-default (form)
|
|
3334 (let ((rest (cdr form)))
|
|
3335 (if (cdr (cdr (cdr form)))
|
|
3336 ;; emit multiple calls to set-default if necessary; all but last
|
|
3337 ;; for-effect (this recurses.)
|
|
3338 (while rest
|
|
3339 (byte-compile-form
|
|
3340 (list 'set-default (car rest) (car (cdr rest)))
|
|
3341 (not (null (cdr rest))))
|
|
3342 (setq rest (cdr (cdr rest))))
|
|
3343 ;; else, this is the one-armed version
|
|
3344 (let ((var (nth 1 form))
|
|
3345 ;;(val (nth 2 form))
|
|
3346 )
|
|
3347 ;; notice calls to set-default/setq-default for variables which
|
|
3348 ;; have not been declared with defvar/defconst.
|
|
3349 (if (and (memq 'free-vars byte-compile-warnings)
|
|
3350 (or (null var)
|
|
3351 (and (eq (car-safe var) 'quote)
|
|
3352 (= 2 (length var)))))
|
|
3353 (let ((sym (nth 1 var))
|
|
3354 cell)
|
|
3355 (or (and sym (symbolp sym) (globally-boundp sym))
|
|
3356 (and (setq cell (assq sym byte-compile-bound-variables))
|
|
3357 (setcdr cell (logior (cdr cell)
|
|
3358 byte-compile-assigned-bit)))
|
|
3359 (memq sym byte-compile-free-assignments)
|
|
3360 (if (or (not (symbolp sym)) (memq sym '(t nil)))
|
|
3361 (progn
|
|
3362 (byte-compile-warn
|
|
3363 "Attempt to set-globally %s %s"
|
|
3364 (if (symbolp sym) "constant" "nonvariable")
|
|
3365 (prin1-to-string sym)))
|
|
3366 (progn
|
|
3367 (byte-compile-warn "assignment to free variable %s" sym)
|
|
3368 (setq byte-compile-free-assignments
|
|
3369 (cons sym byte-compile-free-assignments)))))))
|
|
3370 ;; now emit a normal call to set-default (or possibly multiple calls)
|
|
3371 (byte-compile-normal-call form)))))
|
|
3372
|
|
3373
|
|
3374 (defun byte-compile-quote (form)
|
|
3375 (byte-compile-constant (car (cdr form))))
|
|
3376
|
|
3377 (defun byte-compile-quote-form (form)
|
|
3378 (byte-compile-constant (byte-compile-top-level (nth 1 form))))
|
|
3379
|
|
3380
|
|
3381 ;;; control structures
|
|
3382
|
|
3383 (defun byte-compile-body (body &optional for-effect)
|
|
3384 (while (cdr body)
|
|
3385 (byte-compile-form (car body) t)
|
|
3386 (setq body (cdr body)))
|
|
3387 (byte-compile-form (car body) for-effect))
|
|
3388
|
|
3389 (proclaim-inline byte-compile-body-do-effect)
|
|
3390 (defun byte-compile-body-do-effect (body)
|
|
3391 (byte-compile-body body for-effect)
|
|
3392 (setq for-effect nil))
|
|
3393
|
|
3394 (proclaim-inline byte-compile-form-do-effect)
|
|
3395 (defun byte-compile-form-do-effect (form)
|
|
3396 (byte-compile-form form for-effect)
|
|
3397 (setq for-effect nil))
|
|
3398
|
|
3399 (byte-defop-compiler-1 inline byte-compile-progn)
|
|
3400 (byte-defop-compiler-1 progn)
|
|
3401 (byte-defop-compiler-1 prog1)
|
|
3402 (byte-defop-compiler-1 prog2)
|
|
3403 (byte-defop-compiler-1 if)
|
|
3404 (byte-defop-compiler-1 cond)
|
|
3405 (byte-defop-compiler-1 and)
|
|
3406 (byte-defop-compiler-1 or)
|
|
3407 (byte-defop-compiler-1 while)
|
|
3408 (byte-defop-compiler-1 funcall)
|
|
3409 (byte-defop-compiler-1 apply byte-compile-funarg)
|
|
3410 (byte-defop-compiler-1 mapcar byte-compile-funarg)
|
|
3411 (byte-defop-compiler-1 mapatoms byte-compile-funarg)
|
|
3412 (byte-defop-compiler-1 mapconcat byte-compile-funarg)
|
|
3413 (byte-defop-compiler-1 let)
|
|
3414 (byte-defop-compiler-1 let*)
|
|
3415
|
|
3416 (defun byte-compile-progn (form)
|
|
3417 (byte-compile-body-do-effect (cdr form)))
|
|
3418
|
|
3419 (defun byte-compile-prog1 (form)
|
|
3420 (byte-compile-form-do-effect (car (cdr form)))
|
|
3421 (byte-compile-body (cdr (cdr form)) t))
|
|
3422
|
|
3423 (defun byte-compile-prog2 (form)
|
|
3424 (byte-compile-form (nth 1 form) t)
|
|
3425 (byte-compile-form-do-effect (nth 2 form))
|
|
3426 (byte-compile-body (cdr (cdr (cdr form))) t))
|
|
3427
|
|
3428 (defmacro byte-compile-goto-if (cond discard tag)
|
|
3429 (` (byte-compile-goto
|
|
3430 (if (, cond)
|
|
3431 (if (, discard) 'byte-goto-if-not-nil 'byte-goto-if-not-nil-else-pop)
|
|
3432 (if (, discard) 'byte-goto-if-nil 'byte-goto-if-nil-else-pop))
|
|
3433 (, tag))))
|
|
3434
|
|
3435 (defun byte-compile-if (form)
|
|
3436 (byte-compile-form (car (cdr form)))
|
|
3437 (if (null (nthcdr 3 form))
|
|
3438 ;; No else-forms
|
|
3439 (let ((donetag (byte-compile-make-tag)))
|
|
3440 (byte-compile-goto-if nil for-effect donetag)
|
|
3441 (byte-compile-form (nth 2 form) for-effect)
|
|
3442 (byte-compile-out-tag donetag))
|
|
3443 (let ((donetag (byte-compile-make-tag)) (elsetag (byte-compile-make-tag)))
|
|
3444 (byte-compile-goto 'byte-goto-if-nil elsetag)
|
|
3445 (byte-compile-form (nth 2 form) for-effect)
|
|
3446 (byte-compile-goto 'byte-goto donetag)
|
|
3447 (byte-compile-out-tag elsetag)
|
|
3448 (byte-compile-body (cdr (cdr (cdr form))) for-effect)
|
|
3449 (byte-compile-out-tag donetag)))
|
|
3450 (setq for-effect nil))
|
|
3451
|
|
3452 (defun byte-compile-cond (clauses)
|
|
3453 (let ((donetag (byte-compile-make-tag))
|
|
3454 nexttag clause)
|
|
3455 (while (setq clauses (cdr clauses))
|
|
3456 (setq clause (car clauses))
|
|
3457 (cond ((or (eq (car clause) t)
|
|
3458 (and (eq (car-safe (car clause)) 'quote)
|
|
3459 (car-safe (cdr-safe (car clause)))))
|
|
3460 ;; Unconditional clause
|
|
3461 (setq clause (cons t clause)
|
|
3462 clauses nil))
|
|
3463 ((cdr clauses)
|
|
3464 (byte-compile-form (car clause))
|
|
3465 (if (null (cdr clause))
|
|
3466 ;; First clause is a singleton.
|
|
3467 (byte-compile-goto-if t for-effect donetag)
|
|
3468 (setq nexttag (byte-compile-make-tag))
|
|
3469 (byte-compile-goto 'byte-goto-if-nil nexttag)
|
|
3470 (byte-compile-body (cdr clause) for-effect)
|
|
3471 (byte-compile-goto 'byte-goto donetag)
|
|
3472 (byte-compile-out-tag nexttag)))))
|
|
3473 ;; Last clause
|
|
3474 (and (cdr clause) (not (eq (car clause) t))
|
|
3475 (progn (byte-compile-form (car clause))
|
|
3476 (byte-compile-goto-if nil for-effect donetag)
|
|
3477 (setq clause (cdr clause))))
|
|
3478 (byte-compile-body-do-effect clause)
|
|
3479 (byte-compile-out-tag donetag)))
|
|
3480
|
|
3481 (defun byte-compile-and (form)
|
|
3482 (let ((failtag (byte-compile-make-tag))
|
|
3483 (args (cdr form)))
|
|
3484 (if (null args)
|
|
3485 (byte-compile-form-do-effect t)
|
|
3486 (while (cdr args)
|
|
3487 (byte-compile-form (car args))
|
|
3488 (byte-compile-goto-if nil for-effect failtag)
|
|
3489 (setq args (cdr args)))
|
|
3490 (byte-compile-form-do-effect (car args))
|
|
3491 (byte-compile-out-tag failtag))))
|
|
3492
|
|
3493 (defun byte-compile-or (form)
|
|
3494 (let ((wintag (byte-compile-make-tag))
|
|
3495 (args (cdr form)))
|
|
3496 (if (null args)
|
|
3497 (byte-compile-form-do-effect nil)
|
|
3498 (while (cdr args)
|
|
3499 (byte-compile-form (car args))
|
|
3500 (byte-compile-goto-if t for-effect wintag)
|
|
3501 (setq args (cdr args)))
|
|
3502 (byte-compile-form-do-effect (car args))
|
|
3503 (byte-compile-out-tag wintag))))
|
|
3504
|
|
3505 (defun byte-compile-while (form)
|
|
3506 (let ((endtag (byte-compile-make-tag))
|
|
3507 (looptag (byte-compile-make-tag)))
|
|
3508 (byte-compile-out-tag looptag)
|
|
3509 (byte-compile-form (car (cdr form)))
|
|
3510 (byte-compile-goto-if nil for-effect endtag)
|
|
3511 (byte-compile-body (cdr (cdr form)) t)
|
|
3512 (byte-compile-goto 'byte-goto looptag)
|
|
3513 (byte-compile-out-tag endtag)
|
|
3514 (setq for-effect nil)))
|
|
3515
|
|
3516 (defun byte-compile-funcall (form)
|
|
3517 (mapcar 'byte-compile-form (cdr form))
|
|
3518 (byte-compile-out 'byte-call (length (cdr (cdr form)))))
|
|
3519
|
|
3520
|
|
3521 (defun byte-compile-let (form)
|
|
3522 ;; First compute the binding values in the old scope.
|
|
3523 (let ((varlist (car (cdr form))))
|
|
3524 (while varlist
|
|
3525 (if (consp (car varlist))
|
|
3526 (byte-compile-form (car (cdr (car varlist))))
|
|
3527 (byte-compile-push-constant nil))
|
|
3528 (setq varlist (cdr varlist))))
|
|
3529 (let ((byte-compile-bound-variables
|
|
3530 (cons 'new-scope byte-compile-bound-variables))
|
|
3531 (varlist (reverse (car (cdr form))))
|
|
3532 (extra-flags
|
|
3533 ;; If this let is of the form (let (...) (byte-code ...))
|
|
3534 ;; then assume that it is the result of a transformation of
|
|
3535 ;; ((lambda (...) (byte-code ... )) ...) and thus compile
|
|
3536 ;; the variable bindings as if they were arglist bindings
|
|
3537 ;; (which matters for what warnings.)
|
|
3538 (if (eq 'byte-code (car-safe (nth 2 form)))
|
|
3539 byte-compile-arglist-bit
|
|
3540 nil)))
|
|
3541 (while varlist
|
|
3542 (byte-compile-variable-ref 'byte-varbind
|
|
3543 (if (consp (car varlist))
|
|
3544 (car (car varlist))
|
|
3545 (car varlist))
|
|
3546 extra-flags)
|
|
3547 (setq varlist (cdr varlist)))
|
|
3548 (byte-compile-body-do-effect (cdr (cdr form)))
|
|
3549 (if (memq 'unused-vars byte-compile-warnings)
|
|
3550 ;; done compiling in this scope, warn now.
|
|
3551 (byte-compile-warn-about-unused-variables))
|
|
3552 (byte-compile-out 'byte-unbind (length (car (cdr form))))))
|
|
3553
|
|
3554 (defun byte-compile-let* (form)
|
|
3555 (let ((byte-compile-bound-variables
|
|
3556 (cons 'new-scope byte-compile-bound-variables))
|
|
3557 (varlist (copy-sequence (car (cdr form)))))
|
|
3558 (while varlist
|
|
3559 (if (atom (car varlist))
|
|
3560 (byte-compile-push-constant nil)
|
|
3561 (byte-compile-form (car (cdr (car varlist))))
|
|
3562 (setcar varlist (car (car varlist))))
|
|
3563 (byte-compile-variable-ref 'byte-varbind (car varlist))
|
|
3564 (setq varlist (cdr varlist)))
|
|
3565 (byte-compile-body-do-effect (cdr (cdr form)))
|
|
3566 (if (memq 'unused-vars byte-compile-warnings)
|
|
3567 ;; done compiling in this scope, warn now.
|
|
3568 (byte-compile-warn-about-unused-variables))
|
|
3569 (byte-compile-out 'byte-unbind (length (car (cdr form))))))
|
|
3570
|
|
3571
|
280
|
3572 ;;(byte-defop-compiler-1 /= byte-compile-negated)
|
209
|
3573 (byte-defop-compiler-1 atom byte-compile-negated)
|
|
3574 (byte-defop-compiler-1 nlistp byte-compile-negated)
|
|
3575
|
280
|
3576 ;;(put '/= 'byte-compile-negated-op '=)
|
209
|
3577 (put 'atom 'byte-compile-negated-op 'consp)
|
|
3578 (put 'nlistp 'byte-compile-negated-op 'listp)
|
|
3579
|
|
3580 (defun byte-compile-negated (form)
|
|
3581 (byte-compile-form-do-effect (byte-compile-negation-optimizer form)))
|
|
3582
|
280
|
3583 ;; Even when optimization is off, atom is optimized to (not (consp ...)).
|
209
|
3584 (defun byte-compile-negation-optimizer (form)
|
|
3585 ;; an optimizer for forms where <form1> is less efficient than (not <form2>)
|
|
3586 (list 'not
|
|
3587 (cons (or (get (car form) 'byte-compile-negated-op)
|
|
3588 (error
|
|
3589 "Compiler error: `%s' has no `byte-compile-negated-op' property"
|
|
3590 (car form)))
|
|
3591 (cdr form))))
|
|
3592
|
|
3593 ;;; other tricky macro-like special-forms
|
|
3594
|
|
3595 (byte-defop-compiler-1 catch)
|
|
3596 (byte-defop-compiler-1 unwind-protect)
|
|
3597 (byte-defop-compiler-1 condition-case)
|
|
3598 (byte-defop-compiler-1 save-excursion)
|
|
3599 (byte-defop-compiler-1 save-current-buffer)
|
|
3600 (byte-defop-compiler-1 save-restriction)
|
|
3601 (byte-defop-compiler-1 save-window-excursion)
|
|
3602 (byte-defop-compiler-1 with-output-to-temp-buffer)
|
|
3603 ;; no track-mouse.
|
|
3604
|
|
3605 (defun byte-compile-catch (form)
|
|
3606 (byte-compile-form (car (cdr form)))
|
|
3607 (byte-compile-push-constant
|
|
3608 (byte-compile-top-level (cons 'progn (cdr (cdr form))) for-effect))
|
|
3609 (byte-compile-out 'byte-catch 0))
|
|
3610
|
|
3611 (defun byte-compile-unwind-protect (form)
|
|
3612 (byte-compile-push-constant
|
|
3613 (byte-compile-top-level-body (cdr (cdr form)) t))
|
|
3614 (byte-compile-out 'byte-unwind-protect 0)
|
|
3615 (byte-compile-form-do-effect (car (cdr form)))
|
|
3616 (byte-compile-out 'byte-unbind 1))
|
|
3617
|
|
3618 ;;(defun byte-compile-track-mouse (form)
|
|
3619 ;; (byte-compile-form
|
|
3620 ;; (list
|
|
3621 ;; 'funcall
|
|
3622 ;; (list 'quote
|
|
3623 ;; (list 'lambda nil
|
|
3624 ;; (cons 'track-mouse
|
|
3625 ;; (byte-compile-top-level-body (cdr form))))))))
|
|
3626
|
|
3627 (defun byte-compile-condition-case (form)
|
|
3628 (let* ((var (nth 1 form))
|
|
3629 (byte-compile-bound-variables
|
|
3630 (if var
|
|
3631 (cons (cons var 0)
|
|
3632 (cons 'new-scope byte-compile-bound-variables))
|
|
3633 (cons 'new-scope byte-compile-bound-variables))))
|
|
3634 (or (symbolp var)
|
|
3635 (byte-compile-warn
|
|
3636 "%s is not a variable-name or nil (in condition-case)"
|
|
3637 (prin1-to-string var)))
|
|
3638 (byte-compile-push-constant var)
|
|
3639 (byte-compile-push-constant (byte-compile-top-level
|
|
3640 (nth 2 form) for-effect))
|
|
3641 (let ((clauses (cdr (cdr (cdr form))))
|
|
3642 compiled-clauses)
|
|
3643 (while clauses
|
|
3644 (let* ((clause (car clauses))
|
|
3645 (condition (car clause)))
|
|
3646 (cond ((not (or (symbolp condition)
|
|
3647 (and (listp condition)
|
|
3648 (let ((syms condition) (ok t))
|
|
3649 (while syms
|
|
3650 (if (not (symbolp (car syms)))
|
|
3651 (setq ok nil))
|
|
3652 (setq syms (cdr syms)))
|
|
3653 ok))))
|
|
3654 (byte-compile-warn
|
|
3655 "%s is not a symbol naming a condition or a list of such (in condition-case)"
|
|
3656 (prin1-to-string condition)))
|
|
3657 ;; ((not (or (eq condition 't)
|
|
3658 ;; (and (stringp (get condition 'error-message))
|
|
3659 ;; (consp (get condition 'error-conditions)))))
|
|
3660 ;; (byte-compile-warn
|
272
|
3661 ;; "%s is not a known condition name (in condition-case)"
|
209
|
3662 ;; condition))
|
|
3663 )
|
|
3664 (setq compiled-clauses
|
|
3665 (cons (cons condition
|
|
3666 (byte-compile-top-level-body
|
|
3667 (cdr clause) for-effect))
|
|
3668 compiled-clauses)))
|
|
3669 (setq clauses (cdr clauses)))
|
|
3670 (byte-compile-push-constant (nreverse compiled-clauses)))
|
|
3671 (if (memq 'unused-vars byte-compile-warnings)
|
|
3672 ;; done compiling in this scope, warn now.
|
|
3673 (byte-compile-warn-about-unused-variables))
|
|
3674 (byte-compile-out 'byte-condition-case 0)))
|
|
3675
|
|
3676
|
|
3677 (defun byte-compile-save-excursion (form)
|
|
3678 (byte-compile-out 'byte-save-excursion 0)
|
|
3679 (byte-compile-body-do-effect (cdr form))
|
|
3680 (byte-compile-out 'byte-unbind 1))
|
|
3681
|
|
3682 (defun byte-compile-save-restriction (form)
|
|
3683 (byte-compile-out 'byte-save-restriction 0)
|
|
3684 (byte-compile-body-do-effect (cdr form))
|
|
3685 (byte-compile-out 'byte-unbind 1))
|
|
3686
|
|
3687 (defun byte-compile-save-current-buffer (form)
|
|
3688 (if (byte-compile-version-cond byte-compile-emacs19-compatibility)
|
|
3689 ;; `save-current-buffer' special form is not available in XEmacs 19.
|
|
3690 (byte-compile-form
|
|
3691 `(let ((_byte_compiler_save_buffer_emulation_closure_ (current-buffer)))
|
|
3692 (unwind-protect
|
|
3693 (progn ,@(cdr form))
|
|
3694 (and (buffer-live-p _byte_compiler_save_buffer_emulation_closure_)
|
|
3695 (set-buffer _byte_compiler_save_buffer_emulation_closure_)))))
|
|
3696 (byte-compile-out 'byte-save-current-buffer 0)
|
|
3697 (byte-compile-body-do-effect (cdr form))
|
|
3698 (byte-compile-out 'byte-unbind 1)))
|
|
3699
|
|
3700 (defun byte-compile-save-window-excursion (form)
|
|
3701 (byte-compile-push-constant
|
|
3702 (byte-compile-top-level-body (cdr form) for-effect))
|
|
3703 (byte-compile-out 'byte-save-window-excursion 0))
|
|
3704
|
|
3705 (defun byte-compile-with-output-to-temp-buffer (form)
|
|
3706 (byte-compile-form (car (cdr form)))
|
|
3707 (byte-compile-out 'byte-temp-output-buffer-setup 0)
|
|
3708 (byte-compile-body (cdr (cdr form)))
|
|
3709 (byte-compile-out 'byte-temp-output-buffer-show 0))
|
|
3710
|
|
3711
|
|
3712 ;;; top-level forms elsewhere
|
|
3713
|
|
3714 (byte-defop-compiler-1 defun)
|
|
3715 (byte-defop-compiler-1 defmacro)
|
|
3716 (byte-defop-compiler-1 defvar)
|
367
|
3717 (byte-defop-compiler-1 defvar byte-compile-defvar-or-defconst)
|
|
3718 (byte-defop-compiler-1 defconst byte-compile-defvar-or-defconst)
|
209
|
3719 (byte-defop-compiler-1 autoload)
|
|
3720 ;; According to Mly this can go now that lambda is a macro
|
|
3721 ;(byte-defop-compiler-1 lambda byte-compile-lambda-form)
|
|
3722 (byte-defop-compiler-1 defalias)
|
|
3723 (byte-defop-compiler-1 define-function)
|
|
3724
|
|
3725 (defun byte-compile-defun (form)
|
|
3726 ;; This is not used for file-level defuns with doc strings.
|
|
3727 (byte-compile-two-args ; Use this to avoid byte-compile-fset's warning.
|
|
3728 (list 'fset (list 'quote (nth 1 form))
|
|
3729 (byte-compile-byte-code-maker
|
|
3730 (byte-compile-lambda (cons 'lambda (cdr (cdr form)))))))
|
|
3731 (byte-compile-discard)
|
|
3732 (byte-compile-constant (nth 1 form)))
|
|
3733
|
|
3734 (defun byte-compile-defmacro (form)
|
|
3735 ;; This is not used for file-level defmacros with doc strings.
|
|
3736 (byte-compile-body-do-effect
|
|
3737 (list (list 'fset (list 'quote (nth 1 form))
|
|
3738 (let ((code (byte-compile-byte-code-maker
|
|
3739 (byte-compile-lambda
|
|
3740 (cons 'lambda (cdr (cdr form)))))))
|
|
3741 (if (eq (car-safe code) 'make-byte-code)
|
|
3742 (list 'cons ''macro code)
|
|
3743 (list 'quote (cons 'macro (eval code))))))
|
|
3744 (list 'quote (nth 1 form)))))
|
|
3745
|
367
|
3746 (defun byte-compile-defvar-or-defconst (form)
|
|
3747 ;; This is not used for file-level defvar/defconsts with doc strings:
|
|
3748 ;; byte-compile-file-form-defvar-or-defconst will be used in that case.
|
|
3749 ;; (defvar|defconst VAR [VALUE [DOCSTRING]])
|
|
3750 (let ((fun (nth 0 form))
|
|
3751 (var (nth 1 form))
|
209
|
3752 (value (nth 2 form))
|
|
3753 (string (nth 3 form)))
|
367
|
3754 (when (> (length form) 4)
|
|
3755 (byte-compile-warn
|
|
3756 "%s %s called with %d arguments, but accepts only %s"
|
|
3757 fun var (length (cdr form)) 3))
|
|
3758 (when (memq 'free-vars byte-compile-warnings)
|
|
3759 (push (cons var byte-compile-global-bit) byte-compile-bound-variables))
|
209
|
3760 (byte-compile-body-do-effect
|
367
|
3761 (list
|
209
|
3762 ;; Put the defined variable in this library's load-history entry
|
367
|
3763 ;; just as a real defvar would, but only in top-level forms.
|
|
3764 (when (null byte-compile-current-form)
|
|
3765 `(push ',var current-load-list))
|
|
3766 (when (> (length form) 3)
|
|
3767 (when (and string (not (stringp string)))
|
|
3768 (byte-compile-warn "Third arg to %s %s is not a string: %s"
|
|
3769 fun var string))
|
|
3770 `(put ',var 'variable-documentation ,string))
|
|
3771 (if (cdr (cdr form)) ; `value' provided
|
|
3772 (if (eq fun 'defconst)
|
|
3773 ;; `defconst' sets `var' unconditionally.
|
|
3774 `(setq ,var ,value)
|
|
3775 ;; `defvar' sets `var' only when unbound.
|
|
3776 `(if (not (boundp ',var)) (setq ,var ,value))))
|
|
3777 `',var))))
|
209
|
3778
|
|
3779 (defun byte-compile-autoload (form)
|
|
3780 (and (byte-compile-constp (nth 1 form))
|
|
3781 (byte-compile-constp (nth 5 form))
|
|
3782 (memq (eval (nth 5 form)) '(t macro)) ; macro-p
|
|
3783 (not (fboundp (eval (nth 1 form))))
|
|
3784 (byte-compile-warn
|
272
|
3785 "The compiler ignores `autoload' except at top level. You should
|
209
|
3786 probably put the autoload of the macro `%s' at top-level."
|
|
3787 (eval (nth 1 form))))
|
|
3788 (byte-compile-normal-call form))
|
|
3789
|
|
3790 ;; Lambda's in valid places are handled as special cases by various code.
|
|
3791 ;; The ones that remain are errors.
|
|
3792 ;; According to Mly this can go now that lambda is a macro
|
|
3793 ;(defun byte-compile-lambda-form (form)
|
|
3794 ; (byte-compile-warn
|
|
3795 ; "`lambda' used in function position is invalid: probably you mean #'%s"
|
|
3796 ; (let ((print-escape-newlines t)
|
|
3797 ; (print-level 4)
|
|
3798 ; (print-length 4))
|
|
3799 ; (prin1-to-string form)))
|
|
3800 ; (byte-compile-normal-call
|
|
3801 ; (list 'signal ''error
|
|
3802 ; (list 'quote (list "`lambda' used in function position" form)))))
|
|
3803
|
|
3804 ;; Compile normally, but deal with warnings for the function being defined.
|
|
3805 (defun byte-compile-defalias (form)
|
|
3806 (if (and (consp (cdr form)) (consp (nth 1 form))
|
|
3807 (eq (car (nth 1 form)) 'quote)
|
|
3808 (consp (cdr (nth 1 form)))
|
|
3809 (symbolp (nth 1 (nth 1 form)))
|
|
3810 (consp (nthcdr 2 form))
|
|
3811 (consp (nth 2 form))
|
|
3812 (eq (car (nth 2 form)) 'quote)
|
|
3813 (consp (cdr (nth 2 form)))
|
|
3814 (symbolp (nth 1 (nth 2 form))))
|
|
3815 (progn
|
|
3816 (byte-compile-defalias-warn (nth 1 (nth 1 form))
|
|
3817 (nth 1 (nth 2 form)))
|
|
3818 (setq byte-compile-function-environment
|
|
3819 (cons (cons (nth 1 (nth 1 form))
|
|
3820 (nth 1 (nth 2 form)))
|
|
3821 byte-compile-function-environment))))
|
|
3822 (byte-compile-normal-call form))
|
|
3823
|
|
3824 (defun byte-compile-define-function (form)
|
|
3825 (byte-compile-defalias form))
|
|
3826
|
|
3827 ;; Turn off warnings about prior calls to the function being defalias'd.
|
|
3828 ;; This could be smarter and compare those calls with
|
|
3829 ;; the function it is being aliased to.
|
|
3830 (defun byte-compile-defalias-warn (new alias)
|
|
3831 (let ((calls (assq new byte-compile-unresolved-functions)))
|
|
3832 (if calls
|
|
3833 (setq byte-compile-unresolved-functions
|
|
3834 (delq calls byte-compile-unresolved-functions)))))
|
|
3835
|
|
3836 ;;; tags
|
|
3837
|
|
3838 ;; Note: Most operations will strip off the 'TAG, but it speeds up
|
|
3839 ;; optimization to have the 'TAG as a part of the tag.
|
|
3840 ;; Tags will be (TAG . (tag-number . stack-depth)).
|
|
3841 (defun byte-compile-make-tag ()
|
|
3842 (list 'TAG (setq byte-compile-tag-number (1+ byte-compile-tag-number))))
|
|
3843
|
|
3844
|
|
3845 (defun byte-compile-out-tag (tag)
|
|
3846 (setq byte-compile-output (cons tag byte-compile-output))
|
|
3847 (if (cdr (cdr tag))
|
|
3848 (progn
|
|
3849 ;; ## remove this someday
|
|
3850 (and byte-compile-depth
|
|
3851 (not (= (cdr (cdr tag)) byte-compile-depth))
|
|
3852 (error "Compiler bug: depth conflict at tag %d" (car (cdr tag))))
|
|
3853 (setq byte-compile-depth (cdr (cdr tag))))
|
|
3854 (setcdr (cdr tag) byte-compile-depth)))
|
|
3855
|
|
3856 (defun byte-compile-goto (opcode tag)
|
|
3857 (setq byte-compile-output (cons (cons opcode tag) byte-compile-output))
|
|
3858 (setcdr (cdr tag) (if (memq opcode byte-goto-always-pop-ops)
|
|
3859 (1- byte-compile-depth)
|
|
3860 byte-compile-depth))
|
|
3861 (setq byte-compile-depth (and (not (eq opcode 'byte-goto))
|
|
3862 (1- byte-compile-depth))))
|
|
3863
|
|
3864 (defun byte-compile-out (opcode offset)
|
|
3865 (setq byte-compile-output (cons (cons opcode offset) byte-compile-output))
|
|
3866 (cond ((eq opcode 'byte-call)
|
|
3867 (setq byte-compile-depth (- byte-compile-depth offset)))
|
|
3868 ((eq opcode 'byte-return)
|
|
3869 ;; This is actually an unnecessary case, because there should be
|
|
3870 ;; no more opcodes behind byte-return.
|
|
3871 (setq byte-compile-depth nil))
|
|
3872 (t
|
|
3873 (setq byte-compile-depth (+ byte-compile-depth
|
|
3874 (or (aref byte-stack+-info
|
|
3875 (symbol-value opcode))
|
|
3876 (- (1- offset))))
|
|
3877 byte-compile-maxdepth (max byte-compile-depth
|
|
3878 byte-compile-maxdepth))))
|
|
3879 ;;(if (< byte-compile-depth 0) (error "Compiler error: stack underflow"))
|
|
3880 )
|
|
3881
|
|
3882
|
|
3883 ;;; call tree stuff
|
|
3884
|
|
3885 (defun byte-compile-annotate-call-tree (form)
|
|
3886 (let (entry)
|
|
3887 ;; annotate the current call
|
|
3888 (if (setq entry (assq (car form) byte-compile-call-tree))
|
|
3889 (or (memq byte-compile-current-form (nth 1 entry)) ;callers
|
|
3890 (setcar (cdr entry)
|
|
3891 (cons byte-compile-current-form (nth 1 entry))))
|
|
3892 (setq byte-compile-call-tree
|
|
3893 (cons (list (car form) (list byte-compile-current-form) nil)
|
|
3894 byte-compile-call-tree)))
|
|
3895 ;; annotate the current function
|
|
3896 (if (setq entry (assq byte-compile-current-form byte-compile-call-tree))
|
|
3897 (or (memq (car form) (nth 2 entry)) ;called
|
|
3898 (setcar (cdr (cdr entry))
|
|
3899 (cons (car form) (nth 2 entry))))
|
|
3900 (setq byte-compile-call-tree
|
|
3901 (cons (list byte-compile-current-form nil (list (car form)))
|
|
3902 byte-compile-call-tree)))
|
|
3903 ))
|
|
3904
|
|
3905 ;; Renamed from byte-compile-report-call-tree
|
|
3906 ;; to avoid interfering with completion of byte-compile-file.
|
|
3907 ;;;###autoload
|
|
3908 (defun display-call-tree (&optional filename)
|
|
3909 "Display a call graph of a specified file.
|
|
3910 This lists which functions have been called, what functions called
|
|
3911 them, and what functions they call. The list includes all functions
|
|
3912 whose definitions have been compiled in this Emacs session, as well as
|
|
3913 all functions called by those functions.
|
|
3914
|
|
3915 The call graph does not include macros, inline functions, or
|
|
3916 primitives that the byte-code interpreter knows about directly \(eq,
|
|
3917 cons, etc.\).
|
|
3918
|
|
3919 The call tree also lists those functions which are not known to be called
|
|
3920 \(that is, to which no calls have been compiled\), and which cannot be
|
|
3921 invoked interactively."
|
|
3922 (interactive)
|
|
3923 (message "Generating call tree...")
|
|
3924 (with-output-to-temp-buffer "*Call-Tree*"
|
|
3925 (set-buffer "*Call-Tree*")
|
|
3926 (erase-buffer)
|
|
3927 (message "Generating call tree... (sorting on %s)"
|
|
3928 byte-compile-call-tree-sort)
|
|
3929 (insert "Call tree for "
|
|
3930 (cond ((null byte-compile-current-file) (or filename "???"))
|
|
3931 ((stringp byte-compile-current-file)
|
|
3932 byte-compile-current-file)
|
|
3933 (t (buffer-name byte-compile-current-file)))
|
|
3934 " sorted on "
|
|
3935 (prin1-to-string byte-compile-call-tree-sort)
|
|
3936 ":\n\n")
|
|
3937 (if byte-compile-call-tree-sort
|
|
3938 (setq byte-compile-call-tree
|
|
3939 (sort byte-compile-call-tree
|
|
3940 (cond
|
|
3941 ((eq byte-compile-call-tree-sort 'callers)
|
|
3942 (function (lambda (x y) (< (length (nth 1 x))
|
|
3943 (length (nth 1 y))))))
|
|
3944 ((eq byte-compile-call-tree-sort 'calls)
|
|
3945 (function (lambda (x y) (< (length (nth 2 x))
|
|
3946 (length (nth 2 y))))))
|
|
3947 ((eq byte-compile-call-tree-sort 'calls+callers)
|
|
3948 (function (lambda (x y) (< (+ (length (nth 1 x))
|
|
3949 (length (nth 2 x)))
|
|
3950 (+ (length (nth 1 y))
|
|
3951 (length (nth 2 y)))))))
|
|
3952 ((eq byte-compile-call-tree-sort 'name)
|
|
3953 (function (lambda (x y) (string< (car x)
|
|
3954 (car y)))))
|
|
3955 (t (error
|
|
3956 "`byte-compile-call-tree-sort': `%s' - unknown sort mode"
|
|
3957 byte-compile-call-tree-sort))))))
|
|
3958 (message "Generating call tree...")
|
|
3959 (let ((rest byte-compile-call-tree)
|
|
3960 (b (current-buffer))
|
|
3961 f p
|
|
3962 callers calls)
|
|
3963 (while rest
|
|
3964 (prin1 (car (car rest)) b)
|
|
3965 (setq callers (nth 1 (car rest))
|
|
3966 calls (nth 2 (car rest)))
|
|
3967 (insert "\t"
|
|
3968 (cond ((not (fboundp (setq f (car (car rest)))))
|
|
3969 (if (null f)
|
|
3970 " <top level>";; shouldn't insert nil then, actually -sk
|
|
3971 " <not defined>"))
|
|
3972 ((subrp (setq f (symbol-function f)))
|
|
3973 " <subr>")
|
|
3974 ((symbolp f)
|
|
3975 (format " ==> %s" f))
|
|
3976 ((compiled-function-p f)
|
|
3977 "<compiled function>")
|
|
3978 ((not (consp f))
|
|
3979 "<malformed function>")
|
|
3980 ((eq 'macro (car f))
|
|
3981 (if (or (compiled-function-p (cdr f))
|
|
3982 (assq 'byte-code (cdr (cdr (cdr f)))))
|
|
3983 " <compiled macro>"
|
|
3984 " <macro>"))
|
|
3985 ((assq 'byte-code (cdr (cdr f)))
|
|
3986 "<compiled lambda>")
|
|
3987 ((eq 'lambda (car f))
|
|
3988 "<function>")
|
|
3989 (t "???"))
|
|
3990 (format " (%d callers + %d calls = %d)"
|
|
3991 ;; Does the optimizer eliminate common subexpressions?-sk
|
|
3992 (length callers)
|
|
3993 (length calls)
|
|
3994 (+ (length callers) (length calls)))
|
|
3995 "\n")
|
|
3996 (if callers
|
|
3997 (progn
|
|
3998 (insert " called by:\n")
|
|
3999 (setq p (point))
|
|
4000 (insert " " (if (car callers)
|
|
4001 (mapconcat 'symbol-name callers ", ")
|
|
4002 "<top level>"))
|
|
4003 (let ((fill-prefix " "))
|
|
4004 (fill-region-as-paragraph p (point)))))
|
|
4005 (if calls
|
|
4006 (progn
|
|
4007 (insert " calls:\n")
|
|
4008 (setq p (point))
|
|
4009 (insert " " (mapconcat 'symbol-name calls ", "))
|
|
4010 (let ((fill-prefix " "))
|
|
4011 (fill-region-as-paragraph p (point)))))
|
|
4012 (insert "\n")
|
|
4013 (setq rest (cdr rest)))
|
|
4014
|
|
4015 (message "Generating call tree...(finding uncalled functions...)")
|
|
4016 (setq rest byte-compile-call-tree)
|
|
4017 (let ((uncalled nil))
|
|
4018 (while rest
|
|
4019 (or (nth 1 (car rest))
|
|
4020 (null (setq f (car (car rest))))
|
|
4021 (byte-compile-fdefinition f t)
|
|
4022 (commandp (byte-compile-fdefinition f nil))
|
|
4023 (setq uncalled (cons f uncalled)))
|
|
4024 (setq rest (cdr rest)))
|
|
4025 (if uncalled
|
|
4026 (let ((fill-prefix " "))
|
|
4027 (insert "Noninteractive functions not known to be called:\n ")
|
|
4028 (setq p (point))
|
|
4029 (insert (mapconcat 'symbol-name (nreverse uncalled) ", "))
|
|
4030 (fill-region-as-paragraph p (point)))))
|
|
4031 )
|
|
4032 (message "Generating call tree...done.")
|
|
4033 ))
|
|
4034
|
|
4035
|
|
4036 ;;; by crl@newton.purdue.edu
|
|
4037 ;;; Only works noninteractively.
|
|
4038 ;;;###autoload
|
|
4039 (defun batch-byte-compile ()
|
|
4040 "Run `byte-compile-file' on the files remaining on the command line.
|
|
4041 Use this from the command line, with `-batch';
|
|
4042 it won't work in an interactive Emacs.
|
|
4043 Each file is processed even if an error occurred previously.
|
272
|
4044 For example, invoke \"xemacs -batch -f batch-byte-compile $emacs/ ~/*.el\""
|
209
|
4045 ;; command-line-args-left is what is left of the command line (from
|
|
4046 ;; startup.el)
|
|
4047 (defvar command-line-args-left) ;Avoid 'free variable' warning
|
|
4048 (if (not noninteractive)
|
|
4049 (error "`batch-byte-compile' is to be used only with -batch"))
|
|
4050 (let ((error nil)
|
|
4051 (debug-issue-ebola-notices 0)) ; Hack -slb
|
|
4052 (while command-line-args-left
|
|
4053 (if (file-directory-p (expand-file-name (car command-line-args-left)))
|
|
4054 (let ((files (directory-files (car command-line-args-left)))
|
|
4055 source dest)
|
|
4056 (while files
|
|
4057 (if (and (string-match emacs-lisp-file-regexp (car files))
|
|
4058 (not (auto-save-file-name-p (car files)))
|
|
4059 (setq source (expand-file-name
|
|
4060 (car files)
|
|
4061 (car command-line-args-left)))
|
|
4062 (setq dest (byte-compile-dest-file source))
|
|
4063 (file-exists-p dest)
|
|
4064 (file-newer-than-file-p source dest))
|
|
4065 (if (null (batch-byte-compile-1 source))
|
|
4066 (setq error t)))
|
|
4067 (setq files (cdr files))))
|
|
4068 (if (null (batch-byte-compile-1 (car command-line-args-left)))
|
|
4069 (setq error t)))
|
|
4070 (setq command-line-args-left (cdr command-line-args-left)))
|
|
4071 (message "Done")
|
|
4072 (kill-emacs (if error 1 0))))
|
|
4073
|
|
4074 (defun batch-byte-compile-1 (file)
|
|
4075 (condition-case err
|
|
4076 (progn (byte-compile-file file) t)
|
|
4077 (error
|
|
4078 (princ ">>Error occurred processing ")
|
|
4079 (princ file)
|
|
4080 (princ ": ")
|
|
4081 (if (fboundp 'display-error) ; XEmacs 19.8+
|
|
4082 (display-error err nil)
|
|
4083 (princ (or (get (car err) 'error-message) (car err)))
|
|
4084 (mapcar '(lambda (x) (princ " ") (prin1 x)) (cdr err)))
|
|
4085 (princ "\n")
|
|
4086 nil)))
|
|
4087
|
|
4088 ;;;###autoload
|
|
4089 (defun batch-byte-recompile-directory-norecurse ()
|
|
4090 "Same as `batch-byte-recompile-directory' but without recursion."
|
|
4091 (setq byte-recompile-directory-recursively nil)
|
|
4092 (batch-byte-recompile-directory))
|
|
4093
|
|
4094 ;;;###autoload
|
|
4095 (defun batch-byte-recompile-directory ()
|
|
4096 "Runs `byte-recompile-directory' on the dirs remaining on the command line.
|
|
4097 Must be used only with `-batch', and kills Emacs on completion.
|
|
4098 For example, invoke `xemacs -batch -f batch-byte-recompile-directory .'."
|
|
4099 ;; command-line-args-left is what is left of the command line (startup.el)
|
|
4100 (defvar command-line-args-left) ;Avoid 'free variable' warning
|
|
4101 (if (not noninteractive)
|
|
4102 (error "batch-byte-recompile-directory is to be used only with -batch"))
|
|
4103 (or command-line-args-left
|
|
4104 (setq command-line-args-left '(".")))
|
|
4105 (let ((byte-recompile-directory-ignore-errors-p t)
|
|
4106 (debug-issue-ebola-notices 0))
|
|
4107 (while command-line-args-left
|
|
4108 (byte-recompile-directory (car command-line-args-left))
|
|
4109 (setq command-line-args-left (cdr command-line-args-left))))
|
|
4110 (kill-emacs 0))
|
|
4111
|
|
4112 (make-obsolete 'elisp-compile-defun 'compile-defun)
|
|
4113 (make-obsolete 'byte-compile-report-call-tree 'display-call-tree)
|
|
4114
|
|
4115 ;; other make-obsolete calls in obsolete.el.
|
|
4116
|
|
4117 (provide 'byte-compile)
|
|
4118 (provide 'bytecomp)
|
|
4119
|
|
4120
|
|
4121 ;;; report metering (see the hacks in bytecode.c)
|
|
4122
|
|
4123 (if (boundp 'byte-code-meter)
|
|
4124 (defun byte-compile-report-ops ()
|
|
4125 (defvar byte-code-meter)
|
|
4126 (with-output-to-temp-buffer "*Meter*"
|
|
4127 (set-buffer "*Meter*")
|
|
4128 (let ((i 0) n op off)
|
|
4129 (while (< i 256)
|
|
4130 (setq n (aref (aref byte-code-meter 0) i)
|
|
4131 off nil)
|
|
4132 (if t ;(not (zerop n))
|
|
4133 (progn
|
|
4134 (setq op i)
|
|
4135 (setq off nil)
|
|
4136 (cond ((< op byte-nth)
|
|
4137 (setq off (logand op 7))
|
|
4138 (setq op (logand op 248)))
|
|
4139 ((>= op byte-constant)
|
|
4140 (setq off (- op byte-constant)
|
|
4141 op byte-constant)))
|
|
4142 (setq op (aref byte-code-vector op))
|
|
4143 (insert (format "%-4d" i))
|
|
4144 (insert (symbol-name op))
|
|
4145 (if off (insert " [" (int-to-string off) "]"))
|
|
4146 (indent-to 40)
|
|
4147 (insert (int-to-string n) "\n")))
|
|
4148 (setq i (1+ i)))))))
|
|
4149
|
|
4150
|
|
4151 ;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when bytecomp compiles
|
|
4152 ;; itself, compile some of its most used recursive functions (at load time).
|
|
4153 ;;
|
|
4154 (eval-when-compile
|
|
4155 (or (compiled-function-p (symbol-function 'byte-compile-form))
|
|
4156 (assq 'byte-code (symbol-function 'byte-compile-form))
|
|
4157 (let ((byte-optimize nil) ; do it fast
|
|
4158 (byte-compile-warnings nil))
|
|
4159 (mapcar '(lambda (x)
|
|
4160 (or noninteractive (message "compiling %s..." x))
|
|
4161 (byte-compile x)
|
|
4162 (or noninteractive (message "compiling %s...done" x)))
|
|
4163 '(byte-compile-normal-call
|
|
4164 byte-compile-form
|
|
4165 byte-compile-body
|
|
4166 ;; Inserted some more than necessary, to speed it up.
|
|
4167 byte-compile-top-level
|
|
4168 byte-compile-out-toplevel
|
|
4169 byte-compile-constant
|
|
4170 byte-compile-variable-ref))))
|
|
4171 nil)
|
|
4172
|
|
4173 ;;; bytecomp.el ends here
|