<!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.20">
<title>CallingFromSMLToC</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-ok1
</a>
</h2>
</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">
<pre class="rouge highlight"><code data-lang="c">int foo (double d, char c);</code></pre>
</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">
<pre>_import "foo": real * char -&gt; int;</pre>
</div>
</div>
<div class="paragraph">
<p>This expression denotes a function of type <code>real * char -&gt; int</code> whose
behavior is implemented by calling the C function whose name is <code>foo</code>.
Thinking in terms of C, imagine that there are C variables <code>d</code> of type
<code>double</code>, <code>c</code> of type <code>unsigned char</code>, and <code>i</code> of type <code>int</code>.  Then,
the C statement <code>i = foo (d, c)</code> is executed and <code>i</code> is returned.</p>
</div>
<div class="paragraph">
<p>The general form of an <code>_import</code> expression is:</p>
</div>
<div class="listingblock">
<div class="content">
<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 <code><em>attribute</em></code> specifiers.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_example">Example</h2>
<div class="sectionbody">
<div class="paragraph">
<p><code>import.sml</code> imports the C function <code>ffi</code> and the C variable <code>FFI_INT</code>
as follows.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">(* main.sml *)

(* Declare ffi to be implemented by calling the C function ffi. *)
val ffi = _import "ffi" public: real array * int * int ref * char ref * int -&gt; char;
open Array

val size = 10
val a = tabulate (size, fn i =&gt; real i)
val ri = ref 0
val rc = ref #"0"
val n = 17

(* Call the C function *)
val c = ffi (a, Array.length a, ri, rc, n)

(* FFI_INT is declared as public in ffi-import.c *)
val (nGet, nSet) = _symbol "FFI_INT" public: (unit -&gt; int) * (int -&gt; unit);

val _ = print (concat [Int.toString (nGet ()), "\n"])

val _ =
   print (if c = #"c" andalso !ri = 45 andalso !rc = c
             then "success\n"
          else "fail\n")</code></pre>
</div>
</div>
<div class="paragraph">
<p><code>ffi-import.c</code> is</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="c">#include "export.h"

Int32 FFI_INT = 13;
Word32 FFI_WORD = 0xFF;
Bool FFI_BOOL = 1;
Real64 FFI_REAL = 3.14159;

Char8 ffi (Pointer a1, Int32 a1len, Pointer a2, Pointer a3, Int32 n) {
        double *ds = (double*)a1;
        int *pi = (int*)a2;
        char *pc = (char*)a3;
        int i;
        double sum;

        sum = 0.0;
        for (i = 0; i &lt; a1len; ++i) {
                sum += ds[i];
                ds[i] += n;
        }
        *pi = (int)sum;
        *pc = 'c';
        return 'c';
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Compile and run the program.</p>
</div>
<div class="listingblock">
<div class="content">
<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"><code>import.sml</code></a></p>
</li>
<li>
<p><a href="https://raw.github.com/MLton/mlton/master/doc/examples/ffi/ffi-import.c"><code>ffi-import.c</code></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>
</body>
</html>