diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/nextpnr.cc | 37 | ||||
-rw-r--r-- | common/nextpnr.h | 115 | ||||
-rw-r--r-- | common/pybindings.cc | 2 |
3 files changed, 104 insertions, 50 deletions
diff --git a/common/nextpnr.cc b/common/nextpnr.cc index 374a5edb..541d00ba 100644 --- a/common/nextpnr.cc +++ b/common/nextpnr.cc @@ -21,23 +21,36 @@ NEXTPNR_NAMESPACE_BEGIN -std::unordered_map<std::string, int> *IdString::database_str_to_idx = nullptr; -std::vector<const std::string *> *IdString::database_idx_to_str = nullptr; +Context *IdString::global_ctx = nullptr; -void IdString::initialize() +void IdString::set(Context *ctx, const std::string &s) { - database_str_to_idx = new std::unordered_map<std::string, int>; - database_idx_to_str = new std::vector<const std::string *>; - initialize_add("", 0); - initialize_chip(); + auto it = ctx->idstring_str_to_idx->find(s); + if (it == ctx->idstring_str_to_idx->end()) { + index = ctx->idstring_idx_to_str->size(); + auto insert_rc = ctx->idstring_str_to_idx->insert({s, index}); + ctx->idstring_idx_to_str->push_back(&insert_rc.first->first); + } else { + index = it->second; + } } -void IdString::initialize_add(const char *s, int idx) +const std::string &IdString::str(Context *ctx) const { - assert(database_str_to_idx->count(s) == 0); - assert(int(database_idx_to_str->size()) == idx); - auto insert_rc = database_str_to_idx->insert({s, idx}); - database_idx_to_str->push_back(&insert_rc.first->first); + return *ctx->idstring_idx_to_str->at(index); +} + +const char *IdString::c_str(Context *ctx) const +{ + return str(ctx).c_str(); +} + +void IdString::initialize_add(Context *ctx, const char *s, int idx) +{ + assert(ctx->idstring_str_to_idx->count(s) == 0); + assert(int(ctx->idstring_idx_to_str->size()) == idx); + auto insert_rc = ctx->idstring_str_to_idx->insert({s, idx}); + ctx->idstring_idx_to_str->push_back(&insert_rc.first->first); } NEXTPNR_NAMESPACE_END diff --git a/common/nextpnr.h b/common/nextpnr.h index 8986ba13..e2de6c31 100644 --- a/common/nextpnr.h +++ b/common/nextpnr.h @@ -41,72 +41,95 @@ NEXTPNR_NAMESPACE_BEGIN +struct Context; + struct IdString { int index = 0; - static std::unordered_map<std::string, int> *database_str_to_idx; - static std::vector<const std::string *> *database_idx_to_str; + static Context *global_ctx; - static void initialize(); - static void initialize_chip(); - static void initialize_add(const char *s, int idx); + static void initialize_arch(Context *ctx); + static void initialize_add(Context *ctx, const char *s, int idx); IdString() {} - IdString(const std::string &s) + void set(Context *ctx, const std::string &s); + + IdString(Context *ctx, const std::string &s) { - if (database_str_to_idx == nullptr) - initialize(); - - auto it = database_str_to_idx->find(s); - if (it == database_str_to_idx->end()) { - index = database_idx_to_str->size(); - auto insert_rc = database_str_to_idx->insert({s, index}); - database_idx_to_str->push_back(&insert_rc.first->first); - } else { - index = it->second; - } + assert(global_ctx != nullptr); + set(global_ctx, s); } - IdString(const char *s) + IdString(Context *ctx, const char *s) { - if (database_str_to_idx == nullptr) - initialize(); - - auto it = database_str_to_idx->find(s); - if (it == database_str_to_idx->end()) { - index = database_idx_to_str->size(); - auto insert_rc = database_str_to_idx->insert({s, index}); - database_idx_to_str->push_back(&insert_rc.first->first); - } else { - index = it->second; - } + assert(global_ctx != nullptr); + set(global_ctx, s); } - const std::string &str() const { return *database_idx_to_str->at(index); } - const char *c_str() const { return str().c_str(); } + const std::string &str(Context *ctx) const; + const char *c_str(Context *ctx) const; - operator const char *() const { return c_str(); } - operator const std::string &() const { return str(); } + bool operator<(const IdString &other) const + { + return index < other.index; + } - bool operator<(const IdString &other) const { return index < other.index; } bool operator==(const IdString &other) const { return index == other.index; } - bool operator==(const std::string &s) const { return str() == s; } - bool operator==(const char *s) const { return str() == s; } bool operator!=(const IdString &other) const { return index != other.index; } + + bool empty() const { return index == 0; } + + // --- deprecated old API --- + + IdString(const std::string &s) + { + assert(global_ctx != nullptr); + set(global_ctx, s); + } + + IdString(const char *s) + { + assert(global_ctx != nullptr); + set(global_ctx, s); + } + + const std::string &global_str() const + { + assert(global_ctx != nullptr); + return str(global_ctx); + } + + const std::string &str() const + { + assert(global_ctx != nullptr); + return str(global_ctx); + } + + const char *c_str() const + { + assert(global_ctx != nullptr); + return c_str(global_ctx); + } + + operator const char *() const { return c_str(); } + operator const std::string &() const { return str(); } + + bool operator==(const std::string &s) const { return str() == s; } + bool operator==(const char *s) const { return str() == s; } + bool operator!=(const std::string &s) const { return str() != s; } bool operator!=(const char *s) const { return str() != s; } size_t size() const { return str().size(); } - bool empty() const { return index == 0; } }; NEXTPNR_NAMESPACE_END @@ -191,11 +214,29 @@ struct CellInfo struct Context : Arch { + // -------------------------------------------------------------- + + std::unordered_map<std::string, int> *idstring_str_to_idx; + std::vector<const std::string *> *idstring_idx_to_str; + + IdString id(const std::string &s) { return IdString(this, s); } + IdString id(const char *s) { return IdString(this, s); } + + // -------------------------------------------------------------- + std::unordered_map<IdString, NetInfo *> nets; std::unordered_map<IdString, CellInfo *> cells; Context(ArchArgs args) : Arch(args) { + assert(IdString::global_ctx == nullptr); + IdString::global_ctx = this; + + idstring_str_to_idx = new std::unordered_map<std::string, int>; + idstring_idx_to_str = new std::vector<const std::string *>; + IdString::initialize_add(this, "", 0); + IdString::initialize_arch(this); + // ... } }; diff --git a/common/pybindings.cc b/common/pybindings.cc index 4c10939a..26e8bf69 100644 --- a/common/pybindings.cc +++ b/common/pybindings.cc @@ -124,7 +124,7 @@ BOOST_PYTHON_MODULE(MODULE_NAME) def("load_design", load_design_shim); class_<IdString>("IdString") - .def("__str__", &IdString::str, + .def("__str__", &IdString::global_str, return_value_policy<copy_const_reference>()) .def(self < self) .def(self == self); |