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