OSDN Git Service

* trans-array.c (get_rank, get_loop_upper_bound_for_array):
[pf3gnuchains/gcc-fork.git] / gcc / fortran / trans-array.c
index 0c1dc89..083ce5c 100644 (file)
@@ -604,6 +604,7 @@ gfc_get_scalar_ss (gfc_ss *next, gfc_expr *expr)
 void
 gfc_cleanup_loop (gfc_loopinfo * loop)
 {
+  gfc_loopinfo *loop_next, **ploop;
   gfc_ss *ss;
   gfc_ss *next;
 
@@ -615,6 +616,23 @@ gfc_cleanup_loop (gfc_loopinfo * loop)
       gfc_free_ss (ss);
       ss = next;
     }
+
+  /* Remove reference to self in the parent loop.  */
+  if (loop->parent)
+    for (ploop = &loop->parent->nested; *ploop; ploop = &(*ploop)->next)
+      if (*ploop == loop)
+       {
+         *ploop = loop->next;
+         break;
+       }
+
+  /* Free non-freed nested loops.  */
+  for (loop = loop->nested; loop; loop = loop_next)
+    {
+      loop_next = loop->next;
+      gfc_cleanup_loop (loop);
+      free (loop);
+    }
 }
 
 
@@ -664,10 +682,15 @@ gfc_add_ss_to_loop (gfc_loopinfo * loop, gfc_ss * head)
             added one, to avoid duplicate nested loops.  */
          if (nested_loop != loop->nested)
            {
+             gcc_assert (nested_loop->parent == NULL);
+             nested_loop->parent = loop;
+
              gcc_assert (nested_loop->next == NULL);
              nested_loop->next = loop->nested;
              loop->nested = nested_loop;
            }
+         else
+           gcc_assert (nested_loop->parent == loop);
        }
 
       if (ss->next == gfc_ss_terminator)
@@ -2011,6 +2034,19 @@ trans_constant_array_constructor (gfc_ss * ss, tree type)
 }
 
 
+static int
+get_rank (gfc_loopinfo *loop)
+{
+  int rank;
+
+  rank = 0;
+  for (; loop; loop = loop->parent)
+    rank += loop->dimen;
+
+  return rank;
+}
+
+
 /* Helper routine of gfc_trans_array_constructor to determine if the
    bounds of the loop specified by LOOP are constant and simple enough
    to use with trans_constant_array_constructor.  Returns the
@@ -2049,6 +2085,23 @@ constant_array_constructor_loop_size (gfc_loopinfo * loop)
 }
 
 
+static tree *
+get_loop_upper_bound_for_array (gfc_ss *array, int array_dim)
+{
+  gfc_ss *ss;
+  int n;
+
+  gcc_assert (array->nested_ss == NULL);
+
+  for (ss = array; ss; ss = ss->parent)
+    for (n = 0; n < ss->loop->dimen; n++)
+      if (array_dim == get_array_ref_dim_for_loop_dim (ss, n))
+       return &(ss->loop->to[n]);
+
+  gcc_unreachable ();
+}
+
+
 /* Array constructors are handled by constructing a temporary, then using that
    within the scalarization loop.  This is not optimal, but seems by far the
    simplest method.  */
@@ -2062,6 +2115,7 @@ trans_array_constructor (gfc_ss * ss, locus * where)
   tree desc;
   tree type;
   tree tmp;
+  tree *loop_ubound0;
   bool dynamic;
   bool old_first_len, old_typespec_chararray_ctor;
   tree old_first_len_val;
@@ -2091,7 +2145,7 @@ trans_array_constructor (gfc_ss * ss, locus * where)
       first_len = true;
     }
 
-  gcc_assert (ss->dimen == loop->dimen);
+  gcc_assert (ss->dimen == ss->loop->dimen);
 
   c = expr->value.constructor;
   if (expr->ts.type == BT_CHARACTER)
@@ -2134,7 +2188,9 @@ trans_array_constructor (gfc_ss * ss, locus * where)
   /* See if the constructor determines the loop bounds.  */
   dynamic = false;
 
-  if (expr->shape && loop->dimen > 1 && loop->to[0] == NULL_TREE)
+  loop_ubound0 = get_loop_upper_bound_for_array (ss, 0);
+
+  if (expr->shape && get_rank (loop) > 1 && *loop_ubound0 == NULL_TREE)
     {
       /* We have a multidimensional parameter.  */
       for (s = ss; s; s = s->parent)
@@ -2153,11 +2209,12 @@ trans_array_constructor (gfc_ss * ss, locus * where)
        }
     }
 
-  if (loop->to[0] == NULL_TREE)
+  if (*loop_ubound0 == NULL_TREE)
     {
       mpz_t size;
 
       /* We should have a 1-dimensional, zero-based loop.  */
+      gcc_assert (loop->parent == NULL && loop->nested == NULL);
       gcc_assert (loop->dimen == 1);
       gcc_assert (integer_zerop (loop->from[0]));
 
@@ -2186,7 +2243,7 @@ trans_array_constructor (gfc_ss * ss, locus * where)
        }
     }
 
