[PATCH, fortran] Simplify constant substrings

Steven Bosscher stevenb@suse.de
Fri Jul 22 14:08:00 GMT 2005


On Friday 22 July 2005 06:53, David Edelsohn wrote:
> 	* expr.c (gfc_simplify_expr): Evaluate constant substrings.
>
> Index: expr.c
> ===================================================================
> RCS file: /cvs/gcc/gcc/gcc/fortran/expr.c,v
> retrieving revision 1.27
> diff -c -p -r1.27 expr.c
> *** expr.c	7 Jul 2005 07:54:41 -0000	1.27
> --- expr.c	21 Jul 2005 18:31:38 -0000
> *************** gfc_simplify_expr (gfc_expr * p, int typ
> *** 1130,1136 ****
>         if (simplify_ref_chain (p->ref, type) == FAILURE)
>   	return FAILURE;
>
> !       /* TODO: evaluate constant substrings.  */
>         break;
>
>       case EXPR_OP:
> --- 1130,1156 ----
>         if (simplify_ref_chain (p->ref, type) == FAILURE)
>   	return FAILURE;
>
> !       if (gfc_is_constant_expr (p))
> ! 	{
> ! 	  char *s;
> ! 	  int start, end;
> !
> ! 	  gfc_extract_int (p->ref->u.ss.start, &start);
> ! 	  start--;  /* Convert from one-based to zero-based.  */
> ! 	  gfc_extract_int (p->ref->u.ss.end, &end);
> ! 	  s = gfc_getmem (end - start);
> ! 	  memcpy (s, p->value.character.string + start, end-start);
> ! 	  gfc_free (p->value.character.string);
> ! 	  p->value.character.string = s;
> ! 	  p->value.character.length = end - start;
> ! 	  p->ts.cl = gfc_get_charlen ();
> ! 	  p->ts.cl->next = gfc_current_ns->cl_list;
> ! 	  gfc_current_ns->cl_list = p->ts.cl;
> ! 	  p->ts.cl->length = gfc_int_expr (p->value.character.length);
> ! 	  gfc_free_ref_list (p->ref);
> ! 	  p->ref = NULL;
> ! 	  p->expr_type = EXPR_CONSTANT;
> ! 	}
>         break;
>
>       case EXPR_OP:

In a number of places we add an extra '\0' at the end of the string
value for debugging purposes.  That would mean for your patch, if we
want to be consistent with, well, everywhere else in gfortran, that
you'd have to add the extra '\0' too:

> ! 	  s = gfc_getmem (end - start + 1);
> ! 	  memcpy (s, p->value.character.string + start, end-start);
> + 	  p->value.character.string[end - start + 1] = '\0'; /* For debugger */
> ! 	  gfc_free (p->value.character.string);
> ! 	  p->value.character.string = s;
> ! 	  p->value.character.length = end - start;

However, Paul dislikes the idea of the extra '\0'.  I think that if
we are going to drop it here, then we should drop it everywhere else
too.  But I actually think the extra '\0' is useful because it allows
you to print value.character.string from gdb.
Thoughs, anyone?

Gr.
Steven



More information about the Gcc-patches mailing list