Re: FFI with C structs

Matthew Fluet <[email protected]> Thu, 4 Oct 2018 22:34:47 -0400
Newsgroups gmane.comp.lang.ml.mlton.user
Message-ID <CAMrhFL4sQRbODqTZ_su+-zhrx7fWJPHccs5w3E97RxHqr-ov5A@mail.gmail.com>
--===============6462161371788021899==
Content-Type: multipart/alternative; boundary="0000000000001d1f620577721e5b"

--0000000000001d1f620577721e5b
Content-Type: text/plain; charset="UTF-8"

You could accomplish this with the operations in MLton.Pointer (see
http://mlton.org/MLtonPointer).  The MLton.Pointer.t type is an FFI type (
http://mlton.org/ForeignFunctionInterfaceTypes), so the typical scenario
would be to obtain the pointer from some imported function and then
manipulate it with the MLton.Pointer operations.  Something like:

  val getFoo = _import "getFoo": unit -> MLton.Pointer.t;
  val foo = getFoo ()
  val a = MLton.Pointer.getInt32 (foo, 0)
  val b = MLton.Pointer.getWord8 (MLton.Pointer.add (foo, 4), 0)
  val c = MLton.Pointer.getInt32 (MLton.Pointer.add (foo, 8), 0)

MLton does ship with a version of Matthias Blume's ml-nlffigen tool and
library (http://mlton.org/MLNLFFIGen and http://mlton.org/MLNLFFI), which
can be used to generate interface code from a header file.

But, if you are asking how you could craft an SML type such that it could
be passed to a C function expecting a "foo", then that is not possible with
MLton.  Only a limited number of types are allowed in MLton's FFI,
precisely because we do not guarantee representations of other types.

--Matthew


On Wed, Oct 3, 2018 at 4:03 PM Domagoj Stolfa <[email protected]> wrote:

> Hi all:
>
>
> I was wondering about what an idiomatic way to fill out a C structure in
> SML using mlton would be. Suppose I have a C struct such as:
>
>
> struct foo {
>
>      int a;
>
>      char b;
>
>      int c;
>
> };
>
>
> Is there any way I could specify a datatype or a module with the correct
> ABI for this C struct (alignment, offsets and size). I'm happy to
> specify this manually or use an automated way to do it -- but would
> prefer to avoid doing it with arrays and manually dumping the data to
> conform with the ABI. Another option I'd prefer to avoid, but would be
> more acceptable is building a little C interface that would take some
> SML types and translate them, but was wondering if there was a more
> idiomatic way to accomplish this.
>
>
> Thanks!
>
>
> --
>
> Domagoj
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "MLton-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
>
>
>
> _______________________________________________
> MLton-user mailing list
> [email protected]; [email protected]
> https://lists.sourceforge.net/lists/listinfo/mlton-user
>

-- 
You received this message because you are subscribed to the Google Groups "MLton-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

--0000000000001d1f620577721e5b
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div di=
r=3D"ltr"><div class=3D"gmail_default" style=3D"font-family:&quot;courier n=
ew&quot;,monospace;font-size:large">You could accomplish this with the oper=
ations in MLton.Pointer (see=C2=A0<a href=3D"http://mlton.org/MLtonPointer"=
>http://mlton.org/MLtonPointer</a>).=C2=A0 The MLton.Pointer.t type is an F=
FI type (<a href=3D"http://mlton.org/ForeignFunctionInterfaceTypes">http://=
mlton.org/ForeignFunctionInterfaceTypes</a>), so the typical scenario would=
 be to obtain the pointer from some imported function and then manipulate i=
t with the MLton.Pointer operations.=C2=A0 Something like:</div><div class=
=3D"gmail_default" style=3D"font-family:&quot;courier new&quot;,monospace;f=
ont-size:large"><br></div><div class=3D"gmail_default" style=3D"font-family=
:&quot;courier new&quot;,monospace;font-size:large">=C2=A0 val getFoo =3D _=
import &quot;getFoo&quot;: unit -&gt; MLton.Pointer.t;</div><div class=3D"g=
mail_default" style=3D"font-family:&quot;courier new&quot;,monospace;font-s=
ize:large">=C2=A0 val foo =3D getFoo ()</div><div class=3D"gmail_default" s=
tyle=3D"font-family:&quot;courier new&quot;,monospace;font-size:large">=C2=
=A0 val a =3D MLton.Pointer.getInt32 (foo, 0)</div><div class=3D"gmail_defa=
ult" style=3D"font-family:&quot;courier new&quot;,monospace;font-size:large=
">=C2=A0 val b =3D MLton.Pointer.getWord8 (MLton.Pointer.add (foo, 4), 0)</=
div><div class=3D"gmail_default" style=3D"font-family:&quot;courier new&quo=
t;,monospace;font-size:large">=C2=A0 val c =3D MLton.Pointer.getInt32 (MLto=
n.Pointer.add (foo, 8), 0)</div><div class=3D"gmail_default" style=3D"font-=
family:&quot;courier new&quot;,monospace;font-size:large"><br></div><div cl=
ass=3D"gmail_default" style=3D"font-family:&quot;courier new&quot;,monospac=
e;font-size:large">MLton does ship with a version of Matthias Blume&#39;s m=
l-nlffigen tool and library (<a href=3D"http://mlton.org/MLNLFFIGen">http:/=
/mlton.org/MLNLFFIGen</a> and=C2=A0<a href=3D"http://mlton.org/MLNLFFI">htt=
p://mlton.org/MLNLFFI</a>), which can be used to generate interface code fr=
om a header file.</div><div class=3D"gmail_default" style=3D"font-family:&q=
uot;courier new&quot;,monospace;font-size:large"><br></div><div class=3D"gm=
ail_default" style=3D"font-family:&quot;courier new&quot;,monospace;font-si=
ze:large">But, if you are asking how you could craft an SML type such that =
it could be passed to a C function expecting a &quot;foo&quot;, then that i=
s not possible with MLton.=C2=A0 Only a limited number of types are allowed=
 in MLton&#39;s FFI, precisely because we do not guarantee representations =
of other types.</div><div class=3D"gmail_default" style=3D"font-family:&quo=
t;courier new&quot;,monospace;font-size:large"><br></div><div class=3D"gmai=
l_default" style=3D"font-family:&quot;courier new&quot;,monospace;font-size=
:large">--Matthew</div><div class=3D"gmail_default" style=3D"font-family:&q=
uot;courier new&quot;,monospace;font-size:large"><br></div></div></div></di=
v></div><br><div class=3D"gmail_quote"><div dir=3D"ltr">On Wed, Oct 3, 2018=
 at 4:03 PM Domagoj Stolfa &lt;<a href=3D"mailto:[email protected]">ds815@cam=
.ac.uk</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"m=
argin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all:<br>
<br>
<br>
I was wondering about what an idiomatic way to fill out a C structure in <b=
r>
SML using mlton would be. Suppose I have a C struct such as:<br>
<br>
<br>
struct foo {<br>
<br>
=C2=A0=C2=A0=C2=A0=C2=A0 int a;<br>
<br>
=C2=A0=C2=A0=C2=A0=C2=A0 char b;<br>
<br>
=C2=A0=C2=A0=C2=A0=C2=A0 int c;<br>
<br>
};<br>
<br>
<br>
Is there any way I could specify a datatype or a module with the correct <b=
r>
ABI for this C struct (alignment, offsets and size). I&#39;m happy to <br>
specify this manually or use an automated way to do it -- but would <br>
prefer to avoid doing it with arrays and manually dumping the data to <br>
conform with the ABI. Another option I&#39;d prefer to avoid, but would be =
<br>
more acceptable is building a little C interface that would take some <br>
SML types and translate them, but was wondering if there was a more <br>
idiomatic way to accomplish this.<br>
<br>
<br>
Thanks!<br>
<br>
<br>
-- <br>
<br>
Domagoj<br>
<br>
<br>
-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;MLton-user&quot; group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:mlton-user%[email protected]" target=3D"_bl=
ank">[email protected]</a>.<br>
<br>
<br>
<br>
_______________________________________________<br>
MLton-user mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">MLton=
[email protected]</a>; <a href=3D"mailto:[email protected]" ta=
rget=3D"_blank">[email protected]</a><br>
<a href=3D"https://lists.sourceforge.net/lists/listinfo/mlton-user" rel=3D"=
noreferrer" target=3D"_blank">https://lists.sourceforge.net/lists/listinfo/=
mlton-user</a><br>
</blockquote></div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;MLton-user&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:[email protected]">mlton-user+unsu=
[email protected]</a>.<br />

--0000000000001d1f620577721e5b--


--===============6462161371788021899==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


--===============6462161371788021899==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
MLton-user mailing list
[email protected]; [email protected]
https://lists.sourceforge.net/lists/listinfo/mlton-user

--===============6462161371788021899==--