Commit 0aa5e7f2 by Evan Shaw Committed by Ian Lance Taylor

Use backend interface for basic types

	* go-gcc.c (class Gcc_tree): Make get_tree const.
	(Gcc_backend::void_type): Implement.
	(Gcc_backend::bool_type): Implement.
	(Gcc_backend::integer_type): Implement.
	(Gcc_backend::float_type): Implement.
	(Gcc_backend::complex_type): New function.
	(Gcc_backend::pointer_type): New function.
	(Gcc_backend::make_type): New function.
	(type_to_tree): New function.

From-SVN: r172931
parent 5157a881
2011-04-25 Evan Shaw <edsrzf@gmail.com>
* go-gcc.c (class Gcc_tree): Make get_tree const.
(Gcc_backend::void_type): Implement.
(Gcc_backend::bool_type): Implement.
(Gcc_backend::integer_type): Implement.
(Gcc_backend::float_type): Implement.
(Gcc_backend::complex_type): New function.
(Gcc_backend::pointer_type): New function.
(Gcc_backend::make_type): New function.
(type_to_tree): New function.
2011-04-21 Ian Lance Taylor <iant@google.com> 2011-04-21 Ian Lance Taylor <iant@google.com>
* go-system.h (go_assert, go_unreachable): Define. * go-system.h (go_assert, go_unreachable): Define.
......
...@@ -52,7 +52,7 @@ class Gcc_tree ...@@ -52,7 +52,7 @@ class Gcc_tree
{ } { }
tree tree
get_tree() get_tree() const
{ return this->t_; } { return this->t_; }
private: private:
...@@ -133,25 +133,29 @@ class Gcc_backend : public Backend ...@@ -133,25 +133,29 @@ class Gcc_backend : public Backend
Btype* Btype*
void_type() void_type()
{ gcc_unreachable(); } { return this->make_type(void_type_node); }
Btype* Btype*
bool_type() bool_type()
{ gcc_unreachable(); } { return this->make_type(boolean_type_node); }
Btype* Btype*
integer_type(bool /* is_unsigned */, int /* bits */) integer_type(bool, int);
{ gcc_unreachable(); }
Btype* Btype*
float_type(int /* bits */) float_type(int);
{ gcc_unreachable(); }
Btype*
complex_type(int);
Btype* Btype*
string_type() string_type()
{ gcc_unreachable(); } { gcc_unreachable(); }
Btype* Btype*
pointer_type(const Btype*);
Btype*
function_type(const Function_type*, Btype* /* receiver */, function_type(const Function_type*, Btype* /* receiver */,
const Btypes* /* parameters */, const Btypes* /* parameters */,
const Btypes* /* results */) const Btypes* /* results */)
...@@ -283,6 +287,11 @@ class Gcc_backend : public Backend ...@@ -283,6 +287,11 @@ class Gcc_backend : public Backend
Bstatement* Bstatement*
make_statement(tree t) make_statement(tree t)
{ return new Bstatement(t); } { return new Bstatement(t); }
// Make a Btype from a tree.
Btype*
make_type(tree t)
{ return new Btype(t); }
}; };
// A helper function. // A helper function.
...@@ -293,6 +302,97 @@ get_identifier_from_string(const std::string& str) ...@@ -293,6 +302,97 @@ get_identifier_from_string(const std::string& str)
return get_identifier_with_length(str.data(), str.length()); return get_identifier_with_length(str.data(), str.length());
} }
// Get an unnamed integer type.
Btype*
Gcc_backend::integer_type(bool is_unsigned, int bits)
{
tree type;
if (is_unsigned)
{
if (bits == INT_TYPE_SIZE)
type = unsigned_type_node;
else if (bits == CHAR_TYPE_SIZE)
type = unsigned_char_type_node;
else if (bits == SHORT_TYPE_SIZE)
type = short_unsigned_type_node;
else if (bits == LONG_TYPE_SIZE)
type = long_unsigned_type_node;
else if (bits == LONG_LONG_TYPE_SIZE)
type = long_long_unsigned_type_node;
else
type = make_unsigned_type(bits);
}
else
{
if (bits == INT_TYPE_SIZE)
type = integer_type_node;
else if (bits == CHAR_TYPE_SIZE)
type = signed_char_type_node;
else if (bits == SHORT_TYPE_SIZE)
type = short_integer_type_node;
else if (bits == LONG_TYPE_SIZE)
type = long_integer_type_node;
else if (bits == LONG_LONG_TYPE_SIZE)
type = long_long_integer_type_node;
else
type = make_signed_type(bits);
}
return this->make_type(type);
}
// Get an unnamed float type.
Btype*
Gcc_backend::float_type(int bits)
{
tree type;
if (bits == FLOAT_TYPE_SIZE)
type = float_type_node;
else if (bits == DOUBLE_TYPE_SIZE)
type = double_type_node;
else if (bits == LONG_DOUBLE_TYPE_SIZE)
type = long_double_type_node;
else
{
type = make_node(REAL_TYPE);
TYPE_PRECISION(type) = bits;
layout_type(type);
}
return this->make_type(type);
}
// Get an unnamed complex type.
Btype*
Gcc_backend::complex_type(int bits)
{
tree type;
if (bits == FLOAT_TYPE_SIZE * 2)
type = complex_float_type_node;
else if (bits == DOUBLE_TYPE_SIZE * 2)
type = complex_double_type_node;
else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
type = complex_long_double_type_node;
else
{
type = make_node(REAL_TYPE);
TYPE_PRECISION(type) = bits / 2;
layout_type(type);
type = build_complex_type(type);
}
return this->make_type(type);
}
// Get a pointer type.
Btype*
Gcc_backend::pointer_type(const Btype* to_type)
{
tree type = build_pointer_type(to_type->get_tree());
return this->make_type(type);
}
// An expression as a statement. // An expression as a statement.
Bstatement* Bstatement*
...@@ -867,6 +967,12 @@ tree_to_block(tree t) ...@@ -867,6 +967,12 @@ tree_to_block(tree t)
} }
tree tree
type_to_tree(Btype* bt)
{
return bt->get_tree();
}
tree
expr_to_tree(Bexpression* be) expr_to_tree(Bexpression* be)
{ {
return be->get_tree(); return be->get_tree();
......
...@@ -73,10 +73,18 @@ class Backend ...@@ -73,10 +73,18 @@ class Backend
virtual Btype* virtual Btype*
float_type(int bits) = 0; float_type(int bits) = 0;
// Get an unnamed complex type with the given number of bits.
virtual Btype*
complex_type(int bits) = 0;
// Get the unnamed string type. // Get the unnamed string type.
virtual Btype* virtual Btype*
string_type() = 0; string_type() = 0;
// Get a pointer type.
virtual Btype*
pointer_type(const Btype* to_type) = 0;
// Get a function type. The receiver, parameter, and results are // Get a function type. The receiver, parameter, and results are
// generated from the types in the Function_type. The Function_type // generated from the types in the Function_type. The Function_type
// is provided so that the names are available. // is provided so that the names are available.
...@@ -299,6 +307,7 @@ extern Bexpression* tree_to_expr(tree); ...@@ -299,6 +307,7 @@ extern Bexpression* tree_to_expr(tree);
extern Bstatement* tree_to_stat(tree); extern Bstatement* tree_to_stat(tree);
extern Bfunction* tree_to_function(tree); extern Bfunction* tree_to_function(tree);
extern Bblock* tree_to_block(tree); extern Bblock* tree_to_block(tree);
extern tree type_to_tree(Btype*);
extern tree expr_to_tree(Bexpression*); extern tree expr_to_tree(Bexpression*);
extern tree stat_to_tree(Bstatement*); extern tree stat_to_tree(Bstatement*);
extern tree block_to_tree(Bblock*); extern tree block_to_tree(Bblock*);
......
...@@ -31,6 +31,7 @@ extern "C" ...@@ -31,6 +31,7 @@ extern "C"
#include "statements.h" #include "statements.h"
#include "export.h" #include "export.h"
#include "import.h" #include "import.h"
#include "backend.h"
#include "types.h" #include "types.h"
// Class Type. // Class Type.
...@@ -1622,8 +1623,11 @@ class Void_type : public Type ...@@ -1622,8 +1623,11 @@ class Void_type : public Type
protected: protected:
tree tree
do_get_tree(Gogo*) do_get_tree(Gogo* gogo)
{ return void_type_node; } {
Btype* btype = gogo->backend()->void_type();
return type_to_tree(btype);
}
tree tree
do_get_init_tree(Gogo*, tree, bool) do_get_init_tree(Gogo*, tree, bool)
...@@ -1660,8 +1664,11 @@ class Boolean_type : public Type ...@@ -1660,8 +1664,11 @@ class Boolean_type : public Type
protected: protected:
tree tree
do_get_tree(Gogo*) do_get_tree(Gogo* gogo)
{ return boolean_type_node; } {
Btype* btype = gogo->backend()->bool_type();
return type_to_tree(btype);
}
tree tree
do_get_init_tree(Gogo*, tree type_tree, bool is_clear) do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
...@@ -1806,36 +1813,10 @@ Integer_type::do_get_tree(Gogo*) ...@@ -1806,36 +1813,10 @@ Integer_type::do_get_tree(Gogo*)
return error_mark_node; return error_mark_node;
} }
if (this->is_unsigned_) // FIXME: GOGO can be NULL when called from go_type_for_size, so call
{ // go_get_backend() instead of gogo->backend().
if (this->bits_ == INT_TYPE_SIZE) Btype* btype = go_get_backend()->integer_type(this->is_unsigned_, this->bits_);
return unsigned_type_node; return type_to_tree(btype);
else if (this->bits_ == CHAR_TYPE_SIZE)
return unsigned_char_type_node;
else if (this->bits_ == SHORT_TYPE_SIZE)
return short_unsigned_type_node;
else if (this->bits_ == LONG_TYPE_SIZE)
return long_unsigned_type_node;
else if (this->bits_ == LONG_LONG_TYPE_SIZE)
return long_long_unsigned_type_node;
else
return make_unsigned_type(this->bits_);
}
else
{
if (this->bits_ == INT_TYPE_SIZE)
return integer_type_node;
else if (this->bits_ == CHAR_TYPE_SIZE)
return signed_char_type_node;
else if (this->bits_ == SHORT_TYPE_SIZE)
return short_integer_type_node;
else if (this->bits_ == LONG_TYPE_SIZE)
return long_integer_type_node;
else if (this->bits_ == LONG_LONG_TYPE_SIZE)
return long_long_integer_type_node;
else
return make_signed_type(this->bits_);
}
} }
tree tree
...@@ -1968,19 +1949,8 @@ Float_type::do_hash_for_method(Gogo*) const ...@@ -1968,19 +1949,8 @@ Float_type::do_hash_for_method(Gogo*) const
tree tree
Float_type::type_tree() const Float_type::type_tree() const
{ {
if (this->bits_ == FLOAT_TYPE_SIZE) Btype* btype = go_get_backend()->float_type(this->bits_);
return float_type_node; return type_to_tree(btype);
else if (this->bits_ == DOUBLE_TYPE_SIZE)
return double_type_node;
else if (this->bits_ == LONG_DOUBLE_TYPE_SIZE)
return long_double_type_node;
else
{
tree ret = make_node(REAL_TYPE);
TYPE_PRECISION(ret) = this->bits_;
layout_type(ret);
return ret;
}
} }
// Get a tree. // Get a tree.
...@@ -2124,19 +2094,8 @@ Complex_type::do_hash_for_method(Gogo*) const ...@@ -2124,19 +2094,8 @@ Complex_type::do_hash_for_method(Gogo*) const
tree tree
Complex_type::type_tree() const Complex_type::type_tree() const
{ {
if (this->bits_ == FLOAT_TYPE_SIZE * 2) Btype* btype = go_get_backend()->complex_type(this->bits_);
return complex_float_type_node; return type_to_tree(btype);
else if (this->bits_ == DOUBLE_TYPE_SIZE * 2)
return complex_double_type_node;
else if (this->bits_ == LONG_DOUBLE_TYPE_SIZE * 2)
return complex_long_double_type_node;
else
{
tree ret = make_node(REAL_TYPE);
TYPE_PRECISION(ret) = this->bits_ / 2;
layout_type(ret);
return build_complex_type(ret);
}
} }
// Get a tree. // Get a tree.
...@@ -3136,7 +3095,9 @@ Pointer_type::do_hash_for_method(Gogo* gogo) const ...@@ -3136,7 +3095,9 @@ Pointer_type::do_hash_for_method(Gogo* gogo) const
tree tree
Pointer_type::do_get_tree(Gogo* gogo) Pointer_type::do_get_tree(Gogo* gogo)
{ {
return build_pointer_type(this->to_type_->get_tree(gogo)); Btype* to_btype = tree_to_type(this->to_type_->get_tree(gogo));
Btype* btype = gogo->backend()->pointer_type(to_btype);
return type_to_tree(btype);
} }
// Initialize a pointer type. // Initialize a pointer type.
......
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