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