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