===================================================================
@@ -165,10 +165,10 @@ optimize_assignment (gfc_code * c)
lhs = c->expr1;
rhs = c->expr2;
- /* Optimize away a = trim(b), where a is a character variable. */
-
if (lhs->ts.type == BT_CHARACTER)
{
+ /* Optimize away a = trim(b), where a is a character variable. */
+
if (rhs->expr_type == EXPR_FUNCTION &&
rhs->value.function.isym &&
rhs->value.function.isym->id == GFC_ISYM_TRIM)
@@ -177,8 +177,69 @@ optimize_assignment (gfc_code * c)
optimize_assignment (c);
return;
}
+
+ /* Fill up blanks on the right-hand side on assignment, if they extend
+ the length by less than 8 bytes. This is an arbitrary limit.*/
+
+#define STRING_PAD_LIMIT 8
+
+ if (rhs->expr_type == EXPR_CONSTANT)
+ {
+ mpz_t lhs_l, diff;
+ bool valid_lhs = false;
+
+ mpz_init (lhs_l);
+ mpz_init (diff);
+
+ if (lhs->ref && lhs->ref->type == REF_SUBSTRING)
+ {
+ if (lhs->ref->u.ss.start->expr_type == EXPR_CONSTANT
+ && lhs->ref->u.ss.end->expr_type == EXPR_CONSTANT)
+ {
+ mpz_sub (lhs_l, lhs->ref->u.ss.end->value.integer,
+ lhs->ref->u.ss.start->value.integer);
+ mpz_add_ui (lhs_l, lhs_l, 1u);
+ valid_lhs = true;
+ }
+ }
+ else if (lhs->ts.u.cl->length)
+ {
+ mpz_set (lhs_l, lhs->ts.u.cl->length->value.integer);
+ valid_lhs = true;
+ }
+
+ if (valid_lhs)
+ {
+ mpz_sub_ui (diff, lhs_l, rhs->value.character.length);
+ if (mpz_cmp_si (diff, 0) > 0
+ && mpz_cmp_si (diff, STRING_PAD_LIMIT) < 0)
+ {
+ long int sz;
+ int i;
+ gfc_char_t *v;
+
+ sz = mpz_get_si (lhs_l);
+ v = gfc_get_wide_string (sz);
+
+ memcpy (v, rhs->value.character.string,
+ rhs->value.character.length*sizeof(gfc_char_t));
+
+ for (i=rhs->value.character.length; i<sz; i++)
+ v[i] = ' ';
+
+ gfc_free (rhs->value.character.string);
+ rhs->value.character.string = v;
+ rhs->value.character.length = sz;
+ }
+ }
+
+ mpz_clear (lhs_l);
+ mpz_clear(diff);
+ }
}
+#undef STRING_PAD_LIMIT
+
if (lhs->rank > 0 && gfc_check_dependency (lhs, rhs, true) == 0)
optimize_binop_array_assignment (c, &rhs, false);
}