[LLVMdev] StructType member offset

Duncan Sands baldrick at free.fr
Wed Nov 3 01:04:46 PDT 2010


On 02/11/10 22:48, Mark Leone wrote:
> You can also use getelementptr to get the offset of a struct member:
>
> http://nondot.org/sabre/LLVMNotes/SizeOf-OffsetOf-VariableSizedStructs.txt

Yup, except that this returns a constant expression in general, rather than
a number.  There's even a helper for doing this: ConstantExpr:getOffsetOf

Ciao,

Duncan.

>
> - Mark
>
> On Wed, Oct 27, 2010 at 2:25 AM, Duncan Sands <baldrick at free.fr
> <mailto:baldrick at free.fr>> wrote:
>
>     Hi Alex,
>
>     I think you are looking for getStructLayout from TargetData (the
>     StructLayout type is also defined in TargetData.h).
>
>     Ciao,
>
>     Duncan.
>
>      > how can i correctly calculate the size of a member of a struct
>      > (including alignment etc)?
>      > This doesn't work :
>      >
>      >           const StructType *STy = cast<StructType>(Ty);
>      >           for (StructType::element_iterator I = STy->element_begin(),
>      >                  E = STy->element_end(); I != E; ++I)
>      >           {
>      >               usigned size =I->get()->getScalarSizeInBits(); //often
>      > returns 0, not what I need
>      >           }
>      >
>      > Basically I want to add up the sizes of the different members to
>      > calculate the offset (which I want to compare to
>      > DIType::getOffsetInBits()). Or if there is a direct way to get the
>      > offset of a struct member without adding their sizes that'd be even better.
>      >
>      > Thx,
>      > Alex
>      > _______________________________________________
>      > LLVM Developers mailing list
>      > LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu> http://llvm.cs.uiuc.edu
>      > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>
>     _______________________________________________
>     LLVM Developers mailing list
>     LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu> http://llvm.cs.uiuc.edu
>     http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>
>




More information about the llvm-dev mailing list