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:
parent
d4765e3354
commit
5fa1aecd4f
26
netdarray.cc
26
netdarray.cc
|
|
@ -37,18 +37,22 @@ ivl_variable_type_t netdarray_t::base_type(void) const
|
|||
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;
|
||||
|
||||
if (const netdarray_t*that_da = dynamic_cast<const netdarray_t*>(that))
|
||||
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)
|
||||
// Queues and dynamic arrays are not equivalent, so check for the base
|
||||
// type to make sure both are either dynamic array or queue.
|
||||
if (base_type() != that->base_type())
|
||||
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());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,6 +49,7 @@ class netdarray_t : public netarray_t {
|
|||
|
||||
private:
|
||||
bool test_compatibility(ivl_type_t that) const;
|
||||
bool test_equivalence(ivl_type_t that) const;
|
||||
};
|
||||
|
||||
#endif /* IVL_netdarray_H */
|
||||
|
|
|
|||
10
netparray.cc
10
netparray.cc
|
|
@ -68,6 +68,16 @@ vector<netrange_t> netparray_t::slice_dimensions() const
|
|||
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()
|
||||
{
|
||||
}
|
||||
|
|
|
|||
|
|
@ -68,6 +68,9 @@ class netparray_t : public netsarray_t {
|
|||
long packed_width(void) 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,
|
||||
|
|
|
|||
16
netqueue.cc
16
netqueue.cc
|
|
@ -35,19 +35,3 @@ ivl_variable_type_t netqueue_t::base_type() const
|
|||
{
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,7 +43,6 @@ class netqueue_t : public netdarray_t {
|
|||
std::ostream& debug_dump(std::ostream&) const;
|
||||
|
||||
private:
|
||||
bool test_compatibility(ivl_type_t that) const;
|
||||
long max_idx_;
|
||||
};
|
||||
|
||||
|
|
|
|||
13
netstruct.cc
13
netstruct.cc
|
|
@ -134,3 +134,16 @@ ivl_variable_type_t netstruct_t::base_type() const
|
|||
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -75,6 +75,10 @@ class netstruct_t : public LineInfo, public ivl_type_s {
|
|||
// IVL_VT_NO_TYPE if the record is not packed.
|
||||
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:
|
||||
bool union_;
|
||||
bool packed_;
|
||||
|
|
|
|||
48
nettypes.cc
48
nettypes.cc
|
|
@ -18,6 +18,7 @@
|
|||
*/
|
||||
|
||||
# include "nettypes.h"
|
||||
# include "netenum.h"
|
||||
# include <iostream>
|
||||
# include <cassert>
|
||||
|
||||
|
|
@ -69,7 +70,20 @@ bool ivl_type_s::type_compatible(ivl_type_t that) const
|
|||
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 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;
|
||||
}
|
||||
|
|
@ -166,3 +180,35 @@ bool prefix_to_slice(const std::vector<netrange_t>&dims,
|
|||
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;
|
||||
}
|
||||
|
|
|
|||
17
nettypes.h
17
nettypes.h
|
|
@ -47,17 +47,20 @@ class ivl_type_s {
|
|||
virtual bool get_signed() const;
|
||||
virtual bool get_scalar() const;
|
||||
|
||||
// Return true if "that" type is compatible with this
|
||||
// type. Compatible means the types are essentially the same.
|
||||
// Return true if "that" type is assignment compatible with this
|
||||
// type.
|
||||
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;
|
||||
|
||||
private:
|
||||
// The "type_compatible" method uses this virtual method to
|
||||
// invoke type-specific tests of compatibility. This should
|
||||
// only be called by the type_compatible method above.
|
||||
// The "type_compatible" and "type_equivalent" methods uses this virtual
|
||||
// method to invoke type-specific tests of compatibility.
|
||||
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,
|
||||
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 */
|
||||
|
|
|
|||
17
netvector.cc
17
netvector.cc
|
|
@ -89,19 +89,10 @@ vector<netrange_t> netvector_t::slice_dimensions() const
|
|||
|
||||
bool netvector_t::test_compatibility(ivl_type_t that) const
|
||||
{
|
||||
const netvector_t*that_st = dynamic_cast<const netvector_t*>(that);
|
||||
if (that_st == 0)
|
||||
return false;
|
||||
|
||||
if (type_ != that_st->type_)
|
||||
return false;
|
||||
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 packed_type_compatible(that);
|
||||
}
|
||||
|
||||
return true;
|
||||
bool netvector_t::test_equivalence(const ivl_type_t that) const
|
||||
{
|
||||
return packed_types_equivalent(this, that);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -86,6 +86,7 @@ class netvector_t : public ivl_type_s {
|
|||
|
||||
private:
|
||||
bool test_compatibility(ivl_type_t that) const;
|
||||
bool test_equivalence(ivl_type_t that) const;
|
||||
|
||||
private:
|
||||
std::vector<netrange_t> packed_dims_;
|
||||
|
|
|
|||
Loading…
Reference in New Issue