Generating Function Prototypes

Alastair Reid reid at reid-consulting-uk.ltd.uk
Mon Jul 8 06:54:11 EDT 2002


> [...] if we were somehow able to generate prototypes then there
> would be no type checking at all.

Here's 4 ways to get typechecking - all admittedly harder than relying
on the fact that your Haskell compiler invokes a C compiler as part of
its operation (as does Hugs - not trying to put GHC down).

1) modify a C compiler to perform a consistency check; or

2) find a way of writing code which would cause an unmodified C
   compiler to generate type errors if the Haskell type was completely
   inconsistent with the C type.

   I haven't explored this very thoroughly but C typechecking is most
   fussy about assigning pointer values so code along the lines of the
   following might be a good place to start from:

     // the user-supplied header file
     #include "math.h"  

     // the type the Haskell compiler generates
     typedef HsDouble (*sin_ptr)(HsDouble);  

     // the consistency test
     sin_ptr test = &sin;

   We'd probably have to use 

     #define const /**/

   at the start to make the compiler 'ignore' const-ness.

3) Find a way to extract information about the calling convention from 
   information generated by a C compiler.

     gcc has a number of flags that dump additional information.
     .stabs entries are really informative.

4) Compare the output of the C compiler.

   Compile a piece of code which calls the function in question first
   using the header file and then using the prototype implied by the
   ffi declaration and compare the generated code.  If one pushes a
   float while the other pushes a double, you know you have a problem.



-- 
Alastair




More information about the FFI mailing list