diff mbox

[Ada] Fix layout of packed record type with zero-sized component

Message ID 1548668.7W4sIJHsWO@polaris
State New
Headers show

Commit Message

Eric Botcazou Dec. 4, 2013, 11:25 a.m. UTC
In Ada we have the equivalent of bit-fields with zero size but they don't 
behave as in C (or rather according to most ABIs) when it comes to the layout.
This patch makes it so that they are laid out manually instead of being passed 
to stor-layout.c as for the other fields.

Tested on x86_64-suse-linux, applied on the mainline.


2013-12-04  Eric Botcazou  <ebotcazou@adacore.com>

	* gcc-interface/decl.c (components_to_record): Add specific handling
	for fields with zero size and no representation clause.


2013-12-04  Eric Botcazou  <ebotcazou@adacore.com>

	* gnat.dg/pack19.adb: New test.
diff mbox

Patch

Index: gcc-interface/decl.c
===================================================================
--- gcc-interface/decl.c	(revision 205654)
+++ gcc-interface/decl.c	(working copy)
@@ -6932,6 +6932,7 @@  components_to_record (tree gnu_record_ty
   tree gnu_rep_list = NULL_TREE;
   tree gnu_var_list = NULL_TREE;
   tree gnu_self_list = NULL_TREE;
+  tree gnu_zero_list = NULL_TREE;
 
   /* For each component referenced in a component declaration create a GCC
      field and add it to the list, skipping pragmas in the GNAT list.  */
@@ -7262,6 +7263,10 @@  components_to_record (tree gnu_record_ty
      to do this in a separate pass since we want to handle the discriminants
      but can't play with them until we've used them in debugging data above.
 
+     Similarly, pull out the fields with zero size and no rep clause, as they
+     would otherwise modify the layout and thus very likely run afoul of the
+     Ada semantics, which are different from those of C here.
+
      ??? If we reorder them, debugging information will be wrong but there is
      nothing that can be done about this at the moment.  */
   gnu_last = NULL_TREE;
@@ -7300,6 +7305,19 @@  components_to_record (tree gnu_record_ty
 	  continue;
 	}
 
+      if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field)))
+	{
+	  DECL_FIELD_OFFSET (gnu_field) = size_zero_node;
+	  SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT);
+	  DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node;
+	  if (field_is_aliased (gnu_field))
+	    TYPE_ALIGN (gnu_record_type)
+	      = MAX (TYPE_ALIGN (gnu_record_type),
+		     TYPE_ALIGN (TREE_TYPE (gnu_field)));
+	  MOVE_FROM_FIELD_LIST_TO (gnu_zero_list);
+	  continue;
+	}
+
       gnu_last = gnu_field;
     }
 
@@ -7392,6 +7410,11 @@  components_to_record (tree gnu_record_ty
   finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0,
 		      debug_info && !maybe_unused);
 
+  /* Chain the fields with zero size at the beginning of the field list.  */
+  if (gnu_zero_list)
+    TYPE_FIELDS (gnu_record_type)
+      = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type));
+
   return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep;
 }