Skip to content

Commit

Permalink
rtlil: refactor Const union sanity checks
Browse files Browse the repository at this point in the history
  • Loading branch information
widlarizer committed Sep 11, 2024
1 parent eedf770 commit ebdc704
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions kernel/rtlil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,15 @@ const pool<IdString> &RTLIL::builtin_ff_cell_types() {
return res;
}

#define check(condition) log_assert(condition && "malformed Const union")

Const::bitvectype& Const::get_bits() const {
log_assert(is_bits() && "malformed Const union");
check(is_bits());
return *get_if_bits();
}

std::string& Const::get_str() const {
log_assert(is_str() && "malformed Const union");
check(is_str());
return *get_if_str();
}

Expand Down Expand Up @@ -260,7 +262,7 @@ RTLIL::Const::Const(const RTLIL::Const &other) {
else if (is_bits())
new ((void*)&bits_) bitvectype(other.get_bits());
else
log_assert(false && "malformed Const union");
check(false);
}

RTLIL::Const::Const(RTLIL::Const &&other) {
Expand All @@ -271,15 +273,15 @@ RTLIL::Const::Const(RTLIL::Const &&other) {
else if (is_bits())
new ((void*)&bits_) bitvectype(std::move(other.get_bits()));
else
log_assert(false && "malformed Const union");
check(false);
}

RTLIL::Const &RTLIL::Const::operator =(const RTLIL::Const &other) {
flags = other.flags;
if (other.is_str()) {
if (!is_str()) {
// sketchy zone
log_assert(is_bits() && "malformed Const union");
check(is_bits());
bits_.~bitvectype();
(void)new ((void*)&str_) std::string();
}
Expand All @@ -288,14 +290,14 @@ RTLIL::Const &RTLIL::Const::operator =(const RTLIL::Const &other) {
} else if (other.is_bits()) {
if (!is_bits()) {
// sketchy zone
log_assert(is_str() && "malformed Const union");
check(is_str());
str_.~string();
(void)new ((void*)&bits_) bitvectype();
}
tag = other.tag;
get_bits() = other.get_bits();
} else {
log_assert(false && "malformed Const union");
check(false);
}
return *this;
}
Expand All @@ -306,7 +308,7 @@ RTLIL::Const::~Const() {
else if (is_str())
str_.~string();
else
log_assert(false && "malformed Const union");
check(false);
}

bool RTLIL::Const::operator<(const RTLIL::Const &other) const
Expand Down Expand Up @@ -450,7 +452,7 @@ int RTLIL::Const::size() const {
if (is_str())
return 8 * str_.size();
else {
log_assert(is_bits() && "malformed Const union");
check(is_bits());
return bits_.size();
}
}
Expand All @@ -459,7 +461,7 @@ bool RTLIL::Const::empty() const {
if (is_str())
return str_.empty();
else {
log_assert(is_bits() && "malformed Const union");
check(is_bits());
return bits_.empty();
}
}
Expand All @@ -468,7 +470,7 @@ void RTLIL::Const::bitvectorize() const {
if (tag == backing_tag::bits)
return;

log_assert(is_str() && "malformed Const union");
check(is_str());

bitvectype new_bits;

Expand Down Expand Up @@ -596,6 +598,7 @@ RTLIL::Const RTLIL::Const::extract(int offset, int len, RTLIL::State padding) co
ret_bv.push_back(i < GetSize(*this) ? (*this)[i] : padding);
return RTLIL::Const(ret_bv);
}
#undef check /* check(condition) for Const */

bool RTLIL::AttrObject::has_attribute(const RTLIL::IdString &id) const
{
Expand Down

0 comments on commit ebdc704

Please sign in to comment.