Tuesday, February 27, 2007

Linux/PPC support and assorted FFI improvements

For the last two weeks I've mostly been working on some C library interface (FFI) improvements. FFI work involves dealing with a lot of platform-specific, arcane, poorly documented details, so it is not the most fun thing to code, but it has to be done. It is not too bad, actually, because I get to learn a lot about OS and CPU architecture. So here are the recent improvements:

  • Calling C functions returning structs by value is now supported

  • Factor callbacks can return structs by value too

  • Support for stdcall callbacks on Windows

  • The Linux/ARM port now has working FFI

  • The Linux/PPC port now has working FFI

Now with these out of the way, Factor 0.88 is ready for release.

The C library interface is quite complete now. Here is what it already supports:

  • Easy to use: no glue code has to be written in C, concise

  • Relatively efficient

  • Supports both stdcall and cdecl ABIs on Windows (and as of 0.88 this support is complete)

  • Integer and floating point parameters, return values

  • Automatic conversion for ASCII and UTF16 C string parameters, returns

  • Declaring C struct and union types, allocating/deallocating/mutating C data

  • Passing struct parameters by reference and value (value struct returns were missing until 0.88)

  • Calling C functions indirectly by function pointer

  • Callbacks from C to Factor

I'm quite proud of how well it works. There are only a few missing features, and I will be adding all of them in the near future:

  • "long long" return values work on 32-bit systems, but not "long long" parameters

  • Declaring structs and unions which contain fixed-size value arrays

  • Passing floating point parameters on ARM

Apart from these I can't really think of anything else I could add to the FFI. It really does seem quite complete and portable these days.


u.int.32.t said...

That is seriously slick. I bow to the master.

Slava Pestov said...

I'm just a humble servant.