aboutsummaryrefslogtreecommitdiff
path: root/gcc/fortran/simplify.cc
diff options
context:
space:
mode:
authorHarald Anlauf <anlauf@gmx.de>2022-06-21 23:20:18 +0200
committerHarald Anlauf <anlauf@gmx.de>2022-06-26 22:06:12 +0200
commitff35dbc02092fbcd3d814fcd9fe8e871c3f741fd (patch)
tree8f37d23b6a4c57cf227d568dd989b0a5981ecab7 /gcc/fortran/simplify.cc
parenta312407bd715647f7c11b67e0a52effc94d0f15d (diff)
downloadgcc-ff35dbc02092fbcd3d814fcd9fe8e871c3f741fd.zip
gcc-ff35dbc02092fbcd3d814fcd9fe8e871c3f741fd.tar.gz
gcc-ff35dbc02092fbcd3d814fcd9fe8e871c3f741fd.tar.bz2
Fortran: fix simplification of INDEX(str1,str2) [PR105691]
gcc/fortran/ChangeLog: PR fortran/105691 * simplify.cc (gfc_simplify_index): Replace old simplification code by the equivalent of the runtime library implementation. Use HOST_WIDE_INT instead of int for string index, length variables. gcc/testsuite/ChangeLog: PR fortran/105691 * gfortran.dg/index_6.f90: New test.
Diffstat (limited to 'gcc/fortran/simplify.cc')
-rw-r--r--gcc/fortran/simplify.cc131
1 files changed, 29 insertions, 102 deletions
diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc
index c8f2ef9..e8e3ec6 100644
--- a/gcc/fortran/simplify.cc
+++ b/gcc/fortran/simplify.cc
@@ -3515,17 +3515,15 @@ gfc_expr *
gfc_simplify_index (gfc_expr *x, gfc_expr *y, gfc_expr *b, gfc_expr *kind)
{
gfc_expr *result;
- int back, len, lensub;
- int i, j, k, count, index = 0, start;
+ bool back;
+ HOST_WIDE_INT len, lensub, start, last, i, index = 0;
+ int k, delta;
if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT
|| ( b != NULL && b->expr_type != EXPR_CONSTANT))
return NULL;
- if (b != NULL && b->value.logical != 0)
- back = 1;
- else
- back = 0;
+ back = (b != NULL && b->value.logical != 0);
k = get_kind (BT_INTEGER, kind, "INDEX", gfc_default_integer_kind);
if (k == -1)
@@ -3542,111 +3540,40 @@ gfc_simplify_index (gfc_expr *x, gfc_expr *y, gfc_expr *b, gfc_expr *kind)
return result;
}
- if (back == 0)
+ if (lensub == 0)
{
- if (lensub == 0)
- {
- mpz_set_si (result->value.integer, 1);
- return result;
- }
- else if (lensub == 1)
- {
- for (i = 0; i < len; i++)
- {
- for (j = 0; j < lensub; j++)
- {
- if (y->value.character.string[j]
- == x->value.character.string[i])
- {
- index = i + 1;
- goto done;
- }
- }
- }
- }
+ if (back)
+ index = len + 1;
else
- {
- for (i = 0; i < len; i++)
- {
- for (j = 0; j < lensub; j++)
- {
- if (y->value.character.string[j]
- == x->value.character.string[i])
- {
- start = i;
- count = 0;
-
- for (k = 0; k < lensub; k++)
- {
- if (y->value.character.string[k]
- == x->value.character.string[k + start])
- count++;
- }
-
- if (count == lensub)
- {
- index = start + 1;
- goto done;
- }
- }
- }
- }
- }
+ index = 1;
+ goto done;
+ }
+ if (!back)
+ {
+ last = len + 1 - lensub;
+ start = 0;
+ delta = 1;
}
else
{
- if (lensub == 0)
- {
- mpz_set_si (result->value.integer, len + 1);
- return result;
- }
- else if (lensub == 1)
+ last = -1;
+ start = len - lensub;
+ delta = -1;
+ }
+
+ for (; start != last; start += delta)
+ {
+ for (i = 0; i < lensub; i++)
{
- for (i = 0; i < len; i++)
- {
- for (j = 0; j < lensub; j++)
- {
- if (y->value.character.string[j]
- == x->value.character.string[len - i])
- {
- index = len - i + 1;
- goto done;
- }
- }
- }
+ if (x->value.character.string[start + i]
+ != y->value.character.string[i])
+ break;
}
- else
+ if (i == lensub)
{
- for (i = 0; i < len; i++)
- {
- for (j = 0; j < lensub; j++)
- {
- if (y->value.character.string[j]
- == x->value.character.string[len - i])
- {
- start = len - i;
- if (start <= len - lensub)
- {
- count = 0;
- for (k = 0; k < lensub; k++)
- if (y->value.character.string[k]
- == x->value.character.string[k + start])
- count++;
-
- if (count == lensub)
- {
- index = start + 1;
- goto done;
- }
- }
- else
- {
- continue;
- }
- }
- }
- }
+ index = start + 1;
+ goto done;
}
}