[plt-scheme] tools and practises to derice FFI types from C type declarations?

From: Eli Barzilay (eli at barzilay.org)
Date: Tue Oct 7 02:21:31 EDT 2008

On Oct  5, Ernie Smith wrote:
> [...]
> It really should be computed wherever a PLT scheme script is
> installed.  At this stage I'm starting to feel like I'm reinventing
> the wheel. Again!.  And this time only to accomplish a script.

There are several possible approaches to solve this problem:

1. Make a C parser than can do the equivalent of `gcc -E' and grab the
   details from the result.  This has been suggested several times but
   nobody tried to implement it.  But given the alternatives below, I
   doubt that it is a good approach, for example, it depends on having
   the header files available in the first place.

2. Use SWIG to generate Scheme glue code.  AFAIK, the mzscheme backend
   in SWIG is generating C glue code instead, and it should be near
   trivial to write a new Scheme-code generation backend.  Again, I
   think that there are better alternatives.

3. Write some C code that generates the information you need.  For
   example, write-compile-and-run a C program that prints just writes
   the offset and size of a particular field, then use that in Scheme
   to pull out the value from the struct.  You can also do all of that
   in a macro so there is no run-time penalty; the disadtvantage is
   that you get platform-dependent byte-code (since the numbers will
   be written into it), but the advantage is that the zo file can be
   used in places where no compiler is available.

   This is how the SGL collection is detecting the different GL data
   sizes.  (And also something the Felix wrote about in the context of
   Larceny recently:

4. The best solution, IMO, is to write a small C file that does the
   trivial work, but has a simple foreign-friendly interface.  In your
   case, it will have a function that runs lstat and returns just the
   result you're interested in.  It might seem like a bad idea
   initially, but it is really as portable as the previous solutions,
   the only difference is that you need a C compiler -- in #1 you
   don't need one, but you need the header files.  Also, you replace
   the platform dependent Scheme code of #2 or #3 by platform
   dependent library, but it's the same kind of dependency.

   (BTW, I plan to switch the SGL collection to doing something like
   this, getting rid of the platform dependent .zo file.)

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.