Commit 8dc65b6e by Mark Mitchell Committed by Mark Mitchell

stor-layout.c (update_alignment_for_field): Correct handling of unnamed…

stor-layout.c (update_alignment_for_field): Correct handling of unnamed bitfields on PCC_BITFIELD_TYPE_MATTERS machines.

	* stor-layout.c (update_alignment_for_field): Correct handling of
	unnamed bitfields on PCC_BITFIELD_TYPE_MATTERS machines.
	* doc/tm.texi (PCC_BITFIELD_TYPE_MATTERS): Note that an unnamed
	bitfield does not affect alignment.

	* testsuite/gcc.dg/i386-bitfield3.c: New test.

From-SVN: r60439
parent 7c02ae17
2002-12-23 Mark Mitchell <mark@codesourcery.com>
* stor-layout.c (update_alignment_for_field): Correct handling of
unnamed bitfields on PCC_BITFIELD_TYPE_MATTERS machines.
* doc/tm.texi (PCC_BITFIELD_TYPE_MATTERS): Note that an unnamed
bitfield does not affect alignment.
2002-12-23 David Edelsohn <edelsohn@gnu.org> 2002-12-23 David Edelsohn <edelsohn@gnu.org>
* expr.c (expand_assignment): Apply special treatment to * expr.c (expand_assignment): Apply special treatment to
......
...@@ -1181,17 +1181,19 @@ go slower in that case, define this macro as 0. ...@@ -1181,17 +1181,19 @@ go slower in that case, define this macro as 0.
Define this if you wish to imitate the way many other C compilers handle Define this if you wish to imitate the way many other C compilers handle
alignment of bit-fields and the structures that contain them. alignment of bit-fields and the structures that contain them.
The behavior is that the type written for a bit-field (@code{int}, The behavior is that the type written for a named bit-field (@code{int},
@code{short}, or other integer type) imposes an alignment for the @code{short}, or other integer type) imposes an alignment for the entire
entire structure, as if the structure really did contain an ordinary structure, as if the structure really did contain an ordinary field of
field of that type. In addition, the bit-field is placed within the that type. In addition, the bit-field is placed within the structure so
structure so that it would fit within such a field, not crossing a that it would fit within such a field, not crossing a boundary for it.
boundary for it.
Thus, on most machines, a named bit-field whose type is written as
Thus, on most machines, a bit-field whose type is written as @code{int} @code{int} would not cross a four-byte boundary, and would force
would not cross a four-byte boundary, and would force four-byte four-byte alignment for the whole structure. (The alignment used may
alignment for the whole structure. (The alignment used may not be four not be four bytes; it is controlled by the other alignment parameters.)
bytes; it is controlled by the other alignment parameters.)
An unnamed bit-field will not affect the alignment of the containing
structure.
If the macro is defined, its definition should be a C expression; If the macro is defined, its definition should be a C expression;
a nonzero value for the expression enables this behavior. a nonzero value for the expression enables this behavior.
......
...@@ -715,13 +715,9 @@ update_alignment_for_field (rli, field, known_align) ...@@ -715,13 +715,9 @@ update_alignment_for_field (rli, field, known_align)
&& DECL_BIT_FIELD_TYPE (field) && DECL_BIT_FIELD_TYPE (field)
&& ! integer_zerop (TYPE_SIZE (type))) && ! integer_zerop (TYPE_SIZE (type)))
{ {
/* For these machines, a zero-length field does not /* A zero-length bit-field affects the alignment of the next
affect the alignment of the structure as a whole. field. */
It does, however, affect the alignment of the next field if (!DECL_PACKED (field) && integer_zerop (DECL_SIZE (field)))
within the structure. */
if (! integer_zerop (DECL_SIZE (field)))
rli->record_align = MAX (rli->record_align, desired_align);
else if (! DECL_PACKED (field))
{ {
desired_align = TYPE_ALIGN (type); desired_align = TYPE_ALIGN (type);
#ifdef ADJUST_FIELD_ALIGN #ifdef ADJUST_FIELD_ALIGN
...@@ -729,8 +725,8 @@ update_alignment_for_field (rli, field, known_align) ...@@ -729,8 +725,8 @@ update_alignment_for_field (rli, field, known_align)
#endif #endif
} }
/* A named bit field of declared type `int' /* Named bit-fields cause the entire structure to have the
forces the entire structure to have `int' alignment. */ alignment implied by their type. */
if (DECL_NAME (field) != 0) if (DECL_NAME (field) != 0)
{ {
unsigned int type_align = TYPE_ALIGN (type); unsigned int type_align = TYPE_ALIGN (type);
...@@ -745,7 +741,14 @@ update_alignment_for_field (rli, field, known_align) ...@@ -745,7 +741,14 @@ update_alignment_for_field (rli, field, known_align)
else if (DECL_PACKED (field)) else if (DECL_PACKED (field))
type_align = MIN (type_align, BITS_PER_UNIT); type_align = MIN (type_align, BITS_PER_UNIT);
/* The alignment of the record is increased to the maximum
of the current alignment, the alignment indicated on the
field (i.e., the alignment specified by an __aligned__
attribute), and the alignment indicated by the type of
the field. */
rli->record_align = MAX (rli->record_align, desired_align);
rli->record_align = MAX (rli->record_align, type_align); rli->record_align = MAX (rli->record_align, type_align);
rli->unpadded_align = MAX (rli->unpadded_align, DECL_ALIGN (field)); rli->unpadded_align = MAX (rli->unpadded_align, DECL_ALIGN (field));
if (warn_packed) if (warn_packed)
rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type)); rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
......
2002-12-23 Mark Mitchell <mark@codesourcery.com>
* testsuite/gcc.dg/i386-bitfield3.c: New test.
* testsuite/gcc.dg/i386-bitfield2.c: New test.
2002-12-22 Nathan Sidwell <nathan@codesourcery.com> 2002-12-22 Nathan Sidwell <nathan@codesourcery.com>
* g++.dg/parse/conv_op1.C: New test. * g++.dg/parse/conv_op1.C: New test.
......
// Test for bitfield alignment in structs on IA-32
// { dg-do run { target i?86-*-* } }
// { dg-options "-O2" }
// { dg-options "-mno-align-double -mno-ms-bitfields" { target *-*-interix* } }
extern void abort (void);
extern void exit (int);
struct X {
int : 32;
};
struct Y {
int i : 32;
};
int main () {
if (__alignof__(struct X) != 1)
abort ();
if (__alignof__(struct Y) != 4)
abort ();
exit (0);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment