From 3b5d56e087cbd0d4d351b531b67333a1d33fa2e1 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Mon, 9 Jun 2008 14:53:50 +0100 Subject: [PATCH] Allow n-ary expressions --- tgt-vhdl/scope.cc | 4 ++++ tgt-vhdl/vhdl_syntax.cc | 42 +++++++++++++++++++++++++++++------------ tgt-vhdl/vhdl_syntax.hh | 14 +++++++++++--- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/tgt-vhdl/scope.cc b/tgt-vhdl/scope.cc index 1ff2f5031..18c3a7679 100644 --- a/tgt-vhdl/scope.cc +++ b/tgt-vhdl/scope.cc @@ -53,6 +53,10 @@ static vhdl_var_ref *nexus_to_var_ref(vhdl_arch *arch, ivl_nexus_t nexus) assert(false); } +/* + * Convert the inputs of a logic gate to a binary expression. + */ + /* * Translate all the primitive logic gates into concurrent * signal assignments. diff --git a/tgt-vhdl/vhdl_syntax.cc b/tgt-vhdl/vhdl_syntax.cc index bcd1751b9..572667f74 100644 --- a/tgt-vhdl/vhdl_syntax.cc +++ b/tgt-vhdl/vhdl_syntax.cc @@ -471,29 +471,47 @@ void vhdl_unaryop_expr::emit(std::ofstream &of, int level) const operand_->emit(of, level); } +vhdl_binop_expr::vhdl_binop_expr(vhdl_expr *left, vhdl_binop_t op, + vhdl_expr *right, vhdl_type *type) + : vhdl_expr(type), op_(op) +{ + add_expr(left); + add_expr(right); +} + vhdl_binop_expr::~vhdl_binop_expr() { - delete left_; - delete right_; + delete_children(operands_); +} + +void vhdl_binop_expr::add_expr(vhdl_expr *e) +{ + operands_.push_back(e); } void vhdl_binop_expr::emit(std::ofstream &of, int level) const { // Expressions are fully parenthesized to remove any // ambiguity in the output + of << "("; - right_->emit(of, level); - switch (op_) { - case VHDL_BINOP_AND: - of << " and "; - break; - case VHDL_BINOP_OR: - of << " or "; - break; - } + assert(operands_.size() > 0); + std::list::const_iterator it = operands_.begin(); + + do { + (*it)->emit(of, level); + + switch (op_) { + case VHDL_BINOP_AND: + of << " and "; + break; + case VHDL_BINOP_OR: + of << " or "; + break; + } + } while (++it != operands_.end()); - right_->emit(of, level); of << ")"; } diff --git a/tgt-vhdl/vhdl_syntax.hh b/tgt-vhdl/vhdl_syntax.hh index 9b9c9a9cc..12f14cfff 100644 --- a/tgt-vhdl/vhdl_syntax.hh +++ b/tgt-vhdl/vhdl_syntax.hh @@ -58,16 +58,24 @@ enum vhdl_binop_t { VHDL_BINOP_OR, }; +/* + * A binary expression contains a list of operands rather + * than just two: this is to model n-input gates and the + * like. A second constructor is provided to handle the + * common case of a true binary expression. + */ class vhdl_binop_expr : public vhdl_expr { public: + vhdl_binop_expr(vhdl_binop_t op, vhdl_type *type) + : vhdl_expr(type), op_(op) {} vhdl_binop_expr(vhdl_expr *left, vhdl_binop_t op, - vhdl_expr *right, vhdl_type *type) - : vhdl_expr(type), left_(left), right_(right), op_(op) {} + vhdl_expr *right, vhdl_type *type); ~vhdl_binop_expr(); + void add_expr(vhdl_expr *e); void emit(std::ofstream &of, int level) const; private: - vhdl_expr *left_, *right_; + std::list operands_; vhdl_binop_t op_; };