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