From 039e6014fe095f782c5ca11a5473b7943fa94b96 Mon Sep 17 00:00:00 2001 From: Stephen Williams Date: Sat, 28 Apr 2012 18:37:22 -0700 Subject: [PATCH] Rework VType::emit_def methods / use packed arrays to implement arrays. VHDL arrays are more like SV packed arrays, so use packed arrays to implement them. --- vhdlpp/vtype.h | 32 +++-------- vhdlpp/vtype_emit.cc | 124 ++++++++++++++++--------------------------- 2 files changed, 54 insertions(+), 102 deletions(-) diff --git a/vhdlpp/vtype.h b/vhdlpp/vtype.h index ac15a68b3..fa0d0058f 100644 --- a/vhdlpp/vtype.h +++ b/vhdlpp/vtype.h @@ -52,13 +52,13 @@ class VType { // This virtual method emits a definition for the specific // type. It is used to emit typedef's. - virtual int emit_def(std::ostream&out, perm_string name) const =0; + virtual int emit_def(std::ostream&out) const =0; private: friend class decl_t; // This virtual method is called to emit the declaration. This // is used by the decl_t object to emit variable/wire/port declarations. - virtual int emit_decl(std::ostream&out, perm_string name, bool reg_flag) const =0; + virtual int emit_decl(std::ostream&out, perm_string name, bool reg_flag) const; public: // A couple places use the VType along with a few @@ -101,10 +101,7 @@ class VTypePrimitive : public VType { type_t type() const { return type_; } int emit_primitive_type(std::ostream&fd) const; - - int emit_def(std::ostream&out, perm_string name) const; - private: - int emit_decl(std::ostream&out, perm_string name, bool reg_flag) const; + int emit_def(std::ostream&out) const; private: type_t type_; @@ -155,10 +152,7 @@ class VTypeArray : public VType { const VType* element_type() const; - int emit_def(std::ostream&out, perm_string name) const; - - private: - int emit_decl(std::ostream&out, perm_string name, bool reg_flag) const; + int emit_def(std::ostream&out) const; private: const VType*etype_; @@ -174,10 +168,7 @@ class VTypeRange : public VType { ~VTypeRange(); void write_to_stream(std::ostream&fd) const; - - int emit_def(std::ostream&out, perm_string name) const; - private: - int emit_decl(std::ostream&out, perm_string name, bool reg_flag) const; + int emit_def(std::ostream&out) const; private: const VType*base_; @@ -191,10 +182,7 @@ class VTypeEnum : public VType { ~VTypeEnum(); void show(std::ostream&) const; - - int emit_def(std::ostream&out, perm_string name) const; - private: - int emit_decl(std::ostream&out, perm_string name, bool reg_flag) const; + int emit_def(std::ostream&out) const; private: std::vectornames_; @@ -227,14 +215,10 @@ class VTypeRecord : public VType { void write_to_stream(std::ostream&fd) const; void show(std::ostream&) const; - - int emit_def(std::ostream&out, perm_string name) const; + int emit_def(std::ostream&out) const; const element_t* element_by_name(perm_string name) const; - private: - int emit_decl(std::ostream&out, perm_string name, bool reg_flag) const; - private: std::vector elements_; }; @@ -247,7 +231,7 @@ class VTypeDef : public VType { int emit_typedef(std::ostream&out) const; - int emit_def(std::ostream&out, perm_string name) const; + int emit_def(std::ostream&out) const; private: int emit_decl(std::ostream&out, perm_string name, bool reg_flag) const; diff --git a/vhdlpp/vtype_emit.cc b/vhdlpp/vtype_emit.cc index 41c6dcc3b..2dbe36427 100644 --- a/vhdlpp/vtype_emit.cc +++ b/vhdlpp/vtype_emit.cc @@ -33,54 +33,54 @@ int VType::decl_t::emit(ostream&out, perm_string name) const } -int VTypeArray::emit_def(ostream&out, perm_string name) const +int VType::emit_decl(ostream&out, perm_string name, bool reg_flag) const { int errors = 0; - if (const VTypeArray*sub = dynamic_cast (etype_)) { - sub->emit_def(out, name); - assert(dimensions() == 1); - out << "["; - errors += dimension(0).msb()->emit(out, 0, 0); - out << ":"; - errors += dimension(0).lsb()->emit(out, 0, 0); - out << "] "; - return errors; + if (!reg_flag) + out << "wire "; + + errors += emit_def(out); + + out << " \\" << name << " "; + return errors; +} + +int VTypeArray::emit_def(ostream&out) const +{ + int errors = 0; + + list dims; + const VTypeArray*cur = this; + while (const VTypeArray*sub = dynamic_cast (cur->etype_)) { + dims.push_back(cur); + cur = sub; } - const VTypePrimitive*base = dynamic_cast (etype_); + const VTypePrimitive*base = dynamic_cast (cur->etype_); assert(base != 0); assert(dimensions() == 1); base->emit_primitive_type(out); if (signed_flag_) - out << "signed "; + out << " signed"; - out << "["; - errors += dimension(0).msb()->emit(out, 0, 0); - out << ":"; - errors += dimension(0).lsb()->emit(out, 0, 0); - out << "] "; + dims.push_back(cur); - out << "\\" << name << " "; + while (! dims.empty()) { + cur = dims.front(); + dims.pop_front(); + out << "["; + errors += cur->dimension(0).msb()->emit(out, 0, 0); + out << ":"; + errors += cur->dimension(0).lsb()->emit(out, 0, 0); + out << "]"; + } return errors; } -int VTypeArray::emit_decl(ostream&out, perm_string name, bool reg_flag) const -{ - int errors = 0; - assert(dimensions() == 1); - - if (!reg_flag) - out << "wire "; - - errors += emit_def(out, name); - - return errors; -} - -int VTypeEnum::emit_def(ostream&out, perm_string name) const +int VTypeEnum::emit_def(ostream&out) const { int errors = 0; out << "enum {"; @@ -89,31 +89,24 @@ int VTypeEnum::emit_def(ostream&out, perm_string name) const for (size_t idx = 1 ; idx < names_.size() ; idx += 1) out << ", \\" << names_[idx] << " "; - out << "} \\" << name << " "; + out << "}"; return errors; } -int VTypeEnum::emit_decl(ostream&out, perm_string name, bool reg_flag) const -{ - int errors = 0; - errors += emit_def(out, name); - return errors; -} - int VTypePrimitive::emit_primitive_type(ostream&out) const { int errors = 0; switch (type_) { case BOOLEAN: case BIT: - out << "bool "; + out << "bool"; break; case STDLOGIC: - out << "logic "; + out << "logic"; break; case INTEGER: - out << "bool [31:0] "; + out << "bool [31:0]"; break; default: assert(0); @@ -122,40 +115,22 @@ int VTypePrimitive::emit_primitive_type(ostream&out) const return errors; } -int VTypePrimitive::emit_def(ostream&out, perm_string name) const +int VTypePrimitive::emit_def(ostream&out) const { int errors = 0; errors += emit_primitive_type(out); - out << "\\" << name << " "; return errors; } -int VTypePrimitive::emit_decl(ostream&out, perm_string name, bool reg_flag) const -{ - int errors = 0; - if (!reg_flag) - out << "wire "; - - errors += emit_def(out, name); - - return errors; -} - -int VTypeRange::emit_def(ostream&out, perm_string name) const +int VTypeRange::emit_def(ostream&out) const { int errors = 0; assert(0); + out << "/* Internal error: Don't know how to emit type */"; return errors; } -int VTypeRange::emit_decl(ostream&out, perm_string name, bool reg_flag) const -{ - int errors = 0; - assert(0); - return errors; -} - -int VTypeRecord::emit_def(ostream&out, perm_string name) const +int VTypeRecord::emit_def(ostream&out) const { int errors = 0; out << "struct packed {"; @@ -164,22 +139,15 @@ int VTypeRecord::emit_def(ostream&out, perm_string name) const ; cur != elements_.end() ; ++cur) { perm_string element_name = (*cur)->peek_name(); const VType*element_type = (*cur)->peek_type(); - element_type->emit_def(out, element_name); - out << "; "; + element_type->emit_def(out); + out << " \\" << element_name << " ; "; } - out << "} \\" << name << " "; + out << "}"; return errors; } -int VTypeRecord::emit_decl(ostream&out, perm_string name, bool reg_flag) const -{ - int errors = 0; - errors += emit_def(out, name); - return errors; -} - -int VTypeDef::emit_def(ostream&out, perm_string name) const +int VTypeDef::emit_def(ostream&) const { int errors = 0; assert(0); @@ -202,7 +170,7 @@ int VTypeDef::emit_typedef(ostream&out) const { int errors = 0; out << "typedef "; - errors += type_->emit_def(out, name_); - out << ";" << endl; + errors += type_->emit_def(out); + out << " \\" << name_ << " ;" << endl; return errors; }