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