From 4b4a1c6cac77d4b64b820554a751c4da8b803139 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 8 Jun 2008 12:55:18 +0100 Subject: [PATCH] Tidy up type casting --- tgt-vhdl/vhdl_element.cc | 76 +++++++++++++++------------------------- tgt-vhdl/vhdl_element.hh | 1 + 2 files changed, 30 insertions(+), 47 deletions(-) diff --git a/tgt-vhdl/vhdl_element.cc b/tgt-vhdl/vhdl_element.cc index fdc47b76f..216d04d16 100644 --- a/tgt-vhdl/vhdl_element.cc +++ b/tgt-vhdl/vhdl_element.cc @@ -388,53 +388,6 @@ void vhdl_type::emit(std::ofstream &of, int level) const of << get_string(); } -/* - * The default cast just assumes there's a VHDL cast function to - * do the job for us. - */ -vhdl_expr *vhdl_expr::cast(const vhdl_type *to) -{ - vhdl_fcall *conv = - new vhdl_fcall(to->get_string().c_str(), new vhdl_type(*to)); - conv->add_expr(this); - - return conv; -} - -/* - * Cast something to a scalar type. There are a few ugly hacks here - * to handle special cases. - */ -/*vhdl_expr *vhdl_scalar_type::cast(vhdl_expr *expr) const -{ - if (typeid(*expr) == typeid(vhdl_const_bits) - && name_ == "std_logic") { - - // Converting a literal bit string to std_logic is fairly - // common so this hack is justified by the increase in - // output readability :-) - - const std::string &value = - dynamic_cast(expr)->get_value(); - - // Take the least significant bit - char lsb = value[0]; - - // Discard the given expression and return a brand new one - delete expr; - return new vhdl_const_bit(lsb); - } - else { - // Otherwise just assume there's a pre-defined conversion - const char *c_name = name_.c_str(); - vhdl_fcall *conv = - new vhdl_fcall(c_name, new vhdl_scalar_type(c_name)); - conv->add_expr(expr); - - return conv; - } - }*/ - vhdl_type *vhdl_type::std_logic_vector(int msb, int lsb) { return new vhdl_type(VHDL_TYPE_STD_LOGIC_VECTOR, msb, lsb); @@ -471,6 +424,19 @@ vhdl_expr::~vhdl_expr() delete type_; } +/* + * The default cast just assumes there's a VHDL cast function to + * do the job for us. + */ +vhdl_expr *vhdl_expr::cast(const vhdl_type *to) +{ + vhdl_fcall *conv = + new vhdl_fcall(to->get_string().c_str(), new vhdl_type(*to)); + conv->add_expr(this); + + return conv; +} + void vhdl_expr_list::add_expr(vhdl_expr *e) { exprs_.push_back(e); @@ -543,6 +509,22 @@ vhdl_const_bits::vhdl_const_bits(const char *value) } +vhdl_expr *vhdl_const_bits::cast(const vhdl_type *to) +{ + if (to->get_name() == VHDL_TYPE_STD_LOGIC) { + // VHDL won't let us cast directly between a vector and + // a scalar type + // But we don't need to here as we have the bits available + + // Take the least significant bit + char lsb = value_[0]; + + return new vhdl_const_bit(lsb); + } + else + return vhdl_expr::cast(to); +} + void vhdl_const_bits::emit(std::ofstream &of, int level) const { of << "std_logic_vector'(\""; diff --git a/tgt-vhdl/vhdl_element.hh b/tgt-vhdl/vhdl_element.hh index 5454fd6ed..c657039f4 100644 --- a/tgt-vhdl/vhdl_element.hh +++ b/tgt-vhdl/vhdl_element.hh @@ -127,6 +127,7 @@ public: vhdl_const_bits(const char *value); void emit(std::ofstream &of, int level) const; const std::string &get_value() const { return value_; } + vhdl_expr *cast(const vhdl_type *to); private: std::string value_; };