vhdlpp: get_width() for VType.

This commit is contained in:
Maciej Suminski 2015-02-26 13:27:47 +01:00
parent a42b056b24
commit 4b0d220671
2 changed files with 72 additions and 0 deletions

View File

@ -79,6 +79,26 @@ void VTypePrimitive::show(ostream&out) const
}
}
int VTypePrimitive::get_width() const
{
switch(type_) {
case BOOLEAN:
case BIT:
case STDLOGIC:
case REAL:
return 1;
case INTEGER:
case NATURAL:
return 32;
case CHARACTER:
return 8;
}
return -1;
}
VTypeArray::range_t*VTypeArray::range_t::clone() const
{
return new VTypeArray::range_t(safe_clone(msb_), safe_clone(lsb_), direction_);
@ -176,6 +196,30 @@ void VTypeArray::show(ostream&out) const
out << "<nil>";
}
int VTypeArray::get_width() const
{
int64_t size = 1;
for(vector<range_t>::const_iterator it = ranges_.begin();
it != ranges_.end(); ++it) {
const VTypeArray::range_t&dim = *it;
int64_t msb_val, lsb_val;
if(dim.is_box())
return false;
if(!dim.msb()->evaluate(NULL, msb_val))
return -1;
if(!dim.lsb()->evaluate(NULL, lsb_val))
return -1;
size *= 1 + labs(msb_val - lsb_val);
}
return etype_->get_width() * size;
}
bool VTypeArray::is_unbounded() const {
for(std::vector<range_t>::const_iterator it = ranges_.begin();
it != ranges_.end(); ++it)
@ -281,6 +325,23 @@ void VTypeRecord::show(ostream&out) const
write_to_stream(out);
}
int VTypeRecord::get_width() const
{
int width = 0;
for(vector<element_t*>::const_iterator it = elements_.begin();
it != elements_.end(); ++it) {
int w = (*it)->peek_type()->get_width();
if(w < 0)
return -1;
width += w;
}
return width;
}
const VTypeRecord::element_t* VTypeRecord::element_by_name(perm_string name, int*index) const
{
for (vector<element_t*>::const_iterator cur = elements_.begin()

View File

@ -66,6 +66,7 @@ class VType {
// of this type to the designated stream. This is used for
// writing parsed types to library files.
virtual void write_to_stream(std::ostream&fd) const;
// This is like the above, but is the root function called
// directly after the "type <name> is..." when writing type
// definitions. Most types accept the default definition of this.
@ -100,6 +101,10 @@ class VType {
// typedefs (i.e. not ones defined by the user).
perm_string get_generic_typename() const;
// Returns the type width in bits or negative number if it is impossible
// to evaluate.
virtual int get_width() const { return -1; }
private:
friend struct decl_t;
// This virtual method is called to emit the declaration. This
@ -161,6 +166,7 @@ class VTypePrimitive : public VType {
void write_to_stream(std::ostream&fd) const;
void show(std::ostream&) const;
int get_width() const;
type_t type() const { return type_; }
@ -221,6 +227,7 @@ class VTypeArray : public VType {
void write_to_stream(std::ostream&fd) const;
void write_type_to_stream(std::ostream&fd) const;
void show(std::ostream&) const;
int get_width() const;
inline size_t dimensions() const { return ranges_.size(); };
const range_t&dimension(size_t idx) const
@ -294,6 +301,8 @@ class VTypeEnum : public VType {
void write_to_stream(std::ostream&fd) const;
void show(std::ostream&) const;
int get_width() const { return 32; }
int emit_def(std::ostream&out, perm_string name) const;
// Checks if the name is stored in the enum.
@ -332,6 +341,7 @@ class VTypeRecord : public VType {
void write_to_stream(std::ostream&fd) const;
void show(std::ostream&) const;
int get_width() const;
int emit_def(std::ostream&out, perm_string name) const;
bool can_be_packed() const { return true; }
@ -362,6 +372,7 @@ class VTypeDef : public VType {
void write_to_stream(std::ostream&fd) const;
void write_type_to_stream(std::ostream&fd) const;
int get_width() const { return type_->get_width(); }
int emit_typedef(std::ostream&out, typedef_context_t&ctx) const;
int emit_def(std::ostream&out, perm_string name) const;