fb39ca4 / picoc

Automatically exported from code.google.com/p/picoc
0 stars 0 forks source link

Structure packing is not correct #99

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
This is a complicated subject and is hard to explain fully.
I have uploaded a solution (type.c), so you can do a diff yourself to help 
understand problem.

To be safe it's best to also implement this: 
http://code.google.com/p/picoc/issues/detail?id=94

So once to know the correct compiler alignment you can figure out the rest on 
the fly while parsing.

Some simple rules:

1: For intrinsic members - align using minimum of size-of(type) or alignSize
2: For array members - align using minimum of size-of(fromType) or alignSize
3: For sub-structure members - align using minimum of size-of(maxDeepSubMember) 
or alignSize
4: And finally you must pad the tail of structure using minimum of 
size-of(maxDeepSubMember) or alignSize

Not sure if my explanation was clear. I can give some examples of complex 
structures and explain where padding occurs and why.

Original issue reported on code.google.com by duncan.f...@gmail.com on 27 Jul 2010 at 6:09

GoogleCodeExporter commented 8 years ago
Thanks for the report.

This one's a thorny issue. Alignment varies depending on processor architecture 
so what's probably needed is to step away from the simple system I'm currently 
using and develop a more complex processor-dependent system. Unfortunately this 
will also make the system more awkward to port to new platforms but it's 
probably necessary in the long run. Added to this is the complication that C 
doesn't even guarantee to pack structures in any specific way.

Original comment by zik.sale...@gmail.com on 27 Jul 2010 at 8:40

GoogleCodeExporter commented 8 years ago
To be precise structure packing is compiler dependent, not processor 
architecture.
The processor architecture maybe place some limits on the choices the compiler 
has.

You are right that the method to pack structures is not part of any 
specification.
But, in practice it's done, almost universally, the same by most popular 
compliers.
Usually the main difference comes from the initial alignment size, then usually 
packing is done in a standard way.

I come from a Unix and Windoze background, which means I'm familiar with GCC, 
Sun Compiler, Visual C, etc.
These compilers all work the same once you know the alignment size.
I have no experience in the embedded space, but if you use GCC then I would 
think you would be safe. 

Currently in PicoC sharing structures between interpreted and native code is a 
big no no.
It will work for some very simple cases, but otherwise BOOM.

Original comment by duncan.f...@gmail.com on 27 Jul 2010 at 9:47

GoogleCodeExporter commented 8 years ago
Scheduled for release 2.0

Original comment by zik.sale...@gmail.com on 27 Jul 2010 at 3:10

GoogleCodeExporter commented 8 years ago
After testing on as many platforms as I could, I made some improvements.
To verify each platform I wrote a program to generate very large random 
structures and compared offsets.

Original comment by duncan.f...@gmail.com on 1 Aug 2010 at 3:08

Attachments:

GoogleCodeExporter commented 8 years ago
Duncan - that's not 100% correct.

Some architectures do not allow unaligned access to structure elements, so 
there are limitations on what options are available to the compiler for that 
platform, in terms of structure member-alignment.

Original comment by Alexande...@gmail.com on 10 Oct 2010 at 3:00

GoogleCodeExporter commented 8 years ago
Alexander, to me it's not clear what part you disagree with.

I'm aware unaligned access is not allowed on some architectures. This is 
exactly why I worked to fix the incorrect alignment issue. As I mentioned 
structure packing is in fact done in a standard way, regardless of whether 
misaligned reads are allowed.

Perhaps my explanation was not clear enough for you. My descriptions were very 
brief, perhaps reading the code might make it a little clearer.

Original comment by duncan.f...@gmail.com on 11 Oct 2010 at 10:23

GoogleCodeExporter commented 8 years ago
I've spent some time thinking about this one and I've come up with a method 
which I hope will be fairly portable across architectures and is still fairly 
simple.

I work out the alignments of all the major data types for the target 
architecture during program initialisation. Structures are padded to the 
alignment of the largest member alignment. I think this is correct for x86 and 
should be correct for most other architectures. It's conservative so even if 
not correct it shouldn't be harmful.

"Fixed" in r532.

Original comment by zik.sale...@gmail.com on 15 Feb 2011 at 4:33

GoogleCodeExporter commented 8 years ago
Marking as fixed for now.

Original comment by zik.sale...@gmail.com on 15 Feb 2011 at 4:33