struct alignment question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

struct alignment question

edA-qa mort-ora-y
Is a series of fields in a structure guaranteed to have the same layout
as those fields in a structure on their own? That is, can I cast a
pointer within a main structure to an equivalent type?

X = { a, b, c, d, e }
Y = { c, d, e }

y = BitCast( StructGEP( some_x, 2 ), Y* )

Is that a valid cast?

--
edA-qa mort-ora-y
Leaf Creator

Leaf - the language we always wanted
http://leaflang.org/
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: struct alignment question

Caldarale, Charles R
> From: [hidden email] [mailto:[hidden email]]
> On Behalf Of edA-qa mort-ora-y
> Subject: [LLVMdev] struct alignment question

> Is a series of fields in a structure guaranteed to have the same layout
> as those fields in a structure on their own? That is, can I cast a
> pointer within a main structure to an equivalent type?

> X = { a, b, c, d, e }
> Y = { c, d, e }

> y = BitCast( StructGEP( some_x, 2 ), Y* )

> Is that a valid cast?

In general, no.  If a, b, and c were char, and d was an int, using typical C alignments, there would be one slack byte between c and d in X, whereas there would be three in Y.  You could probably force what you want with packed structures or by playing with the data layout setting.

 - Chuck


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: struct alignment question

edA-qa mort-ora-y
On 19/11/13 08:03, Caldarale, Charles R wrote:
>> X = { a, b, c, d, e }
>> Y = { c, d, e }

> In general, no.  If a, b, and c were char, and d was an int, using
> typical C alignments, there would be one slack byte between c and d
> in X, whereas there would be three in Y.  You could probably force
> what you want with packed structures or by playing with the data
> layout setting.

Yes, I forgot about the simple scenario. If I force the alignment on "c"
should I be okay? That is, if I force it to the maximum alignment for
the platform will { c, d, e } always have the same layout?



--
edA-qa mort-ora-y
Leaf Creator

Leaf - the language we always wanted
http://leaflang.org/
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: struct alignment question

Reid Kleckner-2
Maybe.  Usually people who want this do something like:

Y = { c, d, e }
X = { a, b, Y }


On Tue, Nov 19, 2013 at 12:04 PM, edA-qa mort-ora-y <[hidden email]> wrote:
On 19/11/13 08:03, Caldarale, Charles R wrote:
>> X = { a, b, c, d, e }
>> Y = { c, d, e }

> In general, no.  If a, b, and c were char, and d was an int, using
> typical C alignments, there would be one slack byte between c and d
> in X, whereas there would be three in Y.  You could probably force
> what you want with packed structures or by playing with the data
> layout setting.

Yes, I forgot about the simple scenario. If I force the alignment on "c"
should I be okay? That is, if I force it to the maximum alignment for
the platform will { c, d, e } always have the same layout?



--
edA-qa mort-ora-y
Leaf Creator

Leaf - the language we always wanted
http://leaflang.org/
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev