annotate man/lispref/compile.texi @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children d44af0c54775
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 @c -*-texinfo-*-
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 @c This is part of the XEmacs Lisp Reference Manual.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 @c See the file lispref.texi for copying conditions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 @setfilename ../../info/compile.info
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 @node Byte Compilation, Debugging, Loading, Top
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 @chapter Byte Compilation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 @cindex byte-code
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 @cindex compilation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 XEmacs Lisp has a @dfn{compiler} that translates functions written
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 in Lisp into a special representation called @dfn{byte-code} that can be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 executed more efficiently. The compiler replaces Lisp function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 definitions with byte-code. When a byte-coded function is called, its
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 definition is evaluated by the @dfn{byte-code interpreter}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 Because the byte-compiled code is evaluated by the byte-code
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 interpreter, instead of being executed directly by the machine's
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 hardware (as true compiled code is), byte-code is completely
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 transportable from machine to machine without recompilation. It is not,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 however, as fast as true compiled code.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 In general, any version of Emacs can run byte-compiled code produced
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 by recent earlier versions of Emacs, but the reverse is not true. In
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 particular, if you compile a program with Emacs 19.29, the compiled
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 code does not run in earlier versions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 @iftex
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 @xref{Docs and Compilation}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 @end iftex
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 Files compiled in versions before 19.29 may not work in 19.29 if they
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 contain character constants with modifier bits, because the bits were
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 renumbered in Emacs 19.29.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 @xref{Compilation Errors}, for how to investigate errors occurring in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 byte compilation.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 @menu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 * Speed of Byte-Code:: An example of speedup from byte compilation.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 * Compilation Functions:: Byte compilation functions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 * Docs and Compilation:: Dynamic loading of documentation strings.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 * Dynamic Loading:: Dynamic loading of individual functions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 * Eval During Compile:: Code to be evaluated when you compile.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 * Compiled-Function Objects:: The data type used for byte-compiled functions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 * Disassembly:: Disassembling byte-code; how to read byte-code.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 @end menu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 @node Speed of Byte-Code
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 @section Performance of Byte-Compiled Code
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 A byte-compiled function is not as efficient as a primitive function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 written in C, but runs much faster than the version written in Lisp.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 Here is an example:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 (defun silly-loop (n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 "Return time before and after N iterations of a loop."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 (let ((t1 (current-time-string)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 (while (> (setq n (1- n))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 (list t1 (current-time-string))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 @result{} silly-loop
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 (silly-loop 100000)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 @result{} ("Fri Mar 18 17:25:57 1994"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 "Fri Mar 18 17:26:28 1994") ; @r{31 seconds}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 (byte-compile 'silly-loop)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 @result{} @r{[Compiled code not shown]}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 (silly-loop 100000)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 @result{} ("Fri Mar 18 17:26:52 1994"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 "Fri Mar 18 17:26:58 1994") ; @r{6 seconds}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 In this example, the interpreted code required 31 seconds to run,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 whereas the byte-compiled code required 6 seconds. These results are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 representative, but actual results will vary greatly.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 @node Compilation Functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 @comment node-name, next, previous, up
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 @section The Compilation Functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 @cindex compilation functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 You can byte-compile an individual function or macro definition with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 the @code{byte-compile} function. You can compile a whole file with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 @code{byte-compile-file}, or several files with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 @code{byte-recompile-directory} or @code{batch-byte-compile}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 When you run the byte compiler, you may get warnings in a buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 called @samp{*Compile-Log*}. These report things in your program that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 suggest a problem but are not necessarily erroneous.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 @cindex macro compilation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 Be careful when byte-compiling code that uses macros. Macro calls are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 expanded when they are compiled, so the macros must already be defined
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 for proper compilation. For more details, see @ref{Compiling Macros}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 Normally, compiling a file does not evaluate the file's contents or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 load the file. But it does execute any @code{require} calls at top
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 level in the file. One way to ensure that necessary macro definitions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 are available during compilation is to require the file that defines
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 them (@pxref{Named Features}). To avoid loading the macro definition files
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 when someone @emph{runs} the compiled program, write
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 @code{eval-when-compile} around the @code{require} calls (@pxref{Eval
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 During Compile}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 @defun byte-compile symbol
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 This function byte-compiles the function definition of @var{symbol},
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 replacing the previous definition with the compiled one. The function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 definition of @var{symbol} must be the actual code for the function;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 i.e., the compiler does not follow indirection to another symbol.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 @code{byte-compile} returns the new, compiled definition of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 @var{symbol}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 If @var{symbol}'s definition is a compiled-function object,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 @code{byte-compile} does nothing and returns @code{nil}. Lisp records
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 only one function definition for any symbol, and if that is already
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 compiled, non-compiled code is not available anywhere. So there is no
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 way to ``compile the same definition again.''
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 (defun factorial (integer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 "Compute factorial of INTEGER."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 (if (= 1 integer) 1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 (* integer (factorial (1- integer)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 @result{} factorial
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 (byte-compile 'factorial)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 @result{}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 #<byte-code (integer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 "ÁU«‚Á‡ÂS!_‡"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 [integer 1 factorial]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 3 "Compute factorial of INTEGER.">
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 @noindent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 The result is a compiled-function object. The string it contains is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 the actual byte-code; each character in it is an instruction or an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 operand of an instruction. The vector contains all the constants,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 variable names and function names used by the function, except for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 certain primitives that are coded as special instructions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 @deffn Command compile-defun &optional arg
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 This command reads the defun containing point, compiles it, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 evaluates the result. If you use this on a defun that is actually a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 function definition, the effect is to install a compiled version of that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 function.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 @c XEmacs feature
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 If @var{arg} is non-@code{nil}, the result is inserted in the current
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 buffer after the form; otherwise, it is printed in the minibuffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 @end deffn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 @deffn Command byte-compile-file filename &optional load
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 This function compiles a file of Lisp code named @var{filename} into
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 a file of byte-code. The output file's name is made by appending
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 @samp{c} to the end of @var{filename}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 @c XEmacs feature
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 If @code{load} is non-@code{nil}, the file is loaded after having been
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 compiled.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 Compilation works by reading the input file one form at a time. If it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 is a definition of a function or macro, the compiled function or macro
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 definition is written out. Other forms are batched together, then each
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 batch is compiled, and written so that its compiled code will be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 executed when the file is read. All comments are discarded when the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 input file is read.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 This command returns @code{t}. When called interactively, it prompts
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 for the file name.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 % ls -l push*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 -rw-r--r-- 1 lewis 791 Oct 5 20:31 push.el
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 (byte-compile-file "~/emacs/push.el")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 @result{} t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 % ls -l push*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 -rw-r--r-- 1 lewis 791 Oct 5 20:31 push.el
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 -rw-rw-rw- 1 lewis 638 Oct 8 20:25 push.elc
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 @end deffn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 @c flag is not optional in FSF Emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 @deffn Command byte-recompile-directory directory &optional flag
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 @cindex library compilation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 This function recompiles every @samp{.el} file in @var{directory} that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 needs recompilation. A file needs recompilation if a @samp{.elc} file
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 exists but is older than the @samp{.el} file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 When a @samp{.el} file has no corresponding @samp{.elc} file, then
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 @var{flag} says what to do. If it is @code{nil}, these files are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 ignored. If it is non-@code{nil}, the user is asked whether to compile
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 each such file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 The returned value of this command is unpredictable.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 @end deffn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 @defun batch-byte-compile
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 This function runs @code{byte-compile-file} on files specified on the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 command line. This function must be used only in a batch execution of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 Emacs, as it kills Emacs on completion. An error in one file does not
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 prevent processing of subsequent files. (The file that gets the error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 will not, of course, produce any compiled code.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 % emacs -batch -f batch-byte-compile *.el
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 @c XEmacs feature
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 @defun batch-byte-recompile-directory
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 This function is similar to @code{batch-byte-compile} but runs the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 command @code{byte-recompile-directory} on the files remaining on the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 command line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 @c XEmacs feature
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 @defvar byte-recompile-directory-ignore-errors-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 If non-@code{nil}, this specifies that @code{byte-recompile-directory}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 will continue compiling even when an error occurs in a file. This is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 normally @code{nil}, but is bound to @code{t} by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 @code{batch-byte-recompile-directory}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 @end defvar
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 @defun byte-code code-string data-vector max-stack
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 @cindex byte-code interpreter
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 This function actually interprets byte-code. A byte-compiled function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 is actually defined with a body that calls @code{byte-code}. Don't call
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 this function yourself. Only the byte compiler knows how to generate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 valid calls to this function.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 In newer Emacs versions (19 and up), byte-code is usually executed as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 part of a compiled-function object, and only rarely due to an explicit
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 call to @code{byte-code}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 @node Docs and Compilation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 @section Documentation Strings and Compilation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 @cindex dynamic loading of documentation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 Functions and variables loaded from a byte-compiled file access their
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 documentation strings dynamically from the file whenever needed. This
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 saves space within Emacs, and makes loading faster because the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 documentation strings themselves need not be processed while loading the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 file. Actual access to the documentation strings becomes slower as a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 result, but normally not enough to bother users.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 Dynamic access to documentation strings does have drawbacks:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 @itemize @bullet
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 @item
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 If you delete or move the compiled file after loading it, Emacs can no
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 longer access the documentation strings for the functions and variables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 in the file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 @item
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 If you alter the compiled file (such as by compiling a new version),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 then further access to documentation strings in this file will give
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 nonsense results.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 @end itemize
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 If your site installs Emacs following the usual procedures, these
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 problems will never normally occur. Installing a new version uses a new
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 directory with a different name; as long as the old version remains
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 installed, its files will remain unmodified in the places where they are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 expected to be.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 However, if you have built Emacs yourself and use it from the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 directory where you built it, you will experience this problem
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 occasionally if you edit and recompile Lisp files. When it happens, you
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 can cure the problem by reloading the file after recompiling it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 Byte-compiled files made with Emacs 19.29 will not load into older
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 versions because the older versions don't support this feature. You can
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 turn off this feature by setting @code{byte-compile-dynamic-docstrings}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 to @code{nil}. Once this is done, you can compile files that will load
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 into older Emacs versions. You can do this globally, or for one source
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 file by specifying a file-local binding for the variable. Here's one
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 way to do that:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 -*-byte-compile-dynamic-docstrings: nil;-*-
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 @defvar byte-compile-dynamic-docstrings
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 If this is non-@code{nil}, the byte compiler generates compiled files
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 that are set up for dynamic loading of documentation strings.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 @end defvar
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 @cindex @samp{#@@@var{count}}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 @cindex @samp{#$}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 The dynamic documentation string feature writes compiled files that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 use a special Lisp reader construct, @samp{#@@@var{count}}. This
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 construct skips the next @var{count} characters. It also uses the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 @samp{#$} construct, which stands for ``the name of this file, as a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 string.'' It is best not to use these constructs in Lisp source files.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 @node Dynamic Loading
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 @section Dynamic Loading of Individual Functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 @cindex dynamic loading of functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 @cindex lazy loading
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 When you compile a file, you can optionally enable the @dfn{dynamic
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 function loading} feature (also known as @dfn{lazy loading}). With
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 dynamic function loading, loading the file doesn't fully read the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 function definitions in the file. Instead, each function definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 contains a place-holder which refers to the file. The first time each
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 function is called, it reads the full definition from the file, to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 replace the place-holder.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 The advantage of dynamic function loading is that loading the file
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 becomes much faster. This is a good thing for a file which contains
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 many separate commands, provided that using one of them does not imply
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 you will soon (or ever) use the rest. A specialized mode which provides
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 many keyboard commands often has that usage pattern: a user may invoke
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 the mode, but use only a few of the commands it provides.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 The dynamic loading feature has certain disadvantages:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 @itemize @bullet
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 @item
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 If you delete or move the compiled file after loading it, Emacs can no
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 longer load the remaining function definitions not already loaded.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 @item
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 If you alter the compiled file (such as by compiling a new version),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 then trying to load any function not already loaded will get nonsense
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 results.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 @end itemize
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 If you compile a new version of the file, the best thing to do is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 immediately load the new compiled file. That will prevent any future
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 problems.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 The byte compiler uses the dynamic function loading feature if the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 variable @code{byte-compile-dynamic} is non-@code{nil} at compilation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 time. Do not set this variable globally, since dynamic loading is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 desirable only for certain files. Instead, enable the feature for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 specific source files with file-local variable bindings, like this:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 -*-byte-compile-dynamic: t;-*-
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 @defvar byte-compile-dynamic
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 If this is non-@code{nil}, the byte compiler generates compiled files
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 that are set up for dynamic function loading.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 @end defvar
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 @defun fetch-bytecode function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 This immediately finishes loading the definition of @var{function} from
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 its byte-compiled file, if it is not fully loaded already. The argument
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 @var{function} may be a compiled-function object or a function name.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 @node Eval During Compile
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 @section Evaluation During Compilation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 These features permit you to write code to be evaluated during
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 compilation of a program.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 @defspec eval-and-compile body
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 This form marks @var{body} to be evaluated both when you compile the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 containing code and when you run it (whether compiled or not).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 You can get a similar result by putting @var{body} in a separate file
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 and referring to that file with @code{require}. Using @code{require} is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 preferable if there is a substantial amount of code to be executed in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 this way.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 @end defspec
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 @defspec eval-when-compile body
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 This form marks @var{body} to be evaluated at compile time and not when
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 the compiled program is loaded. The result of evaluation by the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 compiler becomes a constant which appears in the compiled program. When
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 the program is interpreted, not compiled at all, @var{body} is evaluated
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 normally.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 At top level, this is analogous to the Common Lisp idiom
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 @code{(eval-when (compile eval) @dots{})}. Elsewhere, the Common Lisp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 @samp{#.} reader macro (but not when interpreting) is closer to what
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 @code{eval-when-compile} does.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 @end defspec
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 @node Compiled-Function Objects
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 @section Compiled-Function Objects
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 @cindex compiled function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 @cindex byte-code function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 Byte-compiled functions have a special data type: they are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 @dfn{compiled-function objects}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 A compiled-function object is a bit like a vector; however, the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 evaluator handles this data type specially when it appears as a function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 to be called. The printed representation for a compiled-function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 object normally begins with @samp{#<compiled-function} and ends with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 @samp{>}. However, if the variable @code{print-readably} is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 non-@code{nil}, the object is printed beginning with @samp{#[} and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 ending with @samp{]}. This representation can be read directly
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 by the Lisp reader, and is used in byte-compiled files (those ending
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 in @samp{.elc}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 In Emacs version 18, there was no compiled-function object data type;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 compiled functions used the function @code{byte-code} to run the byte
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 code.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 A compiled-function object has a number of different elements.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 They are:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 @table @var
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 @item arglist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 The list of argument symbols.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 @item instructions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437 The string containing the byte-code instructions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 @item constants
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 The vector of Lisp objects referenced by the byte code. These include
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 symbols used as function names and variable names.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 @item stacksize
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 The maximum stack size this function needs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 @item doc-string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 The documentation string (if any); otherwise, @code{nil}. The value may
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 be a number or a list, in case the documentation string is stored in a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 file. Use the function @code{documentation} to get the real
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 documentation string (@pxref{Accessing Documentation}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 @item interactive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 The interactive spec (if any). This can be a string or a Lisp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 expression. It is @code{nil} for a function that isn't interactive.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 @item domain
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 The domain (if any). This is only meaningful if I18N3 (message-translation)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 support was compiled into XEmacs. This is a string defining which
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 domain to find the translation for the documentation string and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 interactive prompt. @xref{Domain Specification}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 @end table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 Here's an example of a compiled-function object, in printed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 representation. It is the definition of the command
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 @code{backward-sexp}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 #<compiled-function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 (&optional arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 "¬‚ÁÂ[!‡"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 [arg 1 forward-sexp]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 579173
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 "_p">
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 The primitive way to create a compiled-function object is with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 @code{make-byte-code}:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 @defun make-byte-code &rest elements
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 This function constructs and returns a compiled-function object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 with @var{elements} as its elements.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484 @emph{NOTE:} Unlike all other Emacs-lisp functions, calling this with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 five arguments is @emph{not} the same as calling it with six arguments,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 the last of which is @code{nil}. If the @var{interactive} arg is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 specified as @code{nil}, then that means that this function was defined
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 with @code{(interactive)}. If the arg is not specified, then that means
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 the function is not interactive. This is terrible behavior which is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 retained for compatibility with old @samp{.elc} files which expected
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491 these semantics.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 You should not try to come up with the elements for a compiled-function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495 object yourself, because if they are inconsistent, XEmacs may crash
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 when you call the function. Always leave it to the byte compiler to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 create these objects; it makes the elements consistent (we hope).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 The following primitives are provided for accessing the elements of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 a compiled-function object.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 @defun compiled-function-arglist function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 This function returns the argument list of compiled-function object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 @var{function}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 @defun compiled-function-instructions function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 This function returns a string describing the byte-code instructions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 of compiled-function object @var{function}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512 @defun compiled-function-constants function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 This function returns the vector of Lisp objects referenced by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 compiled-function object @var{function}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517 @defun compiled-function-stack-size function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518 This function returns the maximum stack size needed by compiled-function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519 object @var{function}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522 @defun compiled-function-doc-string function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 This function returns the doc string of compiled-function object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
524 @var{function}, if available.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527 @defun compiled-function-interactive function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528 This function returns the interactive spec of compiled-function object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529 @var{function}, if any. The return value is @code{nil} or a two-element
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530 list, the first element of which is the symbol @code{interactive} and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531 the second element is the interactive spec (a string or Lisp form).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
532 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534 @defun compiled-function-domain function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535 This function returns the domain of compiled-function object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536 @var{function}, if any. The result will be a string or @code{nil}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537 @xref{Domain Specification}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
538 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 @node Disassembly
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541 @section Disassembled Byte-Code
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 @cindex disassembled byte-code
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544 People do not write byte-code; that job is left to the byte compiler.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 But we provide a disassembler to satisfy a cat-like curiosity. The
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546 disassembler converts the byte-compiled code into humanly readable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547 form.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
548
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
549 The byte-code interpreter is implemented as a simple stack machine.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
550 It pushes values onto a stack of its own, then pops them off to use them
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
551 in calculations whose results are themselves pushed back on the stack.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
552 When a byte-code function returns, it pops a value off the stack and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
553 returns it as the value of the function.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
554
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
555 In addition to the stack, byte-code functions can use, bind, and set
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
556 ordinary Lisp variables, by transferring values between variables and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
557 the stack.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
558
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
559 @deffn Command disassemble object &optional stream
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
560 This function prints the disassembled code for @var{object}. If
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
561 @var{stream} is supplied, then output goes there. Otherwise, the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
562 disassembled code is printed to the stream @code{standard-output}. The
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
563 argument @var{object} can be a function name or a lambda expression.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
564
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
565 As a special exception, if this function is used interactively,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
566 it outputs to a buffer named @samp{*Disassemble*}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
567 @end deffn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
568
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
569 Here are two examples of using the @code{disassemble} function. We
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
570 have added explanatory comments to help you relate the byte-code to the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
571 Lisp source; these do not appear in the output of @code{disassemble}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
572 These examples show unoptimized byte-code. Nowadays byte-code is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
573 usually optimized, but we did not want to rewrite these examples, since
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
574 they still serve their purpose.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
575
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
576 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
577 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
578 (defun factorial (integer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
579 "Compute factorial of an integer."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
580 (if (= 1 integer) 1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
581 (* integer (factorial (1- integer)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
582 @result{} factorial
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
583 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
584
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
585 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
586 (factorial 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
587 @result{} 24
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
588 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
589
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
590 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
591 (disassemble 'factorial)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
592 @print{} byte-code for factorial:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
593 doc: Compute factorial of an integer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
594 args: (integer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
595 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
596
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
597 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
598 0 constant 1 ; @r{Push 1 onto stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
599
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
600 1 varref integer ; @r{Get value of @code{integer}}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
601 ; @r{from the environment}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
602 ; @r{and push the value}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
603 ; @r{onto the stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
604 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
605
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
606 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
607 2 eqlsign ; @r{Pop top two values off stack,}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
608 ; @r{compare them,}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
609 ; @r{and push result onto stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
610 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
611
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
612 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
613 3 goto-if-nil 10 ; @r{Pop and test top of stack;}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
614 ; @r{if @code{nil}, go to 10,}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
615 ; @r{else continue.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
616 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
617
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
618 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
619 6 constant 1 ; @r{Push 1 onto top of stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
620
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
621 7 goto 17 ; @r{Go to 17 (in this case, 1 will be}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
622 ; @r{returned by the function).}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
623 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
624
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
625 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
626 10 constant * ; @r{Push symbol @code{*} onto stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
627
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
628 11 varref integer ; @r{Push value of @code{integer} onto stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
629 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
630
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
631 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
632 12 constant factorial ; @r{Push @code{factorial} onto stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
633
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
634 13 varref integer ; @r{Push value of @code{integer} onto stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
635
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
636 14 sub1 ; @r{Pop @code{integer}, decrement value,}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
637 ; @r{push new value onto stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
638 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
639
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
640 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
641 ; @r{Stack now contains:}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
642 ; @minus{} @r{decremented value of @code{integer}}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
643 ; @minus{} @r{@code{factorial}}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
644 ; @minus{} @r{value of @code{integer}}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
645 ; @minus{} @r{@code{*}}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
646 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
647
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
648 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
649 15 call 1 ; @r{Call function @code{factorial} using}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
650 ; @r{the first (i.e., the top) element}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
651 ; @r{of the stack as the argument;}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
652 ; @r{push returned value onto stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
653 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
654
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
655 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
656 ; @r{Stack now contains:}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
657 ; @minus{} @r{result of recursive}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
658 ; @r{call to @code{factorial}}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
659 ; @minus{} @r{value of @code{integer}}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
660 ; @minus{} @r{@code{*}}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
661 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
662
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
663 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
664 16 call 2 ; @r{Using the first two}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
665 ; @r{(i.e., the top two)}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
666 ; @r{elements of the stack}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
667 ; @r{as arguments,}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
668 ; @r{call the function @code{*},}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
669 ; @r{pushing the result onto the stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
670 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
671
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
672 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
673 17 return ; @r{Return the top element}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
674 ; @r{of the stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
675 @result{} nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
676 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
677 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
678
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
679 The @code{silly-loop} function is somewhat more complex:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
680
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
681 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
682 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
683 (defun silly-loop (n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
684 "Return time before and after N iterations of a loop."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
685 (let ((t1 (current-time-string)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
686 (while (> (setq n (1- n))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
687 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
688 (list t1 (current-time-string))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
689 @result{} silly-loop
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
690 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
691
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
692 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
693 (disassemble 'silly-loop)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
694 @print{} byte-code for silly-loop:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
695 doc: Return time before and after N iterations of a loop.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
696 args: (n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
697
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
698 0 constant current-time-string ; @r{Push}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
699 ; @r{@code{current-time-string}}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
700 ; @r{onto top of stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
701 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
702
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
703 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
704 1 call 0 ; @r{Call @code{current-time-string}}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
705 ; @r{ with no argument,}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
706 ; @r{ pushing result onto stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
707 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
708
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
709 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
710 2 varbind t1 ; @r{Pop stack and bind @code{t1}}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
711 ; @r{to popped value.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
712 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
713
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
714 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
715 3 varref n ; @r{Get value of @code{n} from}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
716 ; @r{the environment and push}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
717 ; @r{the value onto the stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
718 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
719
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
720 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
721 4 sub1 ; @r{Subtract 1 from top of stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
722 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
723
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
724 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
725 5 dup ; @r{Duplicate the top of the stack;}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
726 ; @r{i.e., copy the top of}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
727 ; @r{the stack and push the}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
728 ; @r{copy onto the stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
729 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
730
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
731 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
732 6 varset n ; @r{Pop the top of the stack,}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
733 ; @r{and bind @code{n} to the value.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
734
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
735 ; @r{In effect, the sequence @code{dup varset}}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
736 ; @r{copies the top of the stack}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
737 ; @r{into the value of @code{n}}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
738 ; @r{without popping it.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
739 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
740
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
741 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
742 7 constant 0 ; @r{Push 0 onto stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
743 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
744
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
745 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
746 8 gtr ; @r{Pop top two values off stack,}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
747 ; @r{test if @var{n} is greater than 0}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
748 ; @r{and push result onto stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
749 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
750
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
751 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
752 9 goto-if-nil-else-pop 17 ; @r{Goto 17 if @code{n} <= 0}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
753 ; @r{(this exits the while loop).}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
754 ; @r{else pop top of stack}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
755 ; @r{and continue}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
756 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
757
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
758 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
759 12 constant nil ; @r{Push @code{nil} onto stack}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
760 ; @r{(this is the body of the loop).}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
761 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
762
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
763 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
764 13 discard ; @r{Discard result of the body}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
765 ; @r{of the loop (a while loop}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
766 ; @r{is always evaluated for}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
767 ; @r{its side effects).}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
768 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
769
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
770 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
771 14 goto 3 ; @r{Jump back to beginning}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
772 ; @r{of while loop.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
773 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
774
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
775 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
776 17 discard ; @r{Discard result of while loop}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
777 ; @r{by popping top of stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
778 ; @r{This result is the value @code{nil} that}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
779 ; @r{was not popped by the goto at 9.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
780 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
781
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
782 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
783 18 varref t1 ; @r{Push value of @code{t1} onto stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
784 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
785
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
786 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
787 19 constant current-time-string ; @r{Push}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
788 ; @r{@code{current-time-string}}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
789 ; @r{onto top of stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
790 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
791
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
792 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
793 20 call 0 ; @r{Call @code{current-time-string} again.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
794 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
795
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
796 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
797 21 list2 ; @r{Pop top two elements off stack,}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
798 ; @r{create a list of them,}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
799 ; @r{and push list onto stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
800 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
801
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
802 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
803 22 unbind 1 ; @r{Unbind @code{t1} in local environment.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
804
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
805 23 return ; @r{Return value of the top of stack.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
806
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
807 @result{} nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
808 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
809 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
810
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
811