Commit 3c765286 by Ian Lance Taylor

compiler: Simplify making integer expressions.

Instead of always needing an mpz_t, add helper functions to
create an integer functions from signed or unsigned long
values.

From-SVN: r216610
parent 7a149e7a
......@@ -214,10 +214,20 @@ class Expression
static Expression*
make_character(const mpz_t*, Type*, Location);
// Make a constant integer expression. TYPE should be NULL for an
// abstract type.
// Make a constant integer expression from a multi-precision
// integer. TYPE should be NULL for an abstract type.
static Expression*
make_integer_z(const mpz_t*, Type*, Location);
// Make a constant integer expression from an unsigned long. TYPE
// should be NULL for an abstract type.
static Expression*
make_integer_ul(unsigned long, Type*, Location);
// Make a constant integer expression from a signed long. TYPE
// should be NULL for an abstract type.
static Expression*
make_integer(const mpz_t*, Type*, Location);
make_integer_sl(long, Type*, Location);
// Make a constant float expression. TYPE should be NULL for an
// abstract type.
......
......@@ -587,11 +587,7 @@ Gogo::zero_value(Type *type)
// We will change the type later, when we know the size.
Type* byte_type = this->lookup_global("byte")->type_value();
mpz_t val;
mpz_init_set_ui(val, 0);
Expression* zero = Expression::make_integer(&val, NULL, bloc);
mpz_clear(val);
Expression* zero = Expression::make_integer_ul(0, NULL, bloc);
Type* array_type = Type::make_array_type(byte_type, zero);
Variable* var = new Variable(array_type, NULL, true, false, false, bloc);
......@@ -738,11 +734,8 @@ Gogo::register_gc_vars(const std::vector<Named_object*>& var_gc,
"__size", uint_type);
Location builtin_loc = Linemap::predeclared_location();
size_t count = var_gc.size();
mpz_t lenval;
mpz_init_set_ui(lenval, count);
Expression* length = Expression::make_integer(&lenval, NULL, builtin_loc);
mpz_clear(lenval);
Expression* length = Expression::make_integer_ul(var_gc.size(), NULL,
builtin_loc);
Array_type* root_array_type = Type::make_array_type(root_type, length);
Type* ptdt = Type::make_type_descriptor_ptr_type();
......@@ -783,10 +776,7 @@ Gogo::register_gc_vars(const std::vector<Named_object*>& var_gc,
Expression* nil = Expression::make_nil(builtin_loc);
null_init->push_back(nil);
mpz_t zval;
mpz_init_set_ui(zval, 0UL);
Expression* zero = Expression::make_integer(&zval, NULL, builtin_loc);
mpz_clear(zval);
Expression *zero = Expression::make_integer_ul(0, NULL, builtin_loc);
null_init->push_back(zero);
Expression* null_root_ctor =
......@@ -4029,10 +4019,7 @@ Build_recover_thunks::can_recover_arg(Location location)
Expression* fn = Expression::make_func_reference(builtin_return_address,
NULL, location);
mpz_t zval;
mpz_init_set_ui(zval, 0UL);
Expression* zexpr = Expression::make_integer(&zval, NULL, location);
mpz_clear(zval);
Expression* zexpr = Expression::make_integer_ul(0, NULL, location);
Expression_list *args = new Expression_list();
args->push_back(zexpr);
......@@ -4067,10 +4054,8 @@ Expression*
Gogo::runtime_error(int code, Location location)
{
Type* int32_type = Type::lookup_integer_type("int32");
mpz_t val;
mpz_init_set_ui(val, code);
Expression* code_expr = Expression::make_integer(&val, int32_type, location);
mpz_clear(val);
Expression* code_expr = Expression::make_integer_ul(code, int32_type,
location);
return Runtime::make_call(Runtime::RUNTIME_ERROR, location, 1, code_expr);
}
......
......@@ -2510,7 +2510,7 @@ Parse::operand(bool may_be_sink, bool* is_parenthesized)
return ret;
case Token::TOKEN_INTEGER:
ret = Expression::make_integer(token->integer_value(), NULL,
ret = Expression::make_integer_z(token->integer_value(), NULL,
token->location());
this->advance_token();
return ret;
......@@ -3129,12 +3129,7 @@ Parse::index(Expression* expr)
if (!this->peek_token()->is_op(OPERATOR_COLON))
start = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
else
{
mpz_t zero;
mpz_init_set_ui(zero, 0);
start = Expression::make_integer(&zero, NULL, location);
mpz_clear(zero);
}
start = Expression::make_integer_ul(0, NULL, location);
Expression* end = NULL;
if (this->peek_token()->is_op(OPERATOR_COLON))
......
......@@ -397,12 +397,8 @@ Type*
Runtime::map_iteration_type()
{
const unsigned long map_iteration_size = 4;
mpz_t ival;
mpz_init_set_ui(ival, map_iteration_size);
Expression* iexpr = Expression::make_integer(&ival, NULL,
Expression* iexpr =
Expression::make_integer_ul(map_iteration_size, NULL,
Linemap::predeclared_location());
mpz_clear(ival);
return Type::make_array_type(runtime_function_type(RFT_POINTER), iexpr);
}
......@@ -1838,12 +1838,7 @@ Statement*
Inc_dec_statement::do_lower(Gogo*, Named_object*, Block*, Statement_inserter*)
{
Location loc = this->location();
mpz_t oval;
mpz_init_set_ui(oval, 1UL);
Expression* oexpr = Expression::make_integer(&oval, this->expr_->type(), loc);
mpz_clear(oval);
Expression* oexpr = Expression::make_integer_ul(1, this->expr_->type(), loc);
Operator op = this->is_inc_ ? OPERATOR_PLUSEQ : OPERATOR_MINUSEQ;
return Statement::make_assignment_operation(op, this->expr_, oexpr, loc);
}
......@@ -3441,7 +3436,7 @@ Case_clauses::Case_clause::get_backend(Translate_context* context,
continue;
}
go_assert(nc.type() != NULL);
e = Expression::make_integer(&ival, nc.type(), e->location());
e = Expression::make_integer_z(&ival, nc.type(), e->location());
mpz_clear(ival);
}
......@@ -4559,10 +4554,8 @@ Select_clauses::Select_clause::lower(Gogo* gogo, Named_object* function,
Expression* selref = Expression::make_temporary_reference(sel, loc);
mpz_t ival;
mpz_init_set_ui(ival, this->index_);
Expression* index_expr = Expression::make_integer(&ival, NULL, loc);
mpz_clear(ival);
Expression* index_expr = Expression::make_integer_ul(this->index_, NULL,
loc);
if (this->is_default_)
{
......@@ -4907,11 +4900,8 @@ Select_clauses::get_backend(Translate_context* context,
++p, ++i)
{
int index = p->index();
mpz_t ival;
mpz_init_set_ui(ival, index);
Expression* index_expr = Expression::make_integer(&ival, int32_type,
Expression* index_expr = Expression::make_integer_ul(index, int32_type,
location);
mpz_clear(ival);
cases[i].push_back(index_expr->get_backend(context));
Bstatement* s = p->get_statements_backend(context);
......@@ -4993,11 +4983,8 @@ Select_statement::do_lower(Gogo* gogo, Named_object* function,
go_assert(this->sel_ == NULL);
mpz_t ival;
mpz_init_set_ui(ival, this->clauses_->size());
Expression* size_expr = Expression::make_integer(&ival, NULL, loc);
mpz_clear(ival);
Expression* size_expr = Expression::make_integer_ul(this->clauses_->size(),
NULL, loc);
Expression* call = Runtime::make_call(Runtime::NEWSELECT, loc, 1, size_expr);
this->sel_ = Statement::make_temporary(NULL, call, loc);
......@@ -5488,10 +5475,7 @@ For_range_statement::lower_range_array(Gogo* gogo,
len_call, loc);
init->add_statement(len_temp);
mpz_t zval;
mpz_init_set_ui(zval, 0UL);
Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
mpz_clear(zval);
Expression* zexpr = Expression::make_integer_ul(0, NULL, loc);
Temporary_reference_expression* tref =
Expression::make_temporary_reference(index_temp, loc);
......@@ -5589,10 +5573,7 @@ For_range_statement::lower_range_slice(Gogo* gogo,
len_call, loc);
init->add_statement(len_temp);
mpz_t zval;
mpz_init_set_ui(zval, 0UL);
Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
mpz_clear(zval);
Expression* zexpr = Expression::make_integer_ul(0, NULL, loc);
Temporary_reference_expression* tref =
Expression::make_temporary_reference(index_temp, loc);
......@@ -5681,9 +5662,7 @@ For_range_statement::lower_range_string(Gogo*,
Statement::make_temporary(index_temp->type(), NULL, loc);
init->add_statement(next_index_temp);
mpz_t zval;
mpz_init_set_ui(zval, 0UL);
Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
Expression* zexpr = Expression::make_integer_ul(0, NULL, loc);
Temporary_reference_expression* ref =
Expression::make_temporary_reference(index_temp, loc);
......@@ -5742,8 +5721,7 @@ For_range_statement::lower_range_string(Gogo*,
iter_init->add_statement(s);
ref = Expression::make_temporary_reference(next_index_temp, loc);
zexpr = Expression::make_integer(&zval, NULL, loc);
mpz_clear(zval);
zexpr = Expression::make_integer_ul(0, NULL, loc);
Expression* equals = Expression::make_binary(OPERATOR_EQEQ, ref, zexpr, loc);
Block* then_block = new Block(iter_init, loc);
......@@ -5823,18 +5801,11 @@ For_range_statement::lower_range_map(Gogo*,
// hiter[0] != nil
ref = Expression::make_temporary_reference(hiter, loc);
mpz_t zval;
mpz_init_set_ui(zval, 0UL);
Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
mpz_clear(zval);
Expression* zexpr = Expression::make_integer_ul(0, NULL, loc);
Expression* index = Expression::make_index(ref, zexpr, NULL, NULL, loc);
Expression* ne = Expression::make_binary(OPERATOR_NOTEQ, index,
Expression::make_nil(loc),
loc);
*pcond = ne;
// Set *PITER_INIT to
......
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