m68k structure packing

Paul Koning pkoning@xedia.com
Thu Oct 2 06:49:00 GMT 1997


>>>>> "Peter" == Peter Barada <pbarada@wavemark.com> writes:

 >> Should the above code fail at runtime?  (You have to have a
 >> STRICT_ALIGNMENT machine, pack a structure, and you may have to
 >> use int or long or float or double to make it fail).  If not,
 >> should we make it work by aligning it or by requiring what the
 >> pointer points to is a packed short, then on dereference of a
 >> pointer to a packed short, we could know to do byte accesses.

 Peter> Again it all depends. If you pack and use short accesses, then
 Peter> yes I would expect a SIGBUS error on the short load
 Peter> instruction. If you can pack it and the compiler can produce
 Peter> code that uses only byte accesses, then the bloated code will
 Peter> go dog slow but not SIGBUS.

I have a simple view on this: if the compiler generates code that
breaks at runtime, it's a compiler bug (except if I hack up pointers
in ways contrary to what the compiler was allowed to expect).

In particular, declaring a structure packed MUST have two effects:

1. There is NO padding whatsoever
2. The generated code allows for this and makes all necessary
adjustments for dealing with unaligned references

 Peter> If you want packed structures then they should be sized up to
 Peter> a short, and anything that is not a char in a packed struct
 Peter> should be aligned on a short boundary(BIGGEST_ALIGNMENT). But
 Peter> this is what happens on the 68k anyway.

NFW.  If I say "packed" it HAS to mean NO padding.  If I want padding
I'd either ask for not packed (in which case the normal pad rule
applies, i.e., each primitive element is aligned to its size and each
composite to the largest of the alignment of its components) or I can
ask for packed and align explicitly by manual padding.

If you pad packed structures you WILL break existing code.  (For
example, last year I wrote code that had all its protocol messages
defined as packed structs with explicit padding in a few places.  I
had to do the explicit padding because the Intel i960 compiler doesn't
implement the normal structure padding rules -- with a decent compiler
such as gcc it would been unnecessary.  Anyway, if you change the pad
rules then that code would emit invalid messages and would not conform
to the protocol standard.)

	paul

-- 
!-----------------------------------------------------------------------
! Paul Koning, NI1D, C-24183
! Xedia Corporation, 119 Russell Street, Littleton, MA 01460, USA
! phone: +1 508 952 6000 ext 115, fax: +1 508 952 6090
! email: pkoning@xedia.com
! Pgp:   27 81 A9 73 A6 0B B3 BE 18 A3 BF DD 1A 59 51 75
!-----------------------------------------------------------------------
! "The only purpose for which power can be rightfully exercised over 
!  any member of a civilized community, against his will, is to prevent
!  harm to others.  His own good, either physical or moral, is not
!  a sufficient warrant."    -- John Stuart Mill, "On Liberty" 1859



More information about the Gcc mailing list