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