This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Make C frontend emit &a[0] for array-to-pointer decay
- From: Richard Guenther <rguenth at tat dot physik dot uni-tuebingen dot de>
- To: gcc-patches at gcc dot gnu dot org
- Date: Fri, 20 May 2005 15:16:24 +0200 (CEST)
- Subject: [PATCH] Make C frontend emit &a[0] for array-to-pointer decay
Finally, this patch enables the C frontend to no longer emit
an ADDR_EXPR with mismatched types for array-to-pointer decay,
but instead use &a[0] to represent this.
Together with all prerequesite(*) patches this passed bootstrap
and regtesting on i686-pc-linux-gnu and x86_64-unknown-linux-gnu.
Ok for mainline (after prerequesite patches have gone in)?
Thanks,
Richard.
(*)
http://gcc.gnu.org/ml/gcc-patches/2005-05/msg02050.html
http://gcc.gnu.org/ml/gcc-patches/2005-05/msg01981.html
2005-05-19 Richard Guenther <rguenth@gcc.gnu.org>
* c-typeck.c (default_function_array_conversion): Emit
array-to-pointer decay as &a[0].
(build_unary_op): Emit &a[i] in its original form rather
than splitting it to a + i. Mark compound literal
expressions constant if they are in ARRAY_REF form.
Index: c-typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-typeck.c,v
retrieving revision 1.444
diff -c -3 -p -r1.444 c-typeck.c
*** c-typeck.c 17 May 2005 20:11:38 -0000 1.444
--- c-typeck.c 19 May 2005 14:41:08 -0000
*************** default_function_array_conversion (tree
*** 1357,1378 ****
ptrtype = build_pointer_type (restype);
! if (TREE_CODE (exp) == VAR_DECL)
! {
! /* We are making an ADDR_EXPR of ptrtype. This is a valid
! ADDR_EXPR because it's the best way of representing what
! happens in C when we take the address of an array and place
! it in a pointer to the element type. */
! adr = build1 (ADDR_EXPR, ptrtype, exp);
! if (!c_mark_addressable (exp))
! return error_mark_node;
! TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
! return adr;
! }
! /* This way is better for a COMPONENT_REF since it can
! simplify the offset for a component. */
! adr = build_unary_op (ADDR_EXPR, exp, 1);
! return convert (ptrtype, adr);
}
return exp;
}
--- 1357,1375 ----
ptrtype = build_pointer_type (restype);
! /* We are making an ADDR_EXPR of ptrtype. So we need to
! construct an ARRAY_REF to the first element of the
! array for tree type correctness. This is the best way of
! representing what happens in C when we take the address of
! an array and place it in a pointer to the element type. */
! adr = build_unary_op (ADDR_EXPR,
! build4 (ARRAY_REF, restype,
! exp, integer_zero_node,
! NULL_TREE, NULL_TREE), 1);
! if (adr == error_mark_node)
! return error_mark_node;
! TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
! return adr;
}
return exp;
}
*************** build_unary_op (enum tree_code code, tre
*** 2756,2769 ****
return TREE_OPERAND (arg, 0);
}
! /* For &x[y], return x+y */
! if (TREE_CODE (arg) == ARRAY_REF)
! {
! if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
! return error_mark_node;
! return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
! TREE_OPERAND (arg, 1), 1);
! }
/* Anything not already handled and not a true memory reference
or a non-lvalue array is an error. */
--- 2753,2761 ----
return TREE_OPERAND (arg, 0);
}
! if (TREE_CODE (arg) == ARRAY_REF
! && !c_mark_addressable (TREE_OPERAND (arg, 0)))
! return error_mark_node;
/* Anything not already handled and not a true memory reference
or a non-lvalue array is an error. */
*************** build_unary_op (enum tree_code code, tre
*** 2801,2807 ****
val = build1 (ADDR_EXPR, argtype, arg);
! if (TREE_CODE (arg) == COMPOUND_LITERAL_EXPR)
TREE_INVARIANT (val) = TREE_CONSTANT (val) = 1;
return val;
--- 2793,2801 ----
val = build1 (ADDR_EXPR, argtype, arg);
! if (TREE_CODE (arg) == COMPOUND_LITERAL_EXPR
! || (TREE_CODE (arg) == ARRAY_REF
! && TREE_CODE (TREE_OPERAND (arg, 0)) == COMPOUND_LITERAL_EXPR))
TREE_INVARIANT (val) = TREE_CONSTANT (val) = 1;
return val;