-  if (TREE_CODE (loop->to[0]) == VAR_DECL)
+  if (TREE_CODE (*loop_ubound0) == VAR_DECL)
     dynamic = true;
 
   gfc_trans_create_temp_array (&loop->pre, &loop->post, ss, type, NULL_TREE,
@@ -2209,10 +2266,10 @@ trans_array_constructor (gfc_ss * ss, locus * where)
                             offsetvar, gfc_index_one_node);
       tmp = gfc_evaluate_now (tmp, &loop->pre);
       gfc_conv_descriptor_ubound_set (&loop->pre, desc, gfc_rank_cst[0], tmp);
-      if (loop->to[0] && TREE_CODE (loop->to[0]) == VAR_DECL)
-       gfc_add_modify (&loop->pre, loop->to[0], tmp);
+      if (*loop_ubound0 && TREE_CODE (*loop_ubound0) == VAR_DECL)
+       gfc_add_modify (&loop->pre, *loop_ubound0, tmp);
       else
-       loop->to[0] = tmp;
+       *loop_ubound0 = tmp;
     }
 
   if (TREE_USED (offsetvar))
@@ -2295,10 +2352,12 @@ static void
 gfc_add_loop_ss_code (gfc_loopinfo * loop, gfc_ss * ss, bool subscript,
                      locus * where)
 {
+  gfc_loopinfo *nested_loop;
   gfc_se se;
   gfc_ss_info *ss_info;
   gfc_array_info *info;
   gfc_expr *expr;
+  bool skip_nested = false;
   int n;
 
   /* TODO: This can generate bad code if there are ordering dependencies,
@@ -2309,6 +2368,10 @@ gfc_add_loop_ss_code (gfc_loopinfo * loop, gfc_ss * ss, bool subscript,
     {
       gcc_assert (ss);
 
+      /* Cross loop arrays are handled from within the most nested loop.  */
+      if (ss->nested_ss != NULL)
+       continue;
+
       ss_info = ss->info;
       expr = ss_info->expr;
       info = &ss_info->data.array;
@@ -2355,7 +2418,12 @@ gfc_add_loop_ss_code (gfc_loopinfo * loop, gfc_ss * ss, bool subscript,
          /* Add the expressions for scalar and vector subscripts.  */
          for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
            if (info->subscript[n])
-             gfc_add_loop_ss_code (loop, info->subscript[n], true, where);
+             {
+               gfc_add_loop_ss_code (loop, info->subscript[n], true, where);
+               /* The recursive call will have taken care of the nested loops.
+                  No need to do it twice.  */
+               skip_nested = true;
+             }
 
          set_vector_loop_bounds (ss);
          break;
@@ -2410,6 +2478,11 @@ gfc_add_loop_ss_code (gfc_loopinfo * loop, gfc_ss * ss, bool subscript,
          gcc_unreachable ();
        }
     }
+
+  if (!skip_nested)
+    for (nested_loop = loop->nested; nested_loop;
+        nested_loop = nested_loop->next)
+      gfc_add_loop_ss_code (nested_loop, nested_loop->ss, subscript, where);
 }
 
 
@@ -3495,8 +3568,10 @@ done:
       switch (ss_info->type)
        {
        case GFC_SS_SECTION:
-         /* Get the descriptor for the array.  */
-         gfc_conv_ss_descriptor (&loop->pre, ss, !loop->array_parameter);
+         /* Get the descriptor for the array.  If it is a cross loops array,
+            we got the descriptor already in the outermost loop.  */
+         if (ss->parent == NULL)
+           gfc_conv_ss_descriptor (&loop->pre, ss, !loop->array_parameter);
 
          for (n = 0; n < ss->dimen; n++)
            gfc_conv_section_startstride (loop, ss, ss->dim[n]);
@@ -3785,6 +3860,9 @@ done:
       tmp = gfc_finish_block (&block);
       gfc_add_expr_to_block (&loop->pre, tmp);
     }
+
+  for (loop = loop->nested; loop; loop = loop->next)
+    gfc_conv_ss_startstride (loop);
 }
 
 /* Return true if both symbols could refer to the same data object.  Does
@@ -4246,6 +4324,9 @@ set_loop_bounds (gfc_loopinfo *loop)
        }
     }
   mpz_clear (i);
+
+  for (loop = loop->nested; loop; loop = loop->next)
+    set_loop_bounds (loop);
 }
 
 
@@ -4278,6 +4359,7 @@ gfc_conv_loop_setup (gfc_loopinfo * loop, locus * where)
 
       tmp_ss_info = tmp_ss->info;
       gcc_assert (tmp_ss_info->type == GFC_SS_TEMP);
+      gcc_assert (loop->parent == NULL);
 
       /* Make absolutely sure that this is a complete type.  */
       if (tmp_ss_info->string_length)
@@ -4356,6 +4438,9 @@ set_delta (gfc_loopinfo *loop)
            }
        }
     }
+
+  for (loop = loop->nested; loop; loop = loop->next)
+    set_delta (loop);
 }