From 9aa6a6b5ebdfae228e04804e78de83bbb76ad806 Mon Sep 17 00:00:00 2001 From: "Emil J. Tywoniak" Date: Fri, 2 Aug 2024 23:26:57 +0200 Subject: [PATCH] rtlil: access Const variant alternatives directly --- kernel/rtlil.cc | 56 +++++++++++++++++++------------------------------ kernel/rtlil.h | 33 ++++++++++------------------- 2 files changed, 33 insertions(+), 56 deletions(-) diff --git a/kernel/rtlil.cc b/kernel/rtlil.cc index 7d85097785d..a7b5cad5e1d 100644 --- a/kernel/rtlil.cc +++ b/kernel/rtlil.cc @@ -210,7 +210,7 @@ RTLIL::Const::Const(int val, int width) { flags = RTLIL::CONST_FLAG_NONE; backing = bitvectype(); - bitvectype& bv = assert_get_bits("Const::Const(int, int)"); + bitvectype& bv = std::get(backing); bv.reserve(width); for (int i = 0; i < width; i++) { bv.push_back((val & 1) != 0 ? State::S1 : State::S0); @@ -222,7 +222,7 @@ RTLIL::Const::Const(RTLIL::State bit, int width) { flags = RTLIL::CONST_FLAG_NONE; backing = bitvectype(); - bitvectype& bv = assert_get_bits("Const::Const(State, int)"); + bitvectype& bv = std::get(backing); bv.reserve(width); for (int i = 0; i < width; i++) bv.push_back(bit); @@ -232,33 +232,22 @@ RTLIL::Const::Const(const std::vector &bits) { flags = RTLIL::CONST_FLAG_NONE; backing = bitvectype(); - bitvectype& bv = assert_get_bits("Const::Const(std::vector)"); + bitvectype& bv = std::get(backing); bv.reserve(bits.size()); for (const auto &b : bits) bv.emplace_back(b ? State::S1 : State::S0); } -[[nodiscard]] RTLIL::Const::bitvectype& RTLIL::Const::assert_get_bits(const char* ctx) const { - // return assert_get(&backing, ctx); - return std::get(backing); -} - -[[nodiscard]] std::string& RTLIL::Const::assert_get_str(const char* ctx) const { - // return assert_get(&backing, ctx); - return std::get(backing); -} - bool RTLIL::Const::operator <(const RTLIL::Const &other) const { - const char* ctx = "operator<"; if (std::get_if(&backing) != std::get_if(&other.backing)) return decode_string() < other.decode_string(); if (std::get_if(&backing)) - return assert_get_str(ctx) < other.assert_get_str(ctx); + return std::get(backing) < std::get(other.backing); - bitvectype& bv = assert_get_bits(ctx); - auto other_bv = other.assert_get_bits(ctx); + bitvectype& bv = std::get(backing); + auto other_bv = std::get(other.backing); if (bv.size() != other_bv.size()) return bv.size() < other_bv.size(); @@ -271,14 +260,13 @@ bool RTLIL::Const::operator <(const RTLIL::Const &other) const bool RTLIL::Const::operator ==(const RTLIL::Const &other) const { - const char* ctx = "operator=="; if (std::get_if(&backing) != std::get_if(&other.backing)) return decode_string() == other.decode_string(); if (std::get_if(&backing)) - return assert_get_str(ctx) == other.assert_get_str(ctx); + return std::get(backing) == std::get(other.backing); - return assert_get_bits(ctx) == other.assert_get_bits(ctx); + return std::get(backing) == std::get(other.backing); } bool RTLIL::Const::operator !=(const RTLIL::Const &other) const @@ -289,13 +277,13 @@ bool RTLIL::Const::operator !=(const RTLIL::Const &other) const std::vector& RTLIL::Const::bits() { bitvectorize(); - return assert_get_bits("Const::bits()"); + return std::get(backing); } const std::vector& RTLIL::Const::bits() const { bitvectorize(); - return assert_get_bits("Const::bits()"); + return std::get(backing); } @@ -304,7 +292,7 @@ std::vector RTLIL::Const::to_bits() const if (auto bv = std::get_if(&backing)) { return *bv; } - auto str = assert_get_str("Const::to_bits"); + auto& str = std::get(backing); bitvectype b; b.reserve(str.size() * 8); for (int i = str.size()-1; i >= 0; i--) { @@ -334,7 +322,7 @@ std::string RTLIL::Const::pretty_fmt_undef() const { bool RTLIL::Const::as_bool() const { bitvectorize(); - bitvectype& bv = assert_get_bits("Const::as_bool"); + bitvectype& bv = std::get(backing); for (size_t i = 0; i < bv.size(); i++) if (bv[i] == State::S1) return true; @@ -344,7 +332,7 @@ bool RTLIL::Const::as_bool() const int RTLIL::Const::as_int(bool is_signed) const { bitvectorize(); - bitvectype& bv = assert_get_bits("Const::as_int"); + bitvectype& bv = std::get(backing); int32_t ret = 0; for (size_t i = 0; i < bv.size() && i < 32; i++) if (bv[i] == State::S1) @@ -358,7 +346,7 @@ int RTLIL::Const::as_int(bool is_signed) const std::string RTLIL::Const::as_string(std::string any) const { bitvectorize(); - bitvectype& bv = assert_get_bits("Const::as_bool"); + bitvectype& bv = std::get(backing); std::string ret; ret.reserve(bv.size()); for (size_t i = bv.size(); i > 0; i--) @@ -377,7 +365,7 @@ RTLIL::Const RTLIL::Const::from_string(const std::string &str) { Const c; c.backing = bitvectype(); - bitvectype& bv = c.assert_get_bits("Const::from_string"); + bitvectype& bv = std::get(c.backing); bv.reserve(str.size()); for (auto it = str.rbegin(); it != str.rend(); it++) switch (*it) { @@ -397,7 +385,7 @@ std::string RTLIL::Const::decode_string() const return *str; bitvectorize(); - bitvectype& bv = assert_get_bits("Const::decode_string"); + bitvectype& bv = std::get(backing); const int n = GetSize(bv); const int n_over_8 = n / 8; std::string s; @@ -430,7 +418,7 @@ std::string RTLIL::Const::decode_string() const bool RTLIL::Const::is_fully_zero() const { bitvectorize(); - bitvectype& bv = assert_get_bits("Const::decode_string"); + bitvectype& bv = std::get(backing); cover("kernel.rtlil.const.is_fully_zero"); for (const auto &bit : bv) @@ -443,7 +431,7 @@ bool RTLIL::Const::is_fully_zero() const bool RTLIL::Const::is_fully_ones() const { bitvectorize(); - bitvectype& bv = assert_get_bits("Const::decode_string"); + bitvectype& bv = std::get(backing); cover("kernel.rtlil.const.is_fully_ones"); for (const auto &bit : bv) @@ -458,7 +446,7 @@ bool RTLIL::Const::is_fully_def() const cover("kernel.rtlil.const.is_fully_def"); bitvectorize(); - bitvectype& bv = assert_get_bits("Const::decode_string"); + bitvectype& bv = std::get(backing); for (const auto &bit : bv) if (bit != RTLIL::State::S0 && bit != RTLIL::State::S1) @@ -472,7 +460,7 @@ bool RTLIL::Const::is_fully_undef() const cover("kernel.rtlil.const.is_fully_undef"); bitvectorize(); - bitvectype& bv = assert_get_bits("Const::decode_string"); + bitvectype& bv = std::get(backing); for (const auto &bit : bv) if (bit != RTLIL::State::Sx && bit != RTLIL::State::Sz) @@ -486,7 +474,7 @@ bool RTLIL::Const::is_fully_undef_x_only() const cover("kernel.rtlil.const.is_fully_undef_x_only"); bitvectorize(); - bitvectype& bv = assert_get_bits("Const::decode_string"); + bitvectype& bv = std::get(backing); for (const auto &bit : bv) if (bit != RTLIL::State::Sx) @@ -500,7 +488,7 @@ bool RTLIL::Const::is_onehot(int *pos) const cover("kernel.rtlil.const.is_onehot"); bitvectorize(); - bitvectype& bv = assert_get_bits("Const::decode_string"); + bitvectype& bv = std::get(backing); bool found = false; for (int i = 0; i < GetSize(*this); i++) { diff --git a/kernel/rtlil.h b/kernel/rtlil.h index 92e29143067..062a18463a5 100644 --- a/kernel/rtlil.h +++ b/kernel/rtlil.h @@ -687,9 +687,6 @@ struct RTLIL::Const bool operator ==(const RTLIL::Const &other) const; bool operator !=(const RTLIL::Const &other) const; - bitvectype& assert_get_bits(const char* ctx) const; - std::string& assert_get_str(const char* ctx) const; - const std::vector& bits() const; std::vector& bits(); bool as_bool() const; @@ -706,21 +703,21 @@ struct RTLIL::Const if (auto str = std::get_if(&backing)) return 8 * str->size(); else - return assert_get_bits("Const::size").size(); + return std::get(backing).size(); } inline bool empty() const { if (auto str = std::get_if(&backing)) return str->empty(); else - return assert_get_bits("Const::empty").empty(); + return std::get(backing).empty(); } void bitvectorize() const { if (std::get_if(&backing)) return; - std::string& str = assert_get_str("Const::bitvectorize"); + auto& str = std::get(backing); bitvectype bits; bits.reserve(str.size() * 8); for (int i = str.size() - 1; i >= 0; i--) { @@ -734,20 +731,16 @@ struct RTLIL::Const } inline RTLIL::State &operator[](int index) { - bitvectorize(); - return assert_get_bits("Const::operator[]").at(index); + return bits().at(index); } inline const RTLIL::State &operator[](int index) const { - bitvectorize(); - return assert_get_bits("const Const::operator[]").at(index); + return bits().at(index); } inline bitvectype::iterator begin() { - bitvectorize(); - return assert_get_bits("Const bit iterator begin()").begin(); + return bits().begin(); } inline bitvectype::iterator end() { - bitvectorize(); - return assert_get_bits("Const bit iterator end()").end(); + return bits().end(); } bool is_fully_zero() const; @@ -758,8 +751,7 @@ struct RTLIL::Const bool is_onehot(int *pos = nullptr) const; inline RTLIL::Const extract(int offset, int len = 1, RTLIL::State padding = RTLIL::State::S0) const { - bitvectorize(); - bitvectype& bv = assert_get_bits("Const::extract"); + auto& bv = bits(); bitvectype ret_bv; ret_bv.reserve(len); for (int i = offset; i < offset + len; i++) @@ -768,13 +760,11 @@ struct RTLIL::Const } void extu(int width) { - bitvectorize(); - assert_get_bits("Const::extu").resize(width, RTLIL::State::S0); + bits().resize(width, RTLIL::State::S0); } void exts(int width) { - bitvectorize(); - bitvectype& bv = assert_get_bits("Const::exts"); + bitvectype& bv = bits(); bv.resize(width, bv.empty() ? RTLIL::State::Sx : bv.back()); } @@ -785,8 +775,7 @@ struct RTLIL::Const for (auto c : *str) h = mkhash(h, c); } else { - bitvectype& bv = assert_get_bits("Const::hash"); - for (auto b : bv) + for (auto b : bits()) h = mkhash(h, b); } return h;