[dmd-internals] Testcase in test42.d

David Nadlinger code at klickverbot.at
Fri Apr 5 15:56:27 PDT 2013


On 6 Apr 2013, at 0:06, Walter Bright wrote:
> It's C ABI compatible. Note that:
>
> struct S { int[4] a; }
> struct T { int x; S s; }
>
> has a size of 20 bytes. What's going on is when the statically 
> allocated data is emitted, it is aligned to 16 bytes. This does not 
> break the C ABI.

My point here is that C compiler might not align the same data to 16 
bytes.

--- cpart.c
struct S41 { int a0, a1, a2, a3; };
struct S41 s41;
---

--- dpart.d
struct S41 { int[4] a; }
extern(C) extern shared S41 s41;

void main() {
     import core.stdc.stdio;
     printf("&s = %p\n", &s41);
     assert((cast(int)&s41 & 0xF) == 0);
}
---

---
clang -c cpart.c
dmd cpart.o dpart.d && ./cpart
---
&s = 0x1092b0fe8
core.exception.AssertError at dpart(7): Assertion failure
---

Thus, if int[4] is defined to be equivalent to four int fields (as you 
said it should be in previous ABI discussions, can't remember whether it 
is actually in the spec), and the point of test41() is to test that 
int[4] globals are aligned to 16 bytes, then we have a problem.

If the behavior you want to test for is that (int[4]) globals *defined 
in D code* are aligned to 16 bytes, then this obviously isn't a problem.

But my main point is that I can't see why the assertion in test41() 
should hold. If you think it should be left in, please cite the 
appropriate place in the spec so Iain and I can fix GDC and LDC. If it's 
not in the spec yet, we have to add it.

David


More information about the dmd-internals mailing list