comparison lispref/compile.texi @ 6452:8c7032348e93

*** empty log message ***
author Richard M. Stallman <rms@gnu.org>
date Mon, 21 Mar 1994 17:04:32 +0000
parents e449bfa3caae
children 2f1305fcecf6
comparison
equal deleted inserted replaced
6451:8240c0b1d695 6452:8c7032348e93
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
7 @chapter Byte Compilation 7 @chapter Byte Compilation
8 @cindex byte-code 8 @cindex byte-code
27 27
28 @xref{Compilation Errors}, for how to investigate errors occurring in 28 @xref{Compilation Errors}, for how to investigate errors occurring in
29 byte compilation. 29 byte 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
44 the @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
50 problem, but are not necessarily erroneous.
51
52 @cindex macro compilation
53 Be careful when byte-compiling code that uses macros. Macro calls are
54 expanded when they are compiled, so the macros must already be defined
55 for proper compilation. For more details, see @ref{Compiling Macros}.
56
57 While byte-compiling a file, any @code{require} calls at top-level are
58 executed. One way to ensure that necessary macro definitions are
59 available 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
63 written in C, but runs much faster than the version written in Lisp. 43 written in C, but runs much faster than the version written in Lisp.
64 For a rough comparison, consider the example below: 44 Here is an example:
65 45
66 @example 46 @example
67 @group 47 @group
68 (defun silly-loop (n) 48 (defun silly-loop (n)
69 "Return time before and after N iterations of a loop." 49 "Return time before and after N iterations of a loop."
74 @result{} silly-loop 54 @result{} silly-loop
75 @end group 55 @end group
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
84 (byte-compile 'silly-loop) 64 (byte-compile 'silly-loop)
85 @result{} @r{[Compiled code not shown]} 65 @result{} @r{[Compiled code not shown]}
86 @end group 66 @end group
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,
96 whereas the byte-compiled code required 13 seconds. These results are 76 whereas the byte-compiled code required 6 seconds. These results are
97 representative, but actual results will vary greatly. 77 representative, 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
85 the @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
91 problem, but are not necessarily erroneous.
92
93 @cindex macro compilation
94 Be careful when byte-compiling code that uses macros. Macro calls are
95 expanded when they are compiled, so the macros must already be defined
96 for proper compilation. For more details, see @ref{Compiling Macros}.
97
98 Normally, compiling a file does not evaluate the file's contents or
99 load the file. But it does execute any @code{require} calls at
100 top-level in the file. One way to ensure that necessary macro
101 definitions are available during compilation is to require the file that
102 defines 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}, 105 This function byte-compiles the function definition of @var{symbol},
101 replacing the previous definition with the compiled one. The function 106 replacing the previous definition with the compiled one. The function
102 definition of @var{symbol} must be the actual code for the function; 107 definition of @var{symbol} must be the actual code for the function;
103 i.e., the compiler does not follow indirection to another symbol. 108 i.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
105 returns the new, compiled definition of @var{symbol}. 110 @var{symbol}.
111
112 If @var{symbol}'s definition is a byte-code function object,
113 @code{byte-compile} does nothing and returns @code{nil}. Lisp records
114 only one function definition for any symbol, and if that is already
115 compiled, non-compiled code is not available anywhere. So there is no
116 way to ``compile the same definition again.''
106 117
107 @example 118 @example
108 @group 119 @group
109 (defun factorial (integer) 120 (defun factorial (integer)
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]
122 4 "Compute factorial of INTEGER."] 133 4 "Compute factorial of INTEGER."]
123 @end group 134 @end group
124 @end example 135 @end example
125 136
126 @noindent 137 @noindent
127 The result is a compiled function object. The string it contains is the 138 The result is a byte-code function object. The string it contains is
128 actual byte-code; each character in it is an instruction. The vector 139 the actual byte-code; each character in it is an instruction or an
129 contains all the constants, variable names and function names used by 140 operand of an instruction. The vector contains all the constants,
130 the function, except for certain primitives that are coded as special 141 variable names and function names used by the function, except for
131 instructions. 142 certain 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
135 This command reads the defun containing point, compiles it, and 146 This command reads the defun containing point, compiles it, and
136 evaluates the result. If you use this on a defun that is actually a 147 evaluates the result. If you use this on a defun that is actually a
137 function definition, the effect is to install a compiled version of that 148 function definition, the effect is to install a compiled version of that
138 function. 149 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 153 This function compiles a file of Lisp code named @var{filename} into
143 a file of byte-code. The output file's name is made by appending 154 a 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 157 Compilation works by reading the input file one form at a time. If it
147 is a definition of a function or macro, the compiled function or macro 158 is a definition of a function or macro, the compiled function or macro
148 definition is written out. Other forms are batched together, then each 159 definition is written out. Other forms are batched together, then each
149 batch is compiled, and written so that its compiled code will be 160 batch is compiled, and written so that its compiled code will be
150 executed when the file is read. All comments are discarded when the 161 executed when the file is read. All comments are discarded when the
151 input file is read. 162 input file is read.
152 163
153 This command returns @code{t}. When called interactively, it prompts 164 This command returns @code{t}. When called interactively, it prompts
154 for the file name. 165 for the file name.
155 166
156 @example 167 @example
157 @group 168 @group
158 % ls -l push* 169 % ls -l push*
172 @end example 183 @end example
173 @end deffn 184 @end deffn
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 188 This function recompiles every @samp{.el} file in @var{directory} that
178 needs recompilation. A file needs recompilation if a @samp{.elc} file 189 needs recompilation. A file needs recompilation if a @samp{.elc} file
179 exists but is older than the @samp{.el} file. 190 exists but is older than the @samp{.el} file.
180 191
181 If a @samp{.el} file exists, but there is no corresponding @samp{.elc} 192 If a @samp{.el} file exists, but there is no corresponding @samp{.elc}
182 file, then @var{flag} is examined. If it is @code{nil}, the file is 193 file, then @var{flag} says what to do. If it is @code{nil}, the file is
183 ignored. If it is non-@code{nil}, the user is asked whether the file 194 ignored. If it is non-@code{nil}, the user is asked whether to compile
184 should be compiled. 195 the file.
185 196
186 The returned value of this command is unpredictable. 197 The 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 201 This function runs @code{byte-compile-file} on files specified on the
191 the command line. This function must be used only in a batch execution 202 command line. This function must be used only in a batch execution of
192 of Emacs, as it kills Emacs on completion. An error in one file does 203 Emacs, as it kills Emacs on completion. An error in one file does not
193 not prevent processing of subsequent files. (The file which gets the 204 prevent processing of subsequent files. (The file which gets the error
194 error will not, of course, produce any compiled code.) 205 will 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
198 @end example 209 @end example
199 @end defun 210 @end defun
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 214 This function actually interprets byte-code. A byte-compiled function
204 is actually defined with a body that calls @code{byte-code}. Don't call 215 is actually defined with a body that calls @code{byte-code}. Don't call
205 this function yourself. Only the byte compiler knows how to generate 216 this function yourself. Only the byte compiler knows how to generate
206 valid calls to this function. 217 valid calls to this function.
207 218
208 In newer Emacs versions (19 and up), byte-code is usually executed as 219 In newer Emacs versions (19 and up), byte-code is usually executed as
209 part of a compiled function object, and only rarely as part of a call to 220 part of a byte-code function object, and only rarely due to an explicit
210 @code{byte-code}. 221 call to @code{byte-code}.
211 @end defun 222 @end defun
212 223
213 @node Eval During Compile 224 @node Eval During Compile
214 @section Evaluation During Compilation 225 @section Evaluation During Compilation
215 226
303 @defun make-byte-code &rest elements 314 @defun make-byte-code &rest elements
304 This function constructs and returns a byte-code function object 315 This function constructs and returns a byte-code function object
305 with @var{elements} as its elements. 316 with @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
309 yourself, because if they are inconsistent, Emacs may crash when you 320 function yourself, because if they are inconsistent, Emacs may crash
310 call the function. Always leave it to the byte-compiler to create these 321 when you call the function. Always leave it to the byte-compiler to
311 objects; it, we hope, always makes the elements consistent. 322 create 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};
314 you can also use @code{vconcat} to create a vector with the same 325 you can also use @code{vconcat} to create a vector with the same
315 elements. 326 elements.
316 327
322 But we provide a disassembler to satisfy a cat-like curiosity. The 333 But we provide a disassembler to satisfy a cat-like curiosity. The
323 disassembler converts the byte-compiled code into humanly readable 334 disassembler converts the byte-compiled code into humanly readable
324 form. 335 form.
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.
327 Values get stored by being pushed onto the stack, and are popped off and 338 It pushes values onto a stack of its own, then pops them off to use them
328 manipulated, the results being pushed back onto the stack. When a 339 in calculations and push the result back on the stack. When a byte-code
329 function returns, the top of the stack is popped and returned as the 340 function returns, it pops a value off the stack and returns it as the
330 value of the function. 341 value 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
333 be stored in ordinary Lisp variables. Variable values can be pushed 344 ordinary Lisp variables, by transferring values between variables and
334 onto the stack, and variables can be set by popping the stack. 345 the stack.
335 346
336 @deffn Command disassemble object &optional stream 347 @deffn Command disassemble object &optional stream
337 This function prints the disassembled code for @var{object}. If 348 This function prints the disassembled code for @var{object}. If
338 @var{stream} is supplied, then output goes there. Otherwise, the 349 @var{stream} is supplied, then output goes there. Otherwise, the
339 disassembled code is printed to the stream @code{standard-output}. The 350 disassembled code is printed to the stream @code{standard-output}. The
498 4 sub1 ; @r{Subtract 1 from top of stack.} 509 4 sub1 ; @r{Subtract 1 from top of stack.}
499 @end group 510 @end group
500 511
501 @group 512 @group
502 5 dup ; @r{Duplicate the top of the stack;} 513 5 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
507 518
508 @group 519 @group