Skip to content

Commit 9d97862

Browse files
committed
Stop using mutable in Const.
Now that we only call `bitvectorize()` in non-const methods, we can move the casting-away-const to only happen in `bitvectorize()`, which is deprecated so only some plugins (maybe) are using it. This means `const` `Const` methods don't change the underlying data, which means they'll be safe to use from multiple threads if/when we want to do that.
1 parent 3196d14 commit 9d97862

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

kernel/rtlil.cc

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,22 @@ const pool<IdString> &RTLIL::builtin_ff_cell_types() {
240240

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

243-
Const::bitvectype& Const::get_bits() const {
243+
const Const::bitvectype& Const::get_bits() const {
244244
check(is_bits());
245245
return *get_if_bits();
246246
}
247247

248-
std::string& Const::get_str() const {
248+
const std::string& Const::get_str() const {
249+
check(is_str());
250+
return *get_if_str();
251+
}
252+
253+
Const::bitvectype& Const::get_bits() {
254+
check(is_bits());
255+
return *get_if_bits();
256+
}
257+
258+
std::string& Const::get_str() {
249259
check(is_str());
250260
return *get_if_str();
251261
}
@@ -431,7 +441,7 @@ bool RTLIL::Const::as_bool() const
431441
return false;
432442
}
433443

434-
bitvectype& bv = get_bits();
444+
const bitvectype& bv = get_bits();
435445
for (size_t i = 0; i < bv.size(); i++)
436446
if (bv[i] == State::S1)
437447
return true;
@@ -579,7 +589,7 @@ std::string RTLIL::Const::decode_string() const
579589
if (auto str = get_if_str())
580590
return *str;
581591

582-
bitvectype& bv = get_bits();
592+
const bitvectype& bv = get_bits();
583593
const int n = GetSize(bv);
584594
const int n_over_8 = n / 8;
585595
std::string s;
@@ -627,7 +637,7 @@ bool RTLIL::Const::empty() const {
627637
}
628638
}
629639

630-
void RTLIL::Const::bitvectorize_internal() const {
640+
void RTLIL::Const::bitvectorize_internal() {
631641
if (tag == backing_tag::bits)
632642
return;
633643

@@ -678,7 +688,7 @@ bool RTLIL::Const::is_fully_zero() const
678688
return true;
679689
}
680690

681-
bitvectype& bv = get_bits();
691+
const bitvectype& bv = get_bits();
682692

683693
for (const auto &bit : bv)
684694
if (bit != RTLIL::State::S0)
@@ -698,7 +708,7 @@ bool RTLIL::Const::is_fully_ones() const
698708
return true;
699709
}
700710

701-
bitvectype& bv = get_bits();
711+
const bitvectype& bv = get_bits();
702712
for (const auto &bit : bv)
703713
if (bit != RTLIL::State::S1)
704714
return false;
@@ -713,7 +723,7 @@ bool RTLIL::Const::is_fully_def() const
713723
if (is_str())
714724
return true;
715725

716-
bitvectype& bv = get_bits();
726+
const bitvectype& bv = get_bits();
717727
for (const auto &bit : bv)
718728
if (bit != RTLIL::State::S0 && bit != RTLIL::State::S1)
719729
return false;
@@ -728,7 +738,7 @@ bool RTLIL::Const::is_fully_undef() const
728738
if (auto str = get_if_str())
729739
return str->empty();
730740

731-
bitvectype& bv = get_bits();
741+
const bitvectype& bv = get_bits();
732742
for (const auto &bit : bv)
733743
if (bit != RTLIL::State::Sx && bit != RTLIL::State::Sz)
734744
return false;
@@ -743,7 +753,7 @@ bool RTLIL::Const::is_fully_undef_x_only() const
743753
if (auto str = get_if_str())
744754
return str->empty();
745755

746-
bitvectype& bv = get_bits();
756+
const bitvectype& bv = get_bits();
747757
for (const auto &bit : bv)
748758
if (bit != RTLIL::State::Sx)
749759
return false;
@@ -779,7 +789,7 @@ Hasher RTLIL::Const::hash_into(Hasher h) const
779789

780790
// If the bits are all 0/1, hash packed bits using the string hash.
781791
// Otherwise hash the leading packed bits with the rest of the bits individually.
782-
bitvectype &bv = get_bits();
792+
const bitvectype &bv = get_bits();
783793
int size = GetSize(bv);
784794
std::string packed;
785795
int packed_size = (size + 7) >> 3;

kernel/rtlil.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -831,23 +831,27 @@ struct RTLIL::Const
831831
using bitvectype = std::vector<RTLIL::State>;
832832
enum class backing_tag: bool { bits, string };
833833
// Do not access the union or tag even in Const methods unless necessary
834-
mutable backing_tag tag;
834+
backing_tag tag;
835835
union {
836-
mutable bitvectype bits_;
837-
mutable std::string str_;
836+
bitvectype bits_;
837+
std::string str_;
838838
};
839839

840840
// Use these private utilities instead
841841
bool is_bits() const { return tag == backing_tag::bits; }
842842
bool is_str() const { return tag == backing_tag::string; }
843843

844-
bitvectype* get_if_bits() const { return is_bits() ? &bits_ : NULL; }
845-
std::string* get_if_str() const { return is_str() ? &str_ : NULL; }
844+
bitvectype* get_if_bits() { return is_bits() ? &bits_ : NULL; }
845+
std::string* get_if_str() { return is_str() ? &str_ : NULL; }
846+
const bitvectype* get_if_bits() const { return is_bits() ? &bits_ : NULL; }
847+
const std::string* get_if_str() const { return is_str() ? &str_ : NULL; }
846848

847-
bitvectype& get_bits() const;
848-
std::string& get_str() const;
849+
bitvectype& get_bits();
850+
std::string& get_str();
851+
const bitvectype& get_bits() const;
852+
const std::string& get_str() const;
849853
std::vector<RTLIL::State>& bits_internal();
850-
void bitvectorize_internal() const;
854+
void bitvectorize_internal();
851855

852856
public:
853857
Const() : flags(RTLIL::CONST_FLAG_NONE), tag(backing_tag::bits), bits_(std::vector<RTLIL::State>()) {}
@@ -880,7 +884,7 @@ struct RTLIL::Const
880884
[[deprecated("Don't use direct access to the internal std::vector<State>, that's an implementation detail.")]]
881885
std::vector<RTLIL::State>& bits() { return bits_internal(); }
882886
[[deprecated("Don't call bitvectorize() directly, it's an implementation detail.")]]
883-
void bitvectorize() const { bitvectorize_internal(); }
887+
void bitvectorize() const { const_cast<Const*>(this)->bitvectorize_internal(); }
884888

885889
bool as_bool() const;
886890

0 commit comments

Comments
 (0)