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