This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

PCC_BITFIELD_TYPE_MATTERS patch


This patch is the final version of the patch I posted yesterday to
correct the handling of unnamed bitfiels on PCC_BITFIELD_TYPE_MATTERS
machines.

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

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  Mark Mitchell  <mark@codesourcery.com>

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

Index: stor-layout.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/stor-layout.c,v
retrieving revision 1.138
diff -c -p -r1.138 stor-layout.c
*** stor-layout.c	23 Dec 2002 05:10:14 -0000	1.138
--- stor-layout.c	23 Dec 2002 16:34:20 -0000
*************** update_alignment_for_field (rli, field, 
*** 715,727 ****
        && DECL_BIT_FIELD_TYPE (field)
        && ! integer_zerop (TYPE_SIZE (type)))
      {
!       /* For these machines, a zero-length field does not
! 	 affect the alignment of the structure as a whole.
! 	 It does, however, affect the alignment of the next 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);
  #ifdef ADJUST_FIELD_ALIGN
--- 715,723 ----
        && DECL_BIT_FIELD_TYPE (field)
        && ! integer_zerop (TYPE_SIZE (type)))
      {
!       /* A zero-length bit-field affects the alignment of the next
! 	 field.  */
!       if (!DECL_PACKED (field) && integer_zerop (DECL_SIZE (field)))
  	{
  	  desired_align = TYPE_ALIGN (type);
  #ifdef ADJUST_FIELD_ALIGN
*************** update_alignment_for_field (rli, field, 
*** 729,736 ****
  #endif
  	}
  
!       /* A named bit field of declared type `int'
! 	 forces the entire structure to have `int' alignment.  */
        if (DECL_NAME (field) != 0)
  	{
  	  unsigned int type_align = TYPE_ALIGN (type);
--- 725,732 ----
  #endif
  	}
  
!       /* Named bit-fields cause the entire structure to have the
! 	 alignment implied by their type.  */
        if (DECL_NAME (field) != 0)
  	{
  	  unsigned int type_align = TYPE_ALIGN (type);
*************** update_alignment_for_field (rli, field, 
*** 745,751 ****
--- 741,754 ----
  	  else if (DECL_PACKED (field))
  	    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->unpadded_align = MAX (rli->unpadded_align, DECL_ALIGN (field));
  	  if (warn_packed)
  	    rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
Index: doc/tm.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/tm.texi,v
retrieving revision 1.184
diff -c -p -r1.184 tm.texi
*** doc/tm.texi	17 Dec 2002 16:47:45 -0000	1.184
--- doc/tm.texi	23 Dec 2002 16:34:23 -0000
*************** go slower in that case, define this macr
*** 1181,1197 ****
  Define this if you wish to imitate the way many other C compilers handle
  alignment of bit-fields and the structures that contain them.
  
! The behavior is that the type written for a bit-field (@code{int},
! @code{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.  In addition, the bit-field is placed within the
! structure so that it would fit within such a field, not crossing a
! boundary for it.
  
! Thus, on most machines, a bit-field whose type is written as @code{int}
! would not cross a four-byte boundary, and would force four-byte
! alignment for the whole structure.  (The alignment used may not be four
! bytes; it is controlled by the other alignment parameters.)
  
  If the macro is defined, its definition should be a C expression;
  a nonzero value for the expression enables this behavior.
--- 1181,1199 ----
  Define this if you wish to imitate the way many other C compilers handle
  alignment of bit-fields and the structures that contain them.
  
! 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 entire
! structure, as if the structure really did contain an ordinary field of
! that type.  In addition, the bit-field is placed within the structure so
! that it would fit within such a field, not crossing a boundary for it.
  
! Thus, on most machines, a named bit-field whose type is written as
! @code{int} would not cross a four-byte boundary, and would force
! four-byte alignment for the whole structure.  (The alignment used may
! not be four 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;
  a nonzero value for the expression enables this behavior.
Index: testsuite/gcc.dg/i386-bitfield3.c
===================================================================
RCS file: testsuite/gcc.dg/i386-bitfield3.c
diff -N testsuite/gcc.dg/i386-bitfield3.c
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- testsuite/gcc.dg/i386-bitfield3.c	23 Dec 2002 16:34:24 -0000
***************
*** 0 ****
--- 1,24 ----
+ // 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);
+ }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]