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