comparison lisp/bytecomp.el @ 209:41ff10fd062f r20-4b3

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