[cfe-dev] CGRecordLayoutBuilder for MS ABI

John McCall rjmccall at apple.com
Fri Oct 28 14:21:09 PDT 2011


On Oct 28, 2011, at 12:51 PM, r4start wrote:
> In test we have class A
>   0 | class A
>   0 |   class B (primary base)
>   0 |     (B vftable pointer)
>   4 |     int b_field
>   8 |   int a_field
>  12 |   char one
>  sizeof=16, dsize=16, align=4
>  nvsize=16, nvalign=4
> %class.A = type { %class.B, i32, i8 }
> 
> Can I expect that LLVM add 3 bytes padding at the end?

Yes.  The LLVM layout algorithm for non-packed structs is exactly the C algorithm, minus any support for bit-fields, flexible arrays members, or any of a thousand other non-essential extensions:

(1) The ABI size and alignment of a non-struct, non-array type is determined by the target layout string.  Arrays have the alignment of their element and the size of N elements.  The size and alignment of other structs are determined recursively by this algorithm.
(2) A field occupies a number of consecutive bytes equal to the ABI size of its type.
(3) The first field begins at offset 0.
(4) Every other field begins at the smallest offset that is both (a) a multiple of the  ABI alignment of the field's type and (b) greater than the offset of the last byte in the previous field.
(5) The ABI alignment of the struct is the maximum of the ABI alignments of the field.
(6) The ABI size of the struct is equal to smallest offset that is both (a) a multiple of the ABI alignment of the struct and (b) greater than the offset of the last byte in the last field of the struct.

The layout algorithm for packed structs is the same, except that all alignments are considered to be 1.

John.



More information about the cfe-dev mailing list