Commit 54466dde by Ian Lance Taylor Committed by Ian Lance Taylor

Use backend interface for zero initialization.

	* go-gcc.cc (Gcc_backend::zero_expression): New function.

From-SVN: r174863
parent 3be68b64
2011-06-09 Ian Lance Taylor <iant@google.com>
* go-gcc.cc (Gcc_backend::zero_expression): New function.
2011-06-07 Richard Guenther <rguenther@suse.de>
* go-lang.c (go_langhook_init): Do not set
......
......@@ -194,6 +194,11 @@ class Gcc_backend : public Backend
bool
is_circular_pointer_type(Btype*);
// Expressions.
Bexpression*
zero_expression(Btype*);
// Statements.
Bstatement*
......@@ -700,6 +705,20 @@ Gcc_backend::is_circular_pointer_type(Btype* btype)
return btype->get_tree() == ptr_type_node;
}
// Return the zero value for a type.
Bexpression*
Gcc_backend::zero_expression(Btype* btype)
{
tree t = btype->get_tree();
tree ret;
if (t == error_mark_node)
ret = error_mark_node;
else
ret = build_zero_cst(t);
return tree_to_expr(ret);
}
// An expression as a statement.
Bstatement*
......
......@@ -198,6 +198,14 @@ class Backend
virtual bool
is_circular_pointer_type(Btype*) = 0;
// Expressions.
// Return an expression for a zero value of the given type. This is
// used for cases such as local variable initialization and
// converting nil to other types.
virtual Bexpression*
zero_expression(Btype*) = 0;
// Statements.
// Create an error statement. This is used for cases which should
......
......@@ -318,7 +318,10 @@ Expression::convert_type_to_interface(Translate_context* context,
// When setting an interface to nil, we just set both fields to
// NULL.
if (rhs_type->is_nil_type())
return lhs_type->get_init_tree(gogo, false);
{
Btype* lhs_btype = lhs_type->get_backend(gogo);
return expr_to_tree(gogo->backend()->zero_expression(lhs_btype));
}
// This should have been checked already.
go_assert(lhs_interface_type->implements_interface(rhs_type, NULL));
......@@ -10046,12 +10049,14 @@ Map_index_expression::do_get_tree(Translate_context* context)
}
else
{
Gogo* gogo = context->gogo();
Btype* val_btype = type->val_type()->get_backend(gogo);
Bexpression* val_zero = gogo->backend()->zero_expression(val_btype);
return fold_build3(COND_EXPR, val_type_tree,
fold_build2(EQ_EXPR, boolean_type_node, valptr,
fold_convert(TREE_TYPE(valptr),
null_pointer_node)),
type->val_type()->get_init_tree(context->gogo(),
false),
expr_to_tree(val_zero),
build_fold_indirect_ref(valptr));
}
}
......@@ -10958,7 +10963,10 @@ Struct_construction_expression::do_get_tree(Translate_context* context)
Gogo* gogo = context->gogo();
if (this->vals_ == NULL)
return this->type_->get_init_tree(gogo, false);
{
Btype* btype = this->type_->get_backend(gogo);
return expr_to_tree(gogo->backend()->zero_expression(btype));
}
tree type_tree = type_to_tree(this->type_->get_backend(gogo));
if (type_tree == error_mark_node)
......@@ -10977,12 +10985,14 @@ Struct_construction_expression::do_get_tree(Translate_context* context)
{
go_assert(pf != fields->end());
Btype* fbtype = pf->type()->get_backend(gogo);
tree val;
if (pv == this->vals_->end())
val = pf->type()->get_init_tree(gogo, false);
val = expr_to_tree(gogo->backend()->zero_expression(fbtype));
else if (*pv == NULL)
{
val = pf->type()->get_init_tree(gogo, false);
val = expr_to_tree(gogo->backend()->zero_expression(fbtype));
++pv;
}
else
......@@ -11217,7 +11227,12 @@ Array_construction_expression::get_constructor_tree(Translate_context* context,
constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL);
elt->index = size_int(i);
if (*pv == NULL)
elt->value = element_type->get_init_tree(context->gogo(), false);
{
Gogo* gogo = context->gogo();
Btype* ebtype = element_type->get_backend(gogo);
Bexpression *zv = gogo->backend()->zero_expression(ebtype);
elt->value = expr_to_tree(zv);
}
else
{
tree value_tree = (*pv)->get_tree(context);
......@@ -11363,7 +11378,9 @@ Open_array_construction_expression::do_get_tree(Translate_context* context)
VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1);
constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL);
elt->index = size_int(0);
elt->value = element_type->get_init_tree(context->gogo(), false);
Gogo* gogo = context->gogo();
Btype* btype = element_type->get_backend(gogo);
elt->value = expr_to_tree(gogo->backend()->zero_expression(btype));
values = build_constructor(constructor_type, vec);
if (TREE_CONSTANT(elt->value))
TREE_CONSTANT(values) = 1;
......
......@@ -1035,9 +1035,10 @@ Variable::get_init_tree(Gogo* gogo, Named_object* function)
if (this->init_ == NULL)
{
go_assert(!this->is_parameter_);
return this->type_->get_init_tree(gogo,
(this->is_global_
|| this->is_in_heap()));
if (this->is_global_ || this->is_in_heap())
return NULL;
Btype* btype = this->type_->get_backend(gogo);
return expr_to_tree(gogo->backend()->zero_expression(btype));
}
else
{
......@@ -1286,7 +1287,8 @@ Function::make_receiver_parm_decl(Gogo* gogo, Named_object* no, tree var_decl)
null_pointer_node));
tree ind = build_fold_indirect_ref_loc(loc, parm_decl);
TREE_THIS_NOTRAP(ind) = 1;
tree zero_init = no->var_value()->type()->get_init_tree(gogo, false);
Btype* btype = no->var_value()->type()->get_backend(gogo);
tree zero_init = expr_to_tree(gogo->backend()->zero_expression(btype));
tree init = fold_build3_loc(loc, COND_EXPR, TREE_TYPE(ind),
check, ind, zero_init);
......@@ -1423,7 +1425,10 @@ Function::build_tree(Gogo* gogo, Named_object* named_function)
Type* type = (*p)->result_var_value()->type();
tree init;
if (!(*p)->result_var_value()->is_in_heap())
init = type->get_init_tree(gogo, false);
{
Btype* btype = type->get_backend(gogo);
init = expr_to_tree(gogo->backend()->zero_expression(btype));
}
else
{
source_location loc = (*p)->location();
......@@ -1432,20 +1437,7 @@ Function::build_tree(Gogo* gogo, Named_object* named_function)
TYPE_SIZE_UNIT(type_tree),
loc);
tree ptr_type_tree = build_pointer_type(type_tree);
tree subinit = type->get_init_tree(gogo, true);
if (subinit == NULL_TREE)
init = fold_convert_loc(loc, ptr_type_tree, space);
else
{
space = save_expr(space);
space = fold_convert_loc(loc, ptr_type_tree, space);
tree spaceref = build_fold_indirect_ref_loc(loc, space);
TREE_THIS_NOTRAP(spaceref) = 1;
tree set = fold_build2_loc(loc, MODIFY_EXPR, void_type_node,
spaceref, subinit);
init = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(space),
set, space);
}
init = fold_convert_loc(loc, ptr_type_tree, space);
}
if (var_decl != error_mark_node)
......
......@@ -1498,6 +1498,10 @@ Check_types_traverse::variable(Named_object* named_object)
if (named_object->is_variable())
{
Variable* var = named_object->var_value();
// Give error if variable type is not defined.
var->type()->base();
Expression* init = var->init();
std::string reason;
if (init != NULL
......
......@@ -825,19 +825,6 @@ class Type
Btype*
get_backend(Gogo*);
// Return a tree representing a zero initialization for this type.
// This will be something like an INTEGER_CST or a CONSTRUCTOR. If
// IS_CLEAR is true, then the memory is known to be zeroed; in that
// case, this will return NULL if there is nothing to be done.
tree
get_init_tree(Gogo*, bool is_clear);
// Like get_init_tree, but passing in the type to use for the
// initializer.
tree
get_typed_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
{ return this->do_get_init_tree(gogo, type_tree, is_clear); }
// Return a tree for a make expression applied to this type.
tree
make_expression_tree(Translate_context* context, Expression_list* args,
......@@ -896,9 +883,6 @@ class Type
do_get_backend(Gogo*) = 0;
virtual tree
do_get_init_tree(Gogo*, tree, bool) = 0;
virtual tree
do_make_expression_tree(Translate_context*, Expression_list*,
source_location);
......@@ -1334,9 +1318,6 @@ class Integer_type : public Type
Btype*
do_get_backend(Gogo*);
tree
do_get_init_tree(Gogo*, tree, bool);
Expression*
do_type_descriptor(Gogo*, Named_type*);
......@@ -1406,9 +1387,6 @@ class Float_type : public Type
Btype*
do_get_backend(Gogo*);
tree
do_get_init_tree(Gogo*, tree, bool);
Expression*
do_type_descriptor(Gogo*, Named_type*);
......@@ -1474,9 +1452,6 @@ class Complex_type : public Type
Btype*
do_get_backend(Gogo*);
tree
do_get_init_tree(Gogo*, tree, bool);
Expression*
do_type_descriptor(Gogo*, Named_type*);
......@@ -1530,9 +1505,6 @@ class String_type : public Type
Btype*
do_get_backend(Gogo*);
tree
do_get_init_tree(Gogo* gogo, tree, bool);
Expression*
do_type_descriptor(Gogo*, Named_type*);
......@@ -1650,9 +1622,6 @@ class Function_type : public Type
Btype*
do_get_backend(Gogo*);
tree
do_get_init_tree(Gogo*, tree, bool);
Expression*
do_type_descriptor(Gogo*, Named_type*);
......@@ -1734,9 +1703,6 @@ class Pointer_type : public Type
Btype*
do_get_backend(Gogo*);
tree
do_get_init_tree(Gogo*, tree, bool);
Expression*
do_type_descriptor(Gogo*, Named_type*);
......@@ -1988,9 +1954,6 @@ class Struct_type : public Type
Btype*
do_get_backend(Gogo*);
tree
do_get_init_tree(Gogo*, tree, bool);
Expression*
do_type_descriptor(Gogo*, Named_type*);
......@@ -2106,9 +2069,6 @@ class Array_type : public Type
do_get_backend(Gogo*);
tree
do_get_init_tree(Gogo*, tree, bool);
tree
do_make_expression_tree(Translate_context*, Expression_list*,
source_location);
......@@ -2197,9 +2157,6 @@ class Map_type : public Type
do_get_backend(Gogo*);
tree
do_get_init_tree(Gogo*, tree, bool);
tree
do_make_expression_tree(Translate_context*, Expression_list*,
source_location);
......@@ -2281,9 +2238,6 @@ class Channel_type : public Type
do_get_backend(Gogo*);
tree
do_get_init_tree(Gogo*, tree, bool);
tree
do_make_expression_tree(Translate_context*, Expression_list*,
source_location);
......@@ -2398,9 +2352,6 @@ class Interface_type : public Type
Btype*
do_get_backend(Gogo*);
tree
do_get_init_tree(Gogo* gogo, tree, bool);
Expression*
do_type_descriptor(Gogo*, Named_type*);
......@@ -2630,10 +2581,6 @@ class Named_type : public Type
do_get_backend(Gogo*);
tree
do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
{ return this->type_->get_typed_init_tree(gogo, type_tree, is_clear); }
tree
do_make_expression_tree(Translate_context* context, Expression_list* args,
source_location location)
{ return this->type_->make_expression_tree(context, args, location); }
......@@ -2774,10 +2721,6 @@ class Forward_declaration_type : public Type
do_get_backend(Gogo* gogo);
tree
do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
{ return this->base()->get_typed_init_tree(gogo, type_tree, is_clear); }
tree
do_make_expression_tree(Translate_context* context, Expression_list* args,
source_location location)
{ return this->base()->make_expression_tree(context, args, location); }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment