root/trunk/lisp/emacs-lisp/bytecomp.el

Revision 4220, 159.9 kB (checked in by miyoshi, 9 months ago)

Sync up with Emacs22.2.

  • Property svn:eol-style set to LF
Line 
1 ;;; bytecomp.el --- compilation of Lisp code into byte code
2
3 ;; Copyright (C) 1985, 1986, 1987, 1992, 1994, 1998, 2000, 2001, 2002,
4 ;;   2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Jamie Zawinski <jwz@lucid.com>
7 ;;      Hallvard Furuseth <hbf@ulrik.uio.no>
8 ;; Maintainer: FSF
9 ;; Keywords: lisp
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27
28 ;;; Commentary:
29
30 ;; The Emacs Lisp byte compiler.  This crunches lisp source into a sort
31 ;; of p-code (`lapcode') which takes up less space and can be interpreted
32 ;; faster.  [`LAP' == `Lisp Assembly Program'.]
33 ;; The user entry points are byte-compile-file and byte-recompile-directory.
34
35 ;;; Code:
36
37 ;; ========================================================================
38 ;; Entry points:
39 ;;      byte-recompile-directory, byte-compile-file,
40 ;;     batch-byte-compile, batch-byte-recompile-directory,
41 ;;      byte-compile, compile-defun,
42 ;;      display-call-tree
43 ;; (byte-compile-buffer and byte-compile-and-load-file were turned off
44 ;;  because they are not terribly useful and get in the way of completion.)
45
46 ;; This version of the byte compiler has the following improvements:
47 ;;  + optimization of compiled code:
48 ;;    - removal of unreachable code;
49 ;;    - removal of calls to side-effectless functions whose return-value
50 ;;      is unused;
51 ;;    - compile-time evaluation of safe constant forms, such as (consp nil)
52 ;;      and (ash 1 6);
53 ;;    - open-coding of literal lambdas;
54 ;;    - peephole optimization of emitted code;
55 ;;    - trivial functions are left uncompiled for speed.
56 ;;  + support for inline functions;
57 ;;  + compile-time evaluation of arbitrary expressions;
58 ;;  + compile-time warning messages for:
59 ;;    - functions being redefined with incompatible arglists;
60 ;;    - functions being redefined as macros, or vice-versa;
61 ;;    - functions or macros defined multiple times in the same file;
62 ;;    - functions being called with the incorrect number of arguments;
63 ;;    - functions being called which are not defined globally, in the
64 ;;      file, or as autoloads;
65 ;;    - assignment and reference of undeclared free variables;
66 ;;    - various syntax errors;
67 ;;  + correct compilation of nested defuns, defmacros, defvars and defsubsts;
68 ;;  + correct compilation of top-level uses of macros;
69 ;;  + the ability to generate a histogram of functions called.
70
71 ;; User customization variables:
72 ;;
73 ;; byte-compile-verbose Whether to report the function currently being
74 ;;                              compiled in the echo area;
75 ;; byte-optimize                Whether to do optimizations; this may be
76 ;;                              t, nil, 'source, or 'byte;
77 ;; byte-optimize-log            Whether to report (in excruciating detail)
78 ;;                              exactly which optimizations have been made.
79 ;;                              This may be t, nil, 'source, or 'byte;
80 ;; byte-compile-error-on-warn   Whether to stop compilation when a warning is
81 ;;                              produced;
82 ;; byte-compile-delete-errors   Whether the optimizer may delete calls or
83 ;;                              variable references that are side-effect-free
84 ;;                              except that they may return an error.
85 ;; byte-compile-generate-call-tree      Whether to generate a histogram of
86 ;;                              function calls.  This can be useful for
87 ;;                              finding unused functions, as well as simple
88 ;;                              performance metering.
89 ;; byte-compile-warnings        List of warnings to issue, or t.  May contain
90 ;;                              `free-vars' (references to variables not in the
91 ;;                                           current lexical scope)
92 ;;                              `unresolved' (calls to unknown functions)
93 ;;                              `callargs'  (lambda calls with args that don't
94 ;;                                           match the lambda's definition)
95 ;;                              `redefine'  (function cell redefined from
96 ;;                                           a macro to a lambda or vice versa,
97 ;;                                           or redefined to take other args)
98 ;;                              `obsolete'  (obsolete variables and functions)
99 ;;                              `noruntime' (calls to functions only defined
100 ;;                                           within `eval-when-compile')
101 ;;                              `cl-warnings' (calls to CL functions)
102 ;;                              `interactive-only' (calls to commands that are
103 ;;                                                 not good to call from Lisp)
104 ;; byte-compile-compatibility   Whether the compiler should
105 ;;                              generate .elc files which can be loaded into
106 ;;                              generic emacs 18.
107 ;; emacs-lisp-file-regexp       Regexp for the extension of source-files;
108 ;;                              see also the function byte-compile-dest-file.
109
110 ;; New Features:
111 ;;
112 ;;  o   The form `defsubst' is just like `defun', except that the function
113 ;;      generated will be open-coded in compiled code which uses it.  This
114 ;;      means that no function call will be generated, it will simply be
115 ;;      spliced in.  Lisp functions calls are very slow, so this can be a
116 ;;      big win.
117 ;;
118 ;;      You can generally accomplish the same thing with `defmacro', but in
119 ;;      that case, the defined procedure can't be used as an argument to
120 ;;      mapcar, etc.
121 ;;
122 ;;  o   You can also open-code one particular call to a function without
123 ;;      open-coding all calls.  Use the 'inline' form to do this, like so:
124 ;;
125 ;;              (inline (foo 1 2 3))    ;; `foo' will be open-coded
126 ;;      or...
127 ;;              (inline                 ;;  `foo' and `baz' will be
128 ;;               (foo 1 2 3 (bar 5))    ;; open-coded, but `bar' will not.
129 ;;               (baz 0))
130 ;;
131 ;;  o   It is possible to open-code a function in the same file it is defined
132 ;;      in without having to load that file before compiling it.  The
133 ;;      byte-compiler has been modified to remember function definitions in
134 ;;      the compilation environment in the same way that it remembers macro
135 ;;      definitions.
136 ;;
137 ;;  o  Forms like ((lambda ...) ...) are open-coded.
138 ;;
139 ;;  o  The form `eval-when-compile' is like progn, except that the body
140 ;;     is evaluated at compile-time.  When it appears at top-level, this
141 ;;     is analogous to the Common Lisp idiom (eval-when (compile) ...).
142 ;;     When it does not appear at top-level, it is similar to the
143 ;;     Common Lisp #. reader macro (but not in interpreted code).
144 ;;
145 ;;  o  The form `eval-and-compile' is similar to eval-when-compile, but
146 ;;      the whole form is evalled both at compile-time and at run-time.
147 ;;
148 ;;  o  The command compile-defun is analogous to eval-defun.
149 ;;
150 ;;  o  If you run byte-compile-file on a filename which is visited in a
151 ;;     buffer, and that buffer is modified, you are asked whether you want
152 ;;     to save the buffer before compiling.
153 ;;
154 ;;  o  byte-compiled files now start with the string `;ELC'.
155 ;;     Some versions of `file' can be customized to recognize that.
156
157 (require 'backquote)
158
159 (or (fboundp 'defsubst)
160     ;; This really ought to be loaded already!
161     (load "byte-run"))
162
163 ;; The feature of compiling in a specific target Emacs version
164 ;; has been turned off because compile time options are a bad idea.
165 (defmacro byte-compile-single-version () nil)
166 (defmacro byte-compile-version-cond (cond) cond)
167
168 ;; The crud you see scattered through this file of the form
169 ;;   (or (and (boundp 'epoch::version) epoch::version)
170 ;;        (string-lessp emacs-version "19"))
171 ;; is because the Epoch folks couldn't be bothered to follow the
172 ;; normal emacs version numbering convention.
173
174 ;; (if (byte-compile-version-cond
175 ;;      (or (and (boundp 'epoch::version) epoch::version)
176 ;;       (string-lessp emacs-version "19")))
177 ;;     (progn
178 ;;       ;; emacs-18 compatibility.
179 ;;       (defvar baud-rate (baud-rate)) ;Define baud-rate if it's undefined
180 ;;
181 ;;       (if (byte-compile-single-version)
182 ;;        (defmacro byte-code-function-p (x) "Emacs 18 doesn't have these." nil)
183 ;;      (defun byte-code-function-p (x) "Emacs 18 doesn't have these." nil))
184 ;;
185 ;;       (or (and (fboundp 'member)
186 ;;             ;; avoid using someone else's possibly bogus definition of this.
187 ;;             (subrp (symbol-function 'member)))
188 ;;        (defun member (elt list)
189 ;;          "like memq, but uses equal instead of eq.  In v19, this is a subr."
190 ;;          (while (and list (not (equal elt (car list))))
191 ;;            (setq list (cdr list)))
192 ;;          list))))
193
194
195 (defgroup bytecomp nil
196   "Emacs Lisp byte-compiler."
197   :group 'lisp)
198
199 (defcustom emacs-lisp-file-regexp (if (eq system-type 'vax-vms)
200                                       "\\.EL\\(;[0-9]+\\)?$"
201                                     "\\.el$")
202   "*Regexp which matches Emacs Lisp source files.
203 You may want to redefine the function `byte-compile-dest-file'
204 if you change this variable."
205   :group 'bytecomp
206   :type 'regexp)
207
208 ;; This enables file name handlers such as jka-compr
209 ;; to remove parts of the file name that should not be copied
210 ;; through to the output file name.
211 (defun byte-compiler-base-file-name (filename)
212   (let ((handler (find-file-name-handler filename
213                                          'byte-compiler-base-file-name)))
214     (if handler
215         (funcall handler 'byte-compiler-base-file-name filename)
216       filename)))
217
218 (or (fboundp 'byte-compile-dest-file)
219     ;; The user may want to redefine this along with emacs-lisp-file-regexp,
220     ;; so only define it if it is undefined.
221     (defun byte-compile-dest-file (filename)
222       "Convert an Emacs Lisp source file name to a compiled file name.
223 If FILENAME matches `emacs-lisp-file-regexp' (by default, files
224 with the extension `.el'), add `c' to it; otherwise add `.elc'."
225       (setq filename (byte-compiler-base-file-name filename))
226       (setq filename (file-name-sans-versions filename))
227       (cond ((eq system-type 'vax-vms)
228              (concat (substring filename 0 (string-match ";" filename)) "c"))
229             ((string-match emacs-lisp-file-regexp filename)
230              (concat (substring filename 0 (match-beginning 0)) ".elc"))
231             (t (concat filename ".elc")))))
232
233 ;; This can be the 'byte-compile property of any symbol.
234 (autoload 'byte-compile-inline-expand "byte-opt")
235
236 ;; This is the entrypoint to the lapcode optimizer pass1.
237 (autoload 'byte-optimize-form "byte-opt")
238 ;; This is the entrypoint to the lapcode optimizer pass2.
239 (autoload 'byte-optimize-lapcode "byte-opt")
240 (autoload 'byte-compile-unfold-lambda "byte-opt")
241
242 ;; This is the entry point to the decompiler, which is used by the
243 ;; disassembler.  The disassembler just requires 'byte-compile, but
244 ;; that doesn't define this function, so this seems to be a reasonable
245 ;; thing to do.
246 (autoload 'byte-decompile-bytecode "byte-opt")
247
248 (defcustom byte-compile-verbose
249   (and (not noninteractive) (> baud-rate search-slow-speed))
250   "*Non-nil means print messages describing progress of byte-compiler."
251   :group 'bytecomp
252   :type 'boolean)
253
254 (defcustom byte-compile-compatibility nil
255   "*Non-nil means generate output that can run in Emacs 18.
256 This only means that it can run in principle, if it doesn't require
257 facilities that have been added more recently."
258   :group 'bytecomp
259   :type 'boolean)
260
261 ;; (defvar byte-compile-generate-emacs19-bytecodes
262 ;;         (not (or (and (boundp 'epoch::version) epoch::version)
263 ;;               (string-lessp emacs-version "19")))
264 ;;   "*If this is true, then the byte-compiler will generate bytecode which
265 ;; makes use of byte-ops which are present only in Emacs 19.  Code generated
266 ;; this way can never be run in Emacs 18, and may even cause it to crash.")
267
268 (defcustom byte-optimize t
269   "*Enable optimization in the byte compiler.
270 Possible values are:
271   nil      - no optimization
272   t        - all optimizations
273   `source' - source-level optimizations only
274   `byte'   - code-level optimizations only"
275   :group 'bytecomp
276   :type '(choice (const :tag "none" nil)
277                  (const :tag "all" t)
278                  (const :tag "source-level" source)
279                  (const :tag "byte-level" byte)))
280
281 (defcustom byte-compile-delete-errors nil
282   "*If non-nil, the optimizer may delete forms that may signal an error.
283 This includes variable references and calls to functions such as `car'."
284   :group 'bytecomp
285   :type 'boolean)
286
287 (defvar byte-compile-dynamic nil
288   "If non-nil, compile function bodies so they load lazily.
289 They are hidden in comments in the compiled file,
290 and each one is brought into core when the
291 function is called.
292
293 To enable this option, make it a file-local variable
294 in the source file you want it to apply to.
295 For example, add  -*-byte-compile-dynamic: t;-*- on the first line.
296
297 When this option is true, if you load the compiled file and then move it,
298 the functions you loaded will not be able to run.")
299 ;;;###autoload(put 'byte-compile-dynamic 'safe-local-variable 'booleanp)
300
301 (defvar byte-compile-disable-print-circle nil
302   "If non-nil, disable `print-circle' on printing a byte-compiled code.")
303 ;;;###autoload(put 'byte-compile-disable-print-circle 'safe-local-variable 'booleanp)
304
305 (defcustom byte-compile-dynamic-docstrings t
306   "*If non-nil, compile doc strings for lazy access.
307 We bury the doc strings of functions and variables
308 inside comments in the file, and bring them into core only when they
309 are actually needed.
310
311 When this option is true, if you load the compiled file and then move it,
312 you won't be able to find the documentation of anything in that file.
313
314 To disable this option for a certain file, make it a file-local variable
315 in the source file.  For example, add this to the first line:
316   -*-byte-compile-dynamic-docstrings:nil;-*-
317 You can also set the variable globally.
318
319 This option is enabled by default because it reduces Emacs memory usage."
320   :group 'bytecomp
321   :type 'boolean)
322 ;;;###autoload(put 'byte-compile-dynamic-docstrings 'safe-local-variable 'booleanp)
323
324 (defcustom byte-optimize-log nil
325   "*If true, the byte-compiler will log its optimizations into *Compile-Log*.
326 If this is 'source, then only source-level optimizations will be logged.
327 If it is 'byte, then only byte-level optimizations will be logged."
328   :group 'bytecomp
329   :type '(choice (const :tag "none" nil)
330                  (const :tag "all" t)
331                  (const :tag "source-level" source)
332                  (const :tag "byte-level" byte)))
333
334 (defcustom byte-compile-error-on-warn nil
335   "*If true, the byte-compiler reports warnings with `error'."
336   :group 'bytecomp
337   :type 'boolean)
338
339 (defconst byte-compile-warning-types
340   '(redefine callargs free-vars unresolved
341              obsolete noruntime cl-functions interactive-only)
342   "The list of warning types used when `byte-compile-warnings' is t.")
343 (defcustom byte-compile-warnings t
344   "*List of warnings that the byte-compiler should issue (t for all).
345
346 Elements of the list may be:
347
348   free-vars   references to variables not in the current lexical scope.
349   unresolved  calls to unknown functions.
350   callargs    function calls with args that don't match the definition.
351   redefine    function name redefined from a macro to ordinary function or vice
352               versa, or redefined to take a different number of arguments.
353   obsolete    obsolete variables and functions.
354   noruntime   functions that may not be defined at runtime (typically
355               defined only under `eval-when-compile').
356   cl-functions    calls to runtime functions from the CL package (as
357                   distinguished from macros and aliases).
358   interactive-only
359               commands that normally shouldn't be called from Lisp code."
360   :group 'bytecomp
361   :type `(choice (const :tag "All" t)
362                  (set :menu-tag "Some"
363                       (const free-vars) (const unresolved)
364                       (const callargs) (const redefine)
365                       (const obsolete) (const noruntime)
366                       (const cl-functions) (const interactive-only))))
367 ;;;###autoload(put 'byte-compile-warnings 'safe-local-variable 'byte-compile-warnings-safe-p)
368
369 ;;;###autoload
370 (defun byte-compile-warnings-safe-p (x)
371   (or (booleanp x)
372       (and (listp x)
373            (equal (mapcar
374                    (lambda (e)
375                      (when (memq e '(free-vars unresolved
376                                      callargs redefine
377                                      obsolete noruntime
378                                      cl-functions interactive-only))
379                        e))
380                    x)
381                   x))))
382
383 (defvar byte-compile-interactive-only-functions
384   '(beginning-of-buffer end-of-buffer replace-string replace-regexp
385     insert-file insert-buffer insert-file-literally)
386   "List of commands that are not meant to be called from Lisp.")
387
388 (defvar byte-compile-not-obsolete-var nil
389   "If non-nil, this is a variable that shouldn't be reported as obsolete.")
390
391 (defcustom byte-compile-generate-call-tree nil
392   "*Non-nil means collect call-graph information when compiling.
393 This records which functions were called and from where.
394 If the value is t, compilation displays the call graph when it finishes.
395 If the value is neither t nor nil, compilation asks you whether to display
396 the graph.
397
398 The call tree only lists functions called, not macros used. Those functions
399 which the byte-code interpreter knows about directly (eq, cons, etc.) are
400 not reported.
401
402 The call tree also lists those functions which are not known to be called
403 \(that is, to which no calls have been compiled).  Functions which can be
404 invoked interactively are excluded from this list."
405   :group 'bytecomp
406   :type '(choice (const :tag "Yes" t) (const :tag "No" nil)
407                  (other :tag "Ask" lambda)))
408
409 (defvar byte-compile-call-tree nil "Alist of functions and their call tree.
410 Each element looks like
411
412   \(FUNCTION CALLERS CALLS\)
413
414 where CALLERS is a list of functions that call FUNCTION, and CALLS
415 is a list of functions for which calls were generated while compiling
416 FUNCTION.")
417
418 (defcustom byte-compile-call-tree-sort 'name
419   "*If non-nil, sort the call tree.
420 The values `name', `callers', `calls', `calls+callers'
421 specify different fields to sort on."
422   :group 'bytecomp
423   :type '(choice (const name) (const callers) (const calls)
424                  (const calls+callers) (const nil)))
425
426 (defvar byte-compile-debug nil)
427
428 ;; (defvar byte-compile-overwrite-file t
429 ;;   "If nil, old .elc files are deleted before the new is saved, and .elc
430 ;; files will have the same modes as the corresponding .el file.  Otherwise,
431 ;; existing .elc files will simply be overwritten, and the existing modes
432 ;; will not be changed.  If this variable is nil, then an .elc file which
433 ;; is a symbolic link will be turned into a normal file, instead of the file
434 ;; which the link points to being overwritten.")
435
436 (defvar byte-compile-constants nil
437   "List of all constants encountered during compilation of this form.")
438 (defvar byte-compile-variables nil
439   "List of all variables encountered during compilation of this form.")
440 (defvar byte-compile-bound-variables nil
441   "List of variables bound in the context of the current form.
442 This list lives partly on the stack.")
443 (defvar byte-compile-const-variables nil
444   "List of variables declared as constants during compilation of this file.")
445 (defvar byte-compile-free-references)
446 (defvar byte-compile-free-assignments)
447
448 (defvar byte-compiler-error-flag)
449
450 (defconst byte-compile-initial-macro-environment
451   '(
452 ;;     (byte-compiler-options . (lambda (&rest forms)
453 ;;                             (apply 'byte-compiler-options-handler forms)))
454     (eval-when-compile . (lambda (&rest body)
455                            (list 'quote
456                                  (byte-compile-eval (byte-compile-top-level
457                                                      (cons 'progn body))))))
458     (eval-and-compile . (lambda (&rest body)
459                           (byte-compile-eval-before-compile (cons 'progn body))
460                           (cons 'progn body))))
461   "The default macro-environment passed to macroexpand by the compiler.
462 Placing a macro here will cause a macro to have different semantics when
463 expanded by the compiler as when expanded by the interpreter.")
464
465 (defvar byte-compile-macro-environment byte-compile-initial-macro-environment
466   "Alist of macros defined in the file being compiled.
467 Each element looks like (MACRONAME . DEFINITION).  It is
468 \(MACRONAME . nil) when a macro is redefined as a function.")
469
470 (defvar byte-compile-function-environment nil
471   "Alist of functions defined in the file being compiled.
472 This is so we can inline them when necessary.
473 Each element looks like (FUNCTIONNAME . DEFINITION).  It is
474 \(FUNCTIONNAME . nil) when a function is redefined as a macro.
475 It is \(FUNCTIONNAME . t) when all we know is that it was defined,
476 and we don't know the definition.")
477
478 (defvar byte-compile-unresolved-functions nil
479   "Alist of undefined functions to which calls have been compiled.
480 This variable is only significant whilst compiling an entire buffer.
481 Used for warnings when a function is not known to be defined or is later
482 defined with incorrect args.")
483
484 (defvar byte-compile-noruntime-functions nil
485   "Alist of functions called that may not be defined when the compiled code is run.
486 Used for warnings about calling a function that is defined during compilation
487 but won't necessarily be defined when the compiled file is loaded.")
488
489 (defvar byte-compile-tag-number 0)
490 (defvar byte-compile-output nil
491   "Alist describing contents to put in byte code string.
492 Each element is (INDEX . VALUE)")
493 (defvar byte-compile-depth 0 "Current depth of execution stack.")
494 (defvar byte-compile-maxdepth 0 "Maximum depth of execution stack.")
495
496
497 ;;; The byte codes; this information is duplicated in bytecomp.c
498
499 (defvar byte-code-vector nil
500   "An array containing byte-code names indexed by byte-code values.")
501
502 (defvar byte-stack+-info nil
503   "An array with the stack adjustment for each byte-code.")
504
505 (defmacro byte-defop (opcode stack-adjust opname &optional docstring)
506   ;; This is a speed-hack for building the byte-code-vector at compile-time.
507   ;; We fill in the vector at macroexpand-time, and then after the last call
508   ;; to byte-defop, we write the vector out as a constant instead of writing
509   ;; out a bunch of calls to aset.
510   ;; Actually, we don't fill in the vector itself, because that could make
511   ;; it problematic to compile big changes to this compiler; we store the
512   ;; values on its plist, and remove them later in -extrude.
513   (let ((v1 (or (get 'byte-code-vector 'tmp-compile-time-value)
514                 (put 'byte-code-vector 'tmp-compile-time-value
515                      (make-vector 256 nil))))
516         (v2 (or (get 'byte-stack+-info 'tmp-compile-time-value)
517                 (put 'byte-stack+-info 'tmp-compile-time-value
518                      (make-vector 256 nil)))))
519     (aset v1 opcode opname)
520     (aset v2 opcode stack-adjust))
521   (if docstring
522       (list 'defconst opname opcode (concat "Byte code opcode " docstring "."))
523       (list 'defconst opname opcode)))
524
525 (defmacro byte-extrude-byte-code-vectors ()
526   (prog1 (list 'setq 'byte-code-vector
527                      (get 'byte-code-vector 'tmp-compile-time-value)
528                      'byte-stack+-info
529                      (get 'byte-stack+-info 'tmp-compile-time-value))
530     (put 'byte-code-vector 'tmp-compile-time-value nil)
531     (put 'byte-stack+-info 'tmp-compile-time-value nil)))
532
533
534 ;; unused: 0-7
535
536 ;; These opcodes are special in that they pack their argument into the
537 ;; opcode word.
538 ;;
539 (byte-defop   8  1 byte-varref  "for variable reference")
540 (byte-defop  16 -1 byte-varset  "for setting a variable")
541 (byte-defop  24 -1 byte-varbind "for binding a variable")
542 (byte-defop  32  0 byte-call    "for calling a function")
543 (byte-defop  40  0 byte-unbind  "for unbinding special bindings")
544 ;; codes 8-47 are consumed by the preceding opcodes
545
546 ;; unused: 48-55
547
548 (byte-defop  56 -1 byte-nth)
549 (byte-defop  57  0 byte-symbolp)
550 (byte-defop  58  0 byte-consp)
551 (byte-defop  59  0 byte-stringp)
552 (byte-defop  60  0 byte-listp)
553 (byte-defop  61 -1 byte-eq)
554 (byte-defop  62 -1 byte-memq)
555 (byte-defop  63  0 byte-not)
556 (byte-defop  64  0 byte-car)
557 (byte-defop  65  0 byte-cdr)
558 (byte-defop  66 -1 byte-cons)
559 (byte-defop  67  0 byte-list1)
560 (byte-defop  68 -1 byte-list2)
561 (byte-defop  69 -2 byte-list3)
562 (byte-defop  70 -3 byte-list4)
563 (byte-defop  71  0 byte-length)
564 (byte-defop  72 -1 byte-aref)
565 (byte-defop  73 -2 byte-aset)
566 (byte-defop  74  0 byte-symbol-value)
567 (byte-defop  75  0 byte-symbol-function) ; this was commented out
568 (byte-defop  76 -1 byte-set)
569 (byte-defop  77 -1 byte-fset) ; this was commented out
570 (byte-defop  78 -1 byte-get)
571 (byte-defop  79 -2 byte-substring)
572 (byte-defop  80 -1 byte-concat2)
573 (byte-defop  81 -2 byte-concat3)
574 (byte-defop  82 -3 byte-concat4)
575 (byte-defop  83  0 byte-sub1)
576 (byte-defop  84  0 byte-add1)
577 (byte-defop  85 -1 byte-eqlsign)
578 (byte-defop  86 -1 byte-gtr)
579 (byte-defop  87 -1 byte-lss)
580 (byte-defop  88 -1 byte-leq)
581 (byte-defop  89 -1 byte-geq)
582 (byte-defop  90 -1 byte-diff)
583 (byte-defop  91  0 byte-negate)
584 (byte-defop  92 -1 byte-plus)
585 (byte-defop  93 -1 byte-max)
586 (byte-defop  94 -1 byte-min)
587 (byte-defop  95 -1 byte-mult) ; v19 only
588 (byte-defop  96  1 byte-point)
589 (byte-defop  98  0 byte-goto-char)
590 (byte-defop  99  0 byte-insert)
591 (byte-defop 100  1 byte-point-max)
592 (byte-defop 101  1 byte-point-min)
593 (byte-defop 102  0 byte-char-after)
594 (byte-defop 103  1 byte-following-char)
595 (byte-defop 104  1 byte-preceding-char)
596 (byte-defop 105  1 byte-current-column)
597 (byte-defop 106  0 byte-indent-to)
598 (byte-defop 107  0 byte-scan-buffer-OBSOLETE) ; no longer generated as of v18
599 (byte-defop 108  1 byte-eolp)
600 (byte-defop 109  1 byte-eobp)
601 (byte-defop 110  1 byte-bolp)
602 (byte-defop 111  1 byte-bobp)
603 (byte-defop 112  1 byte-current-buffer)
604 (byte-defop 113  0 byte-set-buffer)
605 (byte-defop 114  0 byte-save-current-buffer
606   "To make a binding to record the current buffer")
607 (byte-defop 115  0 byte-set-mark-OBSOLETE)
608 (byte-defop 116  1 byte-interactive-p)
609
610 ;; These ops are new to v19
611 (byte-defop 117  0 byte-forward-char)
612 (byte-defop 118  0 byte-forward-word)
613 (byte-defop 119 -1 byte-skip-chars-forward)
614 (byte-defop 120 -1 byte-skip-chars-backward)
615 (byte-defop 121  0 byte-forward-line)
616 (byte-defop 122  0 byte-char-syntax)
617 (byte-defop 123 -1 byte-buffer-substring)
618 (byte-defop 124 -1 byte-delete-region)
619 (byte-defop 125 -1 byte-narrow-to-region)
620 (byte-defop 126  1 byte-widen)
621 (byte-defop 127  0 byte-end-of-line)
622
623 ;; unused: 128
624
625 ;; These store their argument in the next two bytes
626 (byte-defop 129  1 byte-constant2
627    "for reference to a constant with vector index >= byte-constant-limit")
628 (byte-defop 130  0 byte-goto "for unconditional jump")
629 (byte-defop 131 -1 byte-goto-if-nil "to pop value and jump if it's nil")
630 (byte-defop 132 -1 byte-goto-if-not-nil "to pop value and jump if it's not nil")
631 (byte-defop 133 -1 byte-goto-if-nil-else-pop
632   "to examine top-of-stack, jump and don't pop it if it's nil,
633 otherwise pop it")
634 (byte-defop 134 -1 byte-goto-if-not-nil-else-pop
635   "to examine top-of-stack, jump and don't pop it if it's non nil,
636 otherwise pop it")
637
638 (byte-defop 135 -1 byte-return "to pop a value and return it from `byte-code'")
639 (byte-defop 136 -1 byte-discard "to discard one value from stack")
640 (byte-defop 137  1 byte-dup     "to duplicate the top of the stack")
641
642 (byte-defop 138  0 byte-save-excursion
643   "to make a binding to record the buffer, point and mark")
644 (byte-defop 139  0 byte-save-window-excursion
645   "to make a binding to record entire window configuration")
646 (byte-defop 140  0 byte-save-restriction
647   "to make a binding to record the current buffer clipping restrictions")
648 (byte-defop 141 -1 byte-catch
649   "for catch.  Takes, on stack, the tag and an expression for the body")
650 (byte-defop 142 -1 byte-unwind-protect
651   "for unwind-protect.  Takes, on stack, an expression for the unwind-action")
652
653 ;; For condition-case.  Takes, on stack, the variable to bind,
654 ;; an expression for the body, and a list of clauses.
655 (byte-defop 143 -2 byte-condition-case)
656
657 ;; For entry to with-output-to-temp-buffer.
658 ;; Takes, on stack, the buffer name.
659 ;; Binds standard-output and does some other things.
660 ;; Returns with temp buffer on the stack in place of buffer name.
661 (byte-defop 144  0 byte-temp-output-buffer-setup)
662
663 ;; For exit from with-output-to-temp-buffer.
664 ;; Expects the temp buffer on the stack underneath value to return.
665 ;; Pops them both, then pushes the value back on.
666 ;; Unbinds standard-output and makes the temp buffer visible.
667 (byte-defop 145 -1 byte-temp-output-buffer-show)
668
669 ;; these ops are new to v19
670
671 ;; To unbind back to the beginning of this frame.
672 ;; Not used yet, but will be needed for tail-recursion elimination.
673 (byte-defop 146  0 byte-unbind-all)
674
675 ;; these ops are new to v19
676 (byte-defop 147 -2 byte-set-marker)
677 (byte-defop 148  0 byte-match-beginning)
678 (byte-defop 149  0 byte-match-end)
679 (byte-defop 150  0 byte-upcase)
680 (byte-defop 151  0 byte-downcase)
681 (byte-defop 152 -1 byte-string=)
682 (byte-defop 153 -1 byte-string<)
683 (byte-defop 154 -1 byte-equal)
684 (byte-defop 155 -1 byte-nthcdr)
685 (byte-defop 156 -1 byte-elt)
686 (byte-defop 157 -1 byte-member)
687 (byte-defop 158 -1 byte-assq)
688 (byte-defop 159  0 byte-nreverse)
689 (byte-defop 160 -1 byte-setcar)
690 (byte-defop 161 -1 byte-setcdr)
691 (byte-defop 162  0 byte-car-safe)
692 (byte-defop 163  0 byte-cdr-safe)
693 (byte-defop 164 -1 byte-nconc)
694 (byte-defop 165 -1 byte-quo)
695 (byte-defop 166 -1 byte-rem)
696 (byte-defop 167  0 byte-numberp)
697 (byte-defop 168  0 byte-integerp)
698
699 ;; unused: 169-174
700 (byte-defop 175 nil byte-listN)
701 (byte-defop 176 nil byte-concatN)
702 (byte-defop 177 nil byte-insertN)
703
704 ;; unused: 178-191
705
706 (byte-defop 192  1 byte-constant        "for reference to a constant")
707 ;; codes 193-255 are consumed by byte-constant.
708 (defconst byte-constant-limit 64
709   "Exclusive maximum index usable in the `byte-constant' opcode.")
710
711 (defconst byte-goto-ops '(byte-goto byte-goto-if-nil byte-goto-if-not-nil
712                           byte-goto-if-nil-else-pop
713                           byte-goto-if-not-nil-else-pop)
714   "List of byte-codes whose offset is a pc.")
715
716 (defconst byte-goto-always-pop-ops '(byte-goto-if-nil byte-goto-if-not-nil))
717
718 (byte-extrude-byte-code-vectors)
719
720 ;;; lapcode generator
721 ;;
722 ;; the byte-compiler now does source -> lapcode -> bytecode instead of
723 ;; source -> bytecode, because it's a lot easier to make optimizations
724 ;; on lapcode than on bytecode.
725 ;;
726 ;; Elements of the lapcode list are of the form (<instruction> . <parameter>)
727 ;; where instruction is a symbol naming a byte-code instruction,
728 ;; and parameter is an argument to that instruction, if any.
729 ;;
730 ;; The instruction can be the pseudo-op TAG, which means that this position
731 ;; in the instruction stream is a target of a goto.  (car PARAMETER) will be
732 ;; the PC for this location, and the whole instruction "(TAG pc)" will be the
733 ;; parameter for some goto op.
734 ;;
735 ;; If the operation is varbind, varref, varset or push-constant, then the
736 ;; parameter is (variable/constant . index_in_constant_vector).
737 ;;
738 ;; First, the source code is macroexpanded and optimized in various ways.
739 ;; Then the resultant code is compiled into lapcode.  Another set of
740 ;; optimizations are then run over the lapcode.  Then the variables and
741 ;; constants referenced by the lapcode are collected and placed in the
742 ;; constants-vector.  (This happens now so that variables referenced by dead
743 ;; code don't consume space.)  And finally, the lapcode is transformed into
744 ;; compacted byte-code.
745 ;;
746 ;; A distinction is made between variables and constants because the variable-
747 ;; referencing instructions are more sensitive to the variables being near the
748 ;; front of the constants-vector than the constant-referencing instructions.
749 ;; Also, this lets us notice references to free variables.
750
751 (defun byte-compile-lapcode (lap)
752   "Turns lapcode into bytecode.  The lapcode is destroyed."
753   ;; Lapcode modifications: changes the ID of a tag to be the tag's PC.
754   (let ((pc 0)                  ; Program counter
755         op off                  ; Operation & offset
756         (bytes '())             ; Put the output bytes here
757         (patchlist nil))        ; List of tags and goto's to patch
758     (while lap
759       (setq op (car (car lap))
760             off (cdr (car lap)))
761       (cond ((not (symbolp op))
762              (error "Non-symbolic opcode `%s'" op))
763             ((eq op 'TAG)
764              (setcar off pc)
765              (setq patchlist (cons off patchlist)))
766             ((memq op byte-goto-ops)
767              (setq pc (+ pc 3))
768              (setq bytes (cons (cons pc (cdr off))
769                                (cons nil
770                                      (cons (symbol-value op) bytes))))
771              (setq patchlist (cons bytes patchlist)))
772             (t
773              (setq bytes
774                    (cond ((cond ((consp off)
775                                  ;; Variable or constant reference
7