<div dir="ltr"><div><div><div><div><div><div><div><div><div><div><div><div>Hi, Hui Zang,<br><br></div><div>It helps to remember that at runtime a variable of a structure type has to be allocated store, and to think about how that variable is laid out in store. <br><br></div><div>You must use createMemberType to create a DIType for *each* field (aka member) in the structure; you then store those DIType's as the elements of a DIArray which you pass as the 'Elements' parameter to createStructType.<br></div><div><br></div>When creating a structure member, you have to specify this information :<br><br></div>    SizeinBits               - the actual size of the member<br></div>    OffsetInBits            - the number of bits between the start of the member in the layout of the structure and the start of the structure<br></div>    AlignInBits              - the address boundary (if any) to which the element has to be aligned.<br><br></div>Remember that on some processor architectures (e.g. Sun), basic data items (e.g shorts, ints, floats) have to be allocated to store at addresses which are multiples of their size; e.g. shorts must be allocated at addresses<br></div> which are multiples of two bytes, ints allocated at addresses which are multiples of 4 bytes, etc.   If this rule is not followed, e.g. a 4-byte integer is allocated at an odd address, then any attempt to load or store that variable will result in a bus error.   On architectures which do not have this constraint, it is sufficient for objects to be byte-aligned.<br><br></div>So in your example, you might have<br><br></div>   age   :  size 32, offset 0,  align 32<br></div>   name:  size 32, offset 32, align 32<br><br></div>or<br><br>   age   :  size 32, offset 0,  align 8<br>   name:  size 32, offset 32, align 8<br><br></div>depending on your target processor.<br><br><br></div><div>The sizeInBits and AliginInBits parameters to createStructType describe the whole structure (e.g. Actor:  sizeInBits:64  aligInBits: 32).  Note that the size of the structure may be more than the sum of the size of the individual members - there may be gaps in the structure layout, depending on the sizes and alignments of its members.   The alignment of a structure is generally the largest alignment of any of its members; the structure type must be laid out so that if a structure variable starts at an address which is a multiple of the structure's alignment then each of its members starts at an address which is a multiple of its alignment. <br><br></div><div>E.g. if you have a structure<br><br></div><div>struct S<br>{<br></div><div>    int I1;<br></div><div>    short S;<br></div><div>    int I2;<br></div><div>} V;<br></div><div><br></div><div>and an int is 4 bytes and a short 2 bytes, then either you must leave a 2-byte gap between S and I2 in the layout or you must re-order the fields (if the language you are implementing allows that) so that S comes after I1 and I2 in the layout; otherwise either I1 or I2 will be misaligned in store regardless of the address at which V is allocated.<br></div><div><br></div><div>The 'Ty' parameter to createMemberType specifies the type of the member.  In your example, you will need to create DIType objects for the types 'int' and 'const char*'; use the first one in the call to createMemberType for "age" and the second one in the call for "name".<br><br></div><div>As a general principle, when creating the DIType object for a given type, you must work from the bottom up (or recursively, from the top down) through the hierarchy of types which you are compiling : you must create the DIype for a type *before* you create the DItype for any other type which uses that type (e.g. as a member of a structure, or an element of an array, or the target type of a pointer type, etc.).    Note that you can have cycles of type dependencies in your hierarchy; e.g. a struct S could have a field P which is of type S* - so before you try to create the DIType for a given type you must check whether you have already created it, otherwise you could recurse endlessly around the cycles!   (until your stack blows up...) <br><br></div><div>    Hope this helps,<br><br></div><div>            Richard<br></div><div><br><br><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 18 December 2014 at 20:44, Hui Zhang <span dir="ltr"><<a href="mailto:wayne.huizhang@gmail.com" target="_blank">wayne.huizhang@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif;font-size:large">Hello,<br><br></div><div class="gmail_default" style="font-family:georgia,serif;font-size:large">I'm using DIBuilder to create debugging information, I'm not clear about two things:<br><br>[1] Could you help explain the meaning and the difference between"alignment" and "offset" of a type ?<br><div class="gmail_default" style="font-family:georgia,serif;font-size:large"><br>e.g class Actor{<br>            int age;<br>            const char* name;<br>     }<br><br></div>Besides, I
 found the denotation of createMemberType and createStructType both have
 "Member Alignment" and "Member Size" fields, are they the same thing ? If they are, then for the "Actor", which member is it referring to ?<br><br>[2] In createMemberType, the last arg “Ty” is parent type, does it mean "Actor" in the example ?<br><br>thanks</div><span class="HOEnZb"><font color="#888888"><br>-- <br><div>Best regards<br><br><br>Hui Zhang<br></div>
</font></span></div>
<br>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
<br></blockquote></div><br></div>