-finit-* creates an initialization for local variables - either as static initializer or by "initializing" at run time.

The latter also works with automatic variables, but was breaking with -fno-automatic, which causes all nonautomatic local variables to be placed in static memory. However, combining -finit-* -fno-automatic with automatic arrays is failing at resolution time. The fix turned out to be rather simple.

I wondered about characters strings where the length is a nonconstant specification question (thus: they are also automatic data objects). It turned out that only the "initialization" was missing - no code generation (trans*.c) change and no other resolution change were required.

The first part fixes a regression as "-finit-* -fno-automatic" could be combined before (albeit without initializing the automatic arrays - but there was no compile error).

Build and regtested on x86-64-linux.
OK for the trunk?

Tobias
2012-01-11  Tobias Burnus  <bur...@net-b.de>

	PR fortran/51800
	* resolve.c (build_default_init_expr): Also initialize
	nonconstant-length strings with -finit-character=<n>.

2012-01-11  Tobias Burnus  <bur...@net-b.de>

	PR fortran/51800
	* gfortran.dg/init_flag_8.f90: New.
	* gfortran.dg/init_flag_9.f90: New.

Index: gcc/fortran/resolve.c
===================================================================
--- gcc/fortran/resolve.c	(revision 183093)
+++ gcc/fortran/resolve.c	(working copy)
@@ -10143,6 +10143,26 @@ build_default_init_expr (gfc_symbol *sym)
 	  gfc_free_expr (init_expr);
 	  init_expr = NULL;
 	}
+      if (!init_expr && gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
+	  && sym->ts.u.cl->length)
+	{
+	  gfc_actual_arglist *arg;
+	  init_expr = gfc_get_expr ();
+	  init_expr->where = sym->declared_at;
+	  init_expr->ts = sym->ts;
+	  init_expr->expr_type = EXPR_FUNCTION;
+	  init_expr->value.function.isym =
+		gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
+	  init_expr->value.function.name = "repeat";
+	  arg = gfc_get_actual_arglist ();
+	  arg->expr = gfc_get_character_expr (sym->ts.kind, &sym->declared_at,
+					      NULL, 1);
+	  arg->expr->value.character.string[0]
+		= gfc_option.flag_init_character_value;
+	  arg->next = gfc_get_actual_arglist ();
+	  arg->next->expr = gfc_copy_expr (sym->ts.u.cl->length);
+	  init_expr->value.function.actual = arg;
+	}
       break;
 	  
     default:
@@ -10169,10 +10189,12 @@ apply_default_init_local (gfc_symbol *sym)
   if (init == NULL)
     return;
 
-  /* For saved variables, we don't want to add an initializer at 
-     function entry, so we just add a static initializer.  */
+  /* For saved variables, we don't want to add an initializer at function
+     entry, so we just add a static initializer. Note that automatic variables
+     are stack allocated even with -fno-automatic.  */
   if (sym->attr.save || sym->ns->save_all 
-      || gfc_option.flag_max_stack_var_size == 0)
+      || (gfc_option.flag_max_stack_var_size == 0
+	  && (!sym->attr.dimension || !is_non_constant_shape_array (sym))))
     {
       /* Don't clobber an existing initializer!  */
       gcc_assert (sym->value == NULL);
Index: gcc/testsuite/gfortran.dg/init_flag_8.f90
===================================================================
--- gcc/testsuite/gfortran.dg/init_flag_8.f90	(revision 0)
+++ gcc/testsuite/gfortran.dg/init_flag_8.f90	(working copy)
@@ -0,0 +1,18 @@
+! { dg-do compile }
+! { dg-options "-fno-automatic -finit-local-zero" }
+!
+! PR fortran/51800
+!
+! Contributed by Mario Baumann
+!
+      SUBROUTINE FOO( N, A )
+      IMPLICIT NONE
+      INTEGER :: N
+      INTEGER :: A(1:N)
+      INTEGER :: J
+      INTEGER :: DUMMY(1:N)
+      DO J=1,N
+         DUMMY(J) = 0
+         A(J) = DUMMY(J)
+      END DO 
+      END SUBROUTINE FOO
Index: gcc/testsuite/gfortran.dg/init_flag_9.f90
===================================================================
--- gcc/testsuite/gfortran.dg/init_flag_9.f90	(revision 0)
+++ gcc/testsuite/gfortran.dg/init_flag_9.f90	(working copy)
@@ -0,0 +1,15 @@
+! { dg-do run }
+! { dg-options "-finit-character=89" }
+!
+! PR fortran/51800
+!
+
+subroutine foo(n)
+  character(len=n) :: str
+!  print *, str
+  if (str /= repeat ('Y', n)) call abort()
+end subroutine foo
+
+call foo(3)
+call foo(10)
+end

Reply via email to