1
0
Fork 0
cl-sites/guile.html_node/Compilation.html

255 lines
16 KiB
HTML
Raw Normal View History

2024-12-17 12:49:28 +01:00
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- This manual documents Guile version 3.0.10.
Copyright (C) 1996-1997, 2000-2005, 2009-2023 Free Software Foundation,
Inc.
Copyright (C) 2021 Maxime Devos
Copyright (C) 2024 Tomas Volf
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with no
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
copy of the license is included in the section entitled "GNU Free
Documentation License." -->
<title>Compilation (Guile Reference Manual)</title>
<meta name="description" content="Compilation (Guile Reference Manual)">
<meta name="keywords" content="Compilation (Guile Reference Manual)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content=".texi2any-real">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Read_002fLoad_002fEval_002fCompile.html" rel="up" title="Read/Load/Eval/Compile">
<link href="Loading.html" rel="next" title="Loading">
<link href="Fly-Evaluation.html" rel="prev" title="Fly Evaluation">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
-->
</style>
<link rel="stylesheet" type="text/css" href="https://www.gnu.org/software/gnulib/manual.css">
</head>
<body lang="en">
<div class="subsection-level-extent" id="Compilation">
<div class="nav-panel">
<p>
Next: <a href="Loading.html" accesskey="n" rel="next">Loading Scheme Code from File</a>, Previous: <a href="Fly-Evaluation.html" accesskey="p" rel="prev">Procedures for On the Fly Evaluation</a>, Up: <a href="Read_002fLoad_002fEval_002fCompile.html" accesskey="u" rel="up">Reading and Evaluating Scheme Code</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="Compiling-Scheme-Code"><span>6.16.6 Compiling Scheme Code<a class="copiable-link" href="#Compiling-Scheme-Code"> &para;</a></span></h4>
<p>The <code class="code">eval</code> procedure directly interprets the S-expression
representation of Scheme. An alternate strategy for evaluation is to
determine ahead of time what computations will be necessary to
evaluate the expression, and then use that recipe to produce the
desired results. This is known as <em class="dfn">compilation</em>.
</p>
<p>While it is possible to compile simple Scheme expressions such as
<code class="code">(+ 2 2)</code> or even <code class="code">&quot;Hello world!&quot;</code>, compilation is most
interesting in the context of procedures. Compiling a lambda expression
produces a compiled procedure, which is just like a normal procedure
except typically much faster, because it can bypass the generic
interpreter.
</p>
<p>Functions from system modules in a Guile installation are normally
compiled already, so they load and run quickly.
</p>
<a class="index-entry-id" id="index-automatic-compilation"></a>
<p>Note that well-written Scheme programs will not typically call the
procedures in this section, for the same reason that it is often bad
taste to use <code class="code">eval</code>. By default, Guile automatically compiles any
files it encounters that have not been compiled yet (see <a class="pxref" href="Invoking-Guile.html"><code class="code">--auto-compile</code></a>). The compiler can also be invoked
explicitly from the shell as <code class="code">guild compile foo.scm</code>.
</p>
<p>(Why are calls to <code class="code">eval</code> and <code class="code">compile</code> usually in bad taste?
Because they are limited, in that they can only really make sense for
top-level expressions. Also, most needs for &ldquo;compile-time&rdquo;
computation are fulfilled by macros and closures. Of course one good
counterexample is the REPL itself, or any code that reads expressions
from a port.)
</p>
<p>Automatic compilation generally works transparently, without any need
for user intervention. However Guile does not yet do proper dependency
tracking, so that if file <samp class="file"><var class="var">a</var>.scm</samp> uses macros from
<samp class="file"><var class="var">b</var>.scm</samp>, and <var class="var"><var class="var">b</var>.scm</var> changes, <code class="code"><var class="var">a</var>.scm</code>
would not be automatically recompiled. To forcibly invalidate the
auto-compilation cache, pass the <code class="code">--fresh-auto-compile</code> option to
Guile, or set the <code class="code">GUILE_AUTO_COMPILE</code> environment variable to
<code class="code">fresh</code> (instead of to <code class="code">0</code> or <code class="code">1</code>).
</p>
<p>For more information on the compiler itself, see <a class="ref" href="Compiling-to-the-Virtual-Machine.html">Compiling to the Virtual Machine</a>. For information on the virtual machine, see <a class="ref" href="A-Virtual-Machine-for-Guile.html">A Virtual Machine for Guile</a>.
</p>
<p>The command-line interface to Guile&rsquo;s compiler is the <code class="command">guild
compile</code> command:
</p>
<dl class="first-deffn">
<dt class="deffn" id="index-guild-compile"><span class="category-def">Command: </span><span><strong class="def-name">guild compile</strong> <var class="def-var-arguments">[<samp class="option">option</samp>...] <var class="var">file</var>...</var><a class="copiable-link" href="#index-guild-compile"> &para;</a></span></dt>
<dd><p>Compile <var class="var">file</var>, a source file, and store bytecode in the compilation cache
or in the file specified by the <samp class="option">-o</samp> option. The following options are
available:
</p>
<dl class="table">
<dt><samp class="option">-L <var class="var">dir</var></samp></dt>
<dt><samp class="option">--load-path=<var class="var">dir</var></samp></dt>
<dd><p>Add <var class="var">dir</var> to the front of the module load path.
</p>
</dd>
<dt><samp class="option">-o <var class="var">ofile</var></samp></dt>
<dt><samp class="option">--output=<var class="var">ofile</var></samp></dt>
<dd><p>Write output bytecode to <var class="var">ofile</var>. By convention, bytecode file
names end in <code class="code">.go</code>. When <samp class="option">-o</samp> is omitted, the output file
name is as for <code class="code">compile-file</code> (see below).
</p>
</dd>
<dt><samp class="option">-x <var class="var">extension</var></samp></dt>
<dd><p>Recognize <var class="var">extension</var> as a valid source file name extension.
</p>
<p>For example, to compile R6RS code, you might want to pass <code class="command">-x
.sls</code> so that files ending in <samp class="file">.sls</samp> can be found.
</p>
</dd>
<dt><a id="index-warnings_002c-compiler"></a><span><samp class="option">-W <var class="var">warning</var></samp><a class="copiable-link" href="#index-warnings_002c-compiler"> &para;</a></span></dt>
<dt><samp class="option">--warn=<var class="var">warning</var></samp></dt>
<dd><p>Enable specific warning passes; use <code class="code">-Whelp</code> for a list of
available options. The default is <code class="code">-W1</code>, which enables a number of
common warnings. Pass <code class="code">-W0</code> to disable all warnings.
</p>
</dd>
<dt><a id="index-optimizations_002c-compiler"></a><span><samp class="option">-O <var class="var">opt</var></samp><a class="copiable-link" href="#index-optimizations_002c-compiler"> &para;</a></span></dt>
<dt><samp class="option">--optimize=<var class="var">opt</var></samp></dt>
<dd><p>Enable or disable specific compiler optimizations; use <code class="code">-Ohelp</code> for
a list of available options. The default is <code class="code">-O2</code>, which enables
most optimizations. <code class="code">-O0</code> is recommended if compilation speed is
more important than the speed of the compiled code. Pass
<code class="code">-Ono-<var class="var">opt</var></code> to disable a specific compiler pass. Any number
of <code class="code">-O</code> options can be passed to the compiler, with later ones
taking precedence.
</p>
</dd>
<dt><samp class="option">--r6rs</samp></dt>
<dt><samp class="option">--r7rs</samp></dt>
<dd><p>Compile in an environment whose default bindings, reader options, and
load paths are adapted for specific Scheme standards. See <a class="xref" href="R6RS-Support.html">R6RS Support</a>, and See <a class="xref" href="R7RS-Support.html">R7RS Support</a>.
</p>
</dd>
<dt><samp class="option">-f <var class="var">lang</var></samp></dt>
<dt><samp class="option">--from=<var class="var">lang</var></samp></dt>
<dd><p>Use <var class="var">lang</var> as the source language of <var class="var">file</var>. If this option is omitted,
<code class="code">scheme</code> is assumed.
</p>
</dd>
<dt><samp class="option">-t <var class="var">lang</var></samp></dt>
<dt><samp class="option">--to=<var class="var">lang</var></samp></dt>
<dd><p>Use <var class="var">lang</var> as the target language of <var class="var">file</var>. If this option is omitted,
<code class="code">rtl</code> is assumed.
</p>
</dd>
<dt><samp class="option">-T <var class="var">target</var></samp></dt>
<dt><samp class="option">--target=<var class="var">target</var></samp></dt>
<dd><p>Produce code for <var class="var">target</var> instead of <var class="var">%host-type</var> (see <a class="pxref" href="Build-Config.html">%host-type</a>). Target must be a valid GNU triplet, such as
<code class="code">armv5tel-unknown-linux-gnueabi</code> (see <a data-manual="autoconf" href="https://www.gnu.org/software/autoconf/manual/html_node/Specifying-Target-Triplets.html#Specifying-Target-Triplets">Specifying Target
Triplets</a> in <cite class="cite">GNU Autoconf Manual</cite>).
</p>
</dd>
</dl>
<p>Each <var class="var">file</var> is assumed to be UTF-8-encoded, unless it contains a
coding declaration as recognized by <code class="code">file-encoding</code>
(see <a class="pxref" href="Character-Encoding-of-Source-Files.html">Character Encoding of Source Files</a>).
</p></dd></dl>
<p>The compiler can also be invoked directly by Scheme code. These
interfaces are in their own module:
</p>
<div class="example">
<pre class="example-preformatted">(use-modules (system base compile))
</pre></div>
<dl class="first-deffn">
<dt class="deffn" id="index-compile-1"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">compile</strong> <var class="def-var-arguments">exp [#:env=#f] [#:from=(current-language)] [#:to=value] [#:opts=&rsquo;()] [#:optimization-level=(default-optimization-level)] [#:warning-level=(default-warning-level)]</var><a class="copiable-link" href="#index-compile-1"> &para;</a></span></dt>
<dd><p>Compile the expression <var class="var">exp</var> in the environment <var class="var">env</var>. If
<var class="var">exp</var> is a procedure, the result will be a compiled procedure;
otherwise <code class="code">compile</code> is mostly equivalent to <code class="code">eval</code>.
</p>
<p>For a discussion of languages and compiler options, See <a class="xref" href="Compiling-to-the-Virtual-Machine.html">Compiling to the Virtual Machine</a>.
</p></dd></dl>
<dl class="first-deffn">
<dt class="deffn" id="index-compile_002dfile-1"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">compile-file</strong> <var class="def-var-arguments">file [#:output-file=#f] [#:from=(current-language)] [#:to=&rsquo;rtl] [#:env=(default-environment from)] [#:opts=&rsquo;()] [#:optimization-level=(default-optimization-level)] [#:warning-level=(default-warning-level)] [#:canonicalization=&rsquo;relative]</var><a class="copiable-link" href="#index-compile_002dfile-1"> &para;</a></span></dt>
<dd><p>Compile the file named <var class="var">file</var>.
</p>
<p>Output will be written to a <var class="var">output-file</var>. If you do not supply an
output file name, output is written to a file in the cache directory, as
computed by <code class="code">(compiled-file-name <var class="var">file</var>)</code>.
</p>
<p><var class="var">from</var> and <var class="var">to</var> specify the source and target languages.
See <a class="xref" href="Compiling-to-the-Virtual-Machine.html">Compiling to the Virtual Machine</a>, for more information on these
options, and on <var class="var">env</var> and <var class="var">opts</var>.
</p>
<p>As with <code class="command">guild compile</code>, <var class="var">file</var> is assumed to be
UTF-8-encoded unless it contains a coding declaration.
</p></dd></dl>
<dl class="first-deffn">
<dt class="deffn" id="index-default_002doptimization_002dlevel"><span class="category-def">Scheme Parameter: </span><span><strong class="def-name">default-optimization-level</strong><a class="copiable-link" href="#index-default_002doptimization_002dlevel"> &para;</a></span></dt>
<dd><p>The default optimization level, as an integer from 0 to 9. The default
is 2.
</p></dd></dl>
<dl class="first-deffn">
<dt class="deffn" id="index-default_002dwarning_002dlevel"><span class="category-def">Scheme Parameter: </span><span><strong class="def-name">default-warning-level</strong><a class="copiable-link" href="#index-default_002dwarning_002dlevel"> &para;</a></span></dt>
<dd><p>The default warning level, as an integer from 0 to 9. The default is 1.
</p></dd></dl>
<p>See <a class="xref" href="Parameters.html">Parameters</a>, for more on how to set parameters.
</p>
<dl class="first-deffn">
<dt class="deffn" id="index-compiled_002dfile_002dname"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">compiled-file-name</strong> <var class="def-var-arguments">file</var><a class="copiable-link" href="#index-compiled_002dfile_002dname"> &para;</a></span></dt>
<dd><p>Compute a cached location for a compiled version of a Scheme file named
<var class="var">file</var>.
</p>
<p>This file will usually be below the <samp class="file">$HOME/.cache/guile/ccache</samp>
directory, depending on the value of the <code class="env">XDG_CACHE_HOME</code>
environment variable. The intention is that <code class="code">compiled-file-name</code>
provides a fallback location for caching auto-compiled files. If you
want to place a compile file in the <code class="code">%load-compiled-path</code>, you
should pass the <var class="var">output-file</var> option to <code class="code">compile-file</code>,
explicitly.
</p></dd></dl>
<dl class="first-defvr">
<dt class="defvr" id="index-_0025auto_002dcompilation_002doptions"><span class="category-def">Scheme Variable: </span><span><strong class="def-name">%auto-compilation-options</strong><a class="copiable-link" href="#index-_0025auto_002dcompilation_002doptions"> &para;</a></span></dt>
<dd><p>This variable contains the options passed to the <code class="code">compile-file</code>
procedure when auto-compiling source files. By default, it enables
useful compilation warnings. It can be customized from <samp class="file">~/.guile</samp>.
</p></dd></dl>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Loading.html">Loading Scheme Code from File</a>, Previous: <a href="Fly-Evaluation.html">Procedures for On the Fly Evaluation</a>, Up: <a href="Read_002fLoad_002fEval_002fCompile.html">Reading and Evaluating Scheme Code</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>