[C2hs] Calling Convention

Trevor McDonell tmcdonell at cse.unsw.edu.au
Thu Apr 22 21:52:09 EDT 2010

Hi all,

I attach a small patch to choose the ccall/stdcall convention based on language.c attributes. I haven't added this for dynamic hooks (callImportDyn) yet, mostly through lack of a test case.

One issue/caveat I'd like some feedback on, is that in most cases the AST attribute will be found thus:

  * CDecl
    * declspecs-list
      + CTypeQual
        + CAttrQual
          * CAttr "__stdcall__" [] nodeinfo
    * declr-list
    * nodeinfo

However, if the attribute is given between the return type and function name:

  int  __stdcall getFoo();
  int* __stdcall getBar();

The first will be extracted as above, whereas in the second the attribute will be attached to the pointer and buried in the declr-list:

  * CDeclr
    * derived-declr-list
      + CPtrDeclr
        * typequal-list
              * CAttr "__stdcall__" []

The latter is the way that my current use case declares its prototypes (CUDA on windows), and microsoft seem to agree: http://msdn.microsoft.com/en-us/library/zxk0tw93(VS.80).aspx

So, my question is whether c2hs should be made to handle this, or language.c ?


P.S. This is fairly windows-only, but you can test the c2hs binding output by using __attribute((__stdcall__)) directly if your compiler doesn't map this to __stdcall (it probably won't).

-------------- next part --------------
A non-text attachment was scrubbed...
Name: stdcall
Type: application/octet-stream
Size: 6110 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/c2hs/attachments/20100422/9349d029/stdcall.obj
-------------- next part --------------

On 12/04/2010, at 10:51 PM, Manuel M T Chakravarty wrote:

> Hi Trev,
>> There was some noise a while back about adding support for foreign import conventions other than 'ccall'. Since the repo has disappeared from darcs.haskell.org, I was wondering if anything has come of this?
>> http://haskell.org/pipermail/c2hs/2009-September/000971.html
>> I essentially have the same problem at the moment, with a library that uses 'stdcall'. With a small amount of work, the header files can be convinced to output the appropriate C attribute, which language-c picks.
>> If somebody can give me some guidelines of what module I should be looking at, I wouldn't mind trying to hack something together...
> What platform are you using?  I'm not sure whether GHC supports stdcall on any platform except Windows.
> As for making use of the C attributes (as Duncan suggested in the follow up to the message you cited), c2hs's code generation pass keeps track of called functions and the foreign import declarations that need to be generated.  You would probably need to extend that to keep track of the calling convention that you gleaned from the attribute, but it's probably easier if we talk about that directly.
> Re the repo, we had to take it off darcs.haskell.org when that migrated to the new server.  I mean to put it up on code.haskell.org, but didn't get around to that.
> Manuel

More information about the C2hs mailing list