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