[cfe-dev] Implementation of the CGRecordLayoutBuilder for Microsoft ABI.

Eli Friedman eli.friedman at gmail.com
Tue Sep 27 11:26:56 PDT 2011


On Mon, Sep 26, 2011 at 11:29 PM, r4start <r4start at gmail.com> wrote:
> On 27/09/2011 04:07, Eli Friedman wrote:
>>
>> On Mon, Sep 26, 2011 at 4:49 PM, David Blaikie<dblaikie at gmail.com>  wrote:
>>>
>>> On Mon, Sep 26, 2011 at 4:36 PM, Francois Pichet<pichet2000 at gmail.com>
>>> wrote:
>>>>
>>>> On Mon, Sep 26, 2011 at 6:52 PM, Eli Friedman<eli.friedman at gmail.com>
>>>> wrote:
>>>>>
>>>>> On Mon, Sep 26, 2011 at 6:09 AM, r4start<r4start at gmail.com>  wrote:
>>>>>>
>>>>>> I really don't understand why align=4 isn't correct.
>>>>>> I always thought that #pragma pack only affects members of the
>>>>>> structure.
>>>>>> And __declspec(align(n)) specifies the alignment for objects of type.
>>>>>> In MSVS align for B is 4 and for A is 1.
>>>>>
>>>>> The only answer I can come up with is the model MSVC uses is simply
>>>>> insane.
>>>>>
>>>>> One more attempt to get some sanity: does the following compile with
>>>>> MSVC?  If so, what is the output?
>>>>>
>>>>> #include<cstdio>
>>>>> class A {
>>>>>  char c;
>>>>> };
>>>>> class B : public virtual A {
>>>>>  public:
>>>>>  void *p;
>>>>> };
>>>>> B* x = new B[10];
>>>>> int main() { std::printf("%d\n", (int)(x[1].p)); }
>>>>>
>>>> it compiles and the output is 0.
>>>> not sure what you are trying to test here.
>>>
>>> I'll guess that Eli meant to print&x[1].p (to see whether the void*
>>> pointer
>>> was aligned or not), perhaps. I modified the program to print:
>>>     std::printf("%d\n", (int)(&x[0].p));
>>>     std::printf("%d\n", (int)(&x[1].p));
>>> &  the result I got (in debug) was:
>>> 1077836
>>> 1077845
>>
>> Right... so the alignment is in fact 1.  So to be compliant with the
>> C++ standard on x86-32 Windows, we can't assume any alignment on loads
>> and stores.  Fun.
>>
> Without a specify __declspec(align(#)) is true.
> So, what approach is the best to resolve this problem?
> Patch StructLayout or smth else?

Just fixing the RecordLayoutBuilder to set the alignment correctly
should be enough to start with.  It has wider implications, though,
which might be a bit tricky to model: it means that we can't assume
*any* struct is actually aligned.

-Eli




More information about the cfe-dev mailing list