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.
This commit is contained in:
Stephen Williams 2012-04-28 18:37:22 -07:00
parent e7a974347e
commit 039e6014fe
2 changed files with 54 additions and 102 deletions

View File

@ -52,13 +52,13 @@ class VType {
// This virtual method emits a definition for the specific // This virtual method emits a definition for the specific
// type. It is used to emit typedef's. // 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: private:
friend class decl_t; friend class decl_t;
// This virtual method is called to emit the declaration. This // This virtual method is called to emit the declaration. This
// is used by the decl_t object to emit variable/wire/port declarations. // 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: public:
// A couple places use the VType along with a few // A couple places use the VType along with a few
@ -101,10 +101,7 @@ class VTypePrimitive : public VType {
type_t type() const { return type_; } type_t type() const { return type_; }
int emit_primitive_type(std::ostream&fd) const; int emit_primitive_type(std::ostream&fd) const;
int emit_def(std::ostream&out) 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;
private: private:
type_t type_; type_t type_;
@ -155,10 +152,7 @@ class VTypeArray : public VType {
const VType* element_type() const; const VType* element_type() 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;
private: private:
const VType*etype_; const VType*etype_;
@ -174,10 +168,7 @@ class VTypeRange : public VType {
~VTypeRange(); ~VTypeRange();
void write_to_stream(std::ostream&fd) const; void write_to_stream(std::ostream&fd) const;
int emit_def(std::ostream&out) 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;
private: private:
const VType*base_; const VType*base_;
@ -191,10 +182,7 @@ class VTypeEnum : public VType {
~VTypeEnum(); ~VTypeEnum();
void show(std::ostream&) const; void show(std::ostream&) const;
int emit_def(std::ostream&out) 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;
private: private:
std::vector<perm_string>names_; std::vector<perm_string>names_;
@ -227,14 +215,10 @@ class VTypeRecord : public VType {
void write_to_stream(std::ostream&fd) const; void write_to_stream(std::ostream&fd) const;
void show(std::ostream&) const; void show(std::ostream&) const;
int emit_def(std::ostream&out) const;
int emit_def(std::ostream&out, perm_string name) const;
const element_t* element_by_name(perm_string name) 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: private:
std::vector<element_t*> elements_; std::vector<element_t*> elements_;
}; };
@ -247,7 +231,7 @@ class VTypeDef : public VType {
int emit_typedef(std::ostream&out) const; 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: private:
int emit_decl(std::ostream&out, perm_string name, bool reg_flag) const; int emit_decl(std::ostream&out, perm_string name, bool reg_flag) const;

View File

@ -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; int errors = 0;
if (const VTypeArray*sub = dynamic_cast<const VTypeArray*> (etype_)) { if (!reg_flag)
sub->emit_def(out, name); out << "wire ";
assert(dimensions() == 1);
out << "["; errors += emit_def(out);
errors += dimension(0).msb()->emit(out, 0, 0);
out << ":"; out << " \\" << name << " ";
errors += dimension(0).lsb()->emit(out, 0, 0); return errors;
out << "] "; }
return errors;
int VTypeArray::emit_def(ostream&out) const
{
int errors = 0;
list<const VTypeArray*> dims;
const VTypeArray*cur = this;
while (const VTypeArray*sub = dynamic_cast<const VTypeArray*> (cur->etype_)) {
dims.push_back(cur);
cur = sub;
} }
const VTypePrimitive*base = dynamic_cast<const VTypePrimitive*> (etype_); const VTypePrimitive*base = dynamic_cast<const VTypePrimitive*> (cur->etype_);
assert(base != 0); assert(base != 0);
assert(dimensions() == 1); assert(dimensions() == 1);
base->emit_primitive_type(out); base->emit_primitive_type(out);
if (signed_flag_) if (signed_flag_)
out << "signed "; out << " signed";
out << "["; dims.push_back(cur);
errors += dimension(0).msb()->emit(out, 0, 0);
out << ":";
errors += dimension(0).lsb()->emit(out, 0, 0);
out << "] ";
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; return errors;
} }
int VTypeArray::emit_decl(ostream&out, perm_string name, bool reg_flag) const int VTypeEnum::emit_def(ostream&out) 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 errors = 0; int errors = 0;
out << "enum {"; 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) for (size_t idx = 1 ; idx < names_.size() ; idx += 1)
out << ", \\" << names_[idx] << " "; out << ", \\" << names_[idx] << " ";
out << "} \\" << name << " "; out << "}";
return errors; 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 VTypePrimitive::emit_primitive_type(ostream&out) const
{ {
int errors = 0; int errors = 0;
switch (type_) { switch (type_) {
case BOOLEAN: case BOOLEAN:
case BIT: case BIT:
out << "bool "; out << "bool";
break; break;
case STDLOGIC: case STDLOGIC:
out << "logic "; out << "logic";
break; break;
case INTEGER: case INTEGER:
out << "bool [31:0] "; out << "bool [31:0]";
break; break;
default: default:
assert(0); assert(0);
@ -122,40 +115,22 @@ int VTypePrimitive::emit_primitive_type(ostream&out) const
return errors; return errors;
} }
int VTypePrimitive::emit_def(ostream&out, perm_string name) const int VTypePrimitive::emit_def(ostream&out) const
{ {
int errors = 0; int errors = 0;
errors += emit_primitive_type(out); errors += emit_primitive_type(out);
out << "\\" << name << " ";
return errors; return errors;
} }
int VTypePrimitive::emit_decl(ostream&out, perm_string name, bool reg_flag) const int VTypeRange::emit_def(ostream&out) 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 errors = 0; int errors = 0;
assert(0); assert(0);
out << "/* Internal error: Don't know how to emit type */";
return errors; return errors;
} }
int VTypeRange::emit_decl(ostream&out, perm_string name, bool reg_flag) const int VTypeRecord::emit_def(ostream&out) const
{
int errors = 0;
assert(0);
return errors;
}
int VTypeRecord::emit_def(ostream&out, perm_string name) const
{ {
int errors = 0; int errors = 0;
out << "struct packed {"; out << "struct packed {";
@ -164,22 +139,15 @@ int VTypeRecord::emit_def(ostream&out, perm_string name) const
; cur != elements_.end() ; ++cur) { ; cur != elements_.end() ; ++cur) {
perm_string element_name = (*cur)->peek_name(); perm_string element_name = (*cur)->peek_name();
const VType*element_type = (*cur)->peek_type(); const VType*element_type = (*cur)->peek_type();
element_type->emit_def(out, element_name); element_type->emit_def(out);
out << "; "; out << " \\" << element_name << " ; ";
} }
out << "} \\" << name << " "; out << "}";
return errors; return errors;
} }
int VTypeRecord::emit_decl(ostream&out, perm_string name, bool reg_flag) const int VTypeDef::emit_def(ostream&) const
{
int errors = 0;
errors += emit_def(out, name);
return errors;
}
int VTypeDef::emit_def(ostream&out, perm_string name) const
{ {
int errors = 0; int errors = 0;
assert(0); assert(0);
@ -202,7 +170,7 @@ int VTypeDef::emit_typedef(ostream&out) const
{ {
int errors = 0; int errors = 0;
out << "typedef "; out << "typedef ";
errors += type_->emit_def(out, name_); errors += type_->emit_def(out);
out << ";" << endl; out << " \\" << name_ << " ;" << endl;
return errors; return errors;
} }