diff options
-rw-r--r-- | common/nextpnr.cc | 43 | ||||
-rw-r--r-- | common/nextpnr.h | 59 | ||||
-rw-r--r-- | dummy/chip.cc | 3 | ||||
-rw-r--r-- | ice40/chip.cc | 26 | ||||
-rw-r--r-- | ice40/chip.h | 1 |
5 files changed, 106 insertions, 26 deletions
diff --git a/common/nextpnr.cc b/common/nextpnr.cc new file mode 100644 index 00000000..d2b887de --- /dev/null +++ b/common/nextpnr.cc @@ -0,0 +1,43 @@ +/* + * nextpnr -- Next Generation Place and Route + * + * Copyright (C) 2018 Clifford Wolf <clifford@clifford.at> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + */ + +#include "nextpnr.h" + +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; + +void IdString::initialize() +{ + 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(); +} + +void IdString::initialize_add(const char *s, int idx) +{ + assert(database_str_to_idx->count(s) == 0); + assert(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); +} + +NEXTPNR_NAMESPACE_END diff --git a/common/nextpnr.h b/common/nextpnr.h index b25750eb..4288f7fc 100644 --- a/common/nextpnr.h +++ b/common/nextpnr.h @@ -43,25 +43,60 @@ NEXTPNR_NAMESPACE_BEGIN struct IdString { - std::string data; + 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 void initialize(); + static void initialize_chip(); + static void initialize_add(const char *s, int idx); IdString() {} - IdString(std::string s) : data(s) {} - IdString(const char *s) : data(s) {} - const char *c_str() const { return data.c_str(); } - const std::string &str() const { return data; } + IdString(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; + } + } + + IdString(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; + } + } + + const std::string &str() const { return *database_idx_to_str->at(index); } + const char *c_str() const { return str().c_str(); } operator const char *() const { return c_str(); } operator const std::string &() const { return str(); } - bool operator<(const IdString &other) const { return data < other.data; } - bool operator==(const IdString &other) const { return data == other.data; } - bool operator==(const std::string &s) const { return data == s; } - bool operator==(const char *s) const { return data == s; } + 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; } - size_t size() const { return data.size(); } - bool empty() const { return data.empty(); } + size_t size() const { return str().size(); } + bool empty() const { return index == 0; } }; NEXTPNR_NAMESPACE_END @@ -72,7 +107,7 @@ template <> struct hash<NEXTPNR_NAMESPACE_PREFIX IdString> std::size_t operator()(const NEXTPNR_NAMESPACE_PREFIX IdString &obj) const noexcept { - return std::hash<std::string>()(obj.data); + return obj.index; } }; } // namespace std diff --git a/dummy/chip.cc b/dummy/chip.cc index 23aa0e9d..ae962508 100644 --- a/dummy/chip.cc +++ b/dummy/chip.cc @@ -24,6 +24,9 @@ NEXTPNR_NAMESPACE_BEGIN Chip::Chip(ChipArgs) {} std::string Chip::getChipName() { return "Dummy"; } + +void IdString::initialize_chip() {} + // --------------------------------------------------------------- BelId Chip::getBelByName(IdString name) const { return BelId(); } diff --git a/ice40/chip.cc b/ice40/chip.cc index 88fcc512..918a7fb4 100644 --- a/ice40/chip.cc +++ b/ice40/chip.cc @@ -52,27 +52,25 @@ BelType belTypeFromId(IdString id) // ----------------------------------------------------------------------- -IdString portPinToId(PortPin type) +void IdString::initialize_chip() { -#define X(t) \ - if (type == PIN_##t) \ - return #t; - +#define X(t) initialize_add(#t, PIN_##t); #include "portpins.inc" - #undef X - return IdString(); } -PortPin portPinFromId(IdString id) +IdString portPinToId(PortPin type) { -#define X(t) \ - if (id == #t) \ - return PIN_##t; - -#include "portpins.inc" + IdString ret; + if (type > 0 && type < PIN_MAXIDX) + ret.index = type; + return ret; +} -#undef X +PortPin portPinFromId(IdString id) +{ + if (id.index > 0 && id.index < PIN_MAXIDX) + return PortPin(id.index); return PIN_NONE; } diff --git a/ice40/chip.h b/ice40/chip.h index 73e8d33b..f8946610 100644 --- a/ice40/chip.h +++ b/ice40/chip.h @@ -62,6 +62,7 @@ enum PortPin #define X(t) PIN_##t, #include "portpins.inc" #undef X + PIN_MAXIDX }; IdString portPinToId(PortPin type); |