Convert `if (foo) ..' to `if foo = '1' then ..'

This commit is contained in:
Nick Gasson 2008-06-12 11:36:21 +01:00
parent 8fe2211e2b
commit 0df3eabe26
3 changed files with 29 additions and 7 deletions

View File

@ -420,11 +420,21 @@ vhdl_expr::~vhdl_expr()
*/
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;
if (to->get_name() == type_->get_name())
return this;
else if (to->get_name() == VHDL_TYPE_BOOLEAN) {
// '1' is true all else are false
vhdl_const_bit *one = new vhdl_const_bit('1');
return new vhdl_binop_expr
(this, VHDL_BINOP_EQ, one, vhdl_type::boolean());
}
else {
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)
@ -572,6 +582,13 @@ void vhdl_assert_stmt::emit(std::ofstream &of, int level) const
of << " report \"" << reason_ << "\" severity failure;";
}
vhdl_if_stmt::vhdl_if_stmt(vhdl_expr *test)
{
// Need to ensure that the expression is Boolean
vhdl_type boolean(VHDL_TYPE_BOOLEAN);
test_ = test->cast(&boolean);
}
vhdl_if_stmt::~vhdl_if_stmt()
{
delete test_;
@ -640,6 +657,9 @@ void vhdl_binop_expr::emit(std::ofstream &of, int level) const
case VHDL_BINOP_OR:
of << " or ";
break;
case VHDL_BINOP_EQ:
of << " = ";
break;
}
(*it)->emit(of, level);

View File

@ -56,6 +56,7 @@ private:
enum vhdl_binop_t {
VHDL_BINOP_AND,
VHDL_BINOP_OR,
VHDL_BINOP_EQ,
};
/*
@ -278,8 +279,7 @@ private:
class vhdl_if_stmt : public vhdl_seq_stmt {
public:
vhdl_if_stmt(vhdl_expr *test)
: test_(test) {}
vhdl_if_stmt(vhdl_expr *test);
~vhdl_if_stmt();
stmt_container *get_then_container() { return &then_part_; }

View File

@ -68,6 +68,8 @@ std::string vhdl_type::get_string() const
return std::string("File");
case VHDL_TYPE_INTEGER:
return std::string("Integer");
case VHDL_TYPE_BOOLEAN:
return std::string("Boolean");
default:
return std::string("BadType");
}