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]

[PATCH] for bug 7054 (#pragma pack)


.. plus additionally a means to set the (initial) packing value from
the command line.

gcc-3.4.0-pragma-pack.patch:
--- 3.4.0-base/gcc/c-pragma.c	2004-01-24 00:35:53.000000000 +0100
+++ 3.4.0/gcc/c-pragma.c	2004-05-03 13:45:55.000000000 +0200
@@ -41,7 +41,6 @@
 typedef struct align_stack GTY(())
 {
   int                  alignment;
-  unsigned int         num_pushes;
   tree                 id;
   struct align_stack * prev;
 } align_stack;
@@ -58,8 +57,9 @@
    happens, we restore the value to this, not to a value of 0 for
    maximum_field_alignment.  Value is in bits.  */
 static int default_alignment;
-#define SET_GLOBAL_ALIGNMENT(ALIGN) \
-  (default_alignment = maximum_field_alignment = (ALIGN))
+#define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment =
*(alignment_stack == NULL \
+	? &default_alignment \
+	: &alignment_stack->alignment) = (ALIGN))
 
 static void push_alignment (int, tree);
 static void pop_alignment (tree);
@@ -68,31 +68,23 @@
 static void
 push_alignment (int alignment, tree id)
 {
-  if (alignment_stack == NULL
-      || alignment_stack->alignment != alignment
-      || id != NULL_TREE)
-    {
-      align_stack * entry;
-
-      entry = ggc_alloc (sizeof (* entry));
-
-      entry->alignment  = alignment;
-      entry->num_pushes = 1;
-      entry->id         = id;
-      entry->prev       = alignment_stack;
-      
-      /* The current value of maximum_field_alignment is not
necessarily 
-	 0 since there may be a #pragma pack(<n>) in effect; remember it

-	 so that we can restore it after the final #pragma pop().  */
-      if (alignment_stack == NULL)
-	default_alignment = maximum_field_alignment;
-      
-      alignment_stack = entry;
+  align_stack * entry;
 
-      maximum_field_alignment = alignment;
-    }
-  else
-    alignment_stack->num_pushes ++;
+  entry = ggc_alloc (sizeof (* entry));
+
+  entry->alignment  = alignment;
+  entry->id         = id;
+  entry->prev       = alignment_stack;
+       
+  /* The current value of maximum_field_alignment is not necessarily 
+     0 since there may be a #pragma pack(<n>) in effect; remember it 
+     so that we can restore it after the final #pragma pop().  */
+  if (alignment_stack == NULL)
+    default_alignment = maximum_field_alignment;
+ 
+  alignment_stack = entry;
+
+  maximum_field_alignment = alignment;
 }
 
 /* Undo a push of an alignment onto the stack.  */
@@ -102,12 +94,7 @@
   align_stack * entry;
       
   if (alignment_stack == NULL)
-    {
-      warning ("\
-#pragma pack (pop) encountered without matching #pragma pack (push,
<n>)"
-	       );
-      return;
-    }
+    GCC_BAD("#pragma pack (pop) encountered without matching #pragma
pack (push)");
 
   /* If we got an identifier, strip away everything above the target
      entry so that the next step will restore the state just below it.
 */
@@ -116,27 +103,20 @@
       for (entry = alignment_stack; entry; entry = entry->prev)
 	if (entry->id == id)
 	  {
-	    entry->num_pushes = 1;
 	    alignment_stack = entry;
 	    break;
 	  }
       if (entry == NULL)
 	warning ("\
-#pragma pack(pop, %s) encountered without matching #pragma pack(push,
%s, <n>)"
+#pragma pack(pop, %s) encountered without matching #pragma pack(push,
%s)"
 		 , IDENTIFIER_POINTER (id), IDENTIFIER_POINTER (id));
     }
 
-  if (-- alignment_stack->num_pushes == 0)
-    {
-      entry = alignment_stack->prev;
+  entry = alignment_stack->prev;
 
-      if (entry == NULL)
-	maximum_field_alignment = default_alignment;
-      else
-	maximum_field_alignment = entry->alignment;
+  maximum_field_alignment = entry ? entry->alignment :
default_alignment;
 
-      alignment_stack = entry;
-    }
+  alignment_stack = entry;
 }
 #else  /* not HANDLE_PRAGMA_PACK_PUSH_POP */
 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment =
(ALIGN))
@@ -149,7 +129,9 @@
 /* #pragma pack ()
    #pragma pack (N)
    
+   #pragma pack (push)
    #pragma pack (push, N)
+   #pragma pack (push, ID)
    #pragma pack (push, ID, N)
    #pragma pack (pop)
    #pragma pack (pop, ID) */
