Improve type compatibility checking for dynamic arrays and queues

SystemVerilog defines different levels of type compatibility.
 * Matching
 * Equivalent
 * Assignment compatible
 * Cast compatible

At the moment the `nettype_t` has only one type compatibility test. It is
used to check assignment compatibility when assigning to a dynamic array,
queue or class.

The current implementation rejects a few cases that should allowed and
allows a few cases that should be rejected.

Dynamic arrays and queues are assignment compatible if their element types
are compatible. And two packed types are equivalent if they are both
2-state or 4-state, both signed or unsigned and have the same packed with.

In the current implementation the sign is not considered and instead of
checking if the packed width is the same it checks that the dimensions are
identical.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
This commit is contained in:
Lars-Peter Clausen 2022-09-17 19:37:47 +02:00
parent d4765e3354
commit 5fa1aecd4f
12 changed files with 113 additions and 50 deletions

View File

@ -37,18 +37,22 @@ ivl_variable_type_t netdarray_t::base_type(void) const
return IVL_VT_DARRAY; return IVL_VT_DARRAY;
} }
bool netdarray_t::test_compatibility(ivl_type_t that) const bool netdarray_t::test_equivalence(ivl_type_t that) const
{ {
ivl_type_t elem_type = 0; // Queues and dynamic arrays are not equivalent, so check for the base
// type to make sure both are either dynamic array or queue.
if (const netdarray_t*that_da = dynamic_cast<const netdarray_t*>(that)) if (base_type() != that->base_type())
elem_type = that_da->element_type();
if (const netqueue_t*that_q = dynamic_cast<const netqueue_t*>(that))
elem_type = that_q->element_type();
if (elem_type == 0)
return false; return false;
return element_type()->type_compatible(elem_type); return test_compatibility(that);
}
bool netdarray_t::test_compatibility(ivl_type_t that) const
{
// This will match both queues and dynamic arrays
const netdarray_t *that_da = dynamic_cast<const netdarray_t*>(that);
if (!that_da)
return false;
return element_type()->type_equivalent(that_da->element_type());
} }

View File

@ -49,6 +49,7 @@ class netdarray_t : public netarray_t {
private: private:
bool test_compatibility(ivl_type_t that) const; bool test_compatibility(ivl_type_t that) const;
bool test_equivalence(ivl_type_t that) const;
}; };
#endif /* IVL_netdarray_H */ #endif /* IVL_netdarray_H */

View File

@ -68,6 +68,16 @@ vector<netrange_t> netparray_t::slice_dimensions() const
return res; return res;
} }
bool netparray_t::test_compatibility(ivl_type_t that) const
{
return packed_type_compatible(that);
}
bool netparray_t::test_equivalence(ivl_type_t that) const
{
return packed_types_equivalent(this, that);
}
netuarray_t::~netuarray_t() netuarray_t::~netuarray_t()
{ {
} }

View File

@ -68,6 +68,9 @@ class netparray_t : public netsarray_t {
long packed_width(void) const; long packed_width(void) const;
std::vector<netrange_t> slice_dimensions() const; std::vector<netrange_t> slice_dimensions() const;
private:
bool test_compatibility(ivl_type_t that) const;
bool test_equivalence(ivl_type_t that) const;
}; };
inline netparray_t::netparray_t(const std::vector<netrange_t>&pd, inline netparray_t::netparray_t(const std::vector<netrange_t>&pd,

View File

@ -35,19 +35,3 @@ ivl_variable_type_t netqueue_t::base_type() const
{ {
return IVL_VT_QUEUE; return IVL_VT_QUEUE;
} }
bool netqueue_t::test_compatibility(ivl_type_t that) const
{
ivl_type_t elem_type = 0;
if (const netqueue_t*that_q = dynamic_cast<const netqueue_t*>(that))
elem_type = that_q->element_type();
if (const netdarray_t*that_da = dynamic_cast<const netdarray_t*>(that))
elem_type = that_da->element_type();
if (elem_type == 0)
return false;
return element_type()->type_compatible(elem_type);
}

View File

@ -43,7 +43,6 @@ class netqueue_t : public netdarray_t {
std::ostream& debug_dump(std::ostream&) const; std::ostream& debug_dump(std::ostream&) const;
private: private:
bool test_compatibility(ivl_type_t that) const;
long max_idx_; long max_idx_;
}; };

View File

@ -134,3 +134,16 @@ ivl_variable_type_t netstruct_t::base_type() const
return IVL_VT_BOOL; return IVL_VT_BOOL;
} }
bool netstruct_t::test_compatibility(ivl_type_t that) const
{
return packed_type_compatible(that);
}
bool netstruct_t::test_equivalence(ivl_type_t that) const
{
if (!packed_)
return this == that;
return packed_types_equivalent(this, that);
}

View File

@ -75,6 +75,10 @@ class netstruct_t : public LineInfo, public ivl_type_s {
// IVL_VT_NO_TYPE if the record is not packed. // IVL_VT_NO_TYPE if the record is not packed.
ivl_variable_type_t base_type() const; ivl_variable_type_t base_type() const;
private:
bool test_compatibility(ivl_type_t that) const;
bool test_equivalence(ivl_type_t that) const;
private: private:
bool union_; bool union_;
bool packed_; bool packed_;

View File

@ -18,6 +18,7 @@
*/ */
# include "nettypes.h" # include "nettypes.h"
# include "netenum.h"
# include <iostream> # include <iostream>
# include <cassert> # include <cassert>
@ -69,9 +70,22 @@ bool ivl_type_s::type_compatible(ivl_type_t that) const
return test_compatibility(that); return test_compatibility(that);
} }
bool ivl_type_s::test_compatibility(const ivl_type_s* /*that*/) const bool ivl_type_s::test_compatibility(ivl_type_t that) const
{ {
return false; return test_equivalence(that);
}
bool ivl_type_s::type_equivalent(ivl_type_t that) const
{
if (this == that)
return true;
return test_equivalence(that);
}
bool ivl_type_s::test_equivalence(ivl_type_t) const
{
return false;
} }
netarray_t::~netarray_t() netarray_t::~netarray_t()
@ -166,3 +180,35 @@ bool prefix_to_slice(const std::vector<netrange_t>&dims,
return true; return true;
} }
bool packed_types_equivalent(ivl_type_t a, ivl_type_t b)
{
if (!a->packed() || !b->packed())
return false;
if (a->base_type() != b->base_type())
return false;
if (a->packed_width() != b->packed_width())
return false;
if (a->get_signed() != b->get_signed())
return false;
// Special case, even though enums are packed they are not equivalent,
// they are only assignment compatible to other packed types
if (dynamic_cast<const netenum_t*>(b))
return false;
return true;
}
bool packed_type_compatible(ivl_type_t type)
{
if (type->packed())
return true;
if (type->base_type() == IVL_VT_REAL)
return true;
return false;
}

View File

@ -47,17 +47,20 @@ class ivl_type_s {
virtual bool get_signed() const; virtual bool get_signed() const;
virtual bool get_scalar() const; virtual bool get_scalar() const;
// Return true if "that" type is compatible with this // Return true if "that" type is assignment compatible with this
// type. Compatible means the types are essentially the same. // type.
bool type_compatible(ivl_type_t that) const; bool type_compatible(ivl_type_t that) const;
// Return true if "that" type is equivalent with this type as defined by
// the standard
bool type_equivalent(ivl_type_t that) const;
virtual std::ostream& debug_dump(std::ostream&) const; virtual std::ostream& debug_dump(std::ostream&) const;
private: private:
// The "type_compatible" method uses this virtual method to // The "type_compatible" and "type_equivalent" methods uses this virtual
// invoke type-specific tests of compatibility. This should // method to invoke type-specific tests of compatibility.
// only be called by the type_compatible method above.
virtual bool test_compatibility(ivl_type_t that) const; virtual bool test_compatibility(ivl_type_t that) const;
virtual bool test_equivalence(ivl_type_t that) const;
}; };
/* /*
@ -162,4 +165,8 @@ extern bool prefix_to_slice(const std::vector<netrange_t>&dims,
const std::list<long>&prefix, long sb, const std::list<long>&prefix, long sb,
long&loff, unsigned long&lwid); long&loff, unsigned long&lwid);
extern bool packed_types_equivalent(ivl_type_t a, ivl_type_t b);
extern bool packed_type_compatible(ivl_type_t type);
#endif /* IVL_nettypes_H */ #endif /* IVL_nettypes_H */

View File

@ -89,19 +89,10 @@ vector<netrange_t> netvector_t::slice_dimensions() const
bool netvector_t::test_compatibility(ivl_type_t that) const bool netvector_t::test_compatibility(ivl_type_t that) const
{ {
const netvector_t*that_st = dynamic_cast<const netvector_t*>(that); return packed_type_compatible(that);
if (that_st == 0) }
return false;
bool netvector_t::test_equivalence(const ivl_type_t that) const
if (type_ != that_st->type_) {
return false; return packed_types_equivalent(this, that);
if (packed_dims_.size() != that_st->packed_dims_.size())
return false;
for (size_t idx = 0 ; idx < packed_dims_.size() ; idx += 1) {
if (packed_dims_[idx] != that_st->packed_dims_[idx])
return false;
}
return true;
} }

View File

@ -86,6 +86,7 @@ class netvector_t : public ivl_type_s {
private: private:
bool test_compatibility(ivl_type_t that) const; bool test_compatibility(ivl_type_t that) const;
bool test_equivalence(ivl_type_t that) const;
private: private:
std::vector<netrange_t> packed_dims_; std::vector<netrange_t> packed_dims_;