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