@@ -168,7 +150,7 @@
   if (token == CPP_CLOSE_PAREN)
     {
       action = set;
-      align = 0;
+      align = initial_max_fld_align;
     }
   else if (token == CPP_NUMBER)
     {
@@ -179,8 +161,8 @@
     }
   else if (token == CPP_NAME)
     {
-#define GCC_BAD_ACTION do { if (action == push) \
-	  GCC_BAD ("malformed '#pragma pack(push[, id], <n>)' -
ignored"); \
+#define GCC_BAD_ACTION do { if (action != pop) \
+	  GCC_BAD ("malformed '#pragma pack(push[, id][, <n>])' -
ignored"); \
 	else \
 	  GCC_BAD ("malformed '#pragma pack(pop[, id])' - ignored"); \
 	} while (0)
@@ -193,31 +175,21 @@
       else
 	GCC_BAD2 ("unknown action '%s' for '#pragma pack' - ignored",
op);
 
-      token = c_lex (&x);
-      if (token != CPP_COMMA && action == push)
-	GCC_BAD_ACTION;
-
-      if (token == CPP_COMMA)
+      while ((token = c_lex (&x)) == CPP_COMMA)
 	{
 	  token = c_lex (&x);
-	  if (token == CPP_NAME)
+	  if (token == CPP_NAME && id == 0)
 	    {
 	      id = x;
-	      if (action == push && c_lex (&x) != CPP_COMMA)
-		GCC_BAD_ACTION;
-	      token = c_lex (&x);
 	    }
-
-	  if (action == push)
+	  else if (token == CPP_NUMBER && action == push && align ==
-1)
 	    {
-	      if (token == CPP_NUMBER)
-		{
-		  align = TREE_INT_CST_LOW (x);
-		  token = c_lex (&x);
-		}
-	      else
-		GCC_BAD_ACTION;
+	      align = TREE_INT_CST_LOW (x);
+	      if (align == -1)
+		action = set;
 	    }
+	  else
+	    GCC_BAD_ACTION;
 	}
 
       if (token != CPP_CLOSE_PAREN)
@@ -230,6 +202,9 @@
   if (c_lex (&x) != CPP_EOF)
     warning ("junk at end of '#pragma pack'");
 
+  if (flag_pack_struct)
+    GCC_BAD ("#pragma pack has no effect with -fpack-struct -
ignored");
+
   if (action != pop)
     switch (align)
       {
@@ -241,6 +216,12 @@
       case 16:
 	align *= BITS_PER_UNIT;
 	break;
+      case -1:
+	if (action == push)
+	  {
+	    align = maximum_field_alignment;
+	    break;
+	  }
       default:
 	GCC_BAD2 ("alignment must be a small power of two, not %d",
align);
       }
@@ -268,7 +249,7 @@
       value = build_string (IDENTIFIER_LENGTH (value),
 			    IDENTIFIER_POINTER (value));
       decl_attributes (&decl, build_tree_list (get_identifier
("alias"),
-					       build_tree_list (NULL,
value)),
+				               build_tree_list (NULL,
value)),
 		       0);
     }
 
--- 3.4.0-base/gcc/opts.c	2004-02-18 01:09:04.000000000 +0100
+++ 3.4.0/gcc/opts.c	2004-05-06 13:43:56.926018224 +0200
@@ -1147,6 +1147,13 @@
       flag_pack_struct = value;
       break;
 
+    case OPT_fpack_struct_:
+      if (value <= 0 || (value & (value - 1)) || value > 16)
+	error("structure alignment must be a small power of two, not
%d", value);
+      else
+	maximum_field_alignment = initial_max_fld_align = value *
BITS_PER_UNIT;
+      break;
+
     case OPT_fpeel_loops:
       flag_peel_loops_set = true;
       flag_peel_loops = value;
--- 3.4.0-base/gcc/tree.h	2004-02-08 02:52:43.000000000 +0100
+++ 3.4.0/gcc/tree.h	2004-05-03 13:38:03.000000000 +0200
@@ -2458,8 +2458,10 @@
    + (BITS_PER_UNIT > 8) + (BITS_PER_UNIT > 16) + (BITS_PER_UNIT > 32)
\
    + (BITS_PER_UNIT > 64) + (BITS_PER_UNIT > 128) + (BITS_PER_UNIT >
256))
 
-/* If nonzero, an upper limit on alignment of structure fields, in
bits.  */
+/* If nonzero, an upper limit on alignment of structure fields, in
bits,  */
 extern unsigned int maximum_field_alignment;
+/* and the original value of it, specified via -fpack-struct=<value>.
*/
+extern unsigned int initial_max_fld_align;
 
 /* If nonzero, the alignment of a bitstring or (power-)set value, in
bits.  */
 extern unsigned int set_alignment;
--- 3.4.0-base/gcc/stor-layout.c	2003-12-30 20:49:59.000000000
+0100
+++ 3.4.0/gcc/stor-layout.c	2004-05-03 13:38:03.000000000 +0200
@@ -48,7 +48,12 @@
 
 /* If nonzero, this is an upper limit on alignment of structure
fields.
    The value is measured in bits.  */
-unsigned int maximum_field_alignment;
+#ifndef DEFAULT_PACK_STRUCT
+#define DEFAULT_PACK_STRUCT 0
+#endif
+unsigned int maximum_field_alignment = DEFAULT_PACK_STRUCT *
BITS_PER_UNIT;
+/* ... and the original value of it, specified via
-fpack-struct=<value>. */
+unsigned int initial_max_fld_align = DEFAULT_PACK_STRUCT *
BITS_PER_UNIT;
 
 /* If nonzero, the alignment of a bitstring or (power-)set value, in
bits.
    May be overridden by front-ends.  */
--- 3.4.0-base/gcc/common.opt	2004-02-18 01:09:04.000000000 +0100
+++ 3.4.0/gcc/common.opt	2004-05-06 13:58:05.571004576 +0200
@@ -480,6 +480,10 @@
 Common
 Pack structure members together without holes
 
+fpack-struct=
+Common RejectNegative Joined UInteger
+-fpack-struct=<number>	Set initial maximum structure member
alignment
+
 fpcc-struct-return
 Common
 Return small aggregates in memory, not registers


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