This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH]: PR c/34921 Misalign stack variable referenced by nested function
- From: "H.J. Lu" <hjl dot tools at gmail dot com>
- To: "Ye, Joey" <joey dot ye at intel dot com>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Tue, 5 Feb 2008 06:47:02 -0800
- Subject: Re: [PATCH]: PR c/34921 Misalign stack variable referenced by nested function
- References: <BB577BF501703042AD7E08EADD8E514F4BDCF4@pdsmsx415.ccr.corp.intel.com>
On Wed, Jan 23, 2008 at 03:49:20PM +0800, Joey Ye wrote:
> Alignment of structure type for nested function is not copied from it's
> field.
>
> gcc/
> 2008-01-22 Joey Ye <joey.ye@intel.com>
>
> * tree-nested.c (insert_field_into_struct): Set type alignment
> to field alignment if the former is less than the latter.
>
> gcc/testsuite/
> 2008-01-22 Joey Ye <joey.ye@intel.com>
>
> * gcc.dg/nested-func-align.c: New test case.
>
There are 2 issues with testcases:
1. Your email broke the long line and the patch won't apply
cleanly.
2. You should use ptrdiff_t instead of long for address. Long on
some 64bit targets is 32bit.
3. Put it in gcc.c-torture/execute will run with different compiler
flags.
This is the patch I am testing. OK for 4.4?
Thanks.
H.J.
---
gcc/
2008-01-22 Joey Ye <joey.ye@intel.com>
* tree-nested.c (insert_field_into_struct): Set type alignment
to field alignment if the former is less than the latter.
gcc/testsuite/
2008-01-22 Joey Ye <joey.ye@intel.com>
H.J. Lu <hongjiu.lu@intel.com>
* gcc.c-torture/execute/nest-align-1.c: New test case.
--- gcc/testsuite/gcc.c-torture/execute/nest-align-1.c.nested 2008-02-05 06:26:01.000000000 -0800
+++ gcc/testsuite/gcc.c-torture/execute/nest-align-1.c 2008-02-05 06:34:07.000000000 -0800
@@ -0,0 +1,41 @@
+/* Test for alignment handling when a variable is accessed by nested
+ function. */
+/* Origin: Joey Ye <joey.ye@intel.com> */
+/* { dg-options "" } */
+
+#include <stddef.h>
+
+typedef int aligned __attribute__((aligned(16)));
+extern void abort (void);
+
+void
+check (int *i)
+{
+ *i = 20;
+ if ((((ptrdiff_t) i) & (__alignof__(aligned) - 1)) != 0)
+ abort ();
+}
+
+void
+foo (void)
+{
+ aligned jj;
+ void bar ()
+ {
+ jj = -20;
+ }
+ jj = 0;
+ bar ();
+ if (jj != -20)
+ abort ();
+ check (&jj);
+ if (jj != 20)
+ abort ();
+}
+
+int
+main()
+{
+ foo ();
+ return 0;
+}
--- gcc/tree-nested.c.nested 2008-01-26 07:53:41.000000000 -0800
+++ gcc/tree-nested.c 2008-02-05 06:26:45.000000000 -0800
@@ -183,6 +183,10 @@ insert_field_into_struct (tree type, tre
TREE_CHAIN (field) = *p;
*p = field;
+
+ /* Set correct alignment for frame struct type. */
+ if (TYPE_ALIGN (type) < DECL_ALIGN (field))
+ TYPE_ALIGN (type) = DECL_ALIGN (field);
}
/* Build or return the RECORD_TYPE that describes the frame state that is