This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: constant slices or string arrays patch
On Wed, 2006-12-06 at 08:41 -0800, Ian Lance Taylor wrote:
> Andrew MacLeod <amacleod@redhat.com> writes:
>
> > This patch works for my testcase, and we get the proper constant array
> > slice. What I'm not sure of is if I am using all the widgetry correctly.
> > Can the offset and lower bound of a string constant be different types?
> > Someone who knows how to use this code properly will have to let me know
> > if I am missing some conversions, or something else :-).
>
> I think you need to fold_convert offset and lower_bound to sizetype.
> I don't think there is any particular guarantee that they have that
> type, or that they have the same type. In particular the lower bound
> will typically have type TYPE_DOMAIN (array_type), which in Ada can
> probably be any random type.
>
> Patch is OK with that change.
Thanks. Bootstrapped and regression checked on i686-pc-linux-gnu both
with and without the TER changes.
Just checked it in.
2006-12-07 Andrew Macleod <amacleod@redhat.com>
* expr.c (string_constant): Account for non-zero lower bound arrays.
Index: expr.c
===================================================================
*** expr.c (revision 119549)
--- expr.c (working copy)
*************** is_aligning_offset (tree offset, tree ex
*** 8923,8929 ****
tree
string_constant (tree arg, tree *ptr_offset)
{
! tree array, offset;
STRIP_NOPS (arg);
if (TREE_CODE (arg) == ADDR_EXPR)
--- 8923,8929 ----
tree
string_constant (tree arg, tree *ptr_offset)
{
! tree array, offset, lower_bound;
STRIP_NOPS (arg);
if (TREE_CODE (arg) == ADDR_EXPR)
*************** string_constant (tree arg, tree *ptr_off
*** 8945,8950 ****
--- 8945,8964 ----
if (TREE_CODE (array) != STRING_CST
&& TREE_CODE (array) != VAR_DECL)
return 0;
+
+ /* Check if the array has a non-zero lower bound. */
+ lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
+ if (!integer_zerop (lower_bound))
+ {
+ /* If the offset and base aren't both constants, return 0. */
+ if (TREE_CODE (lower_bound) != INTEGER_CST)
+ return 0;
+ if (TREE_CODE (offset) != INTEGER_CST)
+ return 0;
+ /* Adjust offset by the lower bound. */
+ offset = size_diffop (fold_convert (sizetype, offset),
+ fold_convert (sizetype, lower_bound));
+ }
}
else
return 0;