language-c icon indicating copy to clipboard operation
language-c copied to clipboard

Add Support For Alignment Specifier In C Struct Declarations

Open noahmartinwilliams opened this issue 2 years ago • 5 comments

C Data structures can now be parsed that have the _Alignas and alignas keywords used in it.

noahmartinwilliams avatar Aug 01 '23 23:08 noahmartinwilliams

Thank you,

If you add a test and give the version number a minor bump then we can make a release on Hackage.

The last I remember was the the tests were a little tricky, sorry.

expipiplus1 avatar Aug 02 '23 01:08 expipiplus1

I can't seem to figure out where the test would fit. test/harness/README says that suite is meant for "existing libraries" (which I'm guessing means compiling C libraries from other projects) and harness is for "tests documenting bugs and problems", but I can't seem to find where I would test new features. Should I just put one in harness?

I'm kind of new to Haskell and it's testing system tbh.

noahmartinwilliams avatar Aug 03 '23 00:08 noahmartinwilliams

Ok, welcome, and no worries, I'll take care of the tests when this is done:

I think that this isn't quite correct however. For example it will fail when the alignment specifier is not the first specifier of the first struct member.

A careful reading of the C11 standard (draft) actually doesn't allow these on struct members (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) however this was corrected in a defect report https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2396.htm#dr_444

I would expect that the example here parses, for example: https://en.cppreference.com/w/c/language/_Alignas

#include <stdalign.h>
#include <stdio.h>
 
// every object of type struct sse_t will be aligned to 16-byte boundary
// (note: needs support for DR 444)
struct sse_t
{
    alignas(16) float sse_data[4];
};
 
// every object of type struct data will be aligned to 128-byte boundary
struct data
{
    char x;
    alignas(128) char cacheline[128]; // over-aligned array of char,
                                      // not array of over-aligned chars
};
 
int main(void)
{
    [printf](http://en.cppreference.com/w/c/io/fprintf)("sizeof(data) = %zu (1 byte + 127 bytes padding + 128-byte array)\n",
           sizeof(struct data));
 
    [printf](http://en.cppreference.com/w/c/io/fprintf)("alignment of sse_t is %zu\n", alignof(struct sse_t));
 
    alignas(2048) struct data d; // this instance of data is aligned even stricter
    (void)d; // suppresses "maybe unused" warning
}

expipiplus1 avatar Aug 03 '23 03:08 expipiplus1

Does this library support statements that declare a variable? I can't seem to find anything that would allow it to declare a local variable. Edit: Actually it seems that this project uses much of the grammar from the C11 standard which, confusingly enough, doesn't count variable declarations as a kind of statement. I've added a test to my version that uses the code you presented and the type checker says it works. I wasn't able to get it to print out the alignment and size of the structures though.

noahmartinwilliams avatar Aug 17 '23 19:08 noahmartinwilliams

Hi! Dropping in from outside -- having in language-c would be awesome for Accelerate. With this PR, and with an appropriate version bump in c2hs, c2hs can parse cuda.h from Cuda 12. With Noah's other PR to cuda this means that Accelerate can support Cuda 12, which would be great. :)

tomsmeding avatar Nov 20 '23 11:11 tomsmeding