[mercury-users] C FFI for discriminated union types.

Guillaume Yziquel guillaume.yziquel at citycable.ch
Sun Jun 19 22:15:54 AEST 2011

Le Sunday 19 Jun 2011 à 21:53:29 (+1000), Julien Fischer a écrit :
> Hi,


> On Sun, 19 Jun 2011, Guillaume Yziquel wrote:
> >I am now concerned with the C foreign function interface, most notably
> >concerning discriminated union types. I currently have the following
> >type declaration:
> >
> >	:- type term_def --->
> >		term_def(term)		;
> >		term_def_string(str)	.
> >
> >And I am passing back values of this type to C code, and would ideally
> >like to map them to an OCaml algebraic datatype. So I'd like to do the
> >conversion in my C stub code.
> >
> >However, everything seems to be an MR_Word, and there isn't much
> >structural type information for such discriminated datatypes available
> >on the C side. So how should I deconstruct them? The reference
> >guide is fairly
> >explicit for C# and such, but not for C...
> The data representation used for (non-enum) discriminated union types in
> the C grades is lower level that used in the non-C grades and is not
> very amenable to being manipulated in handwritten C code.  I suggest
> you consider doing at least some of the conversion in Mercury code.
> Julien.

Thank you for your answer.

Doing some of the conversion in Mercury code means possibly triggering
the OCaml GC, which compacts the heap, when doing the conversion from
Mercury code.

This is possible.

However the tradeoff is:

-1- Do the conversion in Mercury code, and take extra care for OCaml's
GC in every C stub that calls Mercury since it may trigger OCaml's GC.

-2- Do the conversion in C code, no OCaml GC worries (or at least not
too much) in exchange for tedious conversion function in C level.

Both are workable, but I'd appreciate having more background, if only
informally, about the design of data representation for discriminated
union datatypes in C grades. Just to have a clearer idea of which
alternative is the most maintainable in the short run.

     Guillaume Yziquel

mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au

More information about the users mailing list