annotate man/lispref/compile.texi @ 219:262b8bb4a523 r20-4b8

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