home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / bytecomp / bytecomp.el < prev    next >
Encoding:
Text File  |  1995-08-18  |  138.0 KB  |  3,661 lines

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