Updates to Macros and Customization chapters of Lisp manual.
[bpt/emacs.git] / doc / lispref / compile.texi
CommitLineData
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
13in Lisp into a special representation called @dfn{byte-code} that can be
14executed more efficiently. The compiler replaces Lisp function
15definitions with byte-code. When a byte-code function is called, its
16definition is evaluated by the @dfn{byte-code interpreter}.
17
18 Because the byte-compiled code is evaluated by the byte-code
19interpreter, instead of being executed directly by the machine's
20hardware (as true compiled code is), byte-code is completely
21transportable from machine to machine without recompilation. It is not,
22however, as fast as true compiled code.
23
b8d4c8d0
GM
24 In general, any version of Emacs can run byte-compiled code produced
25by 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
29variable 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
36byte 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
53written in C, but runs much faster than the version written in Lisp.
54Here 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
86whereas the byte-compiled code required 6 seconds. These results are
87representative, 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
95the @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
100in a buffer called @samp{*Compile-Log*}. These report things in your
101program 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
105byte-compile. Macro calls are expanded when they are compiled, so the
106macros must already be defined for proper compilation. For more
107details, see @ref{Compiling Macros}. If a program does not work the
108same way when compiled as it does when interpreted, erroneous macro
109definitions are one likely cause (@pxref{Problems with Macros}).
110Inline (@code{defsubst}) functions are less troublesome; if you
111compile a call to such a function before its definition is known, the
112call will still work right, it will just run slower.
113
114 Normally, compiling a file does not evaluate the file's contents or
115load the file. But it does execute any @code{require} calls at top
116level in the file. One way to ensure that necessary macro definitions
117are available during compilation is to require the file that defines
118them (@pxref{Named Features}). To avoid loading the macro definition files
119when someone @emph{runs} the compiled program, write
120@code{eval-when-compile} around the @code{require} calls (@pxref{Eval
121During Compile}).
122
123@defun byte-compile symbol
124This function byte-compiles the function definition of @var{symbol},
125replacing the previous definition with the compiled one. The function
126definition of @var{symbol} must be the actual code for the function;
127i.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
133only one function definition for any symbol, and if that is already
134compiled, non-compiled code is not available anywhere. So there is no
135way 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
157The result is a byte-code function object. The string it contains is
158the actual byte-code; each character in it is an instruction or an
159operand of an instruction. The vector contains all the constants,
160variable names and function names used by the function, except for
161certain primitives that are coded as special instructions.
162
163If the argument to @code{byte-compile} is a @code{lambda} expression,
164it returns the corresponding compiled code, but does not store
165it anywhere.
166@end defun
167
168@deffn Command compile-defun &optional arg
169This command reads the defun containing point, compiles it, and
170evaluates the result. If you use this on a defun that is actually a
171function definition, the effect is to install a compiled version of that
172function.
173
174@code{compile-defun} normally displays the result of evaluation in the
175echo area, but if @var{arg} is non-@code{nil}, it inserts the result
176in the current buffer after the form it compiled.
177@end deffn
178
179@deffn Command byte-compile-file filename &optional load
180This function compiles a file of Lisp code named @var{filename} into a
181file 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
185Compilation works by reading the input file one form at a time. If it
186is a definition of a function or macro, the compiled function or macro
187definition is written out. Other forms are batched together, then each
188batch is compiled, and written so that its compiled code will be
189executed when the file is read. All comments are discarded when the
190input file is read.
191
192This command returns @code{t} if there were no errors and @code{nil}
193otherwise. When called interactively, it prompts for the file name.
194
195If @var{load} is non-@code{nil}, this command loads the compiled file
196after 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
219This command recompiles every @samp{.el} file in @var{directory} (or
220its subdirectories) that needs recompilation. A file needs
221recompilation if a @samp{.elc} file exists but is older than the
222@samp{.el} file.
223
224When 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
226these 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,
228and asks about each subdirectory as well.
229
230Interactively, @code{byte-recompile-directory} prompts for
231@var{directory} and @var{flag} is the prefix argument.
232
233If @var{force} is non-@code{nil}, this command recompiles every
234@samp{.el} file that has a @samp{.elc} file.
235
236The returned value is unpredictable.
237@end deffn
238
239@defun batch-byte-compile &optional noforce
240This function runs @code{byte-compile-file} on files specified on the
241command line. This function must be used only in a batch execution of
242Emacs, as it kills Emacs on completion. An error in one file does not
243prevent processing of subsequent files, but no output file will be
244generated for it, and the Emacs process will terminate with a nonzero
245status code.
246
247If @var{noforce} is non-@code{nil}, this function does not recompile
248files 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
257This function actually interprets byte-code. A byte-compiled function
258is actually defined with a body that calls @code{byte-code}. Don't call
259this function yourself---only the byte compiler knows how to generate
260valid calls to this function.
261
262In Emacs version 18, byte-code was always executed by way of a call to
263the function @code{byte-code}. Nowadays, byte-code is usually executed
264as part of a byte-code function object, and only rarely through an
265explicit 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
273documentation strings dynamically from the file whenever needed. This
274saves space within Emacs, and makes loading faster because the
275documentation strings themselves need not be processed while loading the
276file. Actual access to the documentation strings becomes slower as a
277result, 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
283If you delete or move the compiled file after loading it, Emacs can no
284longer access the documentation strings for the functions and variables
285in the file.
286
287@item
288If you alter the compiled file (such as by compiling a new version),
289then further access to documentation strings in this file will
290probably give nonsense results.
291@end itemize
292
293 If your site installs Emacs following the usual procedures, these
294problems will never normally occur. Installing a new version uses a new
295directory with a different name; as long as the old version remains
296installed, its files will remain unmodified in the places where they are
297expected to be.
298
299 However, if you have built Emacs yourself and use it from the
300directory where you built it, you will experience this problem
301occasionally if you edit and recompile Lisp files. When it happens, you
302can 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
306mainly if you expect to change the file, and you want Emacs processes
307that have already loaded it to keep working when the file changes.
308You can do this globally, or for one source file by specifying a
309file-local binding for the variable. One way to do that is by adding
310this 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
317If this is non-@code{nil}, the byte compiler generates compiled files
318that 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
324use a special Lisp reader construct, @samp{#@@@var{count}}. This
325construct skips the next @var{count} characters. It also uses the
326@samp{#$} construct, which stands for ``the name of this file, as a
327string.'' It is usually best not to use these constructs in Lisp source
328files, since they are not designed to be clear to humans reading the
329file.
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
337function loading} feature (also known as @dfn{lazy loading}). With
338dynamic function loading, loading the file doesn't fully read the
339function definitions in the file. Instead, each function definition
340contains a place-holder which refers to the file. The first time each
341function is called, it reads the full definition from the file, to
342replace the place-holder.
343
344 The advantage of dynamic function loading is that loading the file
345becomes much faster. This is a good thing for a file which contains
346many separate user-callable functions, if using one of them does not
347imply you will probably also use the rest. A specialized mode which
348provides many keyboard commands often has that usage pattern: a user may
349invoke 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
355If you delete or move the compiled file after loading it, Emacs can no
356longer load the remaining function definitions not already loaded.
357
358@item
359If you alter the compiled file (such as by compiling a new version),
360then trying to load any function not already loaded will usually yield
361nonsense results.
362@end itemize
363
364 These problems will never happen in normal circumstances with
365installed Emacs files. But they are quite likely to happen with Lisp
366files that you are changing. The easiest way to prevent these problems
367is to reload the new compiled file immediately after each recompilation.
368
369 The byte compiler uses the dynamic function loading feature if the
370variable @code{byte-compile-dynamic} is non-@code{nil} at compilation
371time. Do not set this variable globally, since dynamic loading is
372desirable only for certain files. Instead, enable the feature for
373specific source files with file-local variable bindings. For example,
374you 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
381If this is non-@code{nil}, the byte compiler generates compiled files
382that are set up for dynamic function loading.
383@end defvar
384
385@defun fetch-bytecode function
386If @var{function} is a byte-code function object, this immediately
387finishes loading the byte code of @var{function} from its
388byte-compiled file, if it is not fully loaded already. Otherwise,
389it 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
396compilation of a program.
397
398@defspec eval-and-compile body@dots{}
399This form marks @var{body} to be evaluated both when you compile the
400containing code and when you run it (whether compiled or not).
401
402You can get a similar result by putting @var{body} in a separate file
403and referring to that file with @code{require}. That method is
404preferable when @var{body} is large. Effectively @code{require} is
405automatically @code{eval-and-compile}, the package is loaded both when
406compiling and executing.
407
408@code{autoload} is also effectively @code{eval-and-compile} too. It's
409recognized when compiling, so uses of such a function don't produce
410``not known to be defined'' warnings.
411
412Most uses of @code{eval-and-compile} are fairly sophisticated.
413
414If a macro has a helper function to build its result, and that macro
415is used both locally and outside the package, then
416@code{eval-and-compile} should be used to get the helper both when
417compiling and then later when running.
418
419If functions are defined programmatically (with @code{fset} say), then
420@code{eval-and-compile} can be used to have that done at compile-time
421as well as run-time, so calls to those functions are checked (and
422warnings about ``not known to be defined'' suppressed).
423@end defspec
424
425@defspec eval-when-compile body@dots{}
426This form marks @var{body} to be evaluated at compile time but not when
427the compiled program is loaded. The result of evaluation by the
428compiler becomes a constant which appears in the compiled program. If
429you load the source file, rather than compiling it, @var{body} is
430evaluated normally.
431
432@cindex compile-time constant
433If 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
442If you're using another package, but only need macros from it (the
443byte compiler will expand those), then @code{eval-when-compile} can be
444used 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
451The same sort of thing goes for macros and @code{defsubst} functions
452defined locally and only for use within the file. They are needed for
453compiling the file, but in most cases they are not needed for
454execution 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
464This is often good for code that's only a fallback for compatibility
465with other versions of Emacs.
466
467@strong{Common Lisp Note:} At top level, @code{eval-when-compile} is analogous to the Common
468Lisp idiom @code{(eval-when (compile eval) @dots{})}. Elsewhere, the
469Common Lisp @samp{#.} reader macro (but not when interpreting) is closer
470to 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
479numbers that identify the location of the problem. The usual Emacs
480commands for operating on compiler diagnostics work properly on
481these messages.
482
483 However, the warnings about functions that were used but not
484defined are always ``located'' at the end of the file, so these
485commands won't find the places they are really used. To do that,
486you must search for the function names.
487
488 You can suppress the compiler warning for calling an undefined
489function @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
497The 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
503can tell the compiler that a variable is defined using @code{defvar}
504with no initial value.
5bb0cda3 505
fc37ae72
RS
506 You can suppress the compiler warning for a specific use of an
507undefined 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
515The 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
520expression 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{}
526In execution, this is equivalent to @code{(progn @var{body}...)},
527but the compiler does not issue warnings for anything that occurs
528inside @var{body}.
529
530We recommend that you use this construct around the smallest
cd1181db 531possible piece of code, to avoid missing possible warnings other than
fc37ae72 532one 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;
547however, the evaluator handles this data type specially when it appears
548as a function to be called. The printed representation for a byte-code
549function object is like that for a vector, with an additional @samp{#}
550before the opening @samp{[}.
551
552 A byte-code function object must have at least four elements; there is
553no maximum number, but only the first six elements have any normal use.
554They are:
555
556@table @var
557@item arglist
558The list of argument symbols.
559
560@item byte-code
561The string containing the byte-code instructions.
562
563@item constants
564The vector of Lisp objects referenced by the byte code. These include
565symbols used as function names and variable names.
566
567@item stacksize
568The maximum stack size this function needs.
569
570@item docstring
571The documentation string (if any); otherwise, @code{nil}. The value may
572be a number or a list, in case the documentation string is stored in a
573file. Use the function @code{documentation} to get the real
574documentation string (@pxref{Accessing Documentation}).
575
576@item interactive
577The interactive spec (if any). This can be a string or a Lisp
578expression. It is @code{nil} for a function that isn't interactive.
579@end table
580
581Here's an example of a byte-code function object, in printed
582representation. 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
598This function constructs and returns a byte-code function object
599with @var{elements} as its elements.
600@end defun
601
602 You should not try to come up with the elements for a byte-code
603function yourself, because if they are inconsistent, Emacs may crash
604when you call the function. Always leave it to the byte compiler to
605create these objects; it makes the elements consistent (we hope).
606
607 You can access the elements of a byte-code object using @code{aref};
608you can also use @code{vconcat} to create a vector with the same
609elements.
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
616compiler. But we provide a disassembler to satisfy a cat-like
617curiosity. The disassembler converts the byte-compiled code into
618human-readable form.
b8d4c8d0
GM
619
620 The byte-code interpreter is implemented as a simple stack machine.
621It pushes values onto a stack of its own, then pops them off to use them
622in calculations whose results are themselves pushed back on the stack.
623When a byte-code function returns, it pops a value off the stack and
624returns it as the value of the function.
625
626 In addition to the stack, byte-code functions can use, bind, and set
627ordinary Lisp variables, by transferring values between variables and
628the stack.
629
630@deffn Command disassemble object &optional buffer-or-name
631This command displays the disassembled code for @var{object}. In
632interactive use, or if @var{buffer-or-name} is @code{nil} or omitted,
633the 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
635name of an existing buffer. Then the output goes there, at point, and
636point is left before the output.
637
638The argument @var{object} can be a function name, a lambda expression
639or a byte-code object. If it is a lambda expression, @code{disassemble}
640compiles it and disassembles the resulting compiled code.
641@end deffn
642
643 Here are two examples of using the @code{disassemble} function. We
644have added explanatory comments to help you relate the byte-code to the
645Lisp 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
6690 varref integer ; @r{Get the value of @code{integer}}
670 ; @r{and push it onto the stack.}
6711 constant 1 ; @r{Push 1 onto stack.}
b8d4c8d0
GM
672@end group
673
674@group
c36745c6
CY
6752 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
6803 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 6836 constant 1 ; @r{Push 1 onto top of stack.}
c36745c6
CY
6847 return ; @r{Return the top element}
685 ; @r{of the stack.}
b8d4c8d0
GM
686@end group
687
688@group
c36745c6
CY
6898:1 varref integer ; @r{Push value of @code{integer} onto stack.}
6909 constant factorial ; @r{Push @code{factorial} onto stack.}
69110 varref integer ; @r{Push value of @code{integer} onto stack.}
69211 sub1 ; @r{Pop @code{integer}, decrement value,}
b8d4c8d0 693 ; @r{push new value onto stack.}
c36745c6 69412 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
70113 mult ; @r{Pop top two values off stack, multiply}
702 ; @r{them, and push result onto stack.}
70314 return ; @r{Return the top element of stack.}
b8d4c8d0
GM
704@end group
705@end example
706
707The @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
7260 constant current-time-string ; @r{Push}
727 ; @r{@code{current-time-string}}
728 ; @r{onto top of stack.}
729@end group
730
731@group
7321 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
7382 varbind t1 ; @r{Pop stack and bind @code{t1}}
739 ; @r{to popped value.}
740@end group
741
742@group
c36745c6 7433: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
7464 sub1 ; @r{Subtract 1 from top of stack.}
747@end group
748
749@group
7505 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
7546 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
7647 constant 0 ; @r{Push 0 onto stack.}
b8d4c8d0
GM
7658 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
7719 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
77712 varref t1 ; @r{Push value of @code{t1} onto stack.}
77813 constant current-time-string ; @r{Push @code{current-time-string}}
b8d4c8d0 779 ; @r{onto top of stack.}
c36745c6 78014 call 0 ; @r{Call @code{current-time-string} again.}
b8d4c8d0
GM
781@end group
782
783@group
c36745c6
CY
78415 unbind 1 ; @r{Unbind @code{t1} in local environment.}
78516 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 78817 return ; @r{Return value of the top of stack.}
b8d4c8d0
GM
789@end group
790@end example
791