Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: jsparkes@bnr.ca (jeff)
Cc: caml-list@margaux.inria.fr
Subject: Re: building stubs and modules from ANSI C include files
Date: Wed, 20 Sep 1995 14:12:42 +0200 (MET DST)	[thread overview]
Message-ID: <199509201212.OAA12294@pauillac.inria.fr> (raw)
In-Reply-To: <"24806 Tue Sep 19 11:05:34 1995"@bnr.ca>  from "jeff" at Sep 19, 95 11:03:00 am

> It seems that the "external" keyword in CSL gives most of the
> information about how to call a C function.  It should be possible to
> build the stub routines from a module description.
> 
> And it should be possible to build a module description from an ANSI C
> include file which gives the types of all of the arguments.

Well - almost. This has been done for some of the X11 include files by
Brad Chen and also Daniel de Rauglaudre, but it worked well because
the X11 functions have a consistent, fairly well-behaved interface.

For other C libraries, there are a number of problems to address,
those you mentioned:

> - no way to say that returned memory needs to be freed
> - how to raise exceptions, and which ones...
> - descriptions of C structures that are returned or passed in
> - currently no interface to dlopen if it exists.  Easy to add on
>   Solaris, at least.

and also:

- "out" and "in/out" parameters; in particular, C functions that
return several results via out parameters are best encoded as
Caml functions that return a tuple or record;

- to copy or not to copy; for instance, a C function that takes a
string can usually be passed a pointer to the first character of the
Caml string in the Caml heap; but if the function is going to keep
that pointer around (e.g. in a global or static variable), then the
GC will move the Caml string unpredictably, so it must be copied to a
malloc-ed block which is then passed to the C function.

The latter point shows that neither the Caml type "string" nor the C
type "char *" is sufficient to generate the stub code; extra info is
needed to determine whether string parameters must be copied or not.

Shail Aditya worked on this problem during a summer internship at
INRIA in '92. His approach was to provide declarations for the C types
and functions in a language (called MLC) that is a superset of Caml's
interfaces. From these declarations, both the Caml interface and the C
stub functions are generated.

For instance, there would be two string types in MLC: "string" and
"copied_string". Both are mapped to "string" in Caml and "char *" in
C, but the conversion functions are different (no copy for "string",
malloc and copy for "copied_string").

MLC also provides various mechanisms to add pre- and post-processing
(arbitrary C code) to generated stubs. A typical post-processing is to
check the return code and raise an exception if it denotes an error.

For various reasons, Shail's generator wasn't used much afterwards,
but I still have the code and can make it available if there's
interest and Shail agrees.

Nowadays, there might be other options worth investigating. For
instance, if your libraries have interfaces written in IDL, then it
could make sense to start with the IDL spec to generate the stub code.

Regards,

- Xavier Leroy




      reply	other threads:[~1995-09-20 12:29 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-09-19 15:03 jeff (j.d.) sparkes
1995-09-20 12:12 ` Xavier Leroy [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=199509201212.OAA12294@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@margaux.inria.fr \
    --cc=jsparkes@bnr.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox