[LLVMdev] Data layout of structs

Stephan Falke falke at iti.uka.de
Fri Feb 4 07:16:34 PST 2011


Hi Thomas,

> Now my problem is as follows. The pointer %4 (pointing to the second
> field in the list element) is calculated by going 8 byte further in
> memory. However, i32 only has a size of 4 byte. What is the reason
> for the struct having the data layout of two times 8 bytes instead
> of having 4 bytes for the integer and 8 bytes for the pointer to the
> next element? Is there a general rule for the data layout of structs
> in LLVM?

There is no general rule except that the layout is target dependent...

TargetData::getStructLayout will give you the StructLayout for a given 
StructType.  StructLayout::getElementOffset then gives you the offset in 
bytes for an element.

In your case, getElementOffset(1) should return 8.

The "deeper" reason is that pointers in x86_64 Linux are 8-byte aligned.

Hope this helps,
Stephan



More information about the llvm-dev mailing list