From caf508602f4b37c8e8cf4286fa1116cb2ef9e1bb Mon Sep 17 00:00:00 2001 From: "Emil J. Tywoniak" Date: Thu, 15 Aug 2024 10:26:57 +0200 Subject: [PATCH] const: unify printing as string --- kernel/cellaigs.cc | 2 +- kernel/rtlil.cc | 14 ++++++++++++++ kernel/rtlil.h | 3 +++ passes/opt/opt_merge.cc | 9 +++++++-- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/kernel/cellaigs.cc b/kernel/cellaigs.cc index 5dda4503fdd..358c4187720 100644 --- a/kernel/cellaigs.cc +++ b/kernel/cellaigs.cc @@ -274,7 +274,7 @@ Aig::Aig(Cell *cell) name = mkname_last + stringf(":%d%c", p.second.as_int(), mkname_is_signed ? 'S' : 'U'); } else { mkname_last = name; - name += stringf(":%d", p.second.as_int()); + name += ":" + p.second.pretty_fmt(); } mkname_a_signed = false; diff --git a/kernel/rtlil.cc b/kernel/rtlil.cc index 76e4f598e7b..2c0ad82eb87 100644 --- a/kernel/rtlil.cc +++ b/kernel/rtlil.cc @@ -365,6 +365,20 @@ std::vector RTLIL::Const::to_bits() const return b; } +std::string RTLIL::Const::pretty_fmt() const { + if (get_if_str()) + return decode_string(); + else + return std::to_string(as_int()); +} + +std::string RTLIL::Const::pretty_fmt_undef() const { + if (get_if_str()) + return decode_string(); + else + return as_string(); +} + bool RTLIL::Const::as_bool() const { bitvectorize(); diff --git a/kernel/rtlil.h b/kernel/rtlil.h index de5b1798e2e..f0c0b17af02 100644 --- a/kernel/rtlil.h +++ b/kernel/rtlil.h @@ -715,6 +715,9 @@ struct RTLIL::Const std::string as_string(std::string any = "-") const; static Const from_string(const std::string &str); std::vector to_bits() const; + std::string pretty_fmt() const; + std::string pretty_fmt_undef() const; + std::string decode_string() const; inline size_t size() const { diff --git a/passes/opt/opt_merge.cc b/passes/opt/opt_merge.cc index eb3aa462e01..c5dc2057e51 100644 --- a/passes/opt/opt_merge.cc +++ b/passes/opt/opt_merge.cc @@ -140,8 +140,13 @@ struct OptMergeWorker hash_conn_strings.push_back(s + "\n"); } - for (auto &it : cell->parameters) - hash_conn_strings.push_back("P " + it.first.str() + "=" + it.second.as_string() + "\n"); + for (auto &it : cell->parameters) { + Const c = it.second; + std::string s = "P " + it.first.str() + "="; + s += c.pretty_fmt_undef(); + s += "\n"; + hash_conn_strings.push_back(s); + } std::sort(hash_conn_strings.begin(), hash_conn_strings.end());