0
|
1 @c -*-texinfo-*-
|
|
2 @c This is part of the XEmacs Lisp Reference Manual.
|
|
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
|
|
4 @c See the file lispref.texi for copying conditions.
|
|
5 @setfilename ../../info/compile.info
|
|
6 @node Byte Compilation, Debugging, Loading, Top
|
|
7 @chapter Byte Compilation
|
|
8 @cindex byte-code
|
|
9 @cindex compilation
|
|
10
|
|
11 XEmacs 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-coded function is called, its
|
|
15 definition is evaluated by the @dfn{byte-code interpreter}.
|
|
16
|
|
17 Because the byte-compiled code is evaluated by the byte-code
|
|
18 interpreter, instead of being executed directly by the machine's
|
|
19 hardware (as true compiled code is), byte-code is completely
|
|
20 transportable from machine to machine without recompilation. It is not,
|
|
21 however, as fast as true compiled code.
|
|
22
|
217
|
23 In general, any version of Emacs can run byte-compiled code produced
|
0
|
24 by recent earlier versions of Emacs, but the reverse is not true. In
|
217
|
25 particular, if you compile a program with XEmacs 20, the compiled code
|
|
26 may not run in earlier versions.
|
0
|
27 @iftex
|
|
28 @xref{Docs and Compilation}.
|
|
29 @end iftex
|
|
30
|
|
31 @xref{Compilation Errors}, for how to investigate errors occurring in
|
|
32 byte compilation.
|
|
33
|
|
34 @menu
|
|
35 * Speed of Byte-Code:: An example of speedup from byte compilation.
|
|
36 * Compilation Functions:: Byte compilation functions.
|
|
37 * Docs and Compilation:: Dynamic loading of documentation strings.
|
|
38 * Dynamic Loading:: Dynamic loading of individual functions.
|
|
39 * Eval During Compile:: Code to be evaluated when you compile.
|
|
40 * Compiled-Function Objects:: The data type used for byte-compiled functions.
|
|
41 * Disassembly:: Disassembling byte-code; how to read byte-code.
|
|
42 @end menu
|
|
43
|
|
44 @node Speed of Byte-Code
|
|
45 @section Performance of Byte-Compiled Code
|
|
46
|
|
47 A byte-compiled function is not as efficient as a primitive function
|
|
48 written in C, but runs much faster than the version written in Lisp.
|
|
49 Here is an example:
|
|
50
|
|
51 @example
|
|
52 @group
|
|
53 (defun silly-loop (n)
|
|
54 "Return time before and after N iterations of a loop."
|
|
55 (let ((t1 (current-time-string)))
|
|
56 (while (> (setq n (1- n))
|
|
57 0))
|
|
58 (list t1 (current-time-string))))
|
|
59 @result{} silly-loop
|
|
60 @end group
|
|
61
|
|
62 @group
|
|
63 (silly-loop 100000)
|
|
64 @result{} ("Fri Mar 18 17:25:57 1994"
|
|
65 "Fri Mar 18 17:26:28 1994") ; @r{31 seconds}
|
|
66 @end group
|
|
67
|
|
68 @group
|
|
69 (byte-compile 'silly-loop)
|
217
|
70 @result{} #<compiled-function ...>
|
0
|
71 @end group
|
|
72
|
|
73 @group
|
|
74 (silly-loop 100000)
|
|
75 @result{} ("Fri Mar 18 17:26:52 1994"
|
|
76 "Fri Mar 18 17:26:58 1994") ; @r{6 seconds}
|
|
77 @end group
|
|
78 @end example
|
|
79
|
|
80 In this example, the interpreted code required 31 seconds to run,
|
|
81 whereas the byte-compiled code required 6 seconds. These results are
|
|
82 representative, but actual results will vary greatly.
|
|
83
|
|
84 @node Compilation Functions
|
|
85 @comment node-name, next, previous, up
|
|
86 @section The Compilation Functions
|
|
87 @cindex compilation functions
|
|
88
|
|
89 You can byte-compile an individual function or macro definition with
|
|
90 the @code{byte-compile} function. You can compile a whole file with
|
|
91 @code{byte-compile-file}, or several files with
|
|
92 @code{byte-recompile-directory} or @code{batch-byte-compile}.
|
|
93
|
|
94 When you run the byte compiler, you may get warnings in a buffer
|
|
95 called @samp{*Compile-Log*}. These report things in your program that
|
|
96 suggest a problem but are not necessarily erroneous.
|
|
97
|
|
98 @cindex macro compilation
|
|
99 Be careful when byte-compiling code that uses macros. Macro calls are
|
|
100 expanded when they are compiled, so the macros must already be defined
|
|
101 for proper compilation. For more details, see @ref{Compiling Macros}.
|
|
102
|
|
103 Normally, compiling a file does not evaluate the file's contents or
|
|
104 load the file. But it does execute any @code{require} calls at top
|
|
105 level in the file. One way to ensure that necessary macro definitions
|
|
106 are available during compilation is to require the file that defines
|
|
107 them (@pxref{Named Features}). To avoid loading the macro definition files
|
|
108 when someone @emph{runs} the compiled program, write
|
|
109 @code{eval-when-compile} around the @code{require} calls (@pxref{Eval
|
|
110 During Compile}).
|
|
111
|
|
112 @defun byte-compile symbol
|
|
113 This function byte-compiles the function definition of @var{symbol},
|
|
114 replacing the previous definition with the compiled one. The function
|
|
115 definition of @var{symbol} must be the actual code for the function;
|
|
116 i.e., the compiler does not follow indirection to another symbol.
|
|
117 @code{byte-compile} returns the new, compiled definition of
|
|
118 @var{symbol}.
|
|
119
|
|
120 If @var{symbol}'s definition is a compiled-function object,
|
|
121 @code{byte-compile} does nothing and returns @code{nil}. Lisp records
|
|
122 only one function definition for any symbol, and if that is already
|
|
123 compiled, non-compiled code is not available anywhere. So there is no
|
|
124 way to ``compile the same definition again.''
|
|
125
|
|
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
|
|
134
|
|
135 @group
|
|
136 (byte-compile 'factorial)
|
|
137 @result{}
|
217
|
138 #<compiled-function (integer)
|
|
139 "ÁU«‚Á‡ÂS!_‡"
|
|
140 [integer 1 factorial]
|
|
141 3 "Compute factorial of INTEGER.">
|
0
|
142 @end group
|
|
143 @end example
|
|
144
|
|
145 @noindent
|
|
146 The result is a compiled-function object. The string it contains is
|
|
147 the actual byte-code; each character in it is an instruction or an
|
|
148 operand of an instruction. The vector contains all the constants,
|
|
149 variable names and function names used by the function, except for
|
|
150 certain primitives that are coded as special instructions.
|
|
151 @end defun
|
|
152
|
|
153 @deffn Command compile-defun &optional arg
|
|
154 This command reads the defun containing point, compiles it, and
|
|
155 evaluates the result. If you use this on a defun that is actually a
|
|
156 function definition, the effect is to install a compiled version of that
|
|
157 function.
|
|
158
|
|
159 @c XEmacs feature
|
|
160 If @var{arg} is non-@code{nil}, the result is inserted in the current
|
|
161 buffer after the form; otherwise, it is printed in the minibuffer.
|
|
162 @end deffn
|
|
163
|
|
164 @deffn Command byte-compile-file filename &optional load
|
|
165 This function compiles a file of Lisp code named @var{filename} into
|
|
166 a file of byte-code. The output file's name is made by appending
|
|
167 @samp{c} to the end of @var{filename}.
|
|
168
|
|
169 @c XEmacs feature
|
|
170 If @code{load} is non-@code{nil}, the file is loaded after having been
|
|
171 compiled.
|
|
172
|
|
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.
|
|
179
|
|
180 This command returns @code{t}. When called interactively, it prompts
|
|
181 for the file name.
|
|
182
|
|
183 @example
|
|
184 @group
|
|
185 % ls -l push*
|
|
186 -rw-r--r-- 1 lewis 791 Oct 5 20:31 push.el
|
|
187 @end group
|
|
188
|
|
189 @group
|
|
190 (byte-compile-file "~/emacs/push.el")
|
|
191 @result{} t
|
|
192 @end group
|
|
193
|
|
194 @group
|
|
195 % ls -l push*
|
|
196 -rw-r--r-- 1 lewis 791 Oct 5 20:31 push.el
|
|
197 -rw-rw-rw- 1 lewis 638 Oct 8 20:25 push.elc
|
|
198 @end group
|
|
199 @end example
|
|
200 @end deffn
|
|
201
|
|
202 @c flag is not optional in FSF Emacs
|
|
203 @deffn Command byte-recompile-directory directory &optional flag
|
|
204 @cindex library compilation
|
|
205 This function recompiles every @samp{.el} file in @var{directory} that
|
|
206 needs recompilation. A file needs recompilation if a @samp{.elc} file
|
|
207 exists but is older than the @samp{.el} file.
|
|
208
|
|
209 When a @samp{.el} file has no corresponding @samp{.elc} file, then
|
|
210 @var{flag} says what to do. If it is @code{nil}, these files are
|
|
211 ignored. If it is non-@code{nil}, the user is asked whether to compile
|
|
212 each such file.
|
|
213
|
|
214 The returned value of this command is unpredictable.
|
|
215 @end deffn
|
|
216
|
|
217 @defun batch-byte-compile
|
|
218 This function runs @code{byte-compile-file} on files specified on the
|
|
219 command line. This function must be used only in a batch execution of
|
|
220 Emacs, as it kills Emacs on completion. An error in one file does not
|
|
221 prevent processing of subsequent files. (The file that gets the error
|
|
222 will not, of course, produce any compiled code.)
|
|
223
|
|
224 @example
|
|
225 % emacs -batch -f batch-byte-compile *.el
|
|
226 @end example
|
|
227 @end defun
|
|
228
|
|
229 @c XEmacs feature
|
|
230 @defun batch-byte-recompile-directory
|
|
231 This function is similar to @code{batch-byte-compile} but runs the
|
|
232 command @code{byte-recompile-directory} on the files remaining on the
|
|
233 command line.
|
|
234 @end defun
|
|
235
|
|
236 @c XEmacs feature
|
|
237 @defvar byte-recompile-directory-ignore-errors-p
|
|
238 If non-@code{nil}, this specifies that @code{byte-recompile-directory}
|
|
239 will continue compiling even when an error occurs in a file. This is
|
|
240 normally @code{nil}, but is bound to @code{t} by
|
|
241 @code{batch-byte-recompile-directory}.
|
|
242 @end defvar
|
|
243
|
|
244 @defun byte-code code-string data-vector max-stack
|
|
245 @cindex byte-code interpreter
|
|
246 This function actually interprets byte-code. A byte-compiled function
|
|
247 is actually defined with a body that calls @code{byte-code}. Don't call
|
|
248 this function yourself. Only the byte compiler knows how to generate
|
|
249 valid calls to this function.
|
|
250
|
|
251 In newer Emacs versions (19 and up), byte-code is usually executed as
|
|
252 part of a compiled-function object, and only rarely due to an explicit
|
|
253 call to @code{byte-code}.
|
|
254 @end defun
|
|
255
|
|
256 @node Docs and Compilation
|
|
257 @section Documentation Strings and Compilation
|
|
258 @cindex dynamic loading of documentation
|
|
259
|
|
260 Functions and variables loaded from a byte-compiled file access their
|
|
261 documentation strings dynamically from the file whenever needed. This
|
|
262 saves space within Emacs, and makes loading faster because the
|
|
263 documentation strings themselves need not be processed while loading the
|
|
264 file. Actual access to the documentation strings becomes slower as a
|
|
265 result, but normally not enough to bother users.
|
|
266
|
|
267 Dynamic access to documentation strings does have drawbacks:
|
|
268
|
|
269 @itemize @bullet
|
|
270 @item
|
|
271 If you delete or move the compiled file after loading it, Emacs can no
|
|
272 longer access the documentation strings for the functions and variables
|
|
273 in the file.
|
|
274
|
|
275 @item
|
|
276 If you alter the compiled file (such as by compiling a new version),
|
|
277 then further access to documentation strings in this file will give
|
|
278 nonsense results.
|
|
279 @end itemize
|
|
280
|
|
281 If your site installs Emacs following the usual procedures, these
|
|
282 problems will never normally occur. Installing a new version uses a new
|
|
283 directory with a different name; as long as the old version remains
|
|
284 installed, its files will remain unmodified in the places where they are
|
|
285 expected to be.
|
|
286
|
|
287 However, if you have built Emacs yourself and use it from the
|
|
288 directory where you built it, you will experience this problem
|
|
289 occasionally if you edit and recompile Lisp files. When it happens, you
|
|
290 can cure the problem by reloading the file after recompiling it.
|
|
291
|
|
292 Byte-compiled files made with Emacs 19.29 will not load into older
|
|
293 versions because the older versions don't support this feature. You can
|
|
294 turn off this feature by setting @code{byte-compile-dynamic-docstrings}
|
|
295 to @code{nil}. Once this is done, you can compile files that will load
|
|
296 into older Emacs versions. You can do this globally, or for one source
|
|
297 file by specifying a file-local binding for the variable. Here's one
|
|
298 way to do that:
|
|
299
|
|
300 @example
|
|
301 -*-byte-compile-dynamic-docstrings: nil;-*-
|
|
302 @end example
|
|
303
|
|
304 @defvar byte-compile-dynamic-docstrings
|
|
305 If this is non-@code{nil}, the byte compiler generates compiled files
|
|
306 that are set up for dynamic loading of documentation strings.
|
|
307 @end defvar
|
|
308
|
|
309 @cindex @samp{#@@@var{count}}
|
|
310 @cindex @samp{#$}
|
|
311 The dynamic documentation string feature writes compiled files that
|
|
312 use a special Lisp reader construct, @samp{#@@@var{count}}. This
|
|
313 construct skips the next @var{count} characters. It also uses the
|
|
314 @samp{#$} construct, which stands for ``the name of this file, as a
|
|
315 string.'' It is best not to use these constructs in Lisp source files.
|
|
316
|
|
317 @node Dynamic Loading
|
|
318 @section Dynamic Loading of Individual Functions
|
|
319
|
|
320 @cindex dynamic loading of functions
|
|
321 @cindex lazy loading
|
|
322 When you compile a file, you can optionally enable the @dfn{dynamic
|
|
323 function loading} feature (also known as @dfn{lazy loading}). With
|
|
324 dynamic function loading, loading the file doesn't fully read the
|
|
325 function definitions in the file. Instead, each function definition
|
|
326 contains a place-holder which refers to the file. The first time each
|
|
327 function is called, it reads the full definition from the file, to
|
|
328 replace the place-holder.
|
|
329
|
|
330 The advantage of dynamic function loading is that loading the file
|
|
331 becomes much faster. This is a good thing for a file which contains
|
|
332 many separate commands, provided that using one of them does not imply
|
|
333 you will soon (or ever) use the rest. A specialized mode which provides
|
|
334 many keyboard commands often has that usage pattern: a user may invoke
|
|
335 the mode, but use only a few of the commands it provides.
|
|
336
|
|
337 The dynamic loading feature has certain disadvantages:
|
|
338
|
|
339 @itemize @bullet
|
|
340 @item
|
|
341 If you delete or move the compiled file after loading it, Emacs can no
|
|
342 longer load the remaining function definitions not already loaded.
|
|
343
|
|
344 @item
|
|
345 If you alter the compiled file (such as by compiling a new version),
|
|
346 then trying to load any function not already loaded will get nonsense
|
|
347 results.
|
|
348 @end itemize
|
|
349
|
|
350 If you compile a new version of the file, the best thing to do is
|
|
351 immediately load the new compiled file. That will prevent any future
|
|
352 problems.
|
|
353
|
|
354 The byte compiler uses the dynamic function loading feature if the
|
|
355 variable @code{byte-compile-dynamic} is non-@code{nil} at compilation
|
|
356 time. Do not set this variable globally, since dynamic loading is
|
|
357 desirable only for certain files. Instead, enable the feature for
|
|
358 specific source files with file-local variable bindings, like this:
|
|
359
|
|
360 @example
|
|
361 -*-byte-compile-dynamic: t;-*-
|
|
362 @end example
|
|
363
|
|
364 @defvar byte-compile-dynamic
|
|
365 If this is non-@code{nil}, the byte compiler generates compiled files
|
|
366 that are set up for dynamic function loading.
|
|
367 @end defvar
|
|
368
|
|
369 @defun fetch-bytecode function
|
|
370 This immediately finishes loading the definition of @var{function} from
|
|
371 its byte-compiled file, if it is not fully loaded already. The argument
|
|
372 @var{function} may be a compiled-function object or a function name.
|
|
373 @end defun
|
|
374
|
|
375 @node Eval During Compile
|
|
376 @section Evaluation During Compilation
|
|
377
|
|
378 These features permit you to write code to be evaluated during
|
|
379 compilation of a program.
|
|
380
|
|
381 @defspec eval-and-compile body
|
|
382 This form marks @var{body} to be evaluated both when you compile the
|
|
383 containing code and when you run it (whether compiled or not).
|
|
384
|
|
385 You can get a similar result by putting @var{body} in a separate file
|
|
386 and referring to that file with @code{require}. Using @code{require} is
|
|
387 preferable if there is a substantial amount of code to be executed in
|
|
388 this way.
|
|
389 @end defspec
|
|
390
|
|
391 @defspec eval-when-compile body
|
|
392 This form marks @var{body} to be evaluated at compile time and not when
|
|
393 the compiled program is loaded. The result of evaluation by the
|
|
394 compiler becomes a constant which appears in the compiled program. When
|
|
395 the program is interpreted, not compiled at all, @var{body} is evaluated
|
|
396 normally.
|
|
397
|
|
398 At top level, this is analogous to the Common Lisp idiom
|
|
399 @code{(eval-when (compile eval) @dots{})}. Elsewhere, the Common Lisp
|
|
400 @samp{#.} reader macro (but not when interpreting) is closer to what
|
|
401 @code{eval-when-compile} does.
|
|
402 @end defspec
|
|
403
|
|
404 @node Compiled-Function Objects
|
|
405 @section Compiled-Function Objects
|
|
406 @cindex compiled function
|
|
407 @cindex byte-code function
|
|
408
|
|
409 Byte-compiled functions have a special data type: they are
|
|
410 @dfn{compiled-function objects}.
|
|
411
|
|
412 A compiled-function object is a bit like a vector; however, the
|
|
413 evaluator handles this data type specially when it appears as a function
|
|
414 to be called. The printed representation for a compiled-function
|
|
415 object normally begins with @samp{#<compiled-function} and ends with
|
|
416 @samp{>}. However, if the variable @code{print-readably} is
|
|
417 non-@code{nil}, the object is printed beginning with @samp{#[} and
|
|
418 ending with @samp{]}. This representation can be read directly
|
|
419 by the Lisp reader, and is used in byte-compiled files (those ending
|
|
420 in @samp{.elc}).
|
|
421
|
|
422 In Emacs version 18, there was no compiled-function object data type;
|
|
423 compiled functions used the function @code{byte-code} to run the byte
|
|
424 code.
|
|
425
|
|
426 A compiled-function object has a number of different elements.
|
|
427 They are:
|
|
428
|
|
429 @table @var
|
|
430 @item arglist
|
|
431 The list of argument symbols.
|
|
432
|
|
433 @item instructions
|
|
434 The string containing the byte-code instructions.
|
|
435
|
|
436 @item constants
|
|
437 The vector of Lisp objects referenced by the byte code. These include
|
|
438 symbols used as function names and variable names.
|
|
439
|
|
440 @item stacksize
|
|
441 The maximum stack size this function needs.
|
|
442
|
|
443 @item doc-string
|
|
444 The documentation string (if any); otherwise, @code{nil}. The value may
|
|
445 be a number or a list, in case the documentation string is stored in a
|
|
446 file. Use the function @code{documentation} to get the real
|
|
447 documentation string (@pxref{Accessing Documentation}).
|
|
448
|
|
449 @item interactive
|
|
450 The interactive spec (if any). This can be a string or a Lisp
|
|
451 expression. It is @code{nil} for a function that isn't interactive.
|
|
452
|
|
453 @item domain
|
|
454 The domain (if any). This is only meaningful if I18N3 (message-translation)
|
|
455 support was compiled into XEmacs. This is a string defining which
|
|
456 domain to find the translation for the documentation string and
|
|
457 interactive prompt. @xref{Domain Specification}.
|
|
458 @end table
|
|
459
|
|
460 Here's an example of a compiled-function object, in printed
|
|
461 representation. It is the definition of the command
|
|
462 @code{backward-sexp}.
|
|
463
|
|
464 @example
|
|
465 #<compiled-function
|
|
466 (&optional arg)
|
|
467 "¬‚ÁÂ[!‡"
|
|
468 [arg 1 forward-sexp]
|
|
469 2
|
|
470 579173
|
|
471 "_p">
|
|
472 @end example
|
|
473
|
|
474 The primitive way to create a compiled-function object is with
|
|
475 @code{make-byte-code}:
|
|
476
|
|
477 @defun make-byte-code &rest elements
|
|
478 This function constructs and returns a compiled-function object
|
|
479 with @var{elements} as its elements.
|
|
480
|
|
481 @emph{NOTE:} Unlike all other Emacs-lisp functions, calling this with
|
|
482 five arguments is @emph{not} the same as calling it with six arguments,
|
|
483 the last of which is @code{nil}. If the @var{interactive} arg is
|
|
484 specified as @code{nil}, then that means that this function was defined
|
|
485 with @code{(interactive)}. If the arg is not specified, then that means
|
|
486 the function is not interactive. This is terrible behavior which is
|
|
487 retained for compatibility with old @samp{.elc} files which expected
|
|
488 these semantics.
|
|
489 @end defun
|
|
490
|
|
491 You should not try to come up with the elements for a compiled-function
|
|
492 object yourself, because if they are inconsistent, XEmacs may crash
|
|
493 when you call the function. Always leave it to the byte compiler to
|
|
494 create these objects; it makes the elements consistent (we hope).
|
|
495
|
|
496 The following primitives are provided for accessing the elements of
|
|
497 a compiled-function object.
|
|
498
|
|
499 @defun compiled-function-arglist function
|
|
500 This function returns the argument list of compiled-function object
|
|
501 @var{function}.
|
|
502 @end defun
|
|
503
|
|
504 @defun compiled-function-instructions function
|
|
505 This function returns a string describing the byte-code instructions
|
|
506 of compiled-function object @var{function}.
|
|
507 @end defun
|
|
508
|
|
509 @defun compiled-function-constants function
|
|
510 This function returns the vector of Lisp objects referenced by
|
|
511 compiled-function object @var{function}.
|
|
512 @end defun
|
|
513
|
|
514 @defun compiled-function-stack-size function
|
|
515 This function returns the maximum stack size needed by compiled-function
|
|
516 object @var{function}.
|
|
517 @end defun
|
|
518
|
|
519 @defun compiled-function-doc-string function
|
|
520 This function returns the doc string of compiled-function object
|
|
521 @var{function}, if available.
|
|
522 @end defun
|
|
523
|
|
524 @defun compiled-function-interactive function
|
|
525 This function returns the interactive spec of compiled-function object
|
|
526 @var{function}, if any. The return value is @code{nil} or a two-element
|
|
527 list, the first element of which is the symbol @code{interactive} and
|
|
528 the second element is the interactive spec (a string or Lisp form).
|
|
529 @end defun
|
|
530
|
|
531 @defun compiled-function-domain function
|
|
532 This function returns the domain of compiled-function object
|
|
533 @var{function}, if any. The result will be a string or @code{nil}.
|
|
534 @xref{Domain Specification}.
|
|
535 @end defun
|
|
536
|
|
537 @node Disassembly
|
|
538 @section Disassembled Byte-Code
|
|
539 @cindex disassembled byte-code
|
|
540
|
|
541 People do not write byte-code; that job is left to the byte compiler.
|
|
542 But we provide a disassembler to satisfy a cat-like curiosity. The
|
|
543 disassembler converts the byte-compiled code into humanly readable
|
|
544 form.
|
|
545
|
|
546 The byte-code interpreter is implemented as a simple stack machine.
|
|
547 It pushes values onto a stack of its own, then pops them off to use them
|
|
548 in calculations whose results are themselves pushed back on the stack.
|
|
549 When a byte-code function returns, it pops a value off the stack and
|
|
550 returns it as the value of the function.
|
|
551
|
|
552 In addition to the stack, byte-code functions can use, bind, and set
|
|
553 ordinary Lisp variables, by transferring values between variables and
|
|
554 the stack.
|
|
555
|
|
556 @deffn Command disassemble object &optional stream
|
|
557 This function prints the disassembled code for @var{object}. If
|
|
558 @var{stream} is supplied, then output goes there. Otherwise, the
|
|
559 disassembled code is printed to the stream @code{standard-output}. The
|
|
560 argument @var{object} can be a function name or a lambda expression.
|
|
561
|
|
562 As a special exception, if this function is used interactively,
|
|
563 it outputs to a buffer named @samp{*Disassemble*}.
|
|
564 @end deffn
|
|
565
|
|
566 Here are two examples of using the @code{disassemble} function. We
|
|
567 have added explanatory comments to help you relate the byte-code to the
|
|
568 Lisp source; these do not appear in the output of @code{disassemble}.
|
|
569 These examples show unoptimized byte-code. Nowadays byte-code is
|
|
570 usually optimized, but we did not want to rewrite these examples, since
|
|
571 they still serve their purpose.
|
|
572
|
|
573 @example
|
|
574 @group
|
|
575 (defun factorial (integer)
|
|
576 "Compute factorial of an integer."
|
|
577 (if (= 1 integer) 1
|
|
578 (* integer (factorial (1- integer)))))
|
|
579 @result{} factorial
|
|
580 @end group
|
|
581
|
|
582 @group
|
|
583 (factorial 4)
|
|
584 @result{} 24
|
|
585 @end group
|
|
586
|
|
587 @group
|
|
588 (disassemble 'factorial)
|
|
589 @print{} byte-code for factorial:
|
|
590 doc: Compute factorial of an integer.
|
|
591 args: (integer)
|
|
592 @end group
|
|
593
|
|
594 @group
|
|
595 0 constant 1 ; @r{Push 1 onto stack.}
|
|
596
|
|
597 1 varref integer ; @r{Get value of @code{integer}}
|
|
598 ; @r{from the environment}
|
|
599 ; @r{and push the value}
|
|
600 ; @r{onto the stack.}
|
|
601 @end group
|
|
602
|
|
603 @group
|
|
604 2 eqlsign ; @r{Pop top two values off stack,}
|
|
605 ; @r{compare them,}
|
|
606 ; @r{and push result onto stack.}
|
|
607 @end group
|
|
608
|
|
609 @group
|
|
610 3 goto-if-nil 10 ; @r{Pop and test top of stack;}
|
|
611 ; @r{if @code{nil}, go to 10,}
|
|
612 ; @r{else continue.}
|
|
613 @end group
|
|
614
|
|
615 @group
|
|
616 6 constant 1 ; @r{Push 1 onto top of stack.}
|
|
617
|
|
618 7 goto 17 ; @r{Go to 17 (in this case, 1 will be}
|
|
619 ; @r{returned by the function).}
|
|
620 @end group
|
|
621
|
|
622 @group
|
|
623 10 constant * ; @r{Push symbol @code{*} onto stack.}
|
|
624
|
|
625 11 varref integer ; @r{Push value of @code{integer} onto stack.}
|
|
626 @end group
|
|
627
|
|
628 @group
|
|
629 12 constant factorial ; @r{Push @code{factorial} onto stack.}
|
|
630
|
|
631 13 varref integer ; @r{Push value of @code{integer} onto stack.}
|
|
632
|
|
633 14 sub1 ; @r{Pop @code{integer}, decrement value,}
|
|
634 ; @r{push new value onto stack.}
|
|
635 @end group
|
|
636
|
|
637 @group
|
|
638 ; @r{Stack now contains:}
|
|
639 ; @minus{} @r{decremented value of @code{integer}}
|
|
640 ; @minus{} @r{@code{factorial}}
|
|
641 ; @minus{} @r{value of @code{integer}}
|
|
642 ; @minus{} @r{@code{*}}
|
|
643 @end group
|
|
644
|
|
645 @group
|
|
646 15 call 1 ; @r{Call function @code{factorial} using}
|
|
647 ; @r{the first (i.e., the top) element}
|
|
648 ; @r{of the stack as the argument;}
|
|
649 ; @r{push returned value onto stack.}
|
|
650 @end group
|
|
651
|
|
652 @group
|
|
653 ; @r{Stack now contains:}
|
|
654 ; @minus{} @r{result of recursive}
|
|
655 ; @r{call to @code{factorial}}
|
|
656 ; @minus{} @r{value of @code{integer}}
|
|
657 ; @minus{} @r{@code{*}}
|
|
658 @end group
|
|
659
|
|
660 @group
|
|
661 16 call 2 ; @r{Using the first two}
|
|
662 ; @r{(i.e., the top two)}
|
|
663 ; @r{elements of the stack}
|
|
664 ; @r{as arguments,}
|
|
665 ; @r{call the function @code{*},}
|
|
666 ; @r{pushing the result onto the stack.}
|
|
667 @end group
|
|
668
|
|
669 @group
|
|
670 17 return ; @r{Return the top element}
|
|
671 ; @r{of the stack.}
|
|
672 @result{} nil
|
|
673 @end group
|
|
674 @end example
|
|
675
|
|
676 The @code{silly-loop} function is somewhat more complex:
|
|
677
|
|
678 @example
|
|
679 @group
|
|
680 (defun silly-loop (n)
|
|
681 "Return time before and after N iterations of a loop."
|
|
682 (let ((t1 (current-time-string)))
|
|
683 (while (> (setq n (1- n))
|
|
684 0))
|
|
685 (list t1 (current-time-string))))
|
|
686 @result{} silly-loop
|
|
687 @end group
|
|
688
|
|
689 @group
|
|
690 (disassemble 'silly-loop)
|
|
691 @print{} byte-code for silly-loop:
|
|
692 doc: Return time before and after N iterations of a loop.
|
|
693 args: (n)
|
|
694
|
|
695 0 constant current-time-string ; @r{Push}
|
|
696 ; @r{@code{current-time-string}}
|
|
697 ; @r{onto top of stack.}
|
|
698 @end group
|
|
699
|
|
700 @group
|
|
701 1 call 0 ; @r{Call @code{current-time-string}}
|
|
702 ; @r{ with no argument,}
|
|
703 ; @r{ pushing result onto stack.}
|
|
704 @end group
|
|
705
|
|
706 @group
|
|
707 2 varbind t1 ; @r{Pop stack and bind @code{t1}}
|
|
708 ; @r{to popped value.}
|
|
709 @end group
|
|
710
|
|
711 @group
|
|
712 3 varref n ; @r{Get value of @code{n} from}
|
|
713 ; @r{the environment and push}
|
|
714 ; @r{the value onto the stack.}
|
|
715 @end group
|
|
716
|
|
717 @group
|
|
718 4 sub1 ; @r{Subtract 1 from top of stack.}
|
|
719 @end group
|
|
720
|
|
721 @group
|
|
722 5 dup ; @r{Duplicate the top of the stack;}
|
|
723 ; @r{i.e., copy the top of}
|
|
724 ; @r{the stack and push the}
|
|
725 ; @r{copy onto the stack.}
|
|
726 @end group
|
|
727
|
|
728 @group
|
|
729 6 varset n ; @r{Pop the top of the stack,}
|
|
730 ; @r{and bind @code{n} to the value.}
|
|
731
|
|
732 ; @r{In effect, the sequence @code{dup varset}}
|
|
733 ; @r{copies the top of the stack}
|
|
734 ; @r{into the value of @code{n}}
|
|
735 ; @r{without popping it.}
|
|
736 @end group
|
|
737
|
|
738 @group
|
|
739 7 constant 0 ; @r{Push 0 onto stack.}
|
|
740 @end group
|
|
741
|
|
742 @group
|
|
743 8 gtr ; @r{Pop top two values off stack,}
|
|
744 ; @r{test if @var{n} is greater than 0}
|
|
745 ; @r{and push result onto stack.}
|
|
746 @end group
|
|
747
|
|
748 @group
|
|
749 9 goto-if-nil-else-pop 17 ; @r{Goto 17 if @code{n} <= 0}
|
|
750 ; @r{(this exits the while loop).}
|
|
751 ; @r{else pop top of stack}
|
|
752 ; @r{and continue}
|
|
753 @end group
|
|
754
|
|
755 @group
|
|
756 12 constant nil ; @r{Push @code{nil} onto stack}
|
|
757 ; @r{(this is the body of the loop).}
|
|
758 @end group
|
|
759
|
|
760 @group
|
|
761 13 discard ; @r{Discard result of the body}
|
|
762 ; @r{of the loop (a while loop}
|
|
763 ; @r{is always evaluated for}
|
|
764 ; @r{its side effects).}
|
|
765 @end group
|
|
766
|
|
767 @group
|
|
768 14 goto 3 ; @r{Jump back to beginning}
|
|
769 ; @r{of while loop.}
|
|
770 @end group
|
|
771
|
|
772 @group
|
|
773 17 discard ; @r{Discard result of while loop}
|
|
774 ; @r{by popping top of stack.}
|
|
775 ; @r{This result is the value @code{nil} that}
|
|
776 ; @r{was not popped by the goto at 9.}
|
|
777 @end group
|
|
778
|
|
779 @group
|
|
780 18 varref t1 ; @r{Push value of @code{t1} onto stack.}
|
|
781 @end group
|
|
782
|
|
783 @group
|
|
784 19 constant current-time-string ; @r{Push}
|
|
785 ; @r{@code{current-time-string}}
|
|
786 ; @r{onto top of stack.}
|
|
787 @end group
|
|
788
|
|
789 @group
|
|
790 20 call 0 ; @r{Call @code{current-time-string} again.}
|
|
791 @end group
|
|
792
|
|
793 @group
|
|
794 21 list2 ; @r{Pop top two elements off stack,}
|
|
795 ; @r{create a list of them,}
|
|
796 ; @r{and push list onto stack.}
|
|
797 @end group
|
|
798
|
|
799 @group
|
|
800 22 unbind 1 ; @r{Unbind @code{t1} in local environment.}
|
|
801
|
|
802 23 return ; @r{Return value of the top of stack.}
|
|
803
|
|
804 @result{} nil
|
|
805 @end group
|
|
806 @end example
|
|
807
|
|
808
|