annotate man/lispref/compile.texi @ 217:d44af0c54775 r20-4b7

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