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