public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* C ABI: struct/union inconsistency
@ 2002-09-27  8:59 Mark Mitchell
  2002-09-27  9:33 ` Joseph S. Myers
  2002-09-27 10:09 ` Richard Henderson
  0 siblings, 2 replies; 27+ messages in thread
From: Mark Mitchell @ 2002-09-27  8:59 UTC (permalink / raw)
  To: gcc


On x86 (and other PCC_BITFIELD_TYPE_MATTERS machines), this program:

  #include <stdio.h>

  struct S { long : 0; };
  union U { long : 0; };

  int main () {
    printf ("Alignment of S: %d\n", __alignof__(struct S));
    printf ("Alignment of U: %d\n", __alignof__(union U));
    printf ("Size of S: %d\n", sizeof(struct S));
    printf ("Size of U: %d\n", sizeof(union U));
  }

gives suprising output:

 Alignment of S: 1
 Alignment of U: 4
 Size of S: 0
 Size of U: 0

According to the doumentation for PCC_BITFIELD_TYPE_MATTERS:

     The behavior is that the type written for a bitfield (`int',
     `short', or other integer type) imposes an alignment for the
     entire structure, as if the structure really did contain an
     ordinary field of that type.

That would suggest that both S and U should have 4-byte alignment.

Note further that the psABI for x86 says explicitly that an unnamed
bitfield does *not* affect the alignment of the structure, although it
does affect the alignment of the next field.

So, as far as I can tell, we are both inconsistent with our own
documentation (in that we do not align S) and wrong as far as the
psABI is concerned (in that we do align U).

Lovely.

The size is another issue.  Giving empty sturctures size zero is
rather unfortunate; note that we also give an array of type "S[3]"
where "S" is an empty class size 0. That's distinctly odd; we have
all the array elements at the same location.

Accepting empty structures in C is an extension, but structures like S
(i.e., with a single zero-width bitfield) are part of the language.

In C++, empty structures are allowed, but are explicitly required to
have non-zero size.

I think we should adopt this same rule in C.  I cannot find anything
in the C99 standard that requires types to have a non-zero size, but
there are suggestions of that -- such as a note mentioning the:

  sizeof (array) / sizeof (array[0])

trick, which clearly does not work if array[0] has zero size.

I think we ought to:

(a) Change our docs to make clear that PCC_BITFIELD_TYPE_MATTERS does
    not cause unnamed bitfields to align structures.

(b) Fix our code so that alignment for unions is handled like
    structures.  This is an ABI change, but one that will affect 
    almost zero programs -- putting an unnamed bitfield in a union
    is something only someone writing ABI tests would try.

(c) Make all structures/unions have size at least one in GCC.

The combination of (a) and (b) will make our code consistent with the
docs and will make us match the psABI.  

The addition of (c) will avoid all kinds of weirdness for anyone who
actually makes empty structures, and will make C and C++ consistent.
I do not know if (c) is actually required by the C standard, but it
might be.  I also do not know if some piece of code in the Linux
kernel is going to depend on the current behavior; we might want to
have a switch for the old behavior.

What do others think we ought to do about this?

--
Mark Mitchell                   mark@codesourcery.com
CodeSourcery, LLC               http://www.codesourcery.com

^ permalink raw reply	[flat|nested] 27+ messages in thread
* Re: C ABI: struct/union inconsistency
@ 2002-09-28  7:02 Robert Dewar
  0 siblings, 0 replies; 27+ messages in thread
From: Robert Dewar @ 2002-09-28  7:02 UTC (permalink / raw)
  To: mark, rth; +Cc: davem, gcc, gdr

Ada allows null reocrds but an interesting glitch is that they cannot
be allocated with zero size, since access to separate objects must
yield separate pointers.

^ permalink raw reply	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2002-09-30 16:20 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-27  8:59 C ABI: struct/union inconsistency Mark Mitchell
2002-09-27  9:33 ` Joseph S. Myers
2002-09-27 10:09 ` Richard Henderson
2002-09-27 10:31   ` Mark Mitchell
2002-09-27 11:41     ` Richard Henderson
2002-09-27 11:54       ` Gabriel Dos Reis
2002-09-27 13:44         ` Mark Mitchell
2002-09-27 15:11           ` Richard Henderson
2002-09-27 15:18             ` Mark Mitchell
2002-09-27 16:26               ` Richard Henderson
2002-09-27 17:53                 ` Mark Mitchell
2002-09-27 18:51                   ` David S. Miller
2002-09-27 19:01                     ` Mark Mitchell
2002-09-27 19:54                       ` Richard Henderson
2002-09-28 10:38                         ` Tim Hollebeek
2002-09-28 12:56                           ` Mark Mitchell
2002-09-28 13:00                             ` Richard Henderson
2002-09-28 13:52                               ` Mark Mitchell
2002-09-30  9:20                                 ` Gabriel Paubert
2002-09-30 10:22                                   ` Gabriel Dos Reis
2002-09-28 16:55                               ` Per Bothner
2002-09-29  2:40                                 ` Tim Hollebeek
2002-09-27 20:02                       ` Michael S. Zick
2002-09-28  9:01                       ` Fergus Henderson
2002-09-27 20:43                     ` Hans-Peter Nilsson
2002-09-28 16:55                     ` Alexandre Oliva
2002-09-28  7:02 Robert Dewar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).