annotate man/lispref/compile.texi @ 412:697ef44129c6 r21-2-14

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