Added or corrected Commentary sections
[bpt/emacs.git] / lisp / emacs-lisp / bytecomp.el
1 ;;; bytecomp.el --- compilation of Lisp code into byte code.
2
3 ;;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
4
5 ;; Author: Jamie Zawinski <jwz@lucid.com>
6 ;; Hallvard Furuseth <hbf@ulrik.uio.no>
7 ;; Keywords: internal
8
9 ;; Subsequently modified by RMS.
10
11 ;;; This version incorporates changes up to version 2.08 of the
12 ;;; Zawinski-Furuseth compiler.
13 (defconst byte-compile-version "FSF 2.08")
14
15 ;; This file is part of GNU Emacs.
16
17 ;; GNU Emacs is free software; you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation; either version 2, or (at your option)
20 ;; any later version.
21
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
26
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs; see the file COPYING. If not, write to
29 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
30
31 ;;; Commentary:
32
33 ;; The Emacs Lisp byte compiler. This crunches lisp source into a sort
34 ;; of p-code which takes up less space and can be interpreted faster.
35 ;; The user entry points are byte-compile-file and byte-recompile-directory.
36
37 ;;; Code:
38
39 ;;; ========================================================================
40 ;;; Entry points:
41 ;;; byte-recompile-directory, byte-compile-file, batch-byte-compile,
42 ;;; byte-compile, compile-defun
43 ;;; display-call-tree
44 ;;; (byte-compile-buffer and byte-compile-and-load-file were turned off
45 ;;; because they are not terribly useful and get in the way of completion.)
46
47 ;;; This version of the byte compiler has the following improvements:
48 ;;; + optimization of compiled code:
49 ;;; - removal of unreachable code;
50 ;;; - removal of calls to side-effectless functions whose return-value
51 ;;; is unused;
52 ;;; - compile-time evaluation of safe constant forms, such as (consp nil)
53 ;;; and (ash 1 6);
54 ;;; - open-coding of literal lambdas;
55 ;;; - peephole optimization of emitted code;
56 ;;; - trivial functions are left uncompiled for speed.
57 ;;; + support for inline functions;
58 ;;; + compile-time evaluation of arbitrary expressions;
59 ;;; + compile-time warning messages for:
60 ;;; - functions being redefined with incompatible arglists;
61 ;;; - functions being redefined as macros, or vice-versa;
62 ;;; - functions or macros defined multiple times in the same file;
63 ;;; - functions being called with the incorrect number of arguments;
64 ;;; - functions being called which are not defined globally, in the
65 ;;; file, or as autoloads;
66 ;;; - assignment and reference of undeclared free variables;
67 ;;; - various syntax errors;
68 ;;; + correct compilation of nested defuns, defmacros, defvars and defsubsts;
69 ;;; + correct compilation of top-level uses of macros;
70 ;;; + the ability to generate a histogram of functions called.
71
72 ;;; User customization variables:
73 ;;;
74 ;;; byte-compile-verbose Whether to report the function currently being
75 ;;; compiled in the minibuffer;
76 ;;; byte-optimize Whether to do optimizations; this may be
77 ;;; t, nil, 'source, or 'byte;
78 ;;; byte-optimize-log Whether to report (in excruciating detail)
79 ;;; exactly which optimizations have been made.
80 ;;; This may be t, nil, 'source, or 'byte;
81 ;;; byte-compile-error-on-warn Whether to stop compilation when a warning is
82 ;;; produced;
83 ;;; byte-compile-delete-errors Whether the optimizer may delete calls or
84 ;;; variable references that are side-effect-free
85 ;;; except that they may return an error.
86 ;;; byte-compile-generate-call-tree Whether to generate a histogram of
87 ;;; function calls. This can be useful for
88 ;;; finding unused functions, as well as simple
89 ;;; performance metering.
90 ;;; byte-compile-warnings List of warnings to issue, or t. May contain
91 ;;; 'free-vars (references to variables not in the
92 ;;; current lexical scope)
93 ;;; 'unresolved (calls to unknown functions)
94 ;;; 'callargs (lambda calls with args that don't
95 ;;; match the lambda's definition)
96 ;;; 'redefine (function cell redefined from
97 ;;; a macro to a lambda or vice versa,
98 ;;; or redefined to take other args)
99 ;;; This defaults to nil in -batch mode, which is
100 ;;; slightly faster.
101 ;;; byte-compile-compatibility Whether the compiler should
102 ;;; generate .elc files which can be loaded into
103 ;;; generic emacs 18.
104 ;;; byte-compile-single-version Normally the byte-compiler will consult the
105 ;;; above two variables at runtime, but if this
106 ;;; is true before the compiler itself is loaded/
107 ;;; compiled, then the runtime checks will not be
108 ;;; made, and compilation will be slightly faster.
109 ;;; To use this, start up a fresh emacs, set this
110 ;;; to t, reload the compiler's .el files, and
111 ;;; recompile. Don't do this in an emacs that has
112 ;;; already had the compiler loaded.
113 ;;; byte-compile-overwrite-file If nil, delete old .elc files before saving.
114
115 ;;; New Features:
116 ;;;
117 ;;; o The form `defsubst' is just like `defun', except that the function
118 ;;; generated will be open-coded in compiled code which uses it. This
119 ;;; means that no function call will be generated, it will simply be
120 ;;; spliced in. Lisp functions calls are very slow, so this can be a
121 ;;; big win.
122 ;;;
123 ;;; You can generally accomplish the same thing with `defmacro', but in
124 ;;; that case, the defined procedure can't be used as an argument to
125 ;;; mapcar, etc.
126 ;;;
127 ;;; o You can also open-code one particular call to a function without
128 ;;; open-coding all calls. Use the 'inline' form to do this, like so:
129 ;;;
130 ;;; (inline (foo 1 2 3)) ;; `foo' will be open-coded
131 ;;; or...
132 ;;; (inline ;; `foo' and `baz' will be
133 ;;; (foo 1 2 3 (bar 5)) ;; open-coded, but `bar' will not.
134 ;;; (baz 0))
135 ;;;
136 ;;; o It is possible to open-code a function in the same file it is defined
137 ;;; in without having to load that file before compiling it. the
138 ;;; byte-compiler has been modified to remember function definitions in
139 ;;; the compilation environment in the same way that it remembers macro
140 ;;; definitions.
141 ;;;
142 ;;; o Forms like ((lambda ...) ...) are open-coded.
143 ;;;
144 ;;; o The form `eval-when-compile' is like progn, except that the body
145 ;;; is evaluated at compile-time. When it appears at top-level, this
146 ;;; is analagous to the Common Lisp idiom (eval-when (compile) ...).
147 ;;; When it does not appear at top-level, it is similar to the
148 ;;; Common Lisp #. reader macro (but not in interpreted code.)
149 ;;;
150 ;;; o The form `eval-and-compile' is similar to eval-when-compile, but
151 ;;; the whole form is evalled both at compile-time and at run-time.
152 ;;;
153 ;;; o The command Meta-X byte-compile-and-load-file does what you'd think.
154 ;;;
155 ;;; o The command compile-defun is analogous to eval-defun.
156 ;;;
157 ;;; o If you run byte-compile-file on a filename which is visited in a
158 ;;; buffer, and that buffer is modified, you are asked whether you want
159 ;;; to save the buffer before compiling.
160
161 (require 'backquote)
162
163 (or (fboundp 'defsubst)
164 ;; This really ought to be loaded already!
165 (load-library "byte-run"))
166
167 ;;; The feature of compiling in a specific target Emacs version
168 ;;; has been turned off because compile time options are a bad idea.
169 (defmacro byte-compile-single-version () nil)
170 (defmacro byte-compile-version-cond (cond) cond)
171
172 ;;; The crud you see scattered through this file of the form
173 ;;; (or (and (boundp 'epoch::version) epoch::version)
174 ;;; (string-lessp emacs-version "19"))
175 ;;; is because the Epoch folks couldn't be bothered to follow the
176 ;;; normal emacs version numbering convention.
177
178 ;; (if (byte-compile-version-cond
179 ;; (or (and (boundp 'epoch::version) epoch::version)
180 ;; (string-lessp emacs-version "19")))
181 ;; (progn
182 ;; ;; emacs-18 compatibility.
183 ;; (defvar baud-rate (baud-rate)) ;Define baud-rate if it's undefined
184 ;;
185 ;; (if (byte-compile-single-version)
186 ;; (defmacro byte-code-function-p (x) "Emacs 18 doesn't have these." nil)
187 ;; (defun byte-code-function-p (x) "Emacs 18 doesn't have these." nil))
188 ;;
189 ;; (or (and (fboundp 'member)
190 ;; ;; avoid using someone else's possibly bogus definition of this.
191 ;; (subrp (symbol-function 'member)))
192 ;; (defun member (elt list)
193 ;; "like memq, but uses equal instead of eq. In v19, this is a subr."
194 ;; (while (and list (not (equal elt (car list))))
195 ;; (setq list (cdr list)))
196 ;; list))))
197
198
199 (defvar emacs-lisp-file-regexp (if (eq system-type 'vax-vms)
200 "\\.EL\\(;[0-9]+\\)?$"
201 "\\.el$")
202 "*Regexp which matches Emacs Lisp source files.
203 You may want to redefine `byte-compile-dest-file' if you change this.")
204
205 (or (fboundp 'byte-compile-dest-file)
206 ;; The user may want to redefine this,
207 ;; so only define it if it is undefined.
208 (defun byte-compile-dest-file (filename)
209 "Convert an Emacs Lisp source file name to a compiled file name."
210 (setq filename (file-name-sans-versions filename))
211 (cond ((eq system-type 'vax-vms)
212 (concat (substring filename 0 (string-match ";" filename)) "c"))
213 (t (concat filename "c")))))
214
215 ;; This can be the 'byte-compile property of any symbol.
216 (autoload 'byte-compile-inline-expand "byte-opt")
217
218 ;; This is the entrypoint to the lapcode optimizer pass1.
219 (autoload 'byte-optimize-form "byte-opt")
220 ;; This is the entrypoint to the lapcode optimizer pass2.
221 (autoload 'byte-optimize-lapcode "byte-opt")
222 (autoload 'byte-compile-unfold-lambda "byte-opt")
223
224 ;; This is the entry point to the decompiler, which is used by the
225 ;; disassembler. The disassembler just requires 'byte-compile, but
226 ;; that doesn't define this function, so this seems to be a reasonable
227 ;; thing to do.
228 (autoload 'byte-decompile-bytecode "byte-opt")
229
230 (defvar byte-compile-verbose
231 (and (not noninteractive) (> baud-rate search-slow-speed))
232 "*Non-nil means print messages describing progress of byte-compiler.")
233
234 (defvar byte-compile-compatibility nil
235 "*Non-nil means generate output that can run in Emacs 18.")
236
237 ;; (defvar byte-compile-generate-emacs19-bytecodes
238 ;; (not (or (and (boundp 'epoch::version) epoch::version)
239 ;; (string-lessp emacs-version "19")))
240 ;; "*If this is true, then the byte-compiler will generate bytecode which
241 ;; makes use of byte-ops which are present only in Emacs 19. Code generated
242 ;; this way can never be run in Emacs 18, and may even cause it to crash.")
243
244 (defvar byte-optimize t
245 "*Enables optimization in the byte compiler.
246 nil means don't do any optimization.
247 t means do all optimizations.
248 `source' means do source-level optimizations only.
249 `byte' means do code-level optimizations only.")
250
251 (defvar byte-compile-delete-errors t
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
255 (defvar byte-optimize-log nil
256 "*If true, the byte-compiler will log its optimizations into *Compile-Log*.
257 If this is 'source, then only source-level optimizations will be logged.
258 If it is 'byte, then only byte-level optimizations will be logged.")
259
260 (defvar byte-compile-error-on-warn nil
261 "*If true, the byte-compiler reports warnings with `error'.")
262
263 (defconst byte-compile-warning-types '(redefine callargs free-vars unresolved))
264 (defvar byte-compile-warnings t
265 "*List of warnings that the byte-compiler should issue (t for all).
266 Elements of the list may be be:
267
268 free-vars references to variables not in the current lexical scope.
269 unresolved calls to unknown functions.
270 callargs lambda calls with args that don't match the definition.
271 redefine function cell redefined from a macro to a lambda or vice
272 versa, or redefined to take a different number of arguments.
273
274 See also the macro `byte-compiler-options'.")
275
276 (defvar byte-compile-generate-call-tree nil
277 "*Non-nil means collect call-graph information when compiling.
278 This records functions were called and from where.
279 If the value is t, compilation displays the call graph when it finishes.
280 If the value is neither t nor nil, compilation asks you whether to display
281 the graph.
282
283 The call tree only lists functions called, not macros used. Those functions
284 which the byte-code interpreter knows about directly (eq, cons, etc.) are
285 not reported.
286
287 The call tree also lists those functions which are not known to be called
288 \(that is, to which no calls have been compiled.) Functions which can be
289 invoked interactively are excluded from this list.")
290
291 (defconst byte-compile-call-tree nil "Alist of functions and their call tree.
292 Each element looks like
293
294 \(FUNCTION CALLERS CALLS\)
295
296 where CALLERS is a list of functions that call FUNCTION, and CALLS
297 is a list of functions for which calls were generated while compiling
298 FUNCTION.")
299
300 (defvar byte-compile-call-tree-sort 'name
301 "*If non-nil, sort the call tree.
302 The values `name', `callers', `calls', `calls+callers'
303 specify different fields to sort on.")
304
305 ;; (defvar byte-compile-overwrite-file t
306 ;; "If nil, old .elc files are deleted before the new is saved, and .elc
307 ;; files will have the same modes as the corresponding .el file. Otherwise,
308 ;; existing .elc files will simply be overwritten, and the existing modes
309 ;; will not be changed. If this variable is nil, then an .elc file which
310 ;; is a symbolic link will be turned into a normal file, instead of the file
311 ;; which the link points to being overwritten.")
312
313 (defvar byte-compile-constants nil
314 "list of all constants encountered during compilation of this form")
315 (defvar byte-compile-variables nil
316 "list of all variables encountered during compilation of this form")
317 (defvar byte-compile-bound-variables nil
318 "list of variables bound in the context of the current form; this list
319 lives partly on the stack.")
320 (defvar byte-compile-free-references)
321 (defvar byte-compile-free-assignments)
322
323 (defvar byte-compiler-error-flag)
324
325 (defconst byte-compile-initial-macro-environment
326 '(
327 ;; (byte-compiler-options . (lambda (&rest forms)
328 ;; (apply 'byte-compiler-options-handler forms)))
329 (eval-when-compile . (lambda (&rest body)
330 (list 'quote (eval (byte-compile-top-level
331 (cons 'progn body))))))
332 (eval-and-compile . (lambda (&rest body)
333 (eval (cons 'progn body))
334 (cons 'progn body))))
335 "The default macro-environment passed to macroexpand by the compiler.
336 Placing a macro here will cause a macro to have different semantics when
337 expanded by the compiler as when expanded by the interpreter.")
338
339 (defvar byte-compile-macro-environment byte-compile-initial-macro-environment
340 "Alist of macros defined in the file being compiled.
341 Each element looks like (MACRONAME . DEFINITION). It is
342 \(MACRONAME . nil) when a function is redefined as a function.")
343
344 (defvar byte-compile-function-environment nil
345 "Alist of functions defined in the file being compiled.
346 This is so we can inline them when necessary.
347 Each element looks like (FUNCTIONNAME . DEFINITION). It is
348 \(FUNCTIONNAME . nil) when a function is redefined as a macro.")
349
350 (defvar byte-compile-unresolved-functions nil
351 "Alist of undefined functions to which calls have been compiled (used for
352 warnings when the function is later defined with incorrect args).")
353
354 (defvar byte-compile-tag-number 0)
355 (defvar byte-compile-output nil
356 "Alist describing contents to put in byte code string.
357 Each element is (INDEX . VALUE)")
358 (defvar byte-compile-depth 0 "Current depth of execution stack.")
359 (defvar byte-compile-maxdepth 0 "Maximum depth of execution stack.")
360
361 \f
362 ;;; The byte codes; this information is duplicated in bytecomp.c
363
364 (defconst byte-code-vector nil
365 "An array containing byte-code names indexed by byte-code values.")
366
367 (defconst byte-stack+-info nil
368 "An array with the stack adjustment for each byte-code.")
369
370 (defmacro byte-defop (opcode stack-adjust opname &optional docstring)
371 ;; This is a speed-hack for building the byte-code-vector at compile-time.
372 ;; We fill in the vector at macroexpand-time, and then after the last call
373 ;; to byte-defop, we write the vector out as a constant instead of writing
374 ;; out a bunch of calls to aset.
375 ;; Actually, we don't fill in the vector itself, because that could make
376 ;; it problematic to compile big changes to this compiler; we store the
377 ;; values on its plist, and remove them later in -extrude.
378 (let ((v1 (or (get 'byte-code-vector 'tmp-compile-time-value)
379 (put 'byte-code-vector 'tmp-compile-time-value
380 (make-vector 256 nil))))
381 (v2 (or (get 'byte-stack+-info 'tmp-compile-time-value)
382 (put 'byte-stack+-info 'tmp-compile-time-value
383 (make-vector 256 nil)))))
384 (aset v1 opcode opname)
385 (aset v2 opcode stack-adjust))
386 (if docstring
387 (list 'defconst opname opcode (concat "Byte code opcode " docstring "."))
388 (list 'defconst opname opcode)))
389
390 (defmacro byte-extrude-byte-code-vectors ()
391 (prog1 (list 'setq 'byte-code-vector
392 (get 'byte-code-vector 'tmp-compile-time-value)
393 'byte-stack+-info
394 (get 'byte-stack+-info 'tmp-compile-time-value))
395 ;; emacs-18 has no REMPROP.
396 (put 'byte-code-vector 'tmp-compile-time-value nil)
397 (put 'byte-stack+-info 'tmp-compile-time-value nil)))
398
399
400 ;; unused: 0-7
401
402 ;; These opcodes are special in that they pack their argument into the
403 ;; opcode word.
404 ;;
405 (byte-defop 8 1 byte-varref "for variable reference")
406 (byte-defop 16 -1 byte-varset "for setting a variable")
407 (byte-defop 24 -1 byte-varbind "for binding a variable")
408 (byte-defop 32 0 byte-call "for calling a function")
409 (byte-defop 40 0 byte-unbind "for unbinding special bindings")
410 ;; codes 8-47 are consumed by the preceeding opcodes
411
412 ;; unused: 48-55
413
414 (byte-defop 56 -1 byte-nth)
415 (byte-defop 57 0 byte-symbolp)
416 (byte-defop 58 0 byte-consp)
417 (byte-defop 59 0 byte-stringp)
418 (byte-defop 60 0 byte-listp)
419 (byte-defop 61 -1 byte-eq)
420 (byte-defop 62 -1 byte-memq)
421 (byte-defop 63 0 byte-not)
422 (byte-defop 64 0 byte-car)
423 (byte-defop 65 0 byte-cdr)
424 (byte-defop 66 -1 byte-cons)
425 (byte-defop 67 0 byte-list1)
426 (byte-defop 68 -1 byte-list2)
427 (byte-defop 69 -2 byte-list3)
428 (byte-defop 70 -3 byte-list4)
429 (byte-defop 71 0 byte-length)
430 (byte-defop 72 -1 byte-aref)
431 (byte-defop 73 -2 byte-aset)
432 (byte-defop 74 0 byte-symbol-value)
433 (byte-defop 75 0 byte-symbol-function) ; this was commented out
434 (byte-defop 76 -1 byte-set)
435 (byte-defop 77 -1 byte-fset) ; this was commented out
436 (byte-defop 78 -1 byte-get)
437 (byte-defop 79 -2 byte-substring)
438 (byte-defop 80 -1 byte-concat2)
439 (byte-defop 81 -2 byte-concat3)
440 (byte-defop 82 -3 byte-concat4)
441 (byte-defop 83 0 byte-sub1)
442 (byte-defop 84 0 byte-add1)
443 (byte-defop 85 -1 byte-eqlsign)
444 (byte-defop 86 -1 byte-gtr)
445 (byte-defop 87 -1 byte-lss)
446 (byte-defop 88 -1 byte-leq)
447 (byte-defop 89 -1 byte-geq)
448 (byte-defop 90 -1 byte-diff)
449 (byte-defop 91 0 byte-negate)
450 (byte-defop 92 -1 byte-plus)
451 (byte-defop 93 -1 byte-max)
452 (byte-defop 94 -1 byte-min)
453 (byte-defop 95 -1 byte-mult) ; v19 only
454 (byte-defop 96 1 byte-point)
455 (byte-defop 97 1 byte-mark-OBSOLETE) ; no longer generated as of v18
456 (byte-defop 98 0 byte-goto-char)
457 (byte-defop 99 0 byte-insert)
458 (byte-defop 100 1 byte-point-max)
459 (byte-defop 101 1 byte-point-min)
460 (byte-defop 102 0 byte-char-after)
461 (byte-defop 103 1 byte-following-char)
462 (byte-defop 104 1 byte-preceding-char)
463 (byte-defop 105 1 byte-current-column)
464 (byte-defop 106 0 byte-indent-to)
465 (byte-defop 107 0 byte-scan-buffer-OBSOLETE) ; no longer generated as of v18
466 (byte-defop 108 1 byte-eolp)
467 (byte-defop 109 1 byte-eobp)
468 (byte-defop 110 1 byte-bolp)
469 (byte-defop 111 1 byte-bobp)
470 (byte-defop 112 1 byte-current-buffer)
471 (byte-defop 113 0 byte-set-buffer)
472 (byte-defop 114 1 byte-read-char-OBSOLETE)
473 (byte-defop 115 0 byte-set-mark-OBSOLETE)
474 (byte-defop 116 1 byte-interactive-p)
475
476 ;; These ops are new to v19
477 (byte-defop 117 0 byte-forward-char)
478 (byte-defop 118 0 byte-forward-word)
479 (byte-defop 119 -1 byte-skip-chars-forward)
480 (byte-defop 120 -1 byte-skip-chars-backward)
481 (byte-defop 121 0 byte-forward-line)
482 (byte-defop 122 0 byte-char-syntax)
483 (byte-defop 123 -1 byte-buffer-substring)
484 (byte-defop 124 -1 byte-delete-region)
485 (byte-defop 125 -1 byte-narrow-to-region)
486 (byte-defop 126 1 byte-widen)
487 (byte-defop 127 0 byte-end-of-line)
488
489 ;; unused: 128
490
491 ;; These store their argument in the next two bytes
492 (byte-defop 129 1 byte-constant2
493 "for reference to a constant with vector index >= byte-constant-limit")
494 (byte-defop 130 0 byte-goto "for unconditional jump")
495 (byte-defop 131 -1 byte-goto-if-nil "to pop value and jump if it's nil")
496 (byte-defop 132 -1 byte-goto-if-not-nil "to pop value and jump if it's not nil")
497 (byte-defop 133 -1 byte-goto-if-nil-else-pop
498 "to examine top-of-stack, jump and don't pop it if it's nil,
499 otherwise pop it")
500 (byte-defop 134 -1 byte-goto-if-not-nil-else-pop
501 "to examine top-of-stack, jump and don't pop it if it's non nil,
502 otherwise pop it")
503
504 (byte-defop 135 -1 byte-return "to pop a value and return it from `byte-code'")
505 (byte-defop 136 -1 byte-discard "to discard one value from stack")
506 (byte-defop 137 1 byte-dup "to duplicate the top of the stack")
507
508 (byte-defop 138 0 byte-save-excursion
509 "to make a binding to record the buffer, point and mark")
510 (byte-defop 139 0 byte-save-window-excursion
511 "to make a binding to record entire window configuration")
512 (byte-defop 140 0 byte-save-restriction
513 "to make a binding to record the current buffer clipping restrictions")
514 (byte-defop 141 -1 byte-catch
515 "for catch. Takes, on stack, the tag and an expression for the body")
516 (byte-defop 142 -1 byte-unwind-protect
517 "for unwind-protect. Takes, on stack, an expression for the unwind-action")
518
519 ;; For condition-case. Takes, on stack, the variable to bind,
520 ;; an expression for the body, and a list of clauses.
521 (byte-defop 143 -2 byte-condition-case)
522
523 ;; For entry to with-output-to-temp-buffer.
524 ;; Takes, on stack, the buffer name.
525 ;; Binds standard-output and does some other things.
526 ;; Returns with temp buffer on the stack in place of buffer name.
527 (byte-defop 144 0 byte-temp-output-buffer-setup)
528
529 ;; For exit from with-output-to-temp-buffer.
530 ;; Expects the temp buffer on the stack underneath value to return.
531 ;; Pops them both, then pushes the value back on.
532 ;; Unbinds standard-output and makes the temp buffer visible.
533 (byte-defop 145 -1 byte-temp-output-buffer-show)
534
535 ;; these ops are new to v19
536
537 ;; To unbind back to the beginning of this frame.
538 ;; Not used yet, but wil be needed for tail-recursion elimination.
539 (byte-defop 146 0 byte-unbind-all)
540
541 ;; these ops are new to v19
542 (byte-defop 147 -2 byte-set-marker)
543 (byte-defop 148 0 byte-match-beginning)
544 (byte-defop 149 0 byte-match-end)
545 (byte-defop 150 0 byte-upcase)
546 (byte-defop 151 0 byte-downcase)
547 (byte-defop 152 -1 byte-string=)
548 (byte-defop 153 -1 byte-string<)
549 (byte-defop 154 -1 byte-equal)
550 (byte-defop 155 -1 byte-nthcdr)
551 (byte-defop 156 -1 byte-elt)
552 (byte-defop 157 -1 byte-member)
553 (byte-defop 158 -1 byte-assq)
554 (byte-defop 159 0 byte-nreverse)
555 (byte-defop 160 -1 byte-setcar)
556 (byte-defop 161 -1 byte-setcdr)
557 (byte-defop 162 0 byte-car-safe)
558 (byte-defop 163 0 byte-cdr-safe)
559 (byte-defop 164 -1 byte-nconc)
560 (byte-defop 165 -1 byte-quo)
561 (byte-defop 166 -1 byte-rem)
562 (byte-defop 167 0 byte-numberp)
563 (byte-defop 168 0 byte-integerp)
564
565 ;; unused: 169-174
566 (byte-defop 175 nil byte-listN)
567 (byte-defop 176 nil byte-concatN)
568 (byte-defop 177 nil byte-insertN)
569
570 ;; unused: 178-191
571
572 (byte-defop 192 1 byte-constant "for reference to a constant")
573 ;; codes 193-255 are consumed by byte-constant.
574 (defconst byte-constant-limit 64
575 "Exclusive maximum index usable in the `byte-constant' opcode.")
576
577 (defconst byte-goto-ops '(byte-goto byte-goto-if-nil byte-goto-if-not-nil
578 byte-goto-if-nil-else-pop
579 byte-goto-if-not-nil-else-pop)
580 "List of byte-codes whose offset is a pc.")
581
582 (defconst byte-goto-always-pop-ops '(byte-goto-if-nil byte-goto-if-not-nil))
583
584 (byte-extrude-byte-code-vectors)
585 \f
586 ;;; lapcode generator
587 ;;;
588 ;;; the byte-compiler now does source -> lapcode -> bytecode instead of
589 ;;; source -> bytecode, because it's a lot easier to make optimizations
590 ;;; on lapcode than on bytecode.
591 ;;;
592 ;;; Elements of the lapcode list are of the form (<instruction> . <parameter>)
593 ;;; where instruction is a symbol naming a byte-code instruction,
594 ;;; and parameter is an argument to that instruction, if any.
595 ;;;
596 ;;; The instruction can be the pseudo-op TAG, which means that this position
597 ;;; in the instruction stream is a target of a goto. (car PARAMETER) will be
598 ;;; the PC for this location, and the whole instruction "(TAG pc)" will be the
599 ;;; parameter for some goto op.
600 ;;;
601 ;;; If the operation is varbind, varref, varset or push-constant, then the
602 ;;; parameter is (variable/constant . index_in_constant_vector).
603 ;;;
604 ;;; First, the source code is macroexpanded and optimized in various ways.
605 ;;; Then the resultant code is compiled into lapcode. Another set of
606 ;;; optimizations are then run over the lapcode. Then the variables and
607 ;;; constants referenced by the lapcode are collected and placed in the
608 ;;; constants-vector. (This happens now so that variables referenced by dead
609 ;;; code don't consume space.) And finally, the lapcode is transformed into
610 ;;; compacted byte-code.
611 ;;;
612 ;;; A distinction is made between variables and constants because the variable-
613 ;;; referencing instructions are more sensitive to the variables being near the
614 ;;; front of the constants-vector than the constant-referencing instructions.
615 ;;; Also, this lets us notice references to free variables.
616
617 (defun byte-compile-lapcode (lap)
618 "Turns lapcode into bytecode. The lapcode is destroyed."
619 ;; Lapcode modifications: changes the ID of a tag to be the tag's PC.
620 (let ((pc 0) ; Program counter
621 op off ; Operation & offset
622 (bytes '()) ; Put the output bytes here
623 (patchlist nil) ; List of tags and goto's to patch
624 rest rel tmp)
625 (while lap
626 (setq op (car (car lap))
627 off (cdr (car lap)))
628 (cond ((not (symbolp op))
629 (error "Non-symbolic opcode `%s'" op))
630 ((eq op 'TAG)
631 (setcar off pc)
632 (setq patchlist (cons off patchlist)))
633 ((memq op byte-goto-ops)
634 (setq pc (+ pc 3))
635 (setq bytes (cons (cons pc (cdr off))
636 (cons nil
637 (cons (symbol-value op) bytes))))
638 (setq patchlist (cons bytes patchlist)))
639 (t
640 (setq bytes
641 (cond ((cond ((consp off)
642 ;; Variable or constant reference
643 (setq off (cdr off))
644 (eq op 'byte-constant)))
645 (cond ((< off byte-constant-limit)
646 (setq pc (1+ pc))
647 (cons (+ byte-constant off) bytes))
648 (t
649 (setq pc (+ 3 pc))
650 (cons (lsh off -8)
651 (cons (logand off 255)
652 (cons byte-constant2 bytes))))))
653 ((<= byte-listN (symbol-value op))
654 (setq pc (+ 2 pc))
655 (cons off (cons (symbol-value op) bytes)))
656 ((< off 6)
657 (setq pc (1+ pc))
658 (cons (+ (symbol-value op) off) bytes))
659 ((< off 256)
660 (setq pc (+ 2 pc))
661 (cons off (cons (+ (symbol-value op) 6) bytes)))
662 (t
663 (setq pc (+ 3 pc))
664 (cons (lsh off -8)
665 (cons (logand off 255)
666 (cons (+ (symbol-value op) 7)
667 bytes))))))))
668 (setq lap (cdr lap)))
669 ;;(if (not (= pc (length bytes)))
670 ;; (error "Compiler error: pc mismatch - %s %s" pc (length bytes)))
671 ;; Patch PC into jumps
672 (let (bytes)
673 (while patchlist
674 (setq bytes (car patchlist))
675 (cond ((atom (car bytes))) ; Tag
676 (t ; Absolute jump
677 (setq pc (car (cdr (car bytes)))) ; Pick PC from tag
678 (setcar (cdr bytes) (logand pc 255))
679 (setcar bytes (lsh pc -8))))
680 (setq patchlist (cdr patchlist))))
681 (concat (nreverse bytes))))
682
683 \f
684 ;;; byte compiler messages
685
686 (defconst byte-compile-current-form nil)
687 (defconst byte-compile-current-file nil)
688
689 (defmacro byte-compile-log (format-string &rest args)
690 (list 'and
691 'byte-optimize
692 '(memq byte-optimize-log '(t source))
693 (list 'let '((print-escape-newlines t)
694 (print-level 4)
695 (print-length 4))
696 (list 'byte-compile-log-1
697 (cons 'format
698 (cons format-string
699 (mapcar
700 '(lambda (x)
701 (if (symbolp x) (list 'prin1-to-string x) x))
702 args)))))))
703
704 (defconst byte-compile-last-warned-form nil)
705
706 (defun byte-compile-log-1 (string &optional fill)
707 (cond (noninteractive
708 (if (or byte-compile-current-file
709 (and byte-compile-last-warned-form
710 (not (eq byte-compile-current-form
711 byte-compile-last-warned-form))))
712 (message (format "While compiling %s%s:"
713 (or byte-compile-current-form "toplevel forms")
714 (if byte-compile-current-file
715 (if (stringp byte-compile-current-file)
716 (concat " in file " byte-compile-current-file)
717 (concat " in buffer "
718 (buffer-name byte-compile-current-file)))
719 ""))))
720 (message " %s" string))
721 (t
722 (save-excursion
723 (set-buffer (get-buffer-create "*Compile-Log*"))
724 (goto-char (point-max))
725 (cond ((or byte-compile-current-file
726 (and byte-compile-last-warned-form
727 (not (eq byte-compile-current-form
728 byte-compile-last-warned-form))))
729 (if byte-compile-current-file
730 (insert "\n\^L\n" (current-time-string) "\n"))
731 (insert "While compiling "
732 (if byte-compile-current-form
733 (format "%s" byte-compile-current-form)
734 "toplevel forms"))
735 (if byte-compile-current-file
736 (if (stringp byte-compile-current-file)
737 (insert " in file " byte-compile-current-file)
738 (insert " in buffer "
739 (buffer-name byte-compile-current-file))))
740 (insert ":\n")))
741 (insert " " string "\n")
742 (if (and fill (not (string-match "\n" string)))
743 (let ((fill-prefix " ")
744 (fill-column 78))
745 (fill-paragraph nil)))
746 )))
747 (setq byte-compile-current-file nil
748 byte-compile-last-warned-form byte-compile-current-form))
749
750 (defun byte-compile-warn (format &rest args)
751 (setq format (apply 'format format args))
752 (if byte-compile-error-on-warn
753 (error "%s" format) ; byte-compile-file catches and logs it
754 (byte-compile-log-1 (concat "** " format) t)
755 ;;; It is useless to flash warnings too fast to be read.
756 ;;; Besides, they will all be shown at the end.
757 ;;; (or noninteractive ; already written on stdout.
758 ;;; (message "Warning: %s" format))
759 ))
760
761 ;;; This function should be used to report errors that have halted
762 ;;; compilation of the current file.
763 (defun byte-compile-report-error (error-info)
764 (setq byte-compiler-error-flag t)
765 (byte-compile-log-1
766 (concat "!! "
767 (format (if (cdr error-info) "%s (%s)" "%s")
768 (get (car error-info) 'error-message)
769 (prin1-to-string (cdr error-info))))))
770
771 ;;; Used by make-obsolete.
772 (defun byte-compile-obsolete (form)
773 (let ((new (get (car form) 'byte-obsolete-info)))
774 (byte-compile-warn "%s is an obsolete function; %s" (car form)
775 (if (stringp (car new))
776 (car new)
777 (format "use %s instead." (car new))))
778 (funcall (or (cdr new) 'byte-compile-normal-call) form)))
779 \f
780 ;; Compiler options
781
782 ;; (defvar byte-compiler-valid-options
783 ;; '((optimize byte-optimize (t nil source byte) val)
784 ;; (file-format byte-compile-compatibility (emacs18 emacs19)
785 ;; (eq val 'emacs18))
786 ;; ;; (new-bytecodes byte-compile-generate-emacs19-bytecodes (t nil) val)
787 ;; (delete-errors byte-compile-delete-errors (t nil) val)
788 ;; (verbose byte-compile-verbose (t nil) val)
789 ;; (warnings byte-compile-warnings ((callargs redefine free-vars unresolved))
790 ;; val)))
791
792 ;; Inhibit v18/v19 selectors if the version is hardcoded.
793 ;; #### This should print a warning if the user tries to change something
794 ;; than can't be changed because the running compiler doesn't support it.
795 ;; (cond
796 ;; ((byte-compile-single-version)
797 ;; (setcar (cdr (cdr (assq 'new-bytecodes byte-compiler-valid-options)))
798 ;; (list (byte-compile-version-cond
799 ;; byte-compile-generate-emacs19-bytecodes)))
800 ;; (setcar (cdr (cdr (assq 'file-format byte-compiler-valid-options)))
801 ;; (if (byte-compile-version-cond byte-compile-compatibility)
802 ;; '(emacs18) '(emacs19)))))
803
804 ;; (defun byte-compiler-options-handler (&rest args)
805 ;; (let (key val desc choices)
806 ;; (while args
807 ;; (if (or (atom (car args)) (nthcdr 2 (car args)) (null (cdr (car args))))
808 ;; (error "Malformed byte-compiler option `%s'" (car args)))
809 ;; (setq key (car (car args))
810 ;; val (car (cdr (car args)))
811 ;; desc (assq key byte-compiler-valid-options))
812 ;; (or desc
813 ;; (error "Unknown byte-compiler option `%s'" key))
814 ;; (setq choices (nth 2 desc))
815 ;; (if (consp (car choices))
816 ;; (let (this
817 ;; (handler 'cons)
818 ;; (ret (and (memq (car val) '(+ -))
819 ;; (copy-sequence (if (eq t (symbol-value (nth 1 desc)))
820 ;; choices
821 ;; (symbol-value (nth 1 desc)))))))
822 ;; (setq choices (car choices))
823 ;; (while val
824 ;; (setq this (car val))
825 ;; (cond ((memq this choices)
826 ;; (setq ret (funcall handler this ret)))
827 ;; ((eq this '+) (setq handler 'cons))
828 ;; ((eq this '-) (setq handler 'delq))
829 ;; ((error "`%s' only accepts %s" key choices)))
830 ;; (setq val (cdr val)))
831 ;; (set (nth 1 desc) ret))
832 ;; (or (memq val choices)
833 ;; (error "`%s' must be one of `%s'" key choices))
834 ;; (set (nth 1 desc) (eval (nth 3 desc))))
835 ;; (setq args (cdr args)))
836 ;; nil))
837 \f
838 ;;; sanity-checking arglists
839
840 (defun byte-compile-fdefinition (name macro-p)
841 (let* ((list (if macro-p
842 byte-compile-macro-environment
843 byte-compile-function-environment))
844 (env (cdr (assq name list))))
845 (or env
846 (let ((fn name))
847 (while (and (symbolp fn)
848 (fboundp fn)
849 (or (symbolp (symbol-function fn))
850 (consp (symbol-function fn))
851 (and (not macro-p)
852 (byte-code-function-p (symbol-function fn)))))
853 (setq fn (symbol-function fn)))
854 (if (and (not macro-p) (byte-code-function-p fn))
855 fn
856 (and (consp fn)
857 (if (eq 'macro (car fn))
858 (cdr fn)
859 (if macro-p
860 nil
861 (if (eq 'autoload (car fn))
862 nil
863 fn)))))))))
864
865 (defun byte-compile-arglist-signature (arglist)
866 (let ((args 0)
867 opts
868 restp)
869 (while arglist
870 (cond ((eq (car arglist) '&optional)
871 (or opts (setq opts 0)))
872 ((eq (car arglist) '&rest)
873 (if (cdr arglist)
874 (setq restp t
875 arglist nil)))
876 (t
877 (if opts
878 (setq opts (1+ opts))
879 (setq args (1+ args)))))
880 (setq arglist (cdr arglist)))
881 (cons args (if restp nil (if opts (+ args opts) args)))))
882
883
884 (defun byte-compile-arglist-signatures-congruent-p (old new)
885 (not (or
886 (> (car new) (car old)) ; requires more args now
887 (and (null (cdr old)) ; tooks rest-args, doesn't any more
888 (cdr new))
889 (and (cdr new) (cdr old) ; can't take as many args now
890 (< (cdr new) (cdr old)))
891 )))
892
893 (defun byte-compile-arglist-signature-string (signature)
894 (cond ((null (cdr signature))
895 (format "%d+" (car signature)))
896 ((= (car signature) (cdr signature))
897 (format "%d" (car signature)))
898 (t (format "%d-%d" (car signature) (cdr signature)))))
899
900
901 ;; Warn if the form is calling a function with the wrong number of arguments.
902 (defun byte-compile-callargs-warn (form)
903 (let* ((def (or (byte-compile-fdefinition (car form) nil)
904 (byte-compile-fdefinition (car form) t)))
905 (sig (and def (byte-compile-arglist-signature
906 (if (eq 'lambda (car-safe def))
907 (nth 1 def)
908 (aref def 0)))))
909 (ncall (length (cdr form))))
910 (if sig
911 (if (or (< ncall (car sig))
912 (and (cdr sig) (> ncall (cdr sig))))
913 (byte-compile-warn
914 "%s called with %d argument%s, but %s %s"
915 (car form) ncall
916 (if (= 1 ncall) "" "s")
917 (if (< ncall (car sig))
918 "requires"
919 "accepts only")
920 (byte-compile-arglist-signature-string sig)))
921 (or (fboundp (car form)) ; might be a subr or autoload.
922 (eq (car form) byte-compile-current-form) ; ## this doesn't work with recursion.
923 ;; It's a currently-undefined function. Remember number of args in call.
924 (let ((cons (assq (car form) byte-compile-unresolved-functions))
925 (n (length (cdr form))))
926 (if cons
927 (or (memq n (cdr cons))
928 (setcdr cons (cons n (cdr cons))))
929 (setq byte-compile-unresolved-functions
930 (cons (list (car form) n)
931 byte-compile-unresolved-functions))))))))
932
933 ;; Warn if the function or macro is being redefined with a different
934 ;; number of arguments.
935 (defun byte-compile-arglist-warn (form macrop)
936 (let ((old (byte-compile-fdefinition (nth 1 form) macrop)))
937 (if old
938 (let ((sig1 (byte-compile-arglist-signature
939 (if (eq 'lambda (car-safe old))
940 (nth 1 old)
941 (aref old 0))))
942 (sig2 (byte-compile-arglist-signature (nth 2 form))))
943 (or (byte-compile-arglist-signatures-congruent-p sig1 sig2)
944 (byte-compile-warn "%s %s used to take %s %s, now takes %s"
945 (if (eq (car form) 'defun) "function" "macro")
946 (nth 1 form)
947 (byte-compile-arglist-signature-string sig1)
948 (if (equal sig1 '(1 . 1)) "argument" "arguments")
949 (byte-compile-arglist-signature-string sig2))))
950 ;; This is the first definition. See if previous calls are compatible.
951 (let ((calls (assq (nth 1 form) byte-compile-unresolved-functions))
952 nums sig min max)
953 (if calls
954 (progn
955 (setq sig (byte-compile-arglist-signature (nth 2 form))
956 nums (sort (copy-sequence (cdr calls)) (function <))
957 min (car nums)
958 max (car (nreverse nums)))
959 (if (or (< min (car sig))
960 (and (cdr sig) (> max (cdr sig))))
961 (byte-compile-warn
962 "%s being defined to take %s%s, but was previously called with %s"
963 (nth 1 form)
964 (byte-compile-arglist-signature-string sig)
965 (if (equal sig '(1 . 1)) " arg" " args")
966 (byte-compile-arglist-signature-string (cons min max))))
967
968 (setq byte-compile-unresolved-functions
969 (delq calls byte-compile-unresolved-functions)))))
970 )))
971
972 ;; If we have compiled any calls to functions which are not known to be
973 ;; defined, issue a warning enumerating them.
974 ;; `unresolved' in the list `byte-compile-warnings' disables this.
975 (defun byte-compile-warn-about-unresolved-functions ()
976 (if (memq 'unresolved byte-compile-warnings)
977 (let ((byte-compile-current-form "the end of the data"))
978 (if (cdr byte-compile-unresolved-functions)
979 (let* ((str "The following functions are not known to be defined: ")
980 (L (length str))
981 (rest (reverse byte-compile-unresolved-functions))
982 s)
983 (while rest
984 (setq s (symbol-name (car (car rest)))
985 L (+ L (length s) 2)
986 rest (cdr rest))
987 (if (< L (1- fill-column))
988 (setq str (concat str " " s (and rest ",")))
989 (setq str (concat str "\n " s (and rest ","))
990 L (+ (length s) 4))))
991 (byte-compile-warn "%s" str))
992 (if byte-compile-unresolved-functions
993 (byte-compile-warn "the function %s is not known to be defined."
994 (car (car byte-compile-unresolved-functions)))))))
995 nil)
996
997 \f
998 (defmacro byte-compile-constp (form)
999 ;; Returns non-nil if FORM is a constant.
1000 (` (cond ((consp (, form)) (eq (car (, form)) 'quote))
1001 ((not (symbolp (, form))))
1002 ((memq (, form) '(nil t))))))
1003
1004 (defmacro byte-compile-close-variables (&rest body)
1005 (cons 'let
1006 (cons '(;;
1007 ;; Close over these variables to encapsulate the
1008 ;; compilation state
1009 ;;
1010 (byte-compile-macro-environment
1011 ;; Copy it because the compiler may patch into the
1012 ;; macroenvironment.
1013 (copy-alist byte-compile-initial-macro-environment))
1014 (byte-compile-function-environment nil)
1015 (byte-compile-bound-variables nil)
1016 (byte-compile-free-references nil)
1017 (byte-compile-free-assignments nil)
1018 ;;
1019 ;; Close over these variables so that `byte-compiler-options'
1020 ;; can change them on a per-file basis.
1021 ;;
1022 (byte-compile-verbose byte-compile-verbose)
1023 (byte-optimize byte-optimize)
1024 ;; (byte-compile-generate-emacs19-bytecodes
1025 ;; byte-compile-generate-emacs19-bytecodes)
1026 (byte-compile-warnings (if (eq byte-compile-warnings t)
1027 byte-compile-warning-types
1028 byte-compile-warnings))
1029 )
1030 body)))
1031
1032 (defvar byte-compile-warnings-point-max)
1033 (defmacro displaying-byte-compile-warnings (&rest body)
1034 (list 'let
1035 '((byte-compile-warnings-point-max
1036 (if (boundp 'byte-compile-warnings-point-max)
1037 byte-compile-warnings-point-max
1038 (save-excursion
1039 (set-buffer (get-buffer-create "*Compile-Log*"))
1040 (point-max)))))
1041 (list 'unwind-protect
1042 (list 'condition-case 'error-info
1043 (cons 'progn body)
1044 '(error
1045 (byte-compile-report-error error-info)))
1046 '(save-excursion
1047 ;; If there were compilation warnings, display them.
1048 (set-buffer "*Compile-Log*")
1049 (if (= byte-compile-warnings-point-max (point-max))
1050 nil
1051 (select-window
1052 (prog1 (selected-window)
1053 (select-window (display-buffer (current-buffer)))
1054 (goto-char byte-compile-warnings-point-max)
1055 (recenter 1))))))))
1056
1057 \f
1058 ;;;###autoload
1059 (defun byte-recompile-directory (directory &optional arg)
1060 "Recompile every `.el' file in DIRECTORY that needs recompilation.
1061 This is if a `.elc' file exists but is older than the `.el' file.
1062
1063 If the `.elc' file does not exist, normally the `.el' file is *not* compiled.
1064 But a prefix argument (optional second arg) means ask user,
1065 for each such `.el' file, whether to compile it. Prefix argument 0 means
1066 don't ask and compile the file anyway."
1067 (interactive "DByte recompile directory: \nP")
1068 (save-some-buffers)
1069 (set-buffer-modified-p (buffer-modified-p)) ;Update the mode line.
1070 (let ((directories (list (expand-file-name directory)))
1071 (file-count 0)
1072 (dir-count 0)
1073 last-dir)
1074 (displaying-byte-compile-warnings
1075 (while directories
1076 (setq directory (car directories))
1077 (message "Checking %s..." directory)
1078 (let ((files (directory-files directory))
1079 source dest)
1080 (while files
1081 (setq source (expand-file-name (car files) directory))
1082 (if (and (not (member (car files) '("." ".." "RCS" "CVS")))
1083 (file-directory-p source))
1084 (if (or (null arg)
1085 (eq arg 0)
1086 (y-or-n-p (concat "Check " source "? ")))
1087 (setq directories
1088 (nconc directories (list source))))
1089 (if (and (string-match emacs-lisp-file-regexp source)
1090 (not (auto-save-file-name-p source))
1091 (setq dest (byte-compile-dest-file source))
1092 (if (file-exists-p dest)
1093 (file-newer-than-file-p source dest)
1094 (and arg
1095 (or (zerop arg)
1096 (y-or-n-p (concat "Compile " source "? "))))))
1097 (progn (byte-compile-file source)
1098 (setq file-count (1+ file-count))
1099 (if (not (eq last-dir directory))
1100 (setq last-dir directory
1101 dir-count (1+ dir-count)))
1102 )))
1103 (setq files (cdr files))))
1104 (setq directories (cdr directories))))
1105 (message "Done (Total of %d file%s compiled%s)"
1106 file-count (if (= file-count 1) "" "s")
1107 (if (> dir-count 1) (format " in %d directories" dir-count) ""))))
1108
1109 ;;;###autoload
1110 (defun byte-compile-file (filename &optional load)
1111 "Compile a file of Lisp code named FILENAME into a file of byte code.
1112 The output file's name is made by appending `c' to the end of FILENAME.
1113 With prefix arg (noninteractively: 2nd arg), load the file after compiling."
1114 ;; (interactive "fByte compile file: \nP")
1115 (interactive
1116 (let ((file buffer-file-name)
1117 (file-name nil)
1118 (file-dir nil))
1119 (and file
1120 (eq (cdr (assq 'major-mode (buffer-local-variables)))
1121 'emacs-lisp-mode)
1122 (setq file-name (file-name-nondirectory file)
1123 file-dir (file-name-directory file)))
1124 (list (read-file-name (if current-prefix-arg
1125 "Byte compile and load file: "
1126 "Byte compile file: ")
1127 file-dir file-name nil)
1128 current-prefix-arg)))
1129 ;; Expand now so we get the current buffer's defaults
1130 (setq filename (expand-file-name filename))
1131
1132 ;; If we're compiling a file that's in a buffer and is modified, offer
1133 ;; to save it first.
1134 (or noninteractive
1135 (let ((b (get-file-buffer (expand-file-name filename))))
1136 (if (and b (buffer-modified-p b)
1137 (y-or-n-p (format "save buffer %s first? " (buffer-name b))))
1138 (save-excursion (set-buffer b) (save-buffer)))))
1139
1140 (if byte-compile-verbose
1141 (message "Compiling %s..." filename))
1142 (let ((byte-compile-current-file (file-name-nondirectory filename))
1143 target-file input-buffer output-buffer)
1144 (save-excursion
1145 (setq input-buffer (get-buffer-create " *Compiler Input*"))
1146 (set-buffer input-buffer)
1147 (erase-buffer)
1148 (insert-file-contents filename)
1149 ;; Run hooks including the uncompression hook.
1150 ;; If they change the file name, then change it for the output also.
1151 (let ((buffer-file-name filename))
1152 (set-auto-mode)
1153 (setq filename buffer-file-name)))
1154 (setq byte-compiler-error-flag nil)
1155 ;; It is important that input-buffer not be current at this call,
1156 ;; so that the value of point set in input-buffer
1157 ;; within byte-compile-from-buffer lingers in that buffer.
1158 (setq output-buffer (byte-compile-from-buffer input-buffer))
1159 (or byte-compiler-error-flag
1160 (kill-buffer input-buffer))
1161 (save-excursion
1162 (set-buffer output-buffer)
1163 (goto-char (point-max))
1164 (insert "\n") ; aaah, unix.
1165 (let ((vms-stmlf-recfm t))
1166 (setq target-file (byte-compile-dest-file filename))
1167 ;; (or byte-compile-overwrite-file
1168 ;; (condition-case ()
1169 ;; (delete-file target-file)
1170 ;; (error nil)))
1171 (if (file-writable-p target-file)
1172 (let ((kanji-flag nil)) ; for nemacs, from Nakagawa Takayuki
1173 (write-region 1 (point-max) target-file))
1174 ;; This is just to give a better error message than
1175 ;; write-region
1176 (signal 'file-error
1177 (list "Opening output file"
1178 (if (file-exists-p target-file)
1179 "cannot overwrite file"
1180 "directory not writable or nonexistent")
1181 target-file)))
1182 ;; (or byte-compile-overwrite-file
1183 ;; (condition-case ()
1184 ;; (set-file-modes target-file (file-modes filename))
1185 ;; (error nil)))
1186 )
1187 (kill-buffer (current-buffer)))
1188 (if (and byte-compile-generate-call-tree
1189 (or (eq t byte-compile-generate-call-tree)
1190 (y-or-n-p (format "Report call tree for %s? " filename))))
1191 (save-excursion
1192 (display-call-tree filename)))
1193 (if load
1194 (load target-file)))
1195 t)
1196
1197 ;;(defun byte-compile-and-load-file (&optional filename)
1198 ;; "Compile a file of Lisp code named FILENAME into a file of byte code,
1199 ;;and then load it. The output file's name is made by appending \"c\" to
1200 ;;the end of FILENAME."
1201 ;; (interactive)
1202 ;; (if filename ; I don't get it, (interactive-p) doesn't always work
1203 ;; (byte-compile-file filename t)
1204 ;; (let ((current-prefix-arg '(4)))
1205 ;; (call-interactively 'byte-compile-file))))
1206
1207 ;;(defun byte-compile-buffer (&optional buffer)
1208 ;; "Byte-compile and evaluate contents of BUFFER (default: the current buffer)."
1209 ;; (interactive "bByte compile buffer: ")
1210 ;; (setq buffer (if buffer (get-buffer buffer) (current-buffer)))
1211 ;; (message "Compiling %s..." (buffer-name buffer))
1212 ;; (let* ((filename (or (buffer-file-name buffer)
1213 ;; (concat "#<buffer " (buffer-name buffer) ">")))
1214 ;; (byte-compile-current-file buffer))
1215 ;; (byte-compile-from-buffer buffer t))
1216 ;; (message "Compiling %s...done" (buffer-name buffer))
1217 ;; t)
1218
1219 ;;; compiling a single function
1220 ;;;###autoload
1221 (defun compile-defun (&optional arg)
1222 "Compile and evaluate the current top-level form.
1223 Print the result in the minibuffer.
1224 With argument, insert value in current buffer after the form."
1225 (interactive "P")
1226 (save-excursion
1227 (end-of-defun)
1228 (beginning-of-defun)
1229 (let* ((byte-compile-current-file nil)
1230 (byte-compile-last-warned-form 'nothing)
1231 (value (eval (displaying-byte-compile-warnings
1232 (byte-compile-sexp (read (current-buffer)))))))
1233 (cond (arg
1234 (message "Compiling from buffer... done.")
1235 (prin1 value (current-buffer))
1236 (insert "\n"))
1237 ((message "%s" (prin1-to-string value)))))))
1238
1239
1240 (defun byte-compile-from-buffer (inbuffer &optional eval)
1241 ;; buffer --> output-buffer, or buffer --> eval form, return nil
1242 (let (outbuffer)
1243 (let (;; Prevent truncation of flonums and lists as we read and print them
1244 (float-output-format "%20e")
1245 (case-fold-search nil)
1246 (print-length nil)
1247 ;; Simulate entry to byte-compile-top-level
1248 (byte-compile-constants nil)
1249 (byte-compile-variables nil)
1250 (byte-compile-tag-number 0)
1251 (byte-compile-depth 0)
1252 (byte-compile-maxdepth 0)
1253 (byte-compile-output nil)
1254 ;; #### This is bound in b-c-close-variables.
1255 ;; (byte-compile-warnings (if (eq byte-compile-warnings t)
1256 ;; byte-compile-warning-types
1257 ;; byte-compile-warnings))
1258 )
1259 (byte-compile-close-variables
1260 (save-excursion
1261 (setq outbuffer
1262 (set-buffer (get-buffer-create " *Compiler Output*")))
1263 (erase-buffer)
1264 ;; (emacs-lisp-mode)
1265 (setq case-fold-search nil)
1266
1267 ;; This is a kludge. Some operating systems (OS/2, DOS) need to
1268 ;; write files containing binary information specially.
1269 ;; Under most circumstances, such files will be in binary
1270 ;; overwrite mode, so those OS's use that flag to guess how
1271 ;; they should write their data. Advise them that .elc files
1272 ;; need to be written carefully.
1273 (setq overwrite-mode 'overwrite-mode-binary))
1274 (displaying-byte-compile-warnings
1275 (save-excursion
1276 (set-buffer inbuffer)
1277 (goto-char 1)
1278 (while (progn
1279 (while (progn (skip-chars-forward " \t\n\^l")
1280 (looking-at ";"))
1281 (forward-line 1))
1282 (not (eobp)))
1283 (byte-compile-file-form (read inbuffer)))
1284 ;; Compile pending forms at end of file.
1285 (byte-compile-flush-pending)
1286 (and (not eval) (byte-compile-insert-header))
1287 (byte-compile-warn-about-unresolved-functions)
1288 ;; always do this? When calling multiple files, it
1289 ;; would be useful to delay this warning until all have
1290 ;; been compiled.
1291 (setq byte-compile-unresolved-functions nil)))
1292 (save-excursion
1293 (set-buffer outbuffer)
1294 (goto-char (point-min)))))
1295 (if (not eval)
1296 outbuffer
1297 (while (condition-case nil
1298 (progn (setq form (read outbuffer))
1299 t)
1300 (end-of-file nil))
1301 (eval form))
1302 (kill-buffer outbuffer)
1303 nil)))
1304
1305 (defun byte-compile-insert-header ()
1306 (save-excursion
1307 (set-buffer outbuffer)
1308 (goto-char 1)
1309 (insert ";;; compiled by " (user-login-name) "@" (system-name) " on "
1310 (current-time-string) "\n;;; from file " filename "\n")
1311 (insert ";;; emacs version " emacs-version ".\n")
1312 (insert ";;; bytecomp version " byte-compile-version "\n;;; "
1313 (cond
1314 ((eq byte-optimize 'source) "source-level optimization only")
1315 ((eq byte-optimize 'byte) "byte-level optimization only")
1316 (byte-optimize "optimization is on")
1317 (t "optimization is off"))
1318 (if (byte-compile-version-cond byte-compile-compatibility)
1319 "; compiled with Emacs 18 compatibility.\n"
1320 ".\n"))
1321 (if (byte-compile-version-cond byte-compile-compatibility)
1322 (insert ";;; this file uses opcodes which do not exist in Emacs 18.\n"
1323 ;; Have to check if emacs-version is bound so that this works
1324 ;; in files loaded early in loadup.el.
1325 "\n(if (and (boundp 'emacs-version)\n"
1326 "\t (or (and (boundp 'epoch::version) epoch::version)\n"
1327 "\t (string-lessp emacs-version \"19\")))\n"
1328 " (error \"This file was compiled for Emacs 19\"))\n"
1329 ))
1330 ))
1331
1332
1333 (defun byte-compile-output-file-form (form)
1334 ;; writes the given form to the output buffer, being careful of docstrings
1335 ;; in defun, defmacro, defvar, defconst and autoload because make-docfile is
1336 ;; so amazingly stupid.
1337 ;; fset's are output directly by byte-compile-file-form-defmumble; it does
1338 ;; not pay to first build the fset in defmumble and then parse it here.
1339 (if (and (memq (car-safe form) '(defun defmacro defvar defconst autoload))
1340 (stringp (nth 3 form)))
1341 (byte-compile-output-docform '("\n(" 3 ")") form)
1342 (let ((print-escape-newlines t)
1343 (print-readably t) ; print #[] for bytecode, 'x for (quote x)
1344 (print-gensym nil)) ; this is too dangerous for now
1345 (princ "\n" outbuffer)
1346 (prin1 form outbuffer)
1347 nil)))
1348
1349 (defun byte-compile-output-docform (info form)
1350 ;; Print a form with a doc string. INFO is (prefix doc-index postfix).
1351 (set-buffer
1352 (prog1 (current-buffer)
1353 (set-buffer outbuffer)
1354 (insert (car info))
1355 (let ((docl (nthcdr (nth 1 info) form))
1356 (print-escape-newlines t)
1357 (print-readably t) ; print #[] for bytecode, 'x for (quote x)
1358 (print-gensym nil)) ; this is too dangerous for now
1359 (prin1 (car form) outbuffer)
1360 (while (setq form (cdr form))
1361 (insert " ")
1362 (if (eq form docl)
1363 (let ((print-escape-newlines nil))
1364 (goto-char (prog1 (1+ (point))
1365 (prin1 (car form) outbuffer)))
1366 (insert "\\\n")
1367 (goto-char (point-max)))
1368 (prin1 (car form) outbuffer))))
1369 (insert (nth 2 info))))
1370 nil)
1371
1372 (defun byte-compile-keep-pending (form &optional handler)
1373 (if (memq byte-optimize '(t source))
1374 (setq form (byte-optimize-form form t)))
1375 (if handler
1376 (let ((for-effect t))
1377 ;; To avoid consing up monstrously large forms at load time, we split
1378 ;; the output regularly.
1379 (and (eq (car-safe form) 'fset) (nthcdr 300 byte-compile-output)
1380 (byte-compile-flush-pending))
1381 (funcall handler form)
1382 (if for-effect
1383 (byte-compile-discard)))
1384 (byte-compile-form form t))
1385 nil)
1386
1387 (defun byte-compile-flush-pending ()
1388 (if byte-compile-output
1389 (let ((form (byte-compile-out-toplevel t 'file)))
1390 (cond ((eq (car-safe form) 'progn)
1391 (mapcar 'byte-compile-output-file-form (cdr form)))
1392 (form
1393 (byte-compile-output-file-form form)))
1394 (setq byte-compile-constants nil
1395 byte-compile-variables nil
1396 byte-compile-depth 0
1397 byte-compile-maxdepth 0
1398 byte-compile-output nil))))
1399
1400 (defun byte-compile-file-form (form)
1401 (let ((byte-compile-current-form nil) ; close over this for warnings.
1402 handler)
1403 (cond
1404 ((not (consp form))
1405 (byte-compile-keep-pending form))
1406 ((and (symbolp (car form))
1407 (setq handler (get (car form) 'byte-hunk-handler)))
1408 (cond ((setq form (funcall handler form))
1409 (byte-compile-flush-pending)
1410 (byte-compile-output-file-form form))))
1411 ((eq form (setq form (macroexpand form byte-compile-macro-environment)))
1412 (byte-compile-keep-pending form))
1413 (t
1414 (byte-compile-file-form form)))))
1415
1416 ;; Functions and variables with doc strings must be output separately,
1417 ;; so make-docfile can recognise them. Most other things can be output
1418 ;; as byte-code.
1419
1420 (put 'defsubst 'byte-hunk-handler 'byte-compile-file-form-defsubst)
1421 (defun byte-compile-file-form-defsubst (form)
1422 (cond ((assq (nth 1 form) byte-compile-unresolved-functions)
1423 (setq byte-compile-current-form (nth 1 form))
1424 (byte-compile-warn "defsubst %s was used before it was defined"
1425 (nth 1 form))))
1426 (byte-compile-file-form
1427 (macroexpand form byte-compile-macro-environment))
1428 ;; Return nil so the form is not output twice.
1429 nil)
1430
1431 (put 'autoload 'byte-hunk-handler 'byte-compile-file-form-autoload)
1432 (defun byte-compile-file-form-autoload (form)
1433 (and (let ((form form))
1434 (while (if (setq form (cdr form)) (byte-compile-constp (car form))))
1435 (null form)) ;Constants only
1436 (eval (nth 5 form)) ;Macro
1437 (eval form)) ;Define the autoload.
1438 (if (stringp (nth 3 form))
1439 form
1440 ;; No doc string, so we can compile this as a normal form.
1441 (byte-compile-keep-pending form 'byte-compile-normal-call)))
1442
1443 (put 'defvar 'byte-hunk-handler 'byte-compile-file-form-defvar)
1444 (put 'defconst 'byte-hunk-handler 'byte-compile-file-form-defvar)
1445 (defun byte-compile-file-form-defvar (form)
1446 (if (null (nth 3 form))
1447 ;; Since there is no doc string, we can compile this as a normal form,
1448 ;; and not do a file-boundary.
1449 (byte-compile-keep-pending form)
1450 (if (memq 'free-vars byte-compile-warnings)
1451 (setq byte-compile-bound-variables
1452 (cons (nth 1 form) byte-compile-bound-variables)))
1453 (cond ((consp (nth 2 form))
1454 (setq form (copy-sequence form))
1455 (setcar (cdr (cdr form))
1456 (byte-compile-top-level (nth 2 form) nil 'file))))
1457 form))
1458
1459 (put 'require 'byte-hunk-handler 'byte-compile-file-form-eval-boundary)
1460 (defun byte-compile-file-form-eval-boundary (form)
1461 (eval form)
1462 (byte-compile-keep-pending form 'byte-compile-normal-call))
1463
1464 (put 'progn 'byte-hunk-handler 'byte-compile-file-form-progn)
1465 (put 'prog1 'byte-hunk-handler 'byte-compile-file-form-progn)
1466 (put 'prog2 'byte-hunk-handler 'byte-compile-file-form-progn)
1467 (defun byte-compile-file-form-progn (form)
1468 (mapcar 'byte-compile-file-form (cdr form))
1469 ;; Return nil so the forms are not output twice.
1470 nil)
1471
1472 ;; This handler is not necessary, but it makes the output from dont-compile
1473 ;; and similar macros cleaner.
1474 (put 'eval 'byte-hunk-handler 'byte-compile-file-form-eval)
1475 (defun byte-compile-file-form-eval (form)
1476 (if (eq (car-safe (nth 1 form)) 'quote)
1477 (nth 1 (nth 1 form))
1478 (byte-compile-keep-pending form)))
1479
1480 (put 'defun 'byte-hunk-handler 'byte-compile-file-form-defun)
1481 (defun byte-compile-file-form-defun (form)
1482 (byte-compile-file-form-defmumble form nil))
1483
1484 (put 'defmacro 'byte-hunk-handler 'byte-compile-file-form-defmacro)
1485 (defun byte-compile-file-form-defmacro (form)
1486 (byte-compile-file-form-defmumble form t))
1487
1488 (defun byte-compile-file-form-defmumble (form macrop)
1489 (let* ((name (car (cdr form)))
1490 (this-kind (if macrop 'byte-compile-macro-environment
1491 'byte-compile-function-environment))
1492 (that-kind (if macrop 'byte-compile-function-environment
1493 'byte-compile-macro-environment))
1494 (this-one (assq name (symbol-value this-kind)))
1495 (that-one (assq name (symbol-value that-kind)))
1496 (byte-compile-free-references nil)
1497 (byte-compile-free-assignments nil))
1498
1499 ;; When a function or macro is defined, add it to the call tree so that
1500 ;; we can tell when functions are not used.
1501 (if byte-compile-generate-call-tree
1502 (or (assq name byte-compile-call-tree)
1503 (setq byte-compile-call-tree
1504 (cons (list name nil nil) byte-compile-call-tree))))
1505
1506 (setq byte-compile-current-form name) ; for warnings
1507 (if (memq 'redefine byte-compile-warnings)
1508 (byte-compile-arglist-warn form macrop))
1509 (if byte-compile-verbose
1510 (message "Compiling %s (%s)..." (or filename "") (nth 1 form)))
1511 (cond (that-one
1512 (if (and (memq 'redefine byte-compile-warnings)
1513 ;; don't warn when compiling the stubs in byte-run...
1514 (not (assq (nth 1 form)
1515 byte-compile-initial-macro-environment)))
1516 (byte-compile-warn
1517 "%s defined multiple times, as both function and macro"
1518 (nth 1 form)))
1519 (setcdr that-one nil))
1520 (this-one
1521 (if (and (memq 'redefine byte-compile-warnings)
1522 ;; hack: don't warn when compiling the magic internal
1523 ;; byte-compiler macros in byte-run.el...
1524 (not (assq (nth 1 form)
1525 byte-compile-initial-macro-environment)))
1526 (byte-compile-warn "%s %s defined multiple times in this file"
1527 (if macrop "macro" "function")
1528 (nth 1 form))))
1529 ((and (fboundp name)
1530 (eq (car-safe (symbol-function name))
1531 (if macrop 'lambda 'macro)))
1532 (if (memq 'redefine byte-compile-warnings)
1533 (byte-compile-warn "%s %s being redefined as a %s"
1534 (if macrop "function" "macro")
1535 (nth 1 form)
1536 (if macrop "macro" "function")))
1537 ;; shadow existing definition
1538 (set this-kind
1539 (cons (cons name nil) (symbol-value this-kind))))
1540 )
1541 (let ((body (nthcdr 3 form)))
1542 (if (and (stringp (car body))
1543 (symbolp (car-safe (cdr-safe body)))
1544 (car-safe (cdr-safe body))
1545 (stringp (car-safe (cdr-safe (cdr-safe body)))))
1546 (byte-compile-warn "Probable `\"' without `\\' in doc string of %s"
1547 (nth 1 form))))
1548 (let* ((new-one (byte-compile-lambda (cons 'lambda (nthcdr 2 form))))
1549 (code (byte-compile-byte-code-maker new-one)))
1550 (if this-one
1551 (setcdr this-one new-one)
1552 (set this-kind
1553 (cons (cons name new-one) (symbol-value this-kind))))
1554 (if (and (stringp (nth 3 form))
1555 (eq 'quote (car-safe code))
1556 (eq 'lambda (car-safe (nth 1 code))))
1557 (cons (car form)
1558 (cons name (cdr (nth 1 code))))
1559 (if (not (stringp (nth 3 form)))
1560 ;; No doc string to make-docfile; insert form in normal code.
1561 (byte-compile-keep-pending
1562 (list 'fset (list 'quote name)
1563 (cond ((not macrop)
1564 code)
1565 ((eq 'make-byte-code (car-safe code))
1566 (list 'cons ''macro code))
1567 ((list 'quote (if macrop
1568 (cons 'macro new-one)
1569 new-one)))))
1570 'byte-compile-two-args)
1571 ;; Output the form by hand, that's much simpler than having
1572 ;; b-c-output-file-form analyze the fset.
1573 (byte-compile-flush-pending)
1574 (princ "\n(fset '" outbuffer)
1575 (prin1 name outbuffer)
1576 (byte-compile-output-docform
1577 (cond ((atom code)
1578 (if macrop '(" '(macro . #[" 4 "])") '(" #[" 4 "]")))
1579 ((eq (car code) 'quote)
1580 (setq code new-one)
1581 (if macrop '(" '(macro " 2 ")") '(" '(" 2 ")")))
1582 ((if macrop '(" (cons 'macro (" 5 "))") '(" (" 5 ")"))))
1583 (append code nil))
1584 (princ ")" outbuffer)
1585 nil)))))
1586
1587 \f
1588 ;;;###autoload
1589 (defun byte-compile (form)
1590 "If FORM is a symbol, byte-compile its function definition.
1591 If FORM is a lambda or a macro, byte-compile it as a function."
1592 (displaying-byte-compile-warnings
1593 (byte-compile-close-variables
1594 (let* ((fun (if (symbolp form)
1595 (and (fboundp form) (symbol-function form))
1596 form))
1597 (macro (eq (car-safe fun) 'macro)))
1598 (if macro
1599 (setq fun (cdr fun)))
1600 (cond ((eq (car-safe fun) 'lambda)
1601 (setq fun (if macro
1602 (cons 'macro (byte-compile-lambda fun))
1603 (byte-compile-lambda fun)))
1604 (if (symbolp form)
1605 (fset form fun)
1606 fun)))))))
1607
1608 (defun byte-compile-sexp (sexp)
1609 "Compile and return SEXP."
1610 (displaying-byte-compile-warnings
1611 (byte-compile-close-variables
1612 (byte-compile-top-level sexp))))
1613
1614 ;; Given a function made by byte-compile-lambda, make a form which produces it.
1615 (defun byte-compile-byte-code-maker (fun)
1616 (cond
1617 ((byte-compile-version-cond byte-compile-compatibility)
1618 ;; Return (quote (lambda ...)).
1619 (list 'quote (byte-compile-byte-code-unmake fun)))
1620 ;; ## atom is faster than compiled-func-p.
1621 ((atom fun) ; compiled function.
1622 ;; generate-emacs19-bytecodes must be on, otherwise byte-compile-lambda
1623 ;; would have produced a lambda.
1624 fun)
1625 ;; b-c-lambda didn't produce a compiled-function, so it's either a trivial
1626 ;; function, or this is Emacs 18, or generate-emacs19-bytecodes is off.
1627 ((let (tmp)
1628 (if (and (setq tmp (assq 'byte-code (cdr-safe (cdr fun))))
1629 (null (cdr (memq tmp fun))))
1630 ;; Generate a make-byte-code call.
1631 (let* ((interactive (assq 'interactive (cdr (cdr fun)))))
1632 (nconc (list 'make-byte-code
1633 (list 'quote (nth 1 fun)) ;arglist
1634 (nth 1 tmp) ;bytes
1635 (nth 2 tmp) ;consts
1636 (nth 3 tmp)) ;depth
1637 (cond ((stringp (nth 2 fun))
1638 (list (nth 2 fun))) ;doc
1639 (interactive
1640 (list nil)))
1641 (cond (interactive
1642 (list (if (or (null (nth 1 interactive))
1643 (stringp (nth 1 interactive)))
1644 (nth 1 interactive)
1645 ;; Interactive spec is a list or a variable
1646 ;; (if it is correct).
1647 (list 'quote (nth 1 interactive))))))))
1648 ;; a non-compiled function (probably trivial)
1649 (list 'quote fun))))))
1650
1651 ;; Turn a function into an ordinary lambda. Needed for v18 files.
1652 (defun byte-compile-byte-code-unmake (function)
1653 (if (consp function)
1654 function;;It already is a lambda.
1655 (setq function (append function nil)) ; turn it into a list
1656 (nconc (list 'lambda (nth 0 function))
1657 (and (nth 4 function) (list (nth 4 function)))
1658 (if (nthcdr 5 function)
1659 (list (cons 'interactive (if (nth 5 function)
1660 (nthcdr 5 function)))))
1661 (list (list 'byte-code
1662 (nth 1 function) (nth 2 function)
1663 (nth 3 function))))))
1664
1665
1666 ;; Byte-compile a lambda-expression and return a valid function.
1667 ;; The value is usually a compiled function but may be the original
1668 ;; lambda-expression.
1669 (defun byte-compile-lambda (fun)
1670 (let* ((arglist (nth 1 fun))
1671 (byte-compile-bound-variables
1672 (nconc (and (memq 'free-vars byte-compile-warnings)
1673 (delq '&rest (delq '&optional (copy-sequence arglist))))
1674 byte-compile-bound-variables))
1675 (body (cdr (cdr fun)))
1676 (doc (if (stringp (car body))
1677 (prog1 (car body)
1678 (setq body (cdr body)))))
1679 (int (assq 'interactive body)))
1680 (cond (int
1681 ;; Skip (interactive) if it is in front (the most usual location).
1682 (if (eq int (car body))
1683 (setq body (cdr body)))
1684 (cond ((consp (cdr int))
1685 (if (cdr (cdr int))
1686 (byte-compile-warn "malformed interactive spec: %s"
1687 (prin1-to-string int)))
1688 ;; If the interactive spec is a call to `list',
1689 ;; don't compile it, because `call-interactively'
1690 ;; looks at the args of `list'.
1691 (or (eq (car-safe (nth 1 int)) 'list)
1692 (setq int (list 'interactive
1693 (byte-compile-top-level (nth 1 int))))))
1694 ((cdr int)
1695 (byte-compile-warn "malformed interactive spec: %s"
1696 (prin1-to-string int))))))
1697 (let ((compiled (byte-compile-top-level (cons 'progn body) nil 'lambda)))
1698 (if (and (eq 'byte-code (car-safe compiled))
1699 (not (byte-compile-version-cond
1700 byte-compile-compatibility)))
1701 (apply 'make-byte-code
1702 (append (list arglist)
1703 ;; byte-string, constants-vector, stack depth
1704 (cdr compiled)
1705 ;; optionally, the doc string.
1706 (if (or doc int)
1707 (list doc))
1708 ;; optionally, the interactive spec.
1709 (if int
1710 (list (nth 1 int)))))
1711 (setq compiled
1712 (nconc (if int (list int))
1713 (cond ((eq (car-safe compiled) 'progn) (cdr compiled))
1714 (compiled (list compiled)))))
1715 (nconc (list 'lambda arglist)
1716 (if (or doc (stringp (car compiled)))
1717 (cons doc (cond (compiled)
1718 (body (list nil))))
1719 compiled))))))
1720
1721 (defun byte-compile-constants-vector ()
1722 ;; Builds the constants-vector from the current variables and constants.
1723 ;; This modifies the constants from (const . nil) to (const . offset).
1724 ;; To keep the byte-codes to look up the vector as short as possible:
1725 ;; First 6 elements are vars, as there are one-byte varref codes for those.
1726 ;; Next up to byte-constant-limit are constants, still with one-byte codes.
1727 ;; Next variables again, to get 2-byte codes for variable lookup.
1728 ;; The rest of the constants and variables need 3-byte byte-codes.
1729 (let* ((i -1)
1730 (rest (nreverse byte-compile-variables)) ; nreverse because the first
1731 (other (nreverse byte-compile-constants)) ; vars often are used most.
1732 ret tmp
1733 (limits '(5 ; Use the 1-byte varref codes,
1734 63 ; 1-constlim ; 1-byte byte-constant codes,
1735 255 ; 2-byte varref codes,
1736 65535)) ; 3-byte codes for the rest.
1737 limit)
1738 (while (or rest other)
1739 (setq limit (car limits))
1740 (while (and rest (not (eq i limit)))
1741 (if (setq tmp (assq (car (car rest)) ret))
1742 (setcdr (car rest) (cdr tmp))
1743 (setcdr (car rest) (setq i (1+ i)))
1744 (setq ret (cons (car rest) ret)))
1745 (setq rest (cdr rest)))
1746 (setq limits (cdr limits)
1747 rest (prog1 other
1748 (setq other rest))))
1749 (apply 'vector (nreverse (mapcar 'car ret)))))
1750
1751 ;; Given an expression FORM, compile it and return an equivalent byte-code
1752 ;; expression (a call to the function byte-code).
1753 (defun byte-compile-top-level (form &optional for-effect output-type)
1754 ;; OUTPUT-TYPE advises about how form is expected to be used:
1755 ;; 'eval or nil -> a single form,
1756 ;; 'progn or t -> a list of forms,
1757 ;; 'lambda -> body of a lambda,
1758 ;; 'file -> used at file-level.
1759 (let ((byte-compile-constants nil)
1760 (byte-compile-variables nil)
1761 (byte-compile-tag-number 0)
1762 (byte-compile-depth 0)
1763 (byte-compile-maxdepth 0)
1764 (byte-compile-output nil))
1765 (if (memq byte-optimize '(t source))
1766 (setq form (byte-optimize-form form for-effect)))
1767 (while (and (eq (car-safe form) 'progn) (null (cdr (cdr form))))
1768 (setq form (nth 1 form)))
1769 (if (and (eq 'byte-code (car-safe form))
1770 (not (memq byte-optimize '(t byte)))
1771 (stringp (nth 1 form)) (vectorp (nth 2 form))
1772 (natnump (nth 3 form)))
1773 form
1774 (byte-compile-form form for-effect)
1775 (byte-compile-out-toplevel for-effect output-type))))
1776
1777 (defun byte-compile-out-toplevel (&optional for-effect output-type)
1778 (if for-effect
1779 ;; The stack is empty. Push a value to be returned from (byte-code ..).
1780 (if (eq (car (car byte-compile-output)) 'byte-discard)
1781 (setq byte-compile-output (cdr byte-compile-output))
1782 (byte-compile-push-constant
1783 ;; Push any constant - preferably one which already is used, and
1784 ;; a number or symbol - ie not some big sequence. The return value
1785 ;; isn't returned, but it would be a shame if some textually large
1786 ;; constant was not optimized away because we chose to return it.
1787 (and (not (assq nil byte-compile-constants)) ; Nil is often there.
1788 (let ((tmp (reverse byte-compile-constants)))
1789 (while (and tmp (not (or (symbolp (car (car tmp)))
1790 (numberp (car (car tmp))))))
1791 (setq tmp (cdr tmp)))
1792 (car (car tmp)))))))
1793 (byte-compile-out 'byte-return 0)
1794 (setq byte-compile-output (nreverse byte-compile-output))
1795 (if (memq byte-optimize '(t byte))
1796 (setq byte-compile-output
1797 (byte-optimize-lapcode byte-compile-output for-effect)))
1798
1799 ;; Decompile trivial functions:
1800 ;; only constants and variables, or a single funcall except in lambdas.
1801 ;; Except for Lisp_Compiled objects, forms like (foo "hi")
1802 ;; are still quicker than (byte-code "..." [foo "hi"] 2).
1803 ;; Note that even (quote foo) must be parsed just as any subr by the
1804 ;; interpreter, so quote should be compiled into byte-code in some contexts.
1805 ;; What to leave uncompiled:
1806 ;; lambda -> a single atom.
1807 ;; eval -> atom, quote or (function atom atom atom)
1808 ;; progn -> as <<same-as-eval>> or (progn <<same-as-eval>> atom)
1809 ;; file -> as progn, but takes both quotes and atoms, and longer forms.
1810 (let (rest
1811 (maycall (not (eq output-type 'lambda))) ; t if we may make a funcall.
1812 tmp body)
1813 (cond
1814 ;; #### This should be split out into byte-compile-nontrivial-function-p.
1815 ((or (nthcdr (if (eq output-type 'file) 50 8) byte-compile-output)
1816 (assq 'TAG byte-compile-output) ; Not necessary, but speeds up a bit.
1817 (not (setq tmp (assq 'byte-return byte-compile-output)))
1818 (progn
1819 (setq rest (nreverse
1820 (cdr (memq tmp (reverse byte-compile-output)))))
1821 (while (cond
1822 ((memq (car (car rest)) '(byte-varref byte-constant))
1823 (setq tmp (car (cdr (car rest))))
1824 (if (if (eq (car (car rest)) 'byte-constant)
1825 (or (consp tmp)
1826 (and (symbolp tmp)
1827 (not (memq tmp '(nil t))))))
1828 (if maycall
1829 (setq body (cons (list 'quote tmp) body)))
1830 (setq body (cons tmp body))))
1831 ((and maycall
1832 ;; Allow a funcall if at most one atom follows it.
1833 (null (nthcdr 3 rest))
1834 (setq tmp (get (car (car rest)) 'byte-opcode-invert))
1835 (or (null (cdr rest))
1836 (and (memq output-type '(file progn t))
1837 (cdr (cdr rest))
1838 (eq (car (nth 1 rest)) 'byte-discard)
1839 (progn (setq rest (cdr rest)) t))))
1840 (setq maycall nil) ; Only allow one real function call.
1841 (setq body (nreverse body))
1842 (setq body (list
1843 (if (and (eq tmp 'funcall)
1844 (eq (car-safe (car body)) 'quote))
1845 (cons (nth 1 (car body)) (cdr body))
1846 (cons tmp body))))
1847 (or (eq output-type 'file)
1848 (not (delq nil (mapcar 'consp (cdr (car body))))))))
1849 (setq rest (cdr rest)))
1850 rest)
1851 (and (consp (car body)) (eq output-type 'lambda)))
1852 (let ((byte-compile-vector (byte-compile-constants-vector)))
1853 (list 'byte-code (byte-compile-lapcode byte-compile-output)
1854 byte-compile-vector byte-compile-maxdepth)))
1855 ;; it's a trivial function
1856 ((cdr body) (cons 'progn (nreverse body)))
1857 ((car body)))))
1858
1859 ;; Given BODY, compile it and return a new body.
1860 (defun byte-compile-top-level-body (body &optional for-effect)
1861 (setq body (byte-compile-top-level (cons 'progn body) for-effect t))
1862 (cond ((eq (car-safe body) 'progn)
1863 (cdr body))
1864 (body
1865 (list body))))
1866 \f
1867 ;; This is the recursive entry point for compiling each subform of an
1868 ;; expression.
1869 ;; If for-effect is non-nil, byte-compile-form will output a byte-discard
1870 ;; before terminating (ie no value will be left on the stack).
1871 ;; A byte-compile handler may, when for-effect is non-nil, choose output code
1872 ;; which does not leave a value on the stack, and then set for-effect to nil
1873 ;; (to prevent byte-compile-form from outputting the byte-discard).
1874 ;; If a handler wants to call another handler, it should do so via
1875 ;; byte-compile-form, or take extreme care to handle for-effect correctly.
1876 ;; (Use byte-compile-form-do-effect to reset the for-effect flag too.)
1877 ;;
1878 (defun byte-compile-form (form &optional for-effect)
1879 (setq form (macroexpand form byte-compile-macro-environment))
1880 (cond ((not (consp form))
1881 (cond ((or (not (symbolp form)) (memq form '(nil t)))
1882 (byte-compile-constant form))
1883 ((and for-effect byte-compile-delete-errors)
1884 (setq for-effect nil))
1885 (t (byte-compile-variable-ref 'byte-varref form))))
1886 ((symbolp (car form))
1887 (let* ((fn (car form))
1888 (handler (get fn 'byte-compile)))
1889 (if (memq fn '(t nil))
1890 (byte-compile-warn "%s called as a function" fn))
1891 (if (and handler
1892 (or (byte-compile-version-cond
1893 byte-compile-compatibility)
1894 (not (get (get fn 'byte-opcode) 'emacs19-opcode))))
1895 (funcall handler form)
1896 (if (memq 'callargs byte-compile-warnings)
1897 (byte-compile-callargs-warn form))
1898 (byte-compile-normal-call form))))
1899 ((and (or (byte-code-function-p (car form))
1900 (eq (car-safe (car form)) 'lambda))
1901 ;; if the form comes out the same way it went in, that's
1902 ;; because it was malformed, and we couldn't unfold it.
1903 (not (eq form (setq form (byte-compile-unfold-lambda form)))))
1904 (byte-compile-form form for-effect)
1905 (setq for-effect nil))
1906 ((byte-compile-normal-call form)))
1907 (if for-effect
1908 (byte-compile-discard)))
1909
1910 (defun byte-compile-normal-call (form)
1911 (if byte-compile-generate-call-tree
1912 (byte-compile-annotate-call-tree form))
1913 (byte-compile-push-constant (car form))
1914 (mapcar 'byte-compile-form (cdr form)) ; wasteful, but faster.
1915 (byte-compile-out 'byte-call (length (cdr form))))
1916
1917 (defun byte-compile-variable-ref (base-op var)
1918 (if (or (not (symbolp var)) (memq var '(nil t)))
1919 (byte-compile-warn (if (eq base-op 'byte-varbind)
1920 "Attempt to let-bind %s %s"
1921 "Variable reference to %s %s")
1922 (if (symbolp var) "constant" "nonvariable")
1923 (prin1-to-string var))
1924 (if (get var 'byte-obsolete-variable)
1925 (let ((ob (get var 'byte-obsolete-variable)))
1926 (byte-compile-warn "%s is an obsolete variable; %s" var
1927 (if (stringp ob)
1928 ob
1929 (format "use %s instead." ob)))))
1930 (if (memq 'free-vars byte-compile-warnings)
1931 (if (eq base-op 'byte-varbind)
1932 (setq byte-compile-bound-variables
1933 (cons var byte-compile-bound-variables))
1934 (or (boundp var)
1935 (memq var byte-compile-bound-variables)
1936 (if (eq base-op 'byte-varset)
1937 (or (memq var byte-compile-free-assignments)
1938 (progn
1939 (byte-compile-warn "assignment to free variable %s" var)
1940 (setq byte-compile-free-assignments
1941 (cons var byte-compile-free-assignments))))
1942 (or (memq var byte-compile-free-references)
1943 (progn
1944 (byte-compile-warn "reference to free variable %s" var)
1945 (setq byte-compile-free-references
1946 (cons var byte-compile-free-references)))))))))
1947 (let ((tmp (assq var byte-compile-variables)))
1948 (or tmp
1949 (setq tmp (list var)
1950 byte-compile-variables (cons tmp byte-compile-variables)))
1951 (byte-compile-out base-op tmp)))
1952
1953 (defmacro byte-compile-get-constant (const)
1954 (` (or (if (stringp (, const))
1955 (assoc (, const) byte-compile-constants)
1956 (assq (, const) byte-compile-constants))
1957 (car (setq byte-compile-constants
1958 (cons (list (, const)) byte-compile-constants))))))
1959
1960 ;; Use this when the value of a form is a constant. This obeys for-effect.
1961 (defun byte-compile-constant (const)
1962 (if for-effect
1963 (setq for-effect nil)
1964 (byte-compile-out 'byte-constant (byte-compile-get-constant const))))
1965
1966 ;; Use this for a constant that is not the value of its containing form.
1967 ;; This ignores for-effect.
1968 (defun byte-compile-push-constant (const)
1969 (let ((for-effect nil))
1970 (inline (byte-compile-constant const))))
1971
1972 \f
1973 ;; Compile those primitive ordinary functions
1974 ;; which have special byte codes just for speed.
1975
1976 (defmacro byte-defop-compiler (function &optional compile-handler)
1977 ;; add a compiler-form for FUNCTION.
1978 ;; If function is a symbol, then the variable "byte-SYMBOL" must name
1979 ;; the opcode to be used. If function is a list, the first element
1980 ;; is the function and the second element is the bytecode-symbol.
1981 ;; COMPILE-HANDLER is the function to use to compile this byte-op, or
1982 ;; may be the abbreviations 0, 1, 2, 3, 0-1, or 1-2.
1983 ;; If it is nil, then the handler is "byte-compile-SYMBOL."
1984 (let (opcode)
1985 (if (symbolp function)
1986 (setq opcode (intern (concat "byte-" (symbol-name function))))
1987 (setq opcode (car (cdr function))
1988 function (car function)))
1989 (let ((fnform
1990 (list 'put (list 'quote function) ''byte-compile
1991 (list 'quote
1992 (or (cdr (assq compile-handler
1993 '((0 . byte-compile-no-args)
1994 (1 . byte-compile-one-arg)
1995 (2 . byte-compile-two-args)
1996 (3 . byte-compile-three-args)
1997 (0-1 . byte-compile-zero-or-one-arg)
1998 (1-2 . byte-compile-one-or-two-args)
1999 (2-3 . byte-compile-two-or-three-args)
2000 )))
2001 compile-handler
2002 (intern (concat "byte-compile-"
2003 (symbol-name function))))))))
2004 (if opcode
2005 (list 'progn fnform
2006 (list 'put (list 'quote function)
2007 ''byte-opcode (list 'quote opcode))
2008 (list 'put (list 'quote opcode)
2009 ''byte-opcode-invert (list 'quote function)))
2010 fnform))))
2011
2012 (defmacro byte-defop-compiler19 (function &optional compile-handler)
2013 ;; Just like byte-defop-compiler, but defines an opcode that will only
2014 ;; be used when byte-compile-compatibility is true.
2015 (if (and (byte-compile-single-version)
2016 (not byte-compile-compatibility))
2017 ;; #### instead of doing nothing, this should do some remprops,
2018 ;; #### to protect against the case where a single-version compiler
2019 ;; #### is loaded into a world that has contained a multi-version one.
2020 nil
2021 (list 'progn
2022 (list 'put
2023 (list 'quote
2024 (or (car (cdr-safe function))
2025 (intern (concat "byte-"
2026 (symbol-name (or (car-safe function) function))))))
2027 ''emacs19-opcode t)
2028 (list 'byte-defop-compiler function compile-handler))))
2029
2030 (defmacro byte-defop-compiler-1 (function &optional compile-handler)
2031 (list 'byte-defop-compiler (list function nil) compile-handler))
2032
2033 \f
2034 (put 'byte-call 'byte-opcode-invert 'funcall)
2035 (put 'byte-list1 'byte-opcode-invert 'list)
2036 (put 'byte-list2 'byte-opcode-invert 'list)
2037 (put 'byte-list3 'byte-opcode-invert 'list)
2038 (put 'byte-list4 'byte-opcode-invert 'list)
2039 (put 'byte-listN 'byte-opcode-invert 'list)
2040 (put 'byte-concat2 'byte-opcode-invert 'concat)
2041 (put 'byte-concat3 'byte-opcode-invert 'concat)
2042 (put 'byte-concat4 'byte-opcode-invert 'concat)
2043 (put 'byte-concatN 'byte-opcode-invert 'concat)
2044 (put 'byte-insertN 'byte-opcode-invert 'insert)
2045
2046 (byte-defop-compiler (dot byte-point) 0)
2047 (byte-defop-compiler (dot-max byte-point-max) 0)
2048 (byte-defop-compiler (dot-min byte-point-min) 0)
2049 (byte-defop-compiler point 0)
2050 ;;(byte-defop-compiler mark 0) ;; obsolete
2051 (byte-defop-compiler point-max 0)
2052 (byte-defop-compiler point-min 0)
2053 (byte-defop-compiler following-char 0)
2054 (byte-defop-compiler preceding-char 0)
2055 (byte-defop-compiler current-column 0)
2056 (byte-defop-compiler eolp 0)
2057 (byte-defop-compiler eobp 0)
2058 (byte-defop-compiler bolp 0)
2059 (byte-defop-compiler bobp 0)
2060 (byte-defop-compiler current-buffer 0)
2061 ;;(byte-defop-compiler read-char 0) ;; obsolete
2062 (byte-defop-compiler interactive-p 0)
2063 (byte-defop-compiler19 widen 0)
2064 (byte-defop-compiler19 end-of-line 0-1)
2065 (byte-defop-compiler19 forward-char 0-1)
2066 (byte-defop-compiler19 forward-line 0-1)
2067 (byte-defop-compiler symbolp 1)
2068 (byte-defop-compiler consp 1)
2069 (byte-defop-compiler stringp 1)
2070 (byte-defop-compiler listp 1)
2071 (byte-defop-compiler not 1)
2072 (byte-defop-compiler (null byte-not) 1)
2073 (byte-defop-compiler car 1)
2074 (byte-defop-compiler cdr 1)
2075 (byte-defop-compiler length 1)
2076 (byte-defop-compiler symbol-value 1)
2077 (byte-defop-compiler symbol-function 1)
2078 (byte-defop-compiler (1+ byte-add1) 1)
2079 (byte-defop-compiler (1- byte-sub1) 1)
2080 (byte-defop-compiler goto-char 1)
2081 (byte-defop-compiler char-after 1)
2082 (byte-defop-compiler set-buffer 1)
2083 ;;(byte-defop-compiler set-mark 1) ;; obsolete
2084 (byte-defop-compiler19 forward-word 1)
2085 (byte-defop-compiler19 char-syntax 1)
2086 (byte-defop-compiler19 nreverse 1)
2087 (byte-defop-compiler19 car-safe 1)
2088 (byte-defop-compiler19 cdr-safe 1)
2089 (byte-defop-compiler19 numberp 1)
2090 (byte-defop-compiler19 integerp 1)
2091 (byte-defop-compiler19 skip-chars-forward 1-2)
2092 (byte-defop-compiler19 skip-chars-backward 1-2)
2093 (byte-defop-compiler (eql byte-eq) 2)
2094 (byte-defop-compiler eq 2)
2095 (byte-defop-compiler memq 2)
2096 (byte-defop-compiler cons 2)
2097 (byte-defop-compiler aref 2)
2098 (byte-defop-compiler set 2)
2099 (byte-defop-compiler (= byte-eqlsign) 2)
2100 (byte-defop-compiler (< byte-lss) 2)
2101 (byte-defop-compiler (> byte-gtr) 2)
2102 (byte-defop-compiler (<= byte-leq) 2)
2103 (byte-defop-compiler (>= byte-geq) 2)
2104 (byte-defop-compiler get 2)
2105 (byte-defop-compiler nth 2)
2106 (byte-defop-compiler substring 2-3)
2107 (byte-defop-compiler19 (move-marker byte-set-marker) 2-3)
2108 (byte-defop-compiler19 set-marker 2-3)
2109 (byte-defop-compiler19 match-beginning 1)
2110 (byte-defop-compiler19 match-end 1)
2111 (byte-defop-compiler19 upcase 1)
2112 (byte-defop-compiler19 downcase 1)
2113 (byte-defop-compiler19 string= 2)
2114 (byte-defop-compiler19 string< 2)
2115 (byte-defop-compiler19 (string-equal byte-string=) 2)
2116 (byte-defop-compiler19 (string-lessp byte-string<) 2)
2117 (byte-defop-compiler19 equal 2)
2118 (byte-defop-compiler19 nthcdr 2)
2119 (byte-defop-compiler19 elt 2)
2120 (byte-defop-compiler19 member 2)
2121 (byte-defop-compiler19 assq 2)
2122 (byte-defop-compiler19 (rplaca byte-setcar) 2)
2123 (byte-defop-compiler19 (rplacd byte-setcdr) 2)
2124 (byte-defop-compiler19 setcar 2)
2125 (byte-defop-compiler19 setcdr 2)
2126 (byte-defop-compiler19 buffer-substring 2)
2127 (byte-defop-compiler19 delete-region 2)
2128 (byte-defop-compiler19 narrow-to-region 2)
2129 (byte-defop-compiler19 (mod byte-rem) 2)
2130 (byte-defop-compiler19 (% byte-rem) 2)
2131 (byte-defop-compiler aset 3)
2132
2133 (byte-defop-compiler max byte-compile-associative)
2134 (byte-defop-compiler min byte-compile-associative)
2135 (byte-defop-compiler (+ byte-plus) byte-compile-associative)
2136 (byte-defop-compiler19 (* byte-mult) byte-compile-associative)
2137
2138 ;;####(byte-defop-compiler19 move-to-column 1)
2139 (byte-defop-compiler-1 interactive byte-compile-noop)
2140
2141 \f
2142 (defun byte-compile-subr-wrong-args (form n)
2143 (byte-compile-warn "%s called with %d arg%s, but requires %s"
2144 (car form) (length (cdr form))
2145 (if (= 1 (length (cdr form))) "" "s") n)
2146 ;; get run-time wrong-number-of-args error.
2147 (byte-compile-normal-call form))
2148
2149 (defun byte-compile-no-args (form)
2150 (if (not (= (length form) 1))
2151 (byte-compile-subr-wrong-args form "none")
2152 (byte-compile-out (get (car form) 'byte-opcode) 0)))
2153
2154 (defun byte-compile-one-arg (form)
2155 (if (not (= (length form) 2))
2156 (byte-compile-subr-wrong-args form 1)
2157 (byte-compile-form (car (cdr form))) ;; Push the argument
2158 (byte-compile-out (get (car form) 'byte-opcode) 0)))
2159
2160 (defun byte-compile-two-args (form)
2161 (if (not (= (length form) 3))
2162 (byte-compile-subr-wrong-args form 2)
2163 (byte-compile-form (car (cdr form))) ;; Push the arguments
2164 (byte-compile-form (nth 2 form))
2165 (byte-compile-out (get (car form) 'byte-opcode) 0)))
2166
2167 (defun byte-compile-three-args (form)
2168 (if (not (= (length form) 4))
2169 (byte-compile-subr-wrong-args form 3)
2170 (byte-compile-form (car (cdr form))) ;; Push the arguments
2171 (byte-compile-form (nth 2 form))
2172 (byte-compile-form (nth 3 form))
2173 (byte-compile-out (get (car form) 'byte-opcode) 0)))
2174
2175 (defun byte-compile-zero-or-one-arg (form)
2176 (let ((len (length form)))
2177 (cond ((= len 1) (byte-compile-one-arg (append form '(nil))))
2178 ((= len 2) (byte-compile-one-arg form))
2179 (t (byte-compile-subr-wrong-args form "0-1")))))
2180
2181 (defun byte-compile-one-or-two-args (form)
2182 (let ((len (length form)))
2183 (cond ((= len 2) (byte-compile-two-args (append form '(nil))))
2184 ((= len 3) (byte-compile-two-args form))
2185 (t (byte-compile-subr-wrong-args form "1-2")))))
2186
2187 (defun byte-compile-two-or-three-args (form)
2188 (let ((len (length form)))
2189 (cond ((= len 3) (byte-compile-three-args (append form '(nil))))
2190 ((= len 4) (byte-compile-three-args form))
2191 (t (byte-compile-subr-wrong-args form "2-3")))))
2192
2193 (defun byte-compile-noop (form)
2194 (byte-compile-constant nil))
2195
2196 (defun byte-compile-discard ()
2197 (byte-compile-out 'byte-discard 0))
2198
2199
2200 ;; Compile a function that accepts one or more args and is right-associative.
2201 (defun byte-compile-associative (form)
2202 (if (cdr form)
2203 (let ((opcode (get (car form) 'byte-opcode)))
2204 ;; To compile all the args first may enable some optimizaions.
2205 (mapcar 'byte-compile-form (setq form (cdr form)))
2206 (while (setq form (cdr form))
2207 (byte-compile-out opcode 0)))
2208 (byte-compile-constant (eval form))))
2209
2210 \f
2211 ;; more complicated compiler macros
2212
2213 (byte-defop-compiler list)
2214 (byte-defop-compiler concat)
2215 (byte-defop-compiler fset)
2216 (byte-defop-compiler (indent-to-column byte-indent-to) byte-compile-indent-to)
2217 (byte-defop-compiler indent-to)
2218 (byte-defop-compiler insert)
2219 (byte-defop-compiler-1 function byte-compile-function-form)
2220 (byte-defop-compiler-1 - byte-compile-minus)
2221 (byte-defop-compiler19 (/ byte-quo) byte-compile-quo)
2222 (byte-defop-compiler19 nconc)
2223 (byte-defop-compiler-1 beginning-of-line)
2224
2225 (defun byte-compile-list (form)
2226 (let ((count (length (cdr form))))
2227 (cond ((= count 0)
2228 (byte-compile-constant nil))
2229 ((< count 5)
2230 (mapcar 'byte-compile-form (cdr form))
2231 (byte-compile-out
2232 (aref [byte-list1 byte-list2 byte-list3 byte-list4] (1- count)) 0))
2233 ((and (< count 256) (byte-compile-version-cond
2234 byte-compile-compatibility))
2235 (mapcar 'byte-compile-form (cdr form))
2236 (byte-compile-out 'byte-listN count))
2237 (t (byte-compile-normal-call form)))))
2238
2239 (defun byte-compile-concat (form)
2240 (let ((count (length (cdr form))))
2241 (cond ((and (< 1 count) (< count 5))
2242 (mapcar 'byte-compile-form (cdr form))
2243 (byte-compile-out
2244 (aref [byte-concat2 byte-concat3 byte-concat4] (- count 2))
2245 0))
2246 ;; Concat of one arg is not a no-op if arg is not a string.
2247 ((= count 0)
2248 (byte-compile-form ""))
2249 ((and (< count 256) (byte-compile-version-cond
2250 byte-compile-compatibility))
2251 (mapcar 'byte-compile-form (cdr form))
2252 (byte-compile-out 'byte-concatN count))
2253 ((byte-compile-normal-call form)))))
2254
2255 (defun byte-compile-minus (form)
2256 (if (null (setq form (cdr form)))
2257 (byte-compile-constant 0)
2258 (byte-compile-form (car form))
2259 (if (cdr form)
2260 (while (setq form (cdr form))
2261 (byte-compile-form (car form))
2262 (byte-compile-out 'byte-diff 0))
2263 (byte-compile-out 'byte-negate 0))))
2264
2265 (defun byte-compile-quo (form)
2266 (let ((len (length form)))
2267 (cond ((<= len 2)
2268 (byte-compile-subr-wrong-args form "2 or more"))
2269 (t
2270 (byte-compile-form (car (setq form (cdr form))))
2271 (while (setq form (cdr form))
2272 (byte-compile-form (car form))
2273 (byte-compile-out 'byte-quo 0))))))
2274
2275 (defun byte-compile-nconc (form)
2276 (let ((len (length form)))
2277 (cond ((= len 1)
2278 (byte-compile-constant nil))
2279 ((= len 2)
2280 ;; nconc of one arg is a noop, even if that arg isn't a list.
2281 (byte-compile-form (nth 1 form)))
2282 (t
2283 (byte-compile-form (car (setq form (cdr form))))
2284 (while (setq form (cdr form))
2285 (byte-compile-form (car form))
2286 (byte-compile-out 'byte-nconc 0))))))
2287
2288 (defun byte-compile-fset (form)
2289 ;; warn about forms like (fset 'foo '(lambda () ...))
2290 ;; (where the lambda expression is non-trivial...)
2291 (let ((fn (nth 2 form))
2292 body)
2293 (if (and (eq (car-safe fn) 'quote)
2294 (eq (car-safe (setq fn (nth 1 fn))) 'lambda))
2295 (progn
2296 (setq body (cdr (cdr fn)))
2297 (if (stringp (car body)) (setq body (cdr body)))
2298 (if (eq 'interactive (car-safe (car body))) (setq body (cdr body)))
2299 (if (and (consp (car body))
2300 (not (eq 'byte-code (car (car body)))))
2301 (byte-compile-warn
2302 "A quoted lambda form is the second argument of fset. This is probably
2303 not what you want, as that lambda cannot be compiled. Consider using
2304 the syntax (function (lambda (...) ...)) instead.")))))
2305 (byte-compile-two-args form))
2306
2307 (defun byte-compile-funarg (form)
2308 ;; (mapcar '(lambda (x) ..) ..) ==> (mapcar (function (lambda (x) ..)) ..)
2309 ;; for cases where it's guarenteed that first arg will be used as a lambda.
2310 (byte-compile-normal-call
2311 (let ((fn (nth 1 form)))
2312 (if (and (eq (car-safe fn) 'quote)
2313 (eq (car-safe (nth 1 fn)) 'lambda))
2314 (cons (car form)
2315 (cons (cons 'function (cdr fn))
2316 (cdr (cdr form))))
2317 form))))
2318
2319 ;; (function foo) must compile like 'foo, not like (symbol-function 'foo).
2320 ;; Otherwise it will be incompatible with the interpreter,
2321 ;; and (funcall (function foo)) will lose with autoloads.
2322
2323 (defun byte-compile-function-form (form)
2324 (byte-compile-constant
2325 (cond ((symbolp (nth 1 form))
2326 (nth 1 form))
2327 ;; If we're not allowed to use #[] syntax, then output a form like
2328 ;; '(lambda (..) (byte-code ..)) instead of a call to make-byte-code.
2329 ;; In this situation, calling make-byte-code at run-time will usually
2330 ;; be less efficient than processing a call to byte-code.
2331 ((byte-compile-version-cond byte-compile-compatibility)
2332 (byte-compile-byte-code-unmake (byte-compile-lambda (nth 1 form))))
2333 ((byte-compile-lambda (nth 1 form))))))
2334
2335 (defun byte-compile-indent-to (form)
2336 (let ((len (length form)))
2337 (cond ((= len 2)
2338 (byte-compile-form (car (cdr form)))
2339 (byte-compile-out 'byte-indent-to 0))
2340 ((= len 3)
2341 ;; no opcode for 2-arg case.
2342 (byte-compile-normal-call form))
2343 (t
2344 (byte-compile-subr-wrong-args form "1-2")))))
2345
2346 (defun byte-compile-insert (form)
2347 (cond ((null (cdr form))
2348 (byte-compile-constant nil))
2349 ((and (byte-compile-version-cond
2350 byte-compile-compatibility)
2351 (<= (length form) 256))
2352 (mapcar 'byte-compile-form (cdr form))
2353 (if (cdr (cdr form))
2354 (byte-compile-out 'byte-insertN (length (cdr form)))
2355 (byte-compile-out 'byte-insert 0)))
2356 ((memq t (mapcar 'consp (cdr (cdr form))))
2357 (byte-compile-normal-call form))
2358 ;; We can split it; there is no function call after inserting 1st arg.
2359 (t
2360 (while (setq form (cdr form))
2361 (byte-compile-form (car form))
2362 (byte-compile-out 'byte-insert 0)
2363 (if (cdr form)
2364 (byte-compile-discard))))))
2365
2366 (defun byte-compile-beginning-of-line (form)
2367 (if (not (byte-compile-constp (nth 1 form)))
2368 (byte-compile-normal-call form)
2369 (byte-compile-form
2370 (list 'forward-line
2371 (if (integerp (setq form (or (eval (nth 1 form)) 1)))
2372 (1- form)
2373 (byte-compile-warn "Non-numeric arg to beginning-of-line: %s"
2374 form)
2375 (list '1- (list 'quote form))))
2376 t)
2377 (byte-compile-constant nil)))
2378
2379 \f
2380 (byte-defop-compiler-1 setq)
2381 (byte-defop-compiler-1 setq-default)
2382 (byte-defop-compiler-1 quote)
2383 (byte-defop-compiler-1 quote-form)
2384
2385 (defun byte-compile-setq (form)
2386 (let ((args (cdr form)))
2387 (if args
2388 (while args
2389 (byte-compile-form (car (cdr args)))
2390 (or for-effect (cdr (cdr args))
2391 (byte-compile-out 'byte-dup 0))
2392 (byte-compile-variable-ref 'byte-varset (car args))
2393 (setq args (cdr (cdr args))))
2394 ;; (setq), with no arguments.
2395 (byte-compile-form nil for-effect))
2396 (setq for-effect nil)))
2397
2398 (defun byte-compile-setq-default (form)
2399 (byte-compile-form
2400 (cons 'set-default (cons (list 'quote (nth 1 form))
2401 (nthcdr 2 form)))))
2402
2403 (defun byte-compile-quote (form)
2404 (byte-compile-constant (car (cdr form))))
2405
2406 (defun byte-compile-quote-form (form)
2407 (byte-compile-constant (byte-compile-top-level (nth 1 form))))
2408
2409 \f
2410 ;;; control structures
2411
2412 (defun byte-compile-body (body &optional for-effect)
2413 (while (cdr body)
2414 (byte-compile-form (car body) t)
2415 (setq body (cdr body)))
2416 (byte-compile-form (car body) for-effect))
2417
2418 (defsubst byte-compile-body-do-effect (body)
2419 (byte-compile-body body for-effect)
2420 (setq for-effect nil))
2421
2422 (defsubst byte-compile-form-do-effect (form)
2423 (byte-compile-form form for-effect)
2424 (setq for-effect nil))
2425
2426 (byte-defop-compiler-1 inline byte-compile-progn)
2427 (byte-defop-compiler-1 progn)
2428 (byte-defop-compiler-1 prog1)
2429 (byte-defop-compiler-1 prog2)
2430 (byte-defop-compiler-1 if)
2431 (byte-defop-compiler-1 cond)
2432 (byte-defop-compiler-1 and)
2433 (byte-defop-compiler-1 or)
2434 (byte-defop-compiler-1 while)
2435 (byte-defop-compiler-1 funcall)
2436 (byte-defop-compiler-1 apply byte-compile-funarg)
2437 (byte-defop-compiler-1 mapcar byte-compile-funarg)
2438 (byte-defop-compiler-1 mapatoms byte-compile-funarg)
2439 (byte-defop-compiler-1 mapconcat byte-compile-funarg)
2440 (byte-defop-compiler-1 let)
2441 (byte-defop-compiler-1 let*)
2442
2443 (defun byte-compile-progn (form)
2444 (byte-compile-body-do-effect (cdr form)))
2445
2446 (defun byte-compile-prog1 (form)
2447 (byte-compile-form-do-effect (car (cdr form)))
2448 (byte-compile-body (cdr (cdr form)) t))
2449
2450 (defun byte-compile-prog2 (form)
2451 (byte-compile-form (nth 1 form) t)
2452 (byte-compile-form-do-effect (nth 2 form))
2453 (byte-compile-body (cdr (cdr (cdr form))) t))
2454
2455 (defmacro byte-compile-goto-if (cond discard tag)
2456 (` (byte-compile-goto
2457 (if (, cond)
2458 (if (, discard) 'byte-goto-if-not-nil 'byte-goto-if-not-nil-else-pop)
2459 (if (, discard) 'byte-goto-if-nil 'byte-goto-if-nil-else-pop))
2460 (, tag))))
2461
2462 (defun byte-compile-if (form)
2463 (byte-compile-form (car (cdr form)))
2464 (if (null (nthcdr 3 form))
2465 ;; No else-forms
2466 (let ((donetag (byte-compile-make-tag)))
2467 (byte-compile-goto-if nil for-effect donetag)
2468 (byte-compile-form (nth 2 form) for-effect)
2469 (byte-compile-out-tag donetag))
2470 (let ((donetag (byte-compile-make-tag)) (elsetag (byte-compile-make-tag)))
2471 (byte-compile-goto 'byte-goto-if-nil elsetag)
2472 (byte-compile-form (nth 2 form) for-effect)
2473 (byte-compile-goto 'byte-goto donetag)
2474 (byte-compile-out-tag elsetag)
2475 (byte-compile-body (cdr (cdr (cdr form))) for-effect)
2476 (byte-compile-out-tag donetag)))
2477 (setq for-effect nil))
2478
2479 (defun byte-compile-cond (clauses)
2480 (let ((donetag (byte-compile-make-tag))
2481 nexttag clause)
2482 (while (setq clauses (cdr clauses))
2483 (setq clause (car clauses))
2484 (cond ((or (eq (car clause) t)
2485 (and (eq (car-safe (car clause)) 'quote)
2486 (car-safe (cdr-safe (car clause)))))
2487 ;; Unconditional clause
2488 (setq clause (cons t clause)
2489 clauses nil))
2490 ((cdr clauses)
2491 (byte-compile-form (car clause))
2492 (if (null (cdr clause))
2493 ;; First clause is a singleton.
2494 (byte-compile-goto-if t for-effect donetag)
2495 (setq nexttag (byte-compile-make-tag))
2496 (byte-compile-goto 'byte-goto-if-nil nexttag)
2497 (byte-compile-body (cdr clause) for-effect)
2498 (byte-compile-goto 'byte-goto donetag)
2499 (byte-compile-out-tag nexttag)))))
2500 ;; Last clause
2501 (and (cdr clause) (not (eq (car clause) t))
2502 (progn (byte-compile-form (car clause))
2503 (byte-compile-goto-if nil for-effect donetag)
2504 (setq clause (cdr clause))))
2505 (byte-compile-body-do-effect clause)
2506 (byte-compile-out-tag donetag)))
2507
2508 (defun byte-compile-and (form)
2509 (let ((failtag (byte-compile-make-tag))
2510 (args (cdr form)))
2511 (if (null args)
2512 (byte-compile-form-do-effect t)
2513 (while (cdr args)
2514 (byte-compile-form (car args))
2515 (byte-compile-goto-if nil for-effect failtag)
2516 (setq args (cdr args)))
2517 (byte-compile-form-do-effect (car args))
2518 (byte-compile-out-tag failtag))))
2519
2520 (defun byte-compile-or (form)
2521 (let ((wintag (byte-compile-make-tag))
2522 (args (cdr form)))
2523 (if (null args)
2524 (byte-compile-form-do-effect nil)
2525 (while (cdr args)
2526 (byte-compile-form (car args))
2527 (byte-compile-goto-if t for-effect wintag)
2528 (setq args (cdr args)))
2529 (byte-compile-form-do-effect (car args))
2530 (byte-compile-out-tag wintag))))
2531
2532 (defun byte-compile-while (form)
2533 (let ((endtag (byte-compile-make-tag))
2534 (looptag (byte-compile-make-tag)))
2535 (byte-compile-out-tag looptag)
2536 (byte-compile-form (car (cdr form)))
2537 (byte-compile-goto-if nil for-effect endtag)
2538 (byte-compile-body (cdr (cdr form)) t)
2539 (byte-compile-goto 'byte-goto looptag)
2540 (byte-compile-out-tag endtag)
2541 (setq for-effect nil)))
2542
2543 (defun byte-compile-funcall (form)
2544 (mapcar 'byte-compile-form (cdr form))
2545 (byte-compile-out 'byte-call (length (cdr (cdr form)))))
2546
2547
2548 (defun byte-compile-let (form)
2549 ;; First compute the binding values in the old scope.
2550 (let ((varlist (car (cdr form))))
2551 (while varlist
2552 (if (consp (car varlist))
2553 (byte-compile-form (car (cdr (car varlist))))
2554 (byte-compile-push-constant nil))
2555 (setq varlist (cdr varlist))))
2556 (let ((byte-compile-bound-variables byte-compile-bound-variables) ;new scope
2557 (varlist (reverse (car (cdr form)))))
2558 (while varlist
2559 (byte-compile-variable-ref 'byte-varbind (if (consp (car varlist))
2560 (car (car varlist))
2561 (car varlist)))
2562 (setq varlist (cdr varlist)))
2563 (byte-compile-body-do-effect (cdr (cdr form)))
2564 (byte-compile-out 'byte-unbind (length (car (cdr form))))))
2565
2566 (defun byte-compile-let* (form)
2567 (let ((byte-compile-bound-variables byte-compile-bound-variables) ;new scope
2568 (varlist (copy-sequence (car (cdr form)))))
2569 (while varlist
2570 (if (atom (car varlist))
2571 (byte-compile-push-constant nil)
2572 (byte-compile-form (car (cdr (car varlist))))
2573 (setcar varlist (car (car varlist))))
2574 (byte-compile-variable-ref 'byte-varbind (car varlist))
2575 (setq varlist (cdr varlist)))
2576 (byte-compile-body-do-effect (cdr (cdr form)))
2577 (byte-compile-out 'byte-unbind (length (car (cdr form))))))
2578
2579
2580 (byte-defop-compiler-1 /= byte-compile-negated)
2581 (byte-defop-compiler-1 atom byte-compile-negated)
2582 (byte-defop-compiler-1 nlistp byte-compile-negated)
2583
2584 (put '/= 'byte-compile-negated-op '=)
2585 (put 'atom 'byte-compile-negated-op 'consp)
2586 (put 'nlistp 'byte-compile-negated-op 'listp)
2587
2588 (defun byte-compile-negated (form)
2589 (byte-compile-form-do-effect (byte-compile-negation-optimizer form)))
2590
2591 ;; Even when optimization is off, /= is optimized to (not (= ...)).
2592 (defun byte-compile-negation-optimizer (form)
2593 ;; an optimizer for forms where <form1> is less efficient than (not <form2>)
2594 (list 'not
2595 (cons (or (get (car form) 'byte-compile-negated-op)
2596 (error
2597 "Compiler error: `%s' has no `byte-compile-negated-op' property"
2598 (car form)))
2599 (cdr form))))
2600 \f
2601 ;;; other tricky macro-like special-forms
2602
2603 (byte-defop-compiler-1 catch)
2604 (byte-defop-compiler-1 unwind-protect)
2605 (byte-defop-compiler-1 condition-case)
2606 (byte-defop-compiler-1 save-excursion)
2607 (byte-defop-compiler-1 save-restriction)
2608 (byte-defop-compiler-1 save-window-excursion)
2609 (byte-defop-compiler-1 with-output-to-temp-buffer)
2610
2611 (defun byte-compile-catch (form)
2612 (byte-compile-form (car (cdr form)))
2613 (byte-compile-push-constant
2614 (byte-compile-top-level (cons 'progn (cdr (cdr form))) for-effect))
2615 (byte-compile-out 'byte-catch 0))
2616
2617 (defun byte-compile-unwind-protect (form)
2618 (byte-compile-push-constant
2619 (byte-compile-top-level-body (cdr (cdr form)) t))
2620 (byte-compile-out 'byte-unwind-protect 0)
2621 (byte-compile-form-do-effect (car (cdr form)))
2622 (byte-compile-out 'byte-unbind 1))
2623
2624 (defun byte-compile-condition-case (form)
2625 (let* ((var (nth 1 form))
2626 (byte-compile-bound-variables
2627 (if var (cons var byte-compile-bound-variables)
2628 byte-compile-bound-variables)))
2629 (or (symbolp var)
2630 (byte-compile-warn
2631 "%s is not a variable-name or nil (in condition-case)" var))
2632 (byte-compile-push-constant var)
2633 (byte-compile-push-constant (byte-compile-top-level
2634 (nth 2 form) for-effect))
2635 (let ((clauses (cdr (cdr (cdr form))))
2636 compiled-clauses)
2637 (while clauses
2638 (let ((clause (car clauses)))
2639 (setq compiled-clauses
2640 (cons (cons (car clause)
2641 (byte-compile-top-level-body
2642 (cdr clause) for-effect))
2643 compiled-clauses)))
2644 (setq clauses (cdr clauses)))
2645 (byte-compile-push-constant (nreverse compiled-clauses)))
2646 (byte-compile-out 'byte-condition-case 0)))
2647
2648
2649 (defun byte-compile-save-excursion (form)
2650 (byte-compile-out 'byte-save-excursion 0)
2651 (byte-compile-body-do-effect (cdr form))
2652 (byte-compile-out 'byte-unbind 1))
2653
2654 (defun byte-compile-save-restriction (form)
2655 (byte-compile-out 'byte-save-restriction 0)
2656 (byte-compile-body-do-effect (cdr form))
2657 (byte-compile-out 'byte-unbind 1))
2658
2659 (defun byte-compile-save-window-excursion (form)
2660 (byte-compile-push-constant
2661 (byte-compile-top-level-body (cdr form) for-effect))
2662 (byte-compile-out 'byte-save-window-excursion 0))
2663
2664 (defun byte-compile-with-output-to-temp-buffer (form)
2665 (byte-compile-form (car (cdr form)))
2666 (byte-compile-out 'byte-temp-output-buffer-setup 0)
2667 (byte-compile-body (cdr (cdr form)))
2668 (byte-compile-out 'byte-temp-output-buffer-show 0))
2669
2670 \f
2671 ;;; top-level forms elsewhere
2672
2673 (byte-defop-compiler-1 defun)
2674 (byte-defop-compiler-1 defmacro)
2675 (byte-defop-compiler-1 defvar)
2676 (byte-defop-compiler-1 defconst byte-compile-defvar)
2677 (byte-defop-compiler-1 autoload)
2678 (byte-defop-compiler-1 lambda byte-compile-lambda-form)
2679
2680 (defun byte-compile-defun (form)
2681 ;; This is not used for file-level defuns with doc strings.
2682 (byte-compile-two-args ; Use this to avoid byte-compile-fset's warning.
2683 (list 'fset (list 'quote (nth 1 form))
2684 (byte-compile-byte-code-maker
2685 (byte-compile-lambda (cons 'lambda (cdr (cdr form)))))))
2686 (byte-compile-discard)
2687 (byte-compile-constant (nth 1 form)))
2688
2689 (defun byte-compile-defmacro (form)
2690 ;; This is not used for file-level defmacros with doc strings.
2691 (byte-compile-body-do-effect
2692 (list (list 'fset (list 'quote (nth 1 form))
2693 (let ((code (byte-compile-byte-code-maker
2694 (byte-compile-lambda
2695 (cons 'lambda (cdr (cdr form)))))))
2696 (if (eq (car-safe code) 'make-byte-code)
2697 (list 'cons ''macro code)
2698 (list 'quote (cons 'macro (eval code))))))
2699 (list 'quote (nth 1 form)))))
2700
2701 (defun byte-compile-defvar (form)
2702 ;; This is not used for file-level defvar/consts with doc strings.
2703 (let ((var (nth 1 form))
2704 (value (nth 2 form))
2705 (string (nth 3 form)))
2706 (if (memq 'free-vars byte-compile-warnings)
2707 (setq byte-compile-bound-variables
2708 (cons var byte-compile-bound-variables)))
2709 (byte-compile-body-do-effect
2710 (list (if (cdr (cdr form))
2711 (if (eq (car form) 'defconst)
2712 (list 'setq var value)
2713 (list 'or (list 'boundp (list 'quote var))
2714 (list 'setq var value))))
2715 (if string
2716 (list 'put (list 'quote var) ''variable-documentation string))
2717 (list 'quote var)))))
2718
2719 (defun byte-compile-autoload (form)
2720 (and (byte-compile-constp (nth 1 form))
2721 (byte-compile-constp (nth 5 form))
2722 (eval (nth 5 form)) ; macro-p
2723 (not (fboundp (eval (nth 1 form))))
2724 (byte-compile-warn
2725 "The compiler ignores `autoload' except at top level. You should
2726 probably put the autoload of the macro `%s' at top-level."
2727 (eval (nth 1 form))))
2728 (byte-compile-normal-call form))
2729
2730 ;; Lambda's in valid places are handled as special cases by various code.
2731 ;; The ones that remain are errors.
2732 (defun byte-compile-lambda-form (form)
2733 (error "`lambda' used as function name is invalid"))
2734
2735 \f
2736 ;;; tags
2737
2738 ;; Note: Most operations will strip off the 'TAG, but it speeds up
2739 ;; optimization to have the 'TAG as a part of the tag.
2740 ;; Tags will be (TAG . (tag-number . stack-depth)).
2741 (defun byte-compile-make-tag ()
2742 (list 'TAG (setq byte-compile-tag-number (1+ byte-compile-tag-number))))
2743
2744
2745 (defun byte-compile-out-tag (tag)
2746 (setq byte-compile-output (cons tag byte-compile-output))
2747 (if (cdr (cdr tag))
2748 (progn
2749 ;; ## remove this someday
2750 (and byte-compile-depth
2751 (not (= (cdr (cdr tag)) byte-compile-depth))
2752 (error "Compiler bug: depth conflict at tag %d" (car (cdr tag))))
2753 (setq byte-compile-depth (cdr (cdr tag))))
2754 (setcdr (cdr tag) byte-compile-depth)))
2755
2756 (defun byte-compile-goto (opcode tag)
2757 (setq byte-compile-output (cons (cons opcode tag) byte-compile-output))
2758 (setcdr (cdr tag) (if (memq opcode byte-goto-always-pop-ops)
2759 (1- byte-compile-depth)
2760 byte-compile-depth))
2761 (setq byte-compile-depth (and (not (eq opcode 'byte-goto))
2762 (1- byte-compile-depth))))
2763
2764 (defun byte-compile-out (opcode offset)
2765 (setq byte-compile-output (cons (cons opcode offset) byte-compile-output))
2766 (cond ((eq opcode 'byte-call)
2767 (setq byte-compile-depth (- byte-compile-depth offset)))
2768 ((eq opcode 'byte-return)
2769 ;; This is actually an unnecessary case, because there should be
2770 ;; no more opcodes behind byte-return.
2771 (setq byte-compile-depth nil))
2772 (t
2773 (setq byte-compile-depth (+ byte-compile-depth
2774 (or (aref byte-stack+-info
2775 (symbol-value opcode))
2776 (- (1- offset))))
2777 byte-compile-maxdepth (max byte-compile-depth
2778 byte-compile-maxdepth))))
2779 ;;(if (< byte-compile-depth 0) (error "Compiler error: stack underflow"))
2780 )
2781
2782 \f
2783 ;;; call tree stuff
2784
2785 (defun byte-compile-annotate-call-tree (form)
2786 (let (entry)
2787 ;; annotate the current call
2788 (if (setq entry (assq (car form) byte-compile-call-tree))
2789 (or (memq byte-compile-current-form (nth 1 entry)) ;callers
2790 (setcar (cdr entry)
2791 (cons byte-compile-current-form (nth 1 entry))))
2792 (setq byte-compile-call-tree
2793 (cons (list (car form) (list byte-compile-current-form) nil)
2794 byte-compile-call-tree)))
2795 ;; annotate the current function
2796 (if (setq entry (assq byte-compile-current-form byte-compile-call-tree))
2797 (or (memq (car form) (nth 2 entry)) ;called
2798 (setcar (cdr (cdr entry))
2799 (cons (car form) (nth 2 entry))))
2800 (setq byte-compile-call-tree
2801 (cons (list byte-compile-current-form nil (list (car form)))
2802 byte-compile-call-tree)))
2803 ))
2804
2805 ;; Renamed from byte-compile-report-call-tree
2806 ;; to avoid interfering with completion of byte-compile-file.
2807 ;;;###autoload
2808 (defun display-call-tree (&optional filename)
2809 "Display a call graph of a specified file.
2810 This lists which functions have been called, what functions called
2811 them, and what functions they call. The list includes all functions
2812 whose definitions have been compiled in this Emacs session, as well as
2813 all functions called by those functions.
2814
2815 The call graph does not include macros, inline functions, or
2816 primitives that the byte-code interpreter knows about directly \(eq,
2817 cons, etc.\).
2818
2819 The call tree also lists those functions which are not known to be called
2820 \(that is, to which no calls have been compiled\), and which cannot be
2821 invoked interactively."
2822 (interactive)
2823 (message "Generating call tree...")
2824 (with-output-to-temp-buffer "*Call-Tree*"
2825 (set-buffer "*Call-Tree*")
2826 (erase-buffer)
2827 (message "Generating call tree (sorting on %s)..."
2828 byte-compile-call-tree-sort)
2829 (insert "Call tree for "
2830 (cond ((null byte-compile-current-file) (or filename "???"))
2831 ((stringp byte-compile-current-file)
2832 byte-compile-current-file)
2833 (t (buffer-name byte-compile-current-file)))
2834 " sorted on "
2835 (prin1-to-string byte-compile-call-tree-sort)
2836 ":\n\n")
2837 (if byte-compile-call-tree-sort
2838 (setq byte-compile-call-tree
2839 (sort byte-compile-call-tree
2840 (cond ((eq byte-compile-call-tree-sort 'callers)
2841 (function (lambda (x y) (< (length (nth 1 x))
2842 (length (nth 1 y))))))
2843 ((eq byte-compile-call-tree-sort 'calls)
2844 (function (lambda (x y) (< (length (nth 2 x))
2845 (length (nth 2 y))))))
2846 ((eq byte-compile-call-tree-sort 'calls+callers)
2847 (function (lambda (x y) (< (+ (length (nth 1 x))
2848 (length (nth 2 x)))
2849 (+ (length (nth 1 y))
2850 (length (nth 2 y)))))))
2851 ((eq byte-compile-call-tree-sort 'name)
2852 (function (lambda (x y) (string< (car x)
2853 (car y)))))
2854 (t (error "`byte-compile-call-tree-sort': `%s' - unknown sort mode"
2855 byte-compile-call-tree-sort))))))
2856 (message "Generating call tree...")
2857 (let ((rest byte-compile-call-tree)
2858 (b (current-buffer))
2859 f p
2860 callers calls)
2861 (while rest
2862 (prin1 (car (car rest)) b)
2863 (setq callers (nth 1 (car rest))
2864 calls (nth 2 (car rest)))
2865 (insert "\t"
2866 (cond ((not (fboundp (setq f (car (car rest)))))
2867 (if (null f)
2868 " <top level>";; shouldn't insert nil then, actually -sk
2869 " <not defined>"))
2870 ((subrp (setq f (symbol-function f)))
2871 " <subr>")
2872 ((symbolp f)
2873 (format " ==> %s" f))
2874 ((byte-code-function-p f)
2875 "<compiled function>")
2876 ((not (consp f))
2877 "<malformed function>")
2878 ((eq 'macro (car f))
2879 (if (or (byte-code-function-p (cdr f))
2880 (assq 'byte-code (cdr (cdr (cdr f)))))
2881 " <compiled macro>"
2882 " <macro>"))
2883 ((assq 'byte-code (cdr (cdr f)))
2884 "<compiled lambda>")
2885 ((eq 'lambda (car f))
2886 "<function>")
2887 (t "???"))
2888 (format " (%d callers + %d calls = %d)"
2889 ;; Does the optimizer eliminate common subexpressions?-sk
2890 (length callers)
2891 (length calls)
2892 (+ (length callers) (length calls)))
2893 "\n")
2894 (if callers
2895 (progn
2896 (insert " called by:\n")
2897 (setq p (point))
2898 (insert " " (if (car callers)
2899 (mapconcat 'symbol-name callers ", ")
2900 "<top level>"))
2901 (let ((fill-prefix " "))
2902 (fill-region-as-paragraph p (point)))))
2903 (if calls
2904 (progn
2905 (insert " calls:\n")
2906 (setq p (point))
2907 (insert " " (mapconcat 'symbol-name calls ", "))
2908 (let ((fill-prefix " "))
2909 (fill-region-as-paragraph p (point)))))
2910 (insert "\n")
2911 (setq rest (cdr rest)))
2912
2913 (message "Generating call tree...(finding uncalled functions...)")
2914 (setq rest byte-compile-call-tree)
2915 (let ((uncalled nil))
2916 (while rest
2917 (or (nth 1 (car rest))
2918 (null (setq f (car (car rest))))
2919 (byte-compile-fdefinition f t)
2920 (commandp (byte-compile-fdefinition f nil))
2921 (setq uncalled (cons f uncalled)))
2922 (setq rest (cdr rest)))
2923 (if uncalled
2924 (let ((fill-prefix " "))
2925 (insert "Noninteractive functions not known to be called:\n ")
2926 (setq p (point))
2927 (insert (mapconcat 'symbol-name (nreverse uncalled) ", "))
2928 (fill-region-as-paragraph p (point)))))
2929 )
2930 (message "Generating call tree...done.")
2931 ))
2932
2933 \f
2934 ;;; by crl@newton.purdue.edu
2935 ;;; Only works noninteractively.
2936 ;;;###autoload
2937 (defun batch-byte-compile ()
2938 "Run `byte-compile-file' on the files remaining on the command line.
2939 Use this from the command line, with `-batch';
2940 it won't work in an interactive Emacs.
2941 Each file is processed even if an error occurred previously.
2942 For example, invoke \"emacs -batch -f batch-byte-compile $emacs/ ~/*.el\""
2943 ;; command-line-args-left is what is left of the command line (from startup.el)
2944 (defvar command-line-args-left) ;Avoid 'free variable' warning
2945 (if (not noninteractive)
2946 (error "`batch-byte-compile' is to be used only with -batch"))
2947 (let ((error nil))
2948 (while command-line-args-left
2949 (if (file-directory-p (expand-file-name (car command-line-args-left)))
2950 (let ((files (directory-files (car command-line-args-left)))
2951 source dest)
2952 (while files
2953 (if (and (string-match emacs-lisp-file-regexp (car files))
2954 (not (auto-save-file-name-p (car files)))
2955 (setq source (expand-file-name (car files)
2956 (car command-line-args-left)))
2957 (setq dest (byte-compile-dest-file source))
2958 (file-exists-p dest)
2959 (file-newer-than-file-p source dest))
2960 (if (null (batch-byte-compile-file source))
2961 (setq error t)))
2962 (setq files (cdr files))))
2963 (if (null (batch-byte-compile-file (car command-line-args-left)))
2964 (setq error t)))
2965 (setq command-line-args-left (cdr command-line-args-left)))
2966 (message "Done")
2967 (kill-emacs (if error 1 0))))
2968
2969 (defun batch-byte-compile-file (file)
2970 (condition-case err
2971 (progn (byte-compile-file file) t)
2972 (error
2973 (message (if (cdr err)
2974 ">>Error occurred processing %s: %s (%s)"
2975 ">>Error occurred processing %s: %s")
2976 file
2977 (get (car err) 'error-message)
2978 (prin1-to-string (cdr err)))
2979 nil)))
2980
2981
2982 (make-obsolete 'mod '%)
2983 (make-obsolete 'dot 'point)
2984 (make-obsolete 'dot-max 'point-max)
2985 (make-obsolete 'dot-min 'point-min)
2986 (make-obsolete 'dot-marker 'point-marker)
2987
2988 (make-obsolete 'buffer-flush-undo 'buffer-disable-undo)
2989 (make-obsolete 'baud-rate "use the baud-rate variable instead")
2990 (make-obsolete 'compiled-function-p 'byte-code-function-p)
2991 (make-obsolete-variable 'auto-fill-hook 'auto-fill-function)
2992 (make-obsolete-variable 'blink-paren-hook 'blink-paren-function)
2993 (make-obsolete-variable 'lisp-indent-hook 'lisp-indent-function)
2994 (make-obsolete-variable 'temp-buffer-show-hook
2995 'temp-buffer-show-function)
2996 (make-obsolete-variable 'inhibit-local-variables
2997 "use enable-local-variables (with the reversed sense.)")
2998 (make-obsolete-variable 'unread-command-char
2999 "use unread-command-events instead. That variable is a list of events to reread, so it now uses nil to mean `no event', instead of -1.")
3000 (make-obsolete-variable 'unread-command-event
3001 "use unread-command-events; this is now a list of events.")
3002 (make-obsolete-variable 'suspend-hook 'suspend-hooks)
3003 (make-obsolete-variable 'comment-indent-hook 'comment-indent-function)
3004
3005 (provide 'byte-compile)
3006
3007 \f
3008 ;;; report metering (see the hacks in bytecode.c)
3009
3010 (defun byte-compile-report-ops ()
3011 (defvar byte-code-meter)
3012 (with-output-to-temp-buffer "*Meter*"
3013 (set-buffer "*Meter*")
3014 (let ((i 0) n op off)
3015 (while (< i 256)
3016 (setq n (aref (aref byte-code-meter 0) i)
3017 off nil)
3018 (if t ;(not (zerop n))
3019 (progn
3020 (setq op i)
3021 (setq off nil)
3022 (cond ((< op byte-nth)
3023 (setq off (logand op 7))
3024 (setq op (logand op 248)))
3025 ((>= op byte-constant)
3026 (setq off (- op byte-constant)
3027 op byte-constant)))
3028 (setq op (aref byte-code-vector op))
3029 (insert (format "%-4d" i))
3030 (insert (symbol-name op))
3031 (if off (insert " [" (int-to-string off) "]"))
3032 (indent-to 40)
3033 (insert (int-to-string n) "\n")))
3034 (setq i (1+ i))))))
3035 \f
3036 ;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when bytecomp compiles
3037 ;; itself, compile some of its most used recursive functions (at load time).
3038 ;;
3039 (eval-when-compile
3040 (or (byte-code-function-p (symbol-function 'byte-compile-form))
3041 (assq 'byte-code (symbol-function 'byte-compile-form))
3042 (let ((byte-optimize nil) ; do it fast
3043 (byte-compile-warnings nil))
3044 (mapcar '(lambda (x)
3045 (or noninteractive (message "compiling %s..." x))
3046 (byte-compile x)
3047 (or noninteractive (message "compiling %s...done" x)))
3048 '(byte-compile-normal-call
3049 byte-compile-form
3050 byte-compile-body
3051 ;; Inserted some more than necessary, to speed it up.
3052 byte-compile-top-level
3053 byte-compile-out-toplevel
3054 byte-compile-constant
3055 byte-compile-variable-ref))))
3056 nil)
3057
3058 ;;; bytecomp.el ends here