annotate man/lispref/compile.texi @ 919:111c4f2ed9c9

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