aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman1994-03-21 17:04:32 +0000
committerRichard M. Stallman1994-03-21 17:04:32 +0000
commita0acfc98dc8b718dc08361c5d2723b3a3ccaa8ff (patch)
treedc924b418066bff4b8ae40fd6739b53aeef882dc
parenta44af9f249228b7ef191d809b5d0d385489bb38a (diff)
downloademacs-a0acfc98dc8b718dc08361c5d2723b3a3ccaa8ff.tar.gz
emacs-a0acfc98dc8b718dc08361c5d2723b3a3ccaa8ff.zip
*** empty log message ***
-rw-r--r--lispref/compile.texi151
1 files changed, 81 insertions, 70 deletions
diff --git a/lispref/compile.texi b/lispref/compile.texi
index 3abe84150fe..38f2b8196f3 100644
--- a/lispref/compile.texi
+++ b/lispref/compile.texi
@@ -1,6 +1,6 @@
1@c -*-texinfo-*- 1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual. 2@c This is part of the GNU Emacs Lisp Reference Manual.
3@c Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc. 3@c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4@c See the file elisp.texi for copying conditions. 4@c See the file elisp.texi for copying conditions.
5@setfilename ../info/compile 5@setfilename ../info/compile
6@node Byte Compilation, Debugging, Loading, Top 6@node Byte Compilation, Debugging, Loading, Top
@@ -29,39 +29,19 @@ compiled code in Emacs 19, but not vice versa.
29byte compilation. 29byte compilation.
30 30
31@menu 31@menu
32* Speed of Byte-Code:: An example of speedup from byte compilation.
32* Compilation Functions:: Byte compilation functions. 33* Compilation Functions:: Byte compilation functions.
33* Eval During Compile:: Code to be evaluated when you compile. 34* Eval During Compile:: Code to be evaluated when you compile.
34* Byte-Code Objects:: The data type used for byte-compiled functions. 35* Byte-Code Objects:: The data type used for byte-compiled functions.
35* Disassembly:: Disassembling byte-code; how to read byte-code. 36* Disassembly:: Disassembling byte-code; how to read byte-code.
36@end menu 37@end menu
37 38
38@node Compilation Functions 39@node Speed of Byte-Code
39@comment node-name, next, previous, up 40@section Performance of Byte-Compiled Code
40@section The Compilation Functions
41@cindex compilation functions
42
43 You can byte-compile an individual function or macro definition with
44the @code{byte-compile} function. You can compile a whole file with
45@code{byte-compile-file}, or several files with
46@code{byte-recompile-directory} or @code{batch-byte-compile}.
47
48 When you run the byte compiler, you may get warnings in a buffer called
49@samp{*Compile-Log*}. These report usage in your program that suggest a
50problem, but are not necessarily erroneous.
51
52@cindex macro compilation
53 Be careful when byte-compiling code that uses macros. Macro calls are
54expanded when they are compiled, so the macros must already be defined
55for proper compilation. For more details, see @ref{Compiling Macros}.
56
57 While byte-compiling a file, any @code{require} calls at top-level are
58executed. One way to ensure that necessary macro definitions are
59available during compilation is to require the file that defines them.
60@xref{Features}.
61 41
62 A byte-compiled function is not as efficient as a primitive function 42 A byte-compiled function is not as efficient as a primitive function
63written in C, but runs much faster than the version written in Lisp. 43written in C, but runs much faster than the version written in Lisp.
64For a rough comparison, consider the example below: 44Here is an example:
65 45
66@example 46@example
67@group 47@group
@@ -76,8 +56,8 @@ For a rough comparison, consider the example below:
76 56
77@group 57@group
78(silly-loop 100000) 58(silly-loop 100000)
79@result{} ("Thu Jan 12 20:18:38 1989" 59@result{} ("Fri Mar 18 17:25:57 1994"
80 "Thu Jan 12 20:19:29 1989") ; @r{51 seconds} 60 "Fri Mar 18 17:26:28 1994") ; @r{31 seconds}
81@end group 61@end group
82 62
83@group 63@group
@@ -87,22 +67,53 @@ For a rough comparison, consider the example below:
87 67
88@group 68@group
89(silly-loop 100000) 69(silly-loop 100000)
90@result{} ("Thu Jan 12 20:21:04 1989" 70@result{} ("Fri Mar 18 17:26:52 1994"
91 "Thu Jan 12 20:21:17 1989") ; @r{13 seconds} 71 "Fri Mar 18 17:26:58 1994") ; @r{6 seconds}
92@end group 72@end group
93@end example 73@end example
94 74
95 In this example, the interpreted code required 51 seconds to run, 75 In this example, the interpreted code required 31 seconds to run,
96whereas the byte-compiled code required 13 seconds. These results are 76whereas the byte-compiled code required 6 seconds. These results are
97representative, but actual results will vary greatly. 77representative, but actual results will vary greatly.
98 78
79@node Compilation Functions
80@comment node-name, next, previous, up
81@section The Compilation Functions
82@cindex compilation functions
83
84 You can byte-compile an individual function or macro definition with
85the @code{byte-compile} function. You can compile a whole file with
86@code{byte-compile-file}, or several files with
87@code{byte-recompile-directory} or @code{batch-byte-compile}.
88
89 When you run the byte compiler, you may get warnings in a buffer called
90@samp{*Compile-Log*}. These report usage in your program that suggest a
91problem, but are not necessarily erroneous.
92
93@cindex macro compilation
94 Be careful when byte-compiling code that uses macros. Macro calls are
95expanded when they are compiled, so the macros must already be defined
96for proper compilation. For more details, see @ref{Compiling Macros}.
97
98 Normally, compiling a file does not evaluate the file's contents or
99load the file. But it does execute any @code{require} calls at
100top-level in the file. One way to ensure that necessary macro
101definitions are available during compilation is to require the file that
102defines them. @xref{Features}.
103
99@defun byte-compile symbol 104@defun byte-compile symbol
100 This function byte-compiles the function definition of @var{symbol}, 105This function byte-compiles the function definition of @var{symbol},
101replacing the previous definition with the compiled one. The function 106replacing the previous definition with the compiled one. The function
102definition of @var{symbol} must be the actual code for the function; 107definition of @var{symbol} must be the actual code for the function;
103i.e., the compiler does not follow indirection to another symbol. 108i.e., the compiler does not follow indirection to another symbol.
104@code{byte-compile} does not compile macros. @code{byte-compile} 109@code{byte-compile} returns the new, compiled definition of
105returns the new, compiled definition of @var{symbol}. 110@var{symbol}.
111
112If @var{symbol}'s definition is a byte-code function object,
113@code{byte-compile} does nothing and returns @code{nil}. Lisp records
114only one function definition for any symbol, and if that is already
115compiled, non-compiled code is not available anywhere. So there is no
116way to ``compile the same definition again.''
106 117
107@example 118@example
108@group 119@group
@@ -110,12 +121,12 @@ returns the new, compiled definition of @var{symbol}.
110 "Compute factorial of INTEGER." 121 "Compute factorial of INTEGER."
111 (if (= 1 integer) 1 122 (if (= 1 integer) 1
112 (* integer (factorial (1- integer))))) 123 (* integer (factorial (1- integer)))))
113 @result{} factorial 124@result{} factorial
114@end group 125@end group
115 126
116@group 127@group
117(byte-compile 'factorial) 128(byte-compile 'factorial)
118 @result{} 129@result{}
119#[(integer) 130#[(integer)
120 "^H\301U\203^H^@@\301\207\302^H\303^HS!\"\207" 131 "^H\301U\203^H^@@\301\207\302^H\303^HS!\"\207"
121 [integer 1 * factorial] 132 [integer 1 * factorial]
@@ -124,11 +135,11 @@ returns the new, compiled definition of @var{symbol}.
124@end example 135@end example
125 136
126@noindent 137@noindent
127The result is a compiled function object. The string it contains is the 138The result is a byte-code function object. The string it contains is
128actual byte-code; each character in it is an instruction. The vector 139the actual byte-code; each character in it is an instruction or an
129contains all the constants, variable names and function names used by 140operand of an instruction. The vector contains all the constants,
130the function, except for certain primitives that are coded as special 141variable names and function names used by the function, except for
131instructions. 142certain primitives that are coded as special instructions.
132@end defun 143@end defun
133 144
134@deffn Command compile-defun 145@deffn Command compile-defun
@@ -139,18 +150,18 @@ function.
139@end deffn 150@end deffn
140 151
141@deffn Command byte-compile-file filename 152@deffn Command byte-compile-file filename
142 This function compiles a file of Lisp code named @var{filename} into 153This function compiles a file of Lisp code named @var{filename} into
143a file of byte-code. The output file's name is made by appending 154a file of byte-code. The output file's name is made by appending
144@samp{c} to the end of @var{filename}. 155@samp{c} to the end of @var{filename}.
145 156
146 Compilation works by reading the input file one form at a time. If it 157Compilation works by reading the input file one form at a time. If it
147is a definition of a function or macro, the compiled function or macro 158is a definition of a function or macro, the compiled function or macro
148definition is written out. Other forms are batched together, then each 159definition is written out. Other forms are batched together, then each
149batch is compiled, and written so that its compiled code will be 160batch is compiled, and written so that its compiled code will be
150executed when the file is read. All comments are discarded when the 161executed when the file is read. All comments are discarded when the
151input file is read. 162input file is read.
152 163
153 This command returns @code{t}. When called interactively, it prompts 164This command returns @code{t}. When called interactively, it prompts
154for the file name. 165for the file name.
155 166
156@example 167@example
@@ -174,24 +185,24 @@ for the file name.
174 185
175@deffn Command byte-recompile-directory directory flag 186@deffn Command byte-recompile-directory directory flag
176@cindex library compilation 187@cindex library compilation
177 This function recompiles every @samp{.el} file in @var{directory} that 188This function recompiles every @samp{.el} file in @var{directory} that
178needs recompilation. A file needs recompilation if a @samp{.elc} file 189needs recompilation. A file needs recompilation if a @samp{.elc} file
179exists but is older than the @samp{.el} file. 190exists but is older than the @samp{.el} file.
180 191
181 If a @samp{.el} file exists, but there is no corresponding @samp{.elc} 192If a @samp{.el} file exists, but there is no corresponding @samp{.elc}
182file, then @var{flag} is examined. If it is @code{nil}, the file is 193file, then @var{flag} says what to do. If it is @code{nil}, the file is
183ignored. If it is non-@code{nil}, the user is asked whether the file 194ignored. If it is non-@code{nil}, the user is asked whether to compile
184should be compiled. 195the file.
185 196
186 The returned value of this command is unpredictable. 197The returned value of this command is unpredictable.
187@end deffn 198@end deffn
188 199
189@defun batch-byte-compile 200@defun batch-byte-compile
190 This function runs @code{byte-compile-file} on the files remaining on 201This function runs @code{byte-compile-file} on files specified on the
191the command line. This function must be used only in a batch execution 202command line. This function must be used only in a batch execution of
192of Emacs, as it kills Emacs on completion. An error in one file does 203Emacs, as it kills Emacs on completion. An error in one file does not
193not prevent processing of subsequent files. (The file which gets the 204prevent processing of subsequent files. (The file which gets the error
194error will not, of course, produce any compiled code.) 205will not, of course, produce any compiled code.)
195 206
196@example 207@example
197% emacs -batch -f batch-byte-compile *.el 208% emacs -batch -f batch-byte-compile *.el
@@ -200,14 +211,14 @@ error will not, of course, produce any compiled code.)
200 211
201@defun byte-code code-string data-vector max-stack 212@defun byte-code code-string data-vector max-stack
202@cindex byte-code interpreter 213@cindex byte-code interpreter
203 This function actually interprets byte-code. A byte-compiled function 214This function actually interprets byte-code. A byte-compiled function
204is actually defined with a body that calls @code{byte-code}. Don't call 215is actually defined with a body that calls @code{byte-code}. Don't call
205this function yourself. Only the byte compiler knows how to generate 216this function yourself. Only the byte compiler knows how to generate
206valid calls to this function. 217valid calls to this function.
207 218
208 In newer Emacs versions (19 and up), byte-code is usually executed as 219In newer Emacs versions (19 and up), byte-code is usually executed as
209part of a compiled function object, and only rarely as part of a call to 220part of a byte-code function object, and only rarely due to an explicit
210@code{byte-code}. 221call to @code{byte-code}.
211@end defun 222@end defun
212 223
213@node Eval During Compile 224@node Eval During Compile
@@ -305,10 +316,10 @@ This function constructs and returns a byte-code function object
305with @var{elements} as its elements. 316with @var{elements} as its elements.
306@end defun 317@end defun
307 318
308 You should not try to come up with the elements for a byte-code function 319 You should not try to come up with the elements for a byte-code
309yourself, because if they are inconsistent, Emacs may crash when you 320function yourself, because if they are inconsistent, Emacs may crash
310call the function. Always leave it to the byte-compiler to create these 321when you call the function. Always leave it to the byte-compiler to
311objects; it, we hope, always makes the elements consistent. 322create these objects; it makes the elements consistent (we hope).
312 323
313 You can access the elements of a byte-code object using @code{aref}; 324 You can access the elements of a byte-code object using @code{aref};
314you can also use @code{vconcat} to create a vector with the same 325you can also use @code{vconcat} to create a vector with the same
@@ -324,14 +335,14 @@ disassembler converts the byte-compiled code into humanly readable
324form. 335form.
325 336
326 The byte-code interpreter is implemented as a simple stack machine. 337 The byte-code interpreter is implemented as a simple stack machine.
327Values get stored by being pushed onto the stack, and are popped off and 338It pushes values onto a stack of its own, then pops them off to use them
328manipulated, the results being pushed back onto the stack. When a 339in calculations and push the result back on the stack. When a byte-code
329function returns, the top of the stack is popped and returned as the 340function returns, it pops a value off the stack and returns it as the
330value of the function. 341value of the function.
331 342
332 In addition to the stack, values used during byte-code execution can 343 In addition to the stack, byte-code functions can use, bind and set
333be stored in ordinary Lisp variables. Variable values can be pushed 344ordinary Lisp variables, by transferring values between variables and
334onto the stack, and variables can be set by popping the stack. 345the stack.
335 346
336@deffn Command disassemble object &optional stream 347@deffn Command disassemble object &optional stream
337This function prints the disassembled code for @var{object}. If 348This function prints the disassembled code for @var{object}. If
@@ -500,7 +511,7 @@ The @code{silly-loop} function is somewhat more complex:
500 511
501@group 512@group
5025 dup ; @r{Duplicate the top of the stack;} 5135 dup ; @r{Duplicate the top of the stack;}
503 ; @r{i.e. copy the top of} 514 ; @r{i.e., copy the top of}
504 ; @r{the stack and push the} 515 ; @r{the stack and push the}
505 ; @r{copy onto the stack.} 516 ; @r{copy onto the stack.}
506@end group 517@end group