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