<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.10">
<title>CallingFromSMLToC</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 20201002</a>
</div>
</div>
<div id="header">
<h1>CallingFromSMLToC</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>MLton&#8217;s <a href="ForeignFunctionInterface">ForeignFunctionInterface</a> allows an SML program to <em>import</em>
C functions.  Suppose you would like to import from C a function with
the following prototype:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="kt">int</span> <span class="nf">foo</span> <span class="p">(</span><span class="kt">double</span> <span class="n">d</span><span class="p">,</span> <span class="kt">char</span> <span class="n">c</span><span class="p">);</span>
</pre></div></div></div>
<div class="paragraph"><p>MLton extends the syntax of SML to allow expressions like the following:</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>_import "foo": real * char -&gt; int;</pre>
</div></div>
<div class="paragraph"><p>This expression denotes a function of type <span class="monospaced">real * char -&gt; int</span> whose
behavior is implemented by calling the C function whose name is <span class="monospaced">foo</span>.
Thinking in terms of C, imagine that there are C variables <span class="monospaced">d</span> of type
<span class="monospaced">double</span>, <span class="monospaced">c</span> of type <span class="monospaced">unsigned char</span>, and <span class="monospaced">i</span> of type <span class="monospaced">int</span>.  Then,
the C statement <span class="monospaced">i = foo (d, c)</span> is executed and <span class="monospaced">i</span> is returned.</p></div>
<div class="paragraph"><p>The general form of an <span class="monospaced">_import</span> expression is:</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>_import "C function name" attr... : cFuncTy;</pre>
</div></div>
<div class="paragraph"><p>The type and the semicolon are not optional.</p></div>
<div class="paragraph"><p>The function name is followed by a (possibly empty) sequence of
attributes, analogous to C <span class="monospaced">__attribute__</span> specifiers.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_example">Example</h2>
<div class="sectionbody">
<div class="paragraph"><p><span class="monospaced">import.sml</span> imports the C function <span class="monospaced">ffi</span> and the C variable <span class="monospaced">FFI_INT</span>
as follows.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="cm">(* main.sml *)</span><span class="w"></span>

<span class="cm">(* Declare ffi to be implemented by calling the C function ffi. *)</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">ffi</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">_</span><span class="n">import</span><span class="w"> </span><span class="s">&quot;ffi&quot;</span><span class="w"> </span><span class="n">public</span><span class="p">:</span><span class="w"> </span><span class="n">real</span><span class="w"> </span><span class="n">array</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">int</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">int</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">char</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">int</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">char</span><span class="p">;</span><span class="w"></span>
<span class="k">open</span><span class="w"> </span><span class="n">Array</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">10</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">tabulate</span><span class="w"> </span><span class="p">(</span><span class="n">size</span><span class="p">,</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">real</span><span class="w"> </span><span class="n">i</span><span class="p">)</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">ri</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="mi">0</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">rc</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="p">#</span><span class="s">&quot;0&quot;</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">17</span><span class="w"></span>

<span class="cm">(* Call the C function *)</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">ffi</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">Array</span><span class="p">.</span><span class="n">length</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">ri</span><span class="p">,</span><span class="w"> </span><span class="n">rc</span><span class="p">,</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"></span>

<span class="cm">(* FFI_INT is declared as public in ffi-import.c *)</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="p">(</span><span class="n">nGet</span><span class="p">,</span><span class="w"> </span><span class="n">nSet</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">_</span><span class="n">symbol</span><span class="w"> </span><span class="s">&quot;FFI_INT&quot;</span><span class="w"> </span><span class="n">public</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">int</span><span class="p">)</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">int</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="p">);</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">print</span><span class="w"> </span><span class="p">(</span><span class="n">concat</span><span class="w"> </span><span class="p">[</span><span class="n">Int</span><span class="p">.</span><span class="n">toString</span><span class="w"> </span><span class="p">(</span><span class="n">nGet</span><span class="w"> </span><span class="p">()),</span><span class="w"> </span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">])</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="n">print</span><span class="w"> </span><span class="p">(</span><span class="k">if</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">#</span><span class="s">&quot;c&quot;</span><span class="w"> </span><span class="k">andalso</span><span class="w"> </span><span class="n">!ri</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">45</span><span class="w"> </span><span class="k">andalso</span><span class="w"> </span><span class="n">!rc</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">c</span><span class="w"></span>
<span class="w">             </span><span class="k">then</span><span class="w"> </span><span class="s">&quot;success</span><span class="se">\n</span><span class="s">&quot;</span><span class="w"></span>
<span class="w">          </span><span class="k">else</span><span class="w"> </span><span class="s">&quot;fail</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p><span class="monospaced">ffi-import.c</span> is</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&quot;export.h&quot;</span><span class="cp"></span>

<span class="n">Int32</span> <span class="n">FFI_INT</span> <span class="o">=</span> <span class="mi">13</span><span class="p">;</span>
<span class="n">Word32</span> <span class="n">FFI_WORD</span> <span class="o">=</span> <span class="mh">0xFF</span><span class="p">;</span>
<span class="n">Bool</span> <span class="n">FFI_BOOL</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="n">Real64</span> <span class="n">FFI_REAL</span> <span class="o">=</span> <span class="mf">3.14159</span><span class="p">;</span>

<span class="n">Char8</span> <span class="nf">ffi</span> <span class="p">(</span><span class="n">Pointer</span> <span class="n">a1</span><span class="p">,</span> <span class="n">Int32</span> <span class="n">a1len</span><span class="p">,</span> <span class="n">Pointer</span> <span class="n">a2</span><span class="p">,</span> <span class="n">Pointer</span> <span class="n">a3</span><span class="p">,</span> <span class="n">Int32</span> <span class="n">n</span><span class="p">)</span> <span class="p">{</span>
        <span class="kt">double</span> <span class="o">*</span><span class="n">ds</span> <span class="o">=</span> <span class="p">(</span><span class="kt">double</span><span class="o">*</span><span class="p">)</span><span class="n">a1</span><span class="p">;</span>
        <span class="kt">int</span> <span class="o">*</span><span class="n">pi</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span><span class="o">*</span><span class="p">)</span><span class="n">a2</span><span class="p">;</span>
        <span class="kt">char</span> <span class="o">*</span><span class="n">pc</span> <span class="o">=</span> <span class="p">(</span><span class="kt">char</span><span class="o">*</span><span class="p">)</span><span class="n">a3</span><span class="p">;</span>
        <span class="kt">int</span> <span class="n">i</span><span class="p">;</span>
        <span class="kt">double</span> <span class="n">sum</span><span class="p">;</span>

        <span class="n">sum</span> <span class="o">=</span> <span class="mf">0.0</span><span class="p">;</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">a1len</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span>
                <span class="n">sum</span> <span class="o">+=</span> <span class="n">ds</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
                <span class="n">ds</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+=</span> <span class="n">n</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="o">*</span><span class="n">pi</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="n">sum</span><span class="p">;</span>
        <span class="o">*</span><span class="n">pc</span> <span class="o">=</span> <span class="sc">&#39;c&#39;</span><span class="p">;</span>
        <span class="k">return</span> <span class="sc">&#39;c&#39;</span><span class="p">;</span>
<span class="p">}</span>
</pre></div></div></div>
<div class="paragraph"><p>Compile and run the program.</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>% mlton -default-ann 'allowFFI true' -export-header export.h  import.sml ffi-import.c
% ./import
13
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/import.sml"><span class="monospaced">import.sml</span></a>
</p>
</li>
<li>
<p>
<a href="https://raw.github.com/MLton/mlton/master/doc/examples/ffi/ffi-import.c"><span class="monospaced">ffi-import.c</span></a>
</p>
</li>
</ul></div>
</div>
</div>
<div class="sect1">
<h2 id="_next_steps">Next Steps</h2>
<div class="sectionbody">
<div class="ulist"><ul>
<li>
<p>
<a href="CallingFromSMLToCFunctionPointer">CallingFromSMLToCFunctionPointer</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>
