Towards IdString as per-context facility

Signed-off-by: Clifford Wolf <clifford@clifford.at>
This commit is contained in:
Clifford Wolf 2018-06-18 14:53:01 +02:00
parent 3fe353ea03
commit babd5f39ab
5 changed files with 107 additions and 53 deletions

View File

@ -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

View File

@ -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)
{
if (database_str_to_idx == nullptr)
initialize();
void set(Context *ctx, const std::string &s);
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(Context *ctx, const std::string &s)
{
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);
// ...
}
};

View File

@ -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);

View File

@ -26,7 +26,7 @@ Arch::Arch(ArchArgs) {}
std::string Arch::getChipName() { return "Dummy"; }
void IdString::initialize_chip() {}
void IdString::initialize_arch(Context *ctx) {}
// ---------------------------------------------------------------

View File

@ -54,9 +54,9 @@ BelType belTypeFromId(IdString id)
// -----------------------------------------------------------------------
void IdString::initialize_chip()
void IdString::initialize_arch(Context *ctx)
{
#define X(t) initialize_add(#t, PIN_##t);
#define X(t) initialize_add(ctx, #t, PIN_##t);
#include "portpins.inc"
#undef X
}