From: Brian Denheyer
Subject: New version of FFIGEN and Gambit-C backend
Date: 
Message-ID: <87n1c35ym0.fsf@soggy.deldotd.com>
Finally...

Please see 

  http://www.zipcon.net/~briand/ffigen.html

For the distribution.

FFIGEN is a modified version of the lcc compiler which reads C header
files and generates an intermediate file, a .ffi, file composed of
sexp's.

I expect that ffigen will be of great use to both Scheme and Lisp
users.  I expect that it will be useful for other languages as well.

lcc has been modified to keep track of typedef definitions which
greatly reduces the number of "extraneous" warnings caused by type
mismatches.  Routines are included which can "de-reference" typedefs
when the underlying primitive type information is needed.

The Gambit-C backend seems to be quite robust and generates a great
deal of FFI code for gsc. In particular I have tested it on Xlib.h.
It resulting scheme ffi file is approximately 1.8Mbytes.  The entire
process for start to the finish of compilation only takes 1m 40s.  it
compiles completely without any warnings.

It generates :

* defines for enums and macros
* accessors and mutators for structure fields
* function interface definitions
* accessors and mutators for variable definitions
* c-define-types to represent typedef definitions.

It is reasonably intelligent about processing the file.  Translation
of information from "incidental" header files should be kept to a
minimum.  IOW, if you include stdio.h as an incidental header, it will
only generate ffi declarations for those data and structures fromt
stdio.h which are actually used in the file of interest.

Unfortunately it does require a minor patch to Gambit-C.

Please download it , use it and let me know how things work (or
don't).

Thanks !


Brian

From: Marco Antoniotti
Subject: Re: New version of FFIGEN and Gambit-C backend
Date: 
Message-ID: <y6cofwjjxbc.fsf@octagon.mrl.nyu.edu>
Brian Denheyer <······@zipcon.net> writes:

> Finally...
> 
> Please see 
> 
>   http://www.zipcon.net/~briand/ffigen.html
> 
> For the distribution.
> 
> FFIGEN is a modified version of the lcc compiler which reads C header
> files and generates an intermediate file, a .ffi, file composed of
> sexp's.
> 
> I expect that ffigen will be of great use to both Scheme and Lisp
> users.  I expect that it will be useful for other languages as well.
> 
> lcc has been modified to keep track of typedef definitions which
> greatly reduces the number of "extraneous" warnings caused by type
> mismatches.  Routines are included which can "de-reference" typedefs
> when the underlying primitive type information is needed.

How difficult it is to add a "backend" for a different CL FFI (CMUCL,
ACL, LispWorks, etc etc?)

This would really make the system worthwhile.

Cheers

-- 
Marco Antoniotti =============================================================
NYU Courant Bioinformatics Group		 tel. +1 - 212 - 998 3488
719 Broadway 12th Floor                          fax  +1 - 212 - 995 4122
New York, NY 10003, USA				 http://galt.mrl.nyu.edu/valis
             Like DNA, such a language [Lisp] does not go out of style.
			      Paul Graham, ANSI Common Lisp
From: Brian Denheyer
Subject: Re: New version of FFIGEN and Gambit-C backend
Date: 
Message-ID: <87elxf5h3d.fsf@soggy.deldotd.com>
>>>>> "Marco" == Marco Antoniotti <·······@cs.nyu.edu> writes:

  Marco> How difficult it is to add a "backend" for a different CL FFI
  Marco> (CMUCL, ACL, LispWorks, etc etc?)

All the backend does is read the .ffi file and write out the ffi
format of your choice.  The gambit backend is 602 lines of scheme.
However, many of the routines can be re-used regardless of the target.

lcc -> ffi file -> backend -> ffi code.

An example is probably helpful at this point :

Here's the C :

    int shift_left(int value, int n);

Here's the .ffi :

    (macro ("<cmdarg>" 1) "__i386__" "1")
    (macro ("<cmdarg>" 1) "i386" "1")
    (macro ("<cmdarg>" 1) "_POSIX_SOURCE" "1")
    (macro ("<cmdarg>" 1) "__linux__" "1")
    (macro ("<cmdarg>" 1) "linux" "1")
    (macro ("<cmdarg>" 1) "__unix__" "1")
    (macro ("<cmdarg>" 1) "unix" "1")
    (macro ("<cmdarg>" 1) "__LCC__" "1")
    (macro ("<cmdarg>" 1) "__STRICT_ANSI__" "1")
    (macro ("<cmdarg>" 1) "__signed__" "signed")
    (function ("temp.h" 1) 
     "shift_left" 
     (function 
      ((int ()) (int ()))
      (int ())))

Here's the Gambit-C specific scheme ffi code :

    (define c-lambda (int int) int "shift_left")

You could just as easily write out C-code which was specific to some
implementation.  That could be a particular Lisp implementation, or it
could be Python.

One of the things on my to-do list is to make the backend process more
"templatized", so that it is easier to tell the backend what you want
to your interface code to look like without having to actually write
your own backend.

That was Lars original design with ffigen.  Unfortunately, I got way
off track and ended up implementing something too specific.  Even so,
it's not difficult to write a backend.


Brian