From: Brandon J. Van Every
Subject: Automagical C header file bindings?
Date: 
Message-ID: <3asp2gF6b0j15U1@individual.net>
Is there any Scheme or Lisp technology that will read a pile of *.h files
and generate the appropriate bindings from C to Scheme or Lisp, without user
intervention?  The scope of *.h binding I'm interested in is:

- automagically generate OpenGL bindings
- automagically generate SDL (Simple DirectMedia Layer) bindings
http://www.libsdl.org

At least these are, realistically speaking, the things I'd like to create
Scheme or Lisp bindings for.  If you say "bindings for these already exist,"
the idea of automagically generating the binding is still important.  For
instance, OpenGL will go to 2.0 this year.  SDL also undergoes change.

I have a small experience now with Bigloo Scheme's Cigloo utility.  It can
help generate a binding, but it can't just suck in arbitrary *.h files and
produce good output.  In fact it appears to be choking on simple things like
#---*/ comments that are split upon a line.  Also more complex things; the
point is, it's potentially useful, but not automagical.  Thought I'd ask if
there's anything automagical or nearly so.

I suppose there's SWIG but I'm wondering if there's anything else.  In
particular, something that is only interested in Scheme <--> C or Lisp <-->
C, and thereby has a simpler, more robust, or more automagical
implementation than SWIG.

-- 
Cheers,                     www.indiegamedesign.com
Brandon Van Every           Seattle, WA

"The pioneer is the one with the arrows in his back."
                          - anonymous entrepreneur

From: Marcin 'Qrczak' Kowalczyk
Subject: Re: Automagical C header file bindings?
Date: 
Message-ID: <87mzsm8wy9.fsf@qrnik.zagroda>
"Brandon J. Van Every" <·····························@yahoo.com> writes:

> I have a small experience now with Bigloo Scheme's Cigloo utility.
> It can help generate a binding, but it can't just suck in arbitrary
> *.h files and produce good output.

In general it's impossible to generate good bindings from *.h files to
any HLL automatically, because they don't provide enough information -
e.g. about memory management, about pointers used for in vs. out
parameters, about the way sizes of arrays are passed, about encoding
of strings, about the association between particular int parameters
and sets of constants or bit masks to be used for them, etc.

Example. Here are functions:

iconv_t iconv_open(const char *tocode, const char *fromcode);
size_t iconv(iconv_t cd,
             char **inbuf, size_t *inbytesleft,
             char **outbuf, size_t *outbytesleft);
int iconv_close(iconv_t cd);

Semantics is described e.g. here:
http://www.opengroup.org/onlinepubs/009695399/functions/iconv_open.html
http://www.opengroup.org/onlinepubs/009695399/functions/iconv.html
http://www.opengroup.org/onlinepubs/009695399/functions/iconv_close.html

The iconv function is the most interesting one. What interface would
you give to it in your HLL of choice, such that a tool could generate
it automatically?

* * *

Compare the signature of iconv to a different function:
   int execv(const char *, char *const *);
Here almost the same type, which differ only in constness, is used
with a completely different meaning than in iconv. How would the tool
producing the interface know what to do with it?

* * *

Here is another function:

int setsockopt(int socket, int level, int option_name,
               const void *option_value, socklen_t option_len);

http://www.opengroup.org/onlinepubs/009695399/functions/setsockopt.html

Depending on option_name, the interpretation of opton_value is
different. Quite often it's a pointer to an int which denotes
a boolean, but can be e.g. a pointer to struct timeval.

The association between option_name and formats of option_value must
be looked up in the documentation, it's not specified formally in C.

The binary layout of option_value depends on the C implementation,
but for each type a portable C code can be written to construct it.
I mean, the program says e.g. "make a pointer to this struct linger,
after filling its members l_onoff and l_linger" but it doesn't care
how many bytes it takes and what is the endianness of these numbers.
This means that providing option_value as a byte array is not
sufficient.

How would a usable Lisp/Scheme interface be generated automatically?

I made an interface fro my language to setsockopt manually, and it
uses a dictionary indexed by option_names of functions which construct
option_value from the appropriate value of the target language. It's
not realistic to expect this to be generated automatically.

-- 
   __("<         Marcin Kowalczyk
   \__/       ······@knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/
From: Hans Oesterholt-Dijkema
Subject: Re: Automagical C header file bindings?
Date: 
Message-ID: <42494899$0$151$e4fe514c@news.xs4all.nl>
Brandon J. Van Every schreef:
> 
> I suppose there's SWIG but I'm wondering if there's anything else.  In
> particular, something that is only interested in Scheme <--> C or Lisp <-->
> C, and thereby has a simpler, more robust, or more automagical
> implementation than SWIG.

I've never seen anything as automagically as SWIG on generating
bindings between <a scripting language supported by SWIG> <--> C/C++.

The only thing still needed is writing type mappings between the
two worlds. SWIGs C/C++ support is *very* good.
From: jayessay
Subject: Re: Automagical C header file bindings?
Date: 
Message-ID: <m3hdiufbni.fsf@rigel.goldenthreadtech.com>
Hans Oesterholt-Dijkema <······@gawab.com> writes:

> Brandon J. Van Every schreef:
> > I suppose there's SWIG but I'm wondering if there's anything else.
> > In
> > particular, something that is only interested in Scheme <--> C or Lisp <-->
> > C, and thereby has a simpler, more robust, or more automagical
> > implementation than SWIG.
> 
> I've never seen anything as automagically as SWIG on generating
> bindings between <a scripting language supported by SWIG> <--> C/C++.
> 
> The only thing still needed is writing type mappings between the
> two worlds. SWIGs C/C++ support is *very* good.

And Allegro CL has SWIG support.  Also, I believe they have released
what they have to the public (adaptable to other CLs?)

http://www.franz.com/support/tech_corner/swig042804.lhtml


/Jon

-- 
'j' - a n t h o n y at romeo/charley/november com
From: Ray Dillinger
Subject: Re: Automagical C header file bindings?
Date: 
Message-ID: <yCf2e.12607$m31.128745@typhoon.sonic.net>
Brandon J. Van Every wrote:
> Is there any Scheme or Lisp technology that will read a pile of *.h files
> and generate the appropriate bindings from C to Scheme or Lisp, without user
> intervention?  The scope of *.h binding I'm interested in is:
> 
> - automagically generate OpenGL bindings
> - automagically generate SDL (Simple DirectMedia Layer) bindings
> http://www.libsdl.org
> 

Well, there's the LispWorks foreign parser, gcc-xml, and ffigen
that I know of.  OpenMCL uses ffigen, and seems to work pretty
well.  Or at least, so I've heard.

				Bear