From 8e0bf3ebff3027a53274c87978893982985c30f7 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 10 Aug 2008 11:22:23 +0100 Subject: [PATCH] Add conversion from std_logic to (un)signed types Implemented using the expression (0 => X, others => '0') --- tgt-vhdl/cast.cc | 9 +++++++++ tgt-vhdl/vhdl_syntax.cc | 31 +++++++++++++++++++++++++++++++ tgt-vhdl/vhdl_syntax.hh | 21 +++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/tgt-vhdl/cast.cc b/tgt-vhdl/cast.cc index 167efc4d2..43fcf6aaf 100644 --- a/tgt-vhdl/cast.cc +++ b/tgt-vhdl/cast.cc @@ -83,6 +83,15 @@ vhdl_expr *vhdl_expr::cast(const vhdl_type *to) return conv; } + else if ((to->get_name() == VHDL_TYPE_UNSIGNED + || to->get_name() == VHDL_TYPE_SIGNED) && + type_->get_name() == VHDL_TYPE_STD_LOGIC) { + vhdl_bit_spec_expr *bs = + new vhdl_bit_spec_expr(new vhdl_type(*to), new vhdl_const_bit('0')); + bs->add_bit(0, this); + + return bs; + } else if (to->get_name() == VHDL_TYPE_STD_LOGIC && type_->get_name() == VHDL_TYPE_BOOLEAN) { require_support_function(SF_BOOLEAN_TO_LOGIC); diff --git a/tgt-vhdl/vhdl_syntax.cc b/tgt-vhdl/vhdl_syntax.cc index 4cb29c66e..c96ca6ffa 100644 --- a/tgt-vhdl/vhdl_syntax.cc +++ b/tgt-vhdl/vhdl_syntax.cc @@ -735,6 +735,37 @@ void vhdl_binop_expr::emit(std::ostream &of, int level) const of << ")"; } +vhdl_bit_spec_expr::~vhdl_bit_spec_expr() +{ + delete others_; + + std::list::iterator it; + for (it = bits_.begin(); it != bits_.end(); ++it) + delete (*it).e; +} + +void vhdl_bit_spec_expr::add_bit(int bit, vhdl_expr *e) +{ + bit_map bm = { bit, e }; + bits_.push_back(bm); +} + +void vhdl_bit_spec_expr::emit(std::ostream &of, int level) const +{ + of << "("; + + std::list::const_iterator it; + for (it = bits_.begin(); it != bits_.end(); ++it) { + of << (*it).bit << " => "; + (*it).e->emit(of, level); + of << ", "; + } + + of << "others => "; + others_->emit(of, level); + of << ")"; +} + vhdl_case_branch::~vhdl_case_branch() { delete when_; diff --git a/tgt-vhdl/vhdl_syntax.hh b/tgt-vhdl/vhdl_syntax.hh index a9fd93024..855173ab6 100644 --- a/tgt-vhdl/vhdl_syntax.hh +++ b/tgt-vhdl/vhdl_syntax.hh @@ -129,6 +129,27 @@ private: }; +/* + * An expression like (0 => '1', 2 => '0', others => 'Z') + */ +class vhdl_bit_spec_expr : public vhdl_expr { +public: + vhdl_bit_spec_expr(vhdl_type *type, vhdl_expr *others) + : vhdl_expr(type), others_(others) {} + ~vhdl_bit_spec_expr(); + + void add_bit(int bit, vhdl_expr *e); + void emit(std::ostream &of, int level) const; +private: + vhdl_expr *others_; + struct bit_map { + int bit; + vhdl_expr *e; + }; + std::list bits_; +}; + + class vhdl_const_string : public vhdl_expr { public: vhdl_const_string(const char *value)