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