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