comparison lispref/compile.texi @ 21682:90da2489c498

*** empty log message ***
author Richard M. Stallman <rms@gnu.org>
date Mon, 20 Apr 1998 17:43:57 +0000
parents 66d807bdc5b4
children d4ac295a98b3
comparison
equal deleted inserted replaced
21681:11eafe90b842 21682:90da2489c498
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, 1994 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, Advising, Loading, Top
7 @chapter Byte Compilation 7 @chapter Byte Compilation
8 @cindex byte-code 8 @cindex byte-code
9 @cindex compilation 9 @cindex compilation
10 10
11 GNU Emacs Lisp has a @dfn{compiler} that translates functions written 11 Emacs Lisp has a @dfn{compiler} that translates functions written
12 in Lisp into a special representation called @dfn{byte-code} that can be 12 in Lisp into a special representation called @dfn{byte-code} that can be
13 executed more efficiently. The compiler replaces Lisp function 13 executed more efficiently. The compiler replaces Lisp function
14 definitions with byte-code. When a byte-code function is called, its 14 definitions with byte-code. When a byte-code function is called, its
15 definition is evaluated by the @dfn{byte-code interpreter}. 15 definition is evaluated by the @dfn{byte-code interpreter}.
16 16
19 hardware (as true compiled code is), byte-code is completely 19 hardware (as true compiled code is), byte-code is completely
20 transportable from machine to machine without recompilation. It is not, 20 transportable from machine to machine without recompilation. It is not,
21 however, as fast as true compiled code. 21 however, as fast as true compiled code.
22 22
23 In general, any version of Emacs can run byte-compiled code produced 23 In general, any version of Emacs can run byte-compiled code produced
24 by recent earlier versions of Emacs, but the reverse is not true. In 24 by recent earlier versions of Emacs, but the reverse is not true. A
25 particular, if you compile a program with Emacs 19.29, the compiled 25 major incompatible change was introduced in Emacs version 19.29, and
26 code does not run in earlier versions. 26 files compiled with versions since that one will definitely not run
27 in earlier versions unless you specify a special option.
27 @iftex 28 @iftex
28 @xref{Docs and Compilation}. 29 @xref{Docs and Compilation}.
29 @end iftex 30 @end iftex
30 Files compiled in versions before 19.29 may not work in 19.29 if they 31 In addition, the modifier bits in keyboard characters were renumbered in
31 contain character constants with modifier bits, because the bits were 32 Emacs 19.29; as a result, files compiled in versions before 19.29 will
32 renumbered in Emacs 19.29. 33 not work in subsequent versions if they contain character constants with
34 modifier bits.
33 35
34 @xref{Compilation Errors}, for how to investigate errors occurring in 36 @xref{Compilation Errors}, for how to investigate errors occurring in
35 byte compilation. 37 byte compilation.
36 38
37 @menu 39 @menu
97 The byte compiler produces error messages and warnings about each file 99 The byte compiler produces error messages and warnings about each file
98 in a buffer called @samp{*Compile-Log*}. These report things in your 100 in a buffer called @samp{*Compile-Log*}. These report things in your
99 program that suggest a problem but are not necessarily erroneous. 101 program that suggest a problem but are not necessarily erroneous.
100 102
101 @cindex macro compilation 103 @cindex macro compilation
102 Be careful when byte-compiling code that uses macros. Macro calls are 104 Be careful when writing macro calls in files that you may someday
103 expanded when they are compiled, so the macros must already be defined 105 byte-compile. Macro calls are expanded when they are compiled, so the
104 for proper compilation. For more details, see @ref{Compiling Macros}. 106 macros must already be defined for proper compilation. For more
107 details, see @ref{Compiling Macros}.
105 108
106 Normally, compiling a file does not evaluate the file's contents or 109 Normally, compiling a file does not evaluate the file's contents or
107 load the file. But it does execute any @code{require} calls at top 110 load the file. But it does execute any @code{require} calls at top
108 level in the file. One way to ensure that necessary macro definitions 111 level in the file. One way to ensure that necessary macro definitions
109 are available during compilation is to require the file that defines 112 are available during compilation is to require the file that defines
198 @cindex library compilation 201 @cindex library compilation
199 This function recompiles every @samp{.el} file in @var{directory} that 202 This function recompiles every @samp{.el} file in @var{directory} that
200 needs recompilation. A file needs recompilation if a @samp{.elc} file 203 needs recompilation. A file needs recompilation if a @samp{.elc} file
201 exists but is older than the @samp{.el} file. 204 exists but is older than the @samp{.el} file.
202 205
203 When a @samp{.el} file has no corresponding @samp{.elc} file, then 206 When a @samp{.el} file has no corresponding @samp{.elc} file, @var{flag}
204 @var{flag} says what to do. If it is @code{nil}, these files are 207 says what to do. If it is @code{nil}, these files are ignored. If it
205 ignored. If it is non-@code{nil}, the user is asked whether to compile 208 is non-@code{nil}, the user is asked whether to compile each such file.
206 each such file.
207 209
208 The returned value of this command is unpredictable. 210 The returned value of this command is unpredictable.
209 @end deffn 211 @end deffn
210 212
211 @defun batch-byte-compile 213 @defun batch-byte-compile
223 225
224 @defun byte-code code-string data-vector max-stack 226 @defun byte-code code-string data-vector max-stack
225 @cindex byte-code interpreter 227 @cindex byte-code interpreter
226 This function actually interprets byte-code. A byte-compiled function 228 This function actually interprets byte-code. A byte-compiled function
227 is actually defined with a body that calls @code{byte-code}. Don't call 229 is actually defined with a body that calls @code{byte-code}. Don't call
228 this function yourself. Only the byte compiler knows how to generate 230 this function yourself---only the byte compiler knows how to generate
229 valid calls to this function. 231 valid calls to this function.
230 232
231 In newer Emacs versions (19 and up), byte-code is usually executed as 233 In Emacs version 18, byte-code was always executed by way of a call to
232 part of a byte-code function object, and only rarely due to an explicit 234 the function @code{byte-code}. Nowadays, byte-code is usually executed
233 call to @code{byte-code}. 235 as part of a byte-code function object, and only rarely through an
236 explicit call to @code{byte-code}.
234 @end defun 237 @end defun
235 238
236 @node Docs and Compilation 239 @node Docs and Compilation
237 @section Documentation Strings and Compilation 240 @section Documentation Strings and Compilation
238 @cindex dynamic loading of documentation 241 @cindex dynamic loading of documentation
267 However, if you have built Emacs yourself and use it from the 270 However, if you have built Emacs yourself and use it from the
268 directory where you built it, you will experience this problem 271 directory where you built it, you will experience this problem
269 occasionally if you edit and recompile Lisp files. When it happens, you 272 occasionally if you edit and recompile Lisp files. When it happens, you
270 can cure the problem by reloading the file after recompiling it. 273 can cure the problem by reloading the file after recompiling it.
271 274
272 Byte-compiled files made with Emacs 19.29 will not load into older 275 Byte-compiled files made with recent versions of Emacs (since 19.29)
273 versions because the older versions don't support this feature. You can 276 will not load into older versions because the older versions don't
274 turn off this feature by setting @code{byte-compile-dynamic-docstrings} 277 support this feature. You can turn off this feature at compile time by
275 to @code{nil}. Once this is done, you can compile files that will load 278 setting @code{byte-compile-dynamic-docstrings} to @code{nil}; then you
276 into older Emacs versions. You can do this globally, or for one source 279 can compile files that will load into older Emacs versions. You can do
277 file by specifying a file-local binding for the variable. Here's one 280 this globally, or for one source file by specifying a file-local binding
278 way to do that: 281 for the variable. One way to do that is by adding this string to the
282 file's first line:
279 283
280 @example 284 @example
281 -*-byte-compile-dynamic-docstrings: nil;-*- 285 -*-byte-compile-dynamic-docstrings: nil;-*-
282 @end example 286 @end example
283 287
290 @cindex @samp{#$} 294 @cindex @samp{#$}
291 The dynamic documentation string feature writes compiled files that 295 The dynamic documentation string feature writes compiled files that
292 use a special Lisp reader construct, @samp{#@@@var{count}}. This 296 use a special Lisp reader construct, @samp{#@@@var{count}}. This
293 construct skips the next @var{count} characters. It also uses the 297 construct skips the next @var{count} characters. It also uses the
294 @samp{#$} construct, which stands for ``the name of this file, as a 298 @samp{#$} construct, which stands for ``the name of this file, as a
295 string.'' It is best not to use these constructs in Lisp source files. 299 string.'' It is usually best not to use these constructs in Lisp source
300 files, since they are not designed to be clear to humans reading the
301 file.
296 302
297 @node Dynamic Loading 303 @node Dynamic Loading
298 @section Dynamic Loading of Individual Functions 304 @section Dynamic Loading of Individual Functions
299 305
300 @cindex dynamic loading of functions 306 @cindex dynamic loading of functions
307 function is called, it reads the full definition from the file, to 313 function is called, it reads the full definition from the file, to
308 replace the place-holder. 314 replace the place-holder.
309 315
310 The advantage of dynamic function loading is that loading the file 316 The advantage of dynamic function loading is that loading the file
311 becomes much faster. This is a good thing for a file which contains 317 becomes much faster. This is a good thing for a file which contains
312 many separate commands, provided that using one of them does not imply 318 many separate user-callable functions, if using one of them does not
313 you will soon (or ever) use the rest. A specialized mode which provides 319 imply you will probably also use the rest. A specialized mode which
314 many keyboard commands often has that usage pattern: a user may invoke 320 provides many keyboard commands often has that usage pattern: a user may
315 the mode, but use only a few of the commands it provides. 321 invoke the mode, but use only a few of the commands it provides.
316 322
317 The dynamic loading feature has certain disadvantages: 323 The dynamic loading feature has certain disadvantages:
318 324
319 @itemize @bullet 325 @itemize @bullet
320 @item 326 @item
321 If you delete or move the compiled file after loading it, Emacs can no 327 If you delete or move the compiled file after loading it, Emacs can no
322 longer load the remaining function definitions not already loaded. 328 longer load the remaining function definitions not already loaded.
323 329
324 @item 330 @item
325 If you alter the compiled file (such as by compiling a new version), 331 If you alter the compiled file (such as by compiling a new version),
326 then trying to load any function not already loaded will get nonsense 332 then trying to load any function not already loaded will yield nonsense
327 results. 333 results.
328 @end itemize 334 @end itemize
329 335
330 If you compile a new version of the file, the best thing to do is 336 These problems will never happen in normal circumstances with
331 immediately load the new compiled file. That will prevent any future 337 installed Emacs files. But they are quite likely to happen with Lisp
332 problems. 338 files that you are changing. The easiest way to prevent these problems
339 is to reload the new compiled file immediately after each recompilation.
333 340
334 The byte compiler uses the dynamic function loading feature if the 341 The byte compiler uses the dynamic function loading feature if the
335 variable @code{byte-compile-dynamic} is non-@code{nil} at compilation 342 variable @code{byte-compile-dynamic} is non-@code{nil} at compilation
336 time. Do not set this variable globally, since dynamic loading is 343 time. Do not set this variable globally, since dynamic loading is
337 desirable only for certain files. Instead, enable the feature for 344 desirable only for certain files. Instead, enable the feature for
338 specific source files with file-local variable bindings, like this: 345 specific source files with file-local variable bindings. For example,
346 you could do it by writing this text in the source file's first line:
339 347
340 @example 348 @example
341 -*-byte-compile-dynamic: t;-*- 349 -*-byte-compile-dynamic: t;-*-
342 @end example 350 @end example
343 351
361 @defspec eval-and-compile body 369 @defspec eval-and-compile body
362 This form marks @var{body} to be evaluated both when you compile the 370 This form marks @var{body} to be evaluated both when you compile the
363 containing code and when you run it (whether compiled or not). 371 containing code and when you run it (whether compiled or not).
364 372
365 You can get a similar result by putting @var{body} in a separate file 373 You can get a similar result by putting @var{body} in a separate file
366 and referring to that file with @code{require}. Using @code{require} is 374 and referring to that file with @code{require}. That method is
367 preferable if there is a substantial amount of code to be executed in 375 preferable when @var{body} is large.
368 this way.
369 @end defspec 376 @end defspec
370 377
371 @defspec eval-when-compile body 378 @defspec eval-when-compile body
372 This form marks @var{body} to be evaluated at compile time but not when 379 This form marks @var{body} to be evaluated at compile time but not when
373 the compiled program is loaded. The result of evaluation by the 380 the compiled program is loaded. The result of evaluation by the
374 compiler becomes a constant which appears in the compiled program. If 381 compiler becomes a constant which appears in the compiled program. If
375 you load the source file, rather than compiling it, @var{body} is 382 you load the source file, rather than compiling it, @var{body} is
376 evaluated normally. 383 evaluated normally.
377 384
378 At top level, this is analogous to the Common Lisp idiom 385 @strong{Common Lisp Note:} At top level, this is analogous to the Common
379 @code{(eval-when (compile eval) @dots{})}. Elsewhere, the Common Lisp 386 Lisp idiom @code{(eval-when (compile eval) @dots{})}. Elsewhere, the
380 @samp{#.} reader macro (but not when interpreting) is closer to what 387 Common Lisp @samp{#.} reader macro (but not when interpreting) is closer
381 @code{eval-when-compile} does. 388 to what @code{eval-when-compile} does.
382 @end defspec 389 @end defspec
383 390
384 @node Byte-Code Objects 391 @node Byte-Code Objects
385 @section Byte-Code Function Objects 392 @section Byte-Code Function Objects
386 @cindex compiled function 393 @cindex compiled function
393 however, the evaluator handles this data type specially when it appears 400 however, the evaluator handles this data type specially when it appears
394 as a function to be called. The printed representation for a byte-code 401 as a function to be called. The printed representation for a byte-code
395 function object is like that for a vector, with an additional @samp{#} 402 function object is like that for a vector, with an additional @samp{#}
396 before the opening @samp{[}. 403 before the opening @samp{[}.
397 404
398 In Emacs version 18, there was no byte-code function object data type;
399 compiled functions used the function @code{byte-code} to run the byte
400 code.
401
402 A byte-code function object must have at least four elements; there is 405 A byte-code function object must have at least four elements; there is
403 no maximum number, but only the first six elements are actually used. 406 no maximum number, but only the first six elements have any normal use.
404 They are: 407 They are:
405 408
406 @table @var 409 @table @var
407 @item arglist 410 @item arglist
408 The list of argument symbols. 411 The list of argument symbols.