home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / packages / bytecomp / bytecomp.el < prev    next >
Encoding:
Text File  |  1992-07-15  |  113.8 KB  |  3,062 lines

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