home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / bytecomp / bytecomp.el < prev    next >
Encoding:
Text File  |  1993-02-17  |  116.6 KB  |  3,130 lines

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