<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.26">
<title>CallingFromCToSML</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
<link rel="stylesheet" href="./asciidoctor.css">
<link rel="stylesheet" href="./mlton.css">

</head>
<body class="article">
<div id="mlton-header">
<div id="mlton-header-text">
<h2>
<a href="./Home">
MLton
20241230+git20251029+dfsg-5
</a>
</h2>
</div>
</div>
<div id="header">
<h1>CallingFromCToSML</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>MLton&#8217;s <a href="ForeignFunctionInterface">ForeignFunctionInterface</a> allows programs to <em>export</em> SML
functions to be called from C.  Suppose you would like export from SML
a function of type <code>real * char -&gt; int</code> as the C function <code>foo</code>.
MLton extends the syntax of SML to allow expressions like the
following:</p>
</div>
<div class="listingblock">
<div class="content">
<pre>_export "foo": (real * char -&gt; int) -&gt; unit;</pre>
</div>
</div>
<div class="paragraph">
<p>The above expression exports a C function named <code>foo</code>, with
prototype</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="c">Int32 foo (Real64 x0, Char x1);</code></pre>
</div>
</div>
<div class="paragraph">
<p>The <code>_export</code> expression denotes a function of type
<code>(real * char -&gt; int) -&gt; unit</code> that when called with a function
<code>f</code>, arranges for the exported <code>foo</code> function to call <code>f</code>
when <code>foo</code> is called.  So, for example, the following exports and
defines <code>foo</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">val e = _export "foo": (real * char -&gt; int) -&gt; unit;
val _ = e (fn (x, c) =&gt; 13 + Real.floor x + Char.ord c)</code></pre>
</div>
</div>
<div class="paragraph">
<p>The general form of an <code>_export</code> expression is</p>
</div>
<div class="listingblock">
<div class="content">
<pre>_export "C function name" attr... : cFuncTy -&gt; unit;</pre>
</div>
</div>
<div class="paragraph">
<p>The type and the semicolon are not optional.  As with <code>_import</code>, a
sequence of attributes may follow the function name.</p>
</div>
<div class="paragraph">
<p>MLton&#8217;s <code>-export-header</code> option generates a C header file with
prototypes for all of the functions exported from SML.  Include this
header file in your C files to type check calls to functions exported
from SML.  This header file includes <code>typedef</code>s for the
<a href="ForeignFunctionInterfaceTypes">types that can be passed between SML and C</a>.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_example">Example</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Suppose that <code>export.sml</code> is</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">Unresolved directive in CallingFromCToSML.adoc - include::https://raw.github.com/MLton/mlton/master/doc/examples/ffi/export.sml[indent=0]</code></pre>
</div>
</div>
<div class="paragraph">
<p>Note that the the <code>reentrant</code> attribute is used for <code>_import</code>-ing the
C functions that will call the <code>_export</code>-ed SML functions.</p>
</div>
<div class="paragraph">
<p>Create the header file with <code>-export-header</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre>% mlton -default-ann 'allowFFI true'    \
        -export-header export.h         \
        -stop tc                        \
        export.sml</pre>
</div>
</div>
<div class="paragraph">
<p><code>export.h</code> now contains the following C prototypes.</p>
</div>
<div class="listingblock">
<div class="content">
<pre>Int8 f (Int32 x0, Real64 x1, Int8 x2);
Pointer f2 (Word8 x0);
void f3 ();
void f4 (Int32 x0);
extern Int32 zzz;</pre>
</div>
</div>
<div class="paragraph">
<p>Use <code>export.h</code> in a C program, <code>ffi-export.c</code>, as follows.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="c">Unresolved directive in CallingFromCToSML.adoc - include::https://raw.github.com/MLton/mlton/master/doc/examples/ffi/ffi-export.c[indent=0]</code></pre>
</div>
</div>
<div class="paragraph">
<p>Compile <code>ffi-export.c</code> and <code>export.sml</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre>% gcc -c ffi-export.c
% mlton -default-ann 'allowFFI true' \
         export.sml ffi-export.o</pre>
</div>
</div>
<div class="paragraph">
<p>Finally, run <code>export</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre>% ./export
g starting
...
g4 (0)
success</pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_download">Download</h2>
<div class="sectionbody">
<div class="ulist">
<ul>
<li>
<p><a href="https://raw.github.com/MLton/mlton/master/doc/examples/ffi/export.sml"><code>export.sml</code></a></p>
</li>
<li>
<p><a href="https://raw.github.com/MLton/mlton/master/doc/examples/ffi/ffi-export.c"><code>ffi-export.c</code></a></p>
</li>
</ul>
</div>
</div>
</div>
</div>
</body>
</html>