Add case statement
This commit is contained in:
parent
037ce08f72
commit
0caf4fd9d0
|
|
@ -181,6 +181,7 @@ static vhdl_expr *translate_binary(ivl_expr_t e)
|
|||
*/
|
||||
vhdl_expr *translate_expr(ivl_expr_t e)
|
||||
{
|
||||
assert(e);
|
||||
ivl_expr_type_t type = ivl_expr_type(e);
|
||||
|
||||
switch (type) {
|
||||
|
|
|
|||
|
|
@ -388,6 +388,30 @@ static int draw_if(vhdl_process *proc, stmt_container *container,
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int draw_case(vhdl_process *proc, stmt_container *container,
|
||||
ivl_statement_t stmt)
|
||||
{
|
||||
vhdl_expr *test = translate_expr(ivl_stmt_cond_expr(stmt));
|
||||
if (NULL == test)
|
||||
return 1;
|
||||
|
||||
vhdl_case_stmt *vhdlcase = new vhdl_case_stmt(test);
|
||||
container->add_stmt(vhdlcase);
|
||||
|
||||
int nbranches = ivl_stmt_case_count(stmt);
|
||||
for (int i = 0; i < nbranches; i++) {
|
||||
vhdl_expr *when = translate_expr(ivl_stmt_case_expr(stmt, i));
|
||||
if (NULL == when)
|
||||
return 1;
|
||||
|
||||
vhdl_case_branch *branch = new vhdl_case_branch(when);
|
||||
|
||||
draw_stmt(proc, branch->get_container(), ivl_stmt_case_stmt(stmt, i));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Generate VHDL statements for the given Verilog statement and
|
||||
* add them to the given VHDL process. The container is the
|
||||
|
|
@ -397,6 +421,8 @@ static int draw_if(vhdl_process *proc, stmt_container *container,
|
|||
int draw_stmt(vhdl_process *proc, stmt_container *container,
|
||||
ivl_statement_t stmt)
|
||||
{
|
||||
assert(stmt);
|
||||
|
||||
switch (ivl_statement_type(stmt)) {
|
||||
case IVL_ST_STASK:
|
||||
return draw_stask(proc, container, stmt);
|
||||
|
|
@ -415,6 +441,8 @@ int draw_stmt(vhdl_process *proc, stmt_container *container,
|
|||
return draw_wait(proc, container, stmt);
|
||||
case IVL_ST_CONDIT:
|
||||
return draw_if(proc, container, stmt);
|
||||
case IVL_ST_CASE:
|
||||
return draw_case(proc, container, stmt);
|
||||
default:
|
||||
error("No VHDL translation for statement at %s:%d (type = %d)",
|
||||
ivl_stmt_file(stmt), ivl_stmt_lineno(stmt),
|
||||
|
|
|
|||
|
|
@ -748,6 +748,34 @@ void vhdl_binop_expr::emit(std::ofstream &of, int level) const
|
|||
of << ")";
|
||||
}
|
||||
|
||||
vhdl_case_branch::~vhdl_case_branch()
|
||||
{
|
||||
delete when_;
|
||||
}
|
||||
|
||||
void vhdl_case_branch::emit(std::ofstream &of, int level) const
|
||||
{
|
||||
of << "when ";
|
||||
when_->emit(of, level);
|
||||
of << " =>";
|
||||
newline(of, indent(level));
|
||||
stmts_.emit(of, indent(level));
|
||||
}
|
||||
|
||||
vhdl_case_stmt::~vhdl_case_stmt()
|
||||
{
|
||||
delete test_;
|
||||
}
|
||||
|
||||
void vhdl_case_stmt::emit(std::ofstream &of, int level) const
|
||||
{
|
||||
of << "case ";
|
||||
test_->emit(of, level);
|
||||
of << " is";
|
||||
emit_children<vhdl_case_branch>(of, branches_, level);
|
||||
of << "end case;";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -333,6 +333,37 @@ private:
|
|||
};
|
||||
|
||||
|
||||
/*
|
||||
* A single branch in a case statement consisting of an
|
||||
* expression part and a statement container.
|
||||
*/
|
||||
class vhdl_case_branch : public vhdl_element {
|
||||
public:
|
||||
vhdl_case_branch(vhdl_expr *when) : when_(when) {}
|
||||
~vhdl_case_branch();
|
||||
|
||||
stmt_container *get_container() { return &stmts_; }
|
||||
void emit(std::ofstream &of, int level) const;
|
||||
private:
|
||||
vhdl_expr *when_;
|
||||
stmt_container stmts_;
|
||||
};
|
||||
|
||||
typedef std::list<vhdl_case_branch*> case_branch_list_t;
|
||||
|
||||
class vhdl_case_stmt : public vhdl_seq_stmt {
|
||||
public:
|
||||
vhdl_case_stmt(vhdl_expr *test) : test_(test) {}
|
||||
~vhdl_case_stmt();
|
||||
|
||||
void add_branch(vhdl_case_branch *b) { branches_.push_back(b); }
|
||||
void emit(std::ofstream &of, int level) const;
|
||||
private:
|
||||
vhdl_expr *test_;
|
||||
case_branch_list_t branches_;
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* A procedure call. Which is a statement, unlike a function
|
||||
* call which is an expression.
|
||||
|
|
|
|||
Loading…
Reference in New Issue