summaryrefslogtreecommitdiff
path: root/gcc/d
diff options
context:
space:
mode:
authorIain Buclaw <ibuclaw@gdcproject.org>2022-06-29 21:52:39 +0200
committerIain Buclaw <ibuclaw@gdcproject.org>2022-07-04 15:16:23 +0200
commit32dfb075ad31413af9086ce546b5f5317a916d34 (patch)
treeb0002a95441c0da2fce1bef52a0fcce4f47a6091 /gcc/d
parent6c175b3d170de2bb02b7bd45b3348eec05d28451 (diff)
d: Fix error: aggregate value used where floating point was expected
Casting from vector to static array is permitted, and the frontend generates a reinterpret cast, but casting back the other way resulted in an error. This has been fixed to be properly handled in the code generation pass of VectorExp, and the conversion for lvalue and rvalue handling done in convert_expr and convert_for_rvalue respectively. PR d/106139 gcc/d/ChangeLog: * d-convert.cc (convert_expr): Handle casting from array to vector. (convert_for_rvalue): Rewrite vector to array casts of the same element type into a constructor. (convert_for_assignment): Return calling convert_for_rvalue. * expr.cc (ExprVisitor::visit (VectorExp *)): Handle generating a vector expression from a static array. * toir.cc (IRVisitor::visit (ReturnStatement *)): Call convert_for_rvalue on return value. gcc/testsuite/ChangeLog: * gdc.dg/pr106139a.d: New test. * gdc.dg/pr106139b.d: New test. * gdc.dg/pr106139c.d: New test. * gdc.dg/pr106139d.d: New test. (cherry picked from commit 329bef49da30158d30fed1106002bb71674776bd)
Diffstat (limited to 'gcc/d')
-rw-r--r--gcc/d/d-convert.cc44
-rw-r--r--gcc/d/expr.cc10
-rw-r--r--gcc/d/toir.cc1
3 files changed, 52 insertions, 3 deletions
diff --git a/gcc/d/d-convert.cc b/gcc/d/d-convert.cc
index 3a6a32ab024..ec5da6c10a6 100644
--- a/gcc/d/d-convert.cc
+++ b/gcc/d/d-convert.cc
@@ -502,6 +502,15 @@ convert_expr (tree exp, Type *etype, Type *totype)
gcc_assert (totype->size () == etype->size ());
result = build_vconvert (build_ctype (totype), exp);
}
+ else if (tbtype->ty == TY::Tvector && tbtype->size () == ebtype->size ())
+ {
+ /* Allow casting from array to vector as if its an unaligned load. */
+ tree type = build_ctype (totype);
+ tree unaligned_type = build_variant_type_copy (type);
+ SET_TYPE_ALIGN (unaligned_type, 1 * BITS_PER_UNIT);
+ TYPE_USER_ALIGN (unaligned_type) = 1;
+ result = convert (type, build_vconvert (unaligned_type, exp));
+ }
else
{
error ("cannot cast expression of type %qs to type %qs",
@@ -643,6 +652,39 @@ convert_for_rvalue (tree expr, Type *etype, Type *totype)
result = convert (build_ctype (tbtype), result);
}
+ if (tbtype->ty == TY::Tsarray
+ && ebtype->ty == TY::Tsarray
+ && tbtype->nextOf ()->ty == ebtype->nextOf ()->ty
+ && INDIRECT_REF_P (expr)
+ && CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (expr, 0)))
+ && TREE_CODE (TREE_OPERAND (TREE_OPERAND (expr, 0), 0)) == ADDR_EXPR)
+ {
+ /* If expression is a vector that was casted to an array either by
+ explicit type cast or by taking the vector's `.array' value, strip the
+ reinterpret cast and build a constructor instead. */
+ tree ptr = TREE_OPERAND (TREE_OPERAND (expr, 0), 0);
+
+ if (VECTOR_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
+ {
+ /* Rewrite: `*(Array *)&vector'
+ into: `{ vector[0], vector[1], ... }' */
+ tree array = d_save_expr (TREE_OPERAND (ptr, 0));
+ array = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expr), array);
+
+ uinteger_t dim = tbtype->isTypeSArray ()->dim->toUInteger ();
+ vec <constructor_elt, va_gc> *elms = NULL;
+ for (uinteger_t i = 0; i < dim; i++)
+ {
+ tree index = size_int (i);
+ tree value = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (array)),
+ array, index, NULL_TREE, NULL_TREE);
+ CONSTRUCTOR_APPEND_ELT (elms, index, value);
+ }
+
+ return build_constructor (build_ctype (totype), elms);
+ }
+ }
+
return result ? result : convert_expr (expr, etype, totype);
}
@@ -703,7 +745,7 @@ convert_for_assignment (tree expr, Type *etype, Type *totype)
return expr;
}
- return convert_expr (expr, etype, totype);
+ return convert_for_rvalue (expr, etype, totype);
}
/* Return a TREE representation of EXPR converted to represent
diff --git a/gcc/d/expr.cc b/gcc/d/expr.cc
index 325cbed892f..73dab463926 100644
--- a/gcc/d/expr.cc
+++ b/gcc/d/expr.cc
@@ -2932,14 +2932,13 @@ public:
void visit (VectorExp *e)
{
- tree type = build_ctype (e->type);
-
/* First handle array literal expressions. */
if (e->e1->op == EXP::arrayLiteral)
{
ArrayLiteralExp *ale = e->e1->isArrayLiteralExp ();
vec <constructor_elt, va_gc> *elms = NULL;
bool constant_p = true;
+ tree type = build_ctype (e->type);
vec_safe_reserve (elms, ale->elements->length);
for (size_t i = 0; i < ale->elements->length; i++)
@@ -2959,9 +2958,16 @@ public:
else
this->result_ = build_constructor (type, elms);
}
+ else if (e->e1->type->toBasetype ()->ty == TY::Tsarray)
+ {
+ /* Build a vector representation from a static array. */
+ this->result_ = convert_expr (build_expr (e->e1, this->constp_),
+ e->e1->type, e->type);
+ }
else
{
/* Build constructor from single value. */
+ tree type = build_ctype (e->type);
tree value = d_convert (TREE_TYPE (type),
build_expr (e->e1, this->constp_, true));
this->result_ = build_vector_from_val (type, value);
diff --git a/gcc/d/toir.cc b/gcc/d/toir.cc
index d20c5c3b927..bf81dc9ad96 100644
--- a/gcc/d/toir.cc
+++ b/gcc/d/toir.cc
@@ -1022,6 +1022,7 @@ public:
/* Generate: (<retval> = expr, return <retval>); */
tree expr = build_expr_dtor (s->exp);
tree init = stabilize_expr (&expr);
+ expr = convert_for_rvalue (expr, s->exp->type, type);
expr = build_assign (INIT_EXPR, this->func_->shidden, expr);
add_stmt (compound_expr (init, expr));
}