[PATCH,fortran]: fix bind(c) common block alignment

Christopher D. Rickett crickett@lanl.gov
Mon Aug 6 16:58:00 GMT 2007


hi all,

as mentioned previously on the list by Jack Howarth, there is a mismatch 
in alignment with bind(c) common blocks.  the Fortran compiler simply sets 
the alignment of all common blocks to BIGGEST_ALIGN, while the C compiler 
tries to align based on the size of the fields in the interoperable C 
struct.  the attached patch should fix the alignment difference by 
changing the alignment setting of bind(c) common blocks.

bootstrapped and regtested on x86 linux with no new failures.

ChangeLog entry:

2007-08-06  Christopher D. Rickett  <crickett@lanl.gov>

 	* trans-common.c (build_common_decl): Fix the alignment for
 	BIND(C) common blocks.
-------------- next part --------------
Index: gcc/fortran/trans-common.c
===================================================================
--- gcc/fortran/trans-common.c	(revision 127182)
+++ gcc/fortran/trans-common.c	(working copy)
@@ -413,7 +413,20 @@ build_common_decl (gfc_common_head *com,
       SET_DECL_ASSEMBLER_NAME (decl, gfc_sym_mangled_common_id (com));
       TREE_PUBLIC (decl) = 1;
       TREE_STATIC (decl) = 1;
-      DECL_ALIGN (decl) = BIGGEST_ALIGNMENT;
+      if (!com->is_bind_c)
+	DECL_ALIGN (decl) = BIGGEST_ALIGNMENT;
+      else
+        {
+	  /* Do not set the alignment for bind(c) common blocks to
+	     BIGGEST_ALIGNMENT because that won't match what C does.  Also,
+	     for common blocks with one element, the alignment must be
+	     that of the field within the common block in order to match
+	     what C will do.  */
+	  tree field = NULL_TREE;
+	  field = TYPE_FIELDS (TREE_TYPE (decl));
+	  if (TREE_CHAIN (field) == NULL_TREE)
+	    DECL_ALIGN (decl) = TYPE_ALIGN (TREE_TYPE (field));
+	}
       DECL_USER_ALIGN (decl) = 0;
       GFC_DECL_COMMON_OR_EQUIV (decl) = 1;
 


More information about the Gcc-patches mailing list