This chapter explains how you can use Larceny to compile Scheme source code to native machine code.
The native varieties of Larceny have a just-in-time compiler that compiles to native code automatically whenever you evaluate an expression, load a source file, or import a source library. Even so, files will load faster if they are compiled ahead of time.
Petit Larceny does not have a just-in-time compiler, so compiling ahead of time is the only way to enjoy the speed of native machine code in Petit Larceny.
The main disadvantage of compiling files and libraries is that compiled code goes stale when its original source code is changed or when a library on which the compiled code depends is changed or recompiled. Stale compiled code can be dangerously inconsistent with libraries on which it depends, so Larceny checks for staleness and refuses to execute a stale library or program.
On Unix machines, the most convenient way to compile
a group of R7RS/R6RS libraries and top-level programs
is to use the compile-stale
script in Larceny's root
directory.
If Larceny's root directory is in your execution path,
then there are just two steps:
cd
to change to the directory that contains
the R7RS/R6RS files you want to compile. (Files
that lie within subdirectories of that directory will
be compiled also.)
compile-stale
script.
For example:
% cd lib/R7RS % compile-stale
On non-Unix machines, you can accomplish the same thing
using Larceny's R7RS mode and the (larceny compiler)
library:
% pushd lib\R7RS % ../../larceny -r7rs Larceny v0.98 "General Ripper" > (import (larceny compiler)) > (compile-stale-libraries)
To compile individual files, use the compile-file
or
compile-library
procedures that are exported by
(larceny compiler)
.
Compiles sourcefile, which must be a string naming
a file that contains R5RS source code.
If faslfile is supplied as a second argument,
then it must be a string naming the file that will
contain the compiled code; otherwise the name of
the compiled file is obtained from sourcefile
by replacing the ".sch
" or ".scm
" suffix with
".fasl
".
For R7RS/R6RS libraries and top-level programs, see above.