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