<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.9">
<title>CallingFromCToSML</title>
<link rel="stylesheet" href="./asciidoc.css" type="text/css">
<link rel="stylesheet" href="./pygments.css" type="text/css">


<script type="text/javascript" src="./asciidoc.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
asciidoc.install();
/*]]>*/
</script>
<link rel="stylesheet" href="./mlton.css" type="text/css"/>
</head>
<body class="article">
<div id="banner">
<div id="banner-home">
<a href="./Home">MLton 20130715</a>
</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 <span class="monospaced">real * char -&gt; int</span> as the C function <span class="monospaced">foo</span>.
MLton extends the syntax of SML to allow expressions like the
following:</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>_export "foo": (real * char -&gt; int) -&gt; unit;</pre>
</div></div>
<div class="paragraph"><p>The above expression exports a C function named <span class="monospaced">foo</span>, with
prototype</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">Int32</span> <span class="nf">foo</span> <span class="p">(</span><span class="n">Real64</span> <span class="n">x0</span><span class="p">,</span> <span class="n">Char</span> <span class="n">x1</span><span class="p">);</span>
</pre></div></div></div>
<div class="paragraph"><p>The <span class="monospaced">_export</span> expression denotes a function of type
<span class="monospaced">(real * char -&gt; int) -&gt; unit</span> that when called with a function
<span class="monospaced">f</span>, arranges for the exported <span class="monospaced">foo</span> function to call <span class="monospaced">f</span>
when <span class="monospaced">foo</span> is called.  So, for example, the following exports and
defines <span class="monospaced">foo</span>.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="kr">val</span> <span class="nv">e</span> <span class="p">=</span> <span class="p">_</span><span class="n">export</span> <span class="s2">&quot;foo&quot;</span><span class="p">:</span> <span class="p">(</span><span class="n">real</span> <span class="n">*</span> <span class="n">char</span> <span class="p">-&gt;</span> <span class="n">int</span><span class="p">)</span> <span class="p">-&gt;</span> <span class="n">unit</span><span class="p">;</span>
<span class="kr">val</span> <span class="p">_</span> <span class="p">=</span> <span class="n">e</span> <span class="p">(</span><span class="kr">fn</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="mi">13</span> <span class="n">+</span> <span class="nn">Real</span><span class="p">.</span><span class="n">floor</span> <span class="n">x</span> <span class="n">+</span> <span class="nn">Char</span><span class="p">.</span><span class="n">ord</span> <span class="n">c</span><span class="p">)</span>
</pre></div></div></div>
<div class="paragraph"><p>The general form of an <span class="monospaced">_export</span> expression is</p></div>
<div class="listingblock">
<div class="content monospaced">
<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 <span class="monospaced">_import</span>, a
sequence of attributes may follow the function name.</p></div>
<div class="paragraph"><p>MLton&#8217;s <span class="monospaced">-export-header</span> 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 <span class="monospaced">typedef</span>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 <span class="monospaced">export.sml</span> is</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span>
</pre></div></div></div>
<div class="paragraph"><p>Create the header file with <span class="monospaced">-export-header</span>.</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>% mlton -default-ann 'allowFFI true'    \
        -export-header export.h         \
        -stop tc                        \
        export.sml</pre>
</div></div>
<div class="paragraph"><p><span class="monospaced">export.h</span> now contains the following C prototypes.</p></div>
<div class="listingblock">
<div class="content monospaced">
<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 <span class="monospaced">export.h</span> in a C program, <span class="monospaced">ffi-export.c</span>, as follows.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span>
</pre></div></div></div>
<div class="paragraph"><p>Compile <span class="monospaced">ffi-export.c</span> and <span class="monospaced">export.sml</span>.</p></div>
<div class="listingblock">
<div class="content monospaced">
<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 <span class="monospaced">export</span>.</p></div>
<div class="listingblock">
<div class="content monospaced">
<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"><span class="monospaced">export.sml</span></a>
</p>
</li>
<li>
<p>
<a href="https://raw.github.com/MLton/mlton/master/doc/examples/ffi/ffi-export.c"><span class="monospaced">ffi-export.c</span></a>
</p>
</li>
</ul></div>
</div>
</div>
</div>
<div id="footnotes"><hr></div>
<div id="footer">
<div id="footer-text">
</div>
<div id="footer-badges">
</div>
</div>
</body>
</html>
