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