The great inapplicable attribute debate

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Mon Apr 13 06:43:53 PDT 2009


Stewart Gordon wrote:
> Frits van Bommel wrote:
>> Kagamin wrote:
> <snip>
>>> Such mood was always in the spec: "AlignAttribute is ignored when 
>>> applied to declarations that are not structs or struct members".
>>
>> I never saw that before. So it doesn't work for class members?
> 
> http://www.digitalmars.com/d/1.0/class.html
> "The D compiler is free to rearrange the order of fields in a class to 
> optimally pack them in an implementation-defined manner. Consider the 
> fields much like the local variables in a function - the compiler 
> assigns some to registers and shuffles others around all to get the 
> optimal stack frame layout. This frees the code designer to organize the 
> fields in a manner that makes the code more readable rather than being 
> forced to organize it according to machine optimization rules. Explicit 
> control of field layout is provided by struct/union types, not classes."

Right, forgot about that bit. Probably because AFAIK no D compiler actually 
reorganizes them...

(How does this combine with the "D ABI" anyway? I know binary compatibility 
between the different compilers is a bit of a pipe dream at the moment, but the 
only way that could work would be to standardize any and all shuffling of class 
fields...)

>> And it won't change the alignment of unions if applied to union 
>> members (by changing the maximum alignment of the members)?
> 
> I'm not sure what you mean....

Unions are normally aligned to the maximum alignment for any member. Now consider:

union U {
     align(1) void* p;
     ubyte[size_t.sizeof] bytes;
}
union U2 {
     align(4) ubyte[12] data;
     char[12] str;
}
What are U.alignof and U2.alignof?

According to that piece of the spec, those align() attributes should be useless.
However, according to DMD[1] U.alignof is 1, and without the attribute it's 4.
U2 on the other hand is align(1) regardless of attribute.

It seems DMD disagrees with the spec here.
Same goes for GDC, except U.alignof is 8 without the attribute instead of 4 
because I'm using a 64-bit version.
LDC seems to agree with the spec (it ignores the aligns), but that may just be 
because it doesn't fully support align() in the first place...

[1]: 1.042 on Linux:
void main() {
     printf("%d %d\n", cast(int)U.alignof, cast(int)U2.alignof);
}



More information about the Digitalmars-d mailing list