From d1314d53bd79d9ce07bec11e1b9f67c6c50a1e72 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 15 Aug 2010 19:05:27 +0100 Subject: [PATCH] Reduce superflous parens in generated VHDL Purely cosmetic, replaces output like: if (x + foo(x + (2 * y))) then ... With: if x + foo(x + (2 * y)) then ... --- tgt-vhdl/vhdl_syntax.cc | 29 ++++++++++++++++++++++------- tgt-vhdl/vhdl_syntax.hh | 5 +++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/tgt-vhdl/vhdl_syntax.cc b/tgt-vhdl/vhdl_syntax.cc index 0391b0137..60bd1a412 100644 --- a/tgt-vhdl/vhdl_syntax.cc +++ b/tgt-vhdl/vhdl_syntax.cc @@ -767,6 +767,22 @@ void vhdl_if_stmt::emit(std::ostream &of, int level) const of << "end if;"; } +int vhdl_expr::paren_levels(0); + +void vhdl_expr::open_parens(std::ostream& of) +{ + if (paren_levels++ > 0) + of << "("; +} + +void vhdl_expr::close_parens(std::ostream& of) +{ + assert(paren_levels > 0); + + if (--paren_levels > 0) + of << ")"; +} + vhdl_unaryop_expr::~vhdl_unaryop_expr() { @@ -774,7 +790,8 @@ vhdl_unaryop_expr::~vhdl_unaryop_expr() void vhdl_unaryop_expr::emit(std::ostream &of, int level) const { - of << "("; + open_parens(of); + switch (op_) { case VHDL_UNARYOP_NOT: of << "not "; @@ -784,7 +801,8 @@ void vhdl_unaryop_expr::emit(std::ostream &of, int level) const break; } operand_->emit(of, level); - of << ")"; + + close_parens(of); } vhdl_binop_expr::vhdl_binop_expr(vhdl_expr *left, vhdl_binop_t op, @@ -807,10 +825,7 @@ void vhdl_binop_expr::add_expr(vhdl_expr *e) void vhdl_binop_expr::emit(std::ostream &of, int level) const { - // Expressions are fully parenthesised to remove any - // ambiguity in the output - - of << "("; + open_parens(of); assert(operands_.size() > 0); std::list::const_iterator it = operands_.begin(); @@ -828,7 +843,7 @@ void vhdl_binop_expr::emit(std::ostream &of, int level) const (*it)->emit(of, level); } - of << ")"; + close_parens(of); } vhdl_bit_spec_expr::~vhdl_bit_spec_expr() diff --git a/tgt-vhdl/vhdl_syntax.hh b/tgt-vhdl/vhdl_syntax.hh index 2d792a6a8..8603e7090 100644 --- a/tgt-vhdl/vhdl_syntax.hh +++ b/tgt-vhdl/vhdl_syntax.hh @@ -48,7 +48,12 @@ public: virtual vhdl_expr *to_std_logic(); virtual vhdl_expr *to_std_ulogic(); virtual vhdl_expr *to_vector(vhdl_type_name_t name, int w); + protected: + static void open_parens(ostream& of); + static void close_parens(ostream& of); + static int paren_levels; + const vhdl_type *type_; bool isconst_; };