Towards IdString as per-context facility
Signed-off-by: Clifford Wolf <clifford@clifford.at>
This commit is contained in:
parent
3fe353ea03
commit
babd5f39ab
@ -21,23 +21,36 @@
|
|||||||
|
|
||||||
NEXTPNR_NAMESPACE_BEGIN
|
NEXTPNR_NAMESPACE_BEGIN
|
||||||
|
|
||||||
std::unordered_map<std::string, int> *IdString::database_str_to_idx = nullptr;
|
Context *IdString::global_ctx = nullptr;
|
||||||
std::vector<const std::string *> *IdString::database_idx_to_str = nullptr;
|
|
||||||
|
|
||||||
void IdString::initialize()
|
void IdString::set(Context *ctx, const std::string &s)
|
||||||
{
|
{
|
||||||
database_str_to_idx = new std::unordered_map<std::string, int>;
|
auto it = ctx->idstring_str_to_idx->find(s);
|
||||||
database_idx_to_str = new std::vector<const std::string *>;
|
if (it == ctx->idstring_str_to_idx->end()) {
|
||||||
initialize_add("", 0);
|
index = ctx->idstring_idx_to_str->size();
|
||||||
initialize_chip();
|
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);
|
return *ctx->idstring_idx_to_str->at(index);
|
||||||
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);
|
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
|
NEXTPNR_NAMESPACE_END
|
||||||
|
115
common/nextpnr.h
115
common/nextpnr.h
@ -41,72 +41,95 @@
|
|||||||
|
|
||||||
NEXTPNR_NAMESPACE_BEGIN
|
NEXTPNR_NAMESPACE_BEGIN
|
||||||
|
|
||||||
|
struct Context;
|
||||||
|
|
||||||
struct IdString
|
struct IdString
|
||||||
{
|
{
|
||||||
int index = 0;
|
int index = 0;
|
||||||
|
|
||||||
static std::unordered_map<std::string, int> *database_str_to_idx;
|
static Context *global_ctx;
|
||||||
static std::vector<const std::string *> *database_idx_to_str;
|
|
||||||
|
|
||||||
static void initialize();
|
static void initialize_arch(Context *ctx);
|
||||||
static void initialize_chip();
|
static void initialize_add(Context *ctx, const char *s, int idx);
|
||||||
static void initialize_add(const char *s, int idx);
|
|
||||||
|
|
||||||
IdString() {}
|
IdString() {}
|
||||||
|
|
||||||
IdString(const std::string &s)
|
void set(Context *ctx, const std::string &s);
|
||||||
{
|
|
||||||
if (database_str_to_idx == nullptr)
|
|
||||||
initialize();
|
|
||||||
|
|
||||||
auto it = database_str_to_idx->find(s);
|
IdString(Context *ctx, const std::string &s)
|
||||||
if (it == database_str_to_idx->end()) {
|
{
|
||||||
index = database_idx_to_str->size();
|
assert(global_ctx != nullptr);
|
||||||
auto insert_rc = database_str_to_idx->insert({s, index});
|
set(global_ctx, s);
|
||||||
database_idx_to_str->push_back(&insert_rc.first->first);
|
|
||||||
} else {
|
|
||||||
index = it->second;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
IdString(const char *s)
|
IdString(Context *ctx, const char *s)
|
||||||
{
|
{
|
||||||
if (database_str_to_idx == nullptr)
|
assert(global_ctx != nullptr);
|
||||||
initialize();
|
set(global_ctx, 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string &str() const { return *database_idx_to_str->at(index); }
|
const std::string &str(Context *ctx) const;
|
||||||
const char *c_str() const { return str().c_str(); }
|
const char *c_str(Context *ctx) const;
|
||||||
|
|
||||||
operator const char *() const { return c_str(); }
|
bool operator<(const IdString &other) const
|
||||||
operator const std::string &() const { return str(); }
|
{
|
||||||
|
return index < other.index;
|
||||||
|
}
|
||||||
|
|
||||||
bool operator<(const IdString &other) const { return index < other.index; }
|
|
||||||
bool operator==(const IdString &other) const
|
bool operator==(const IdString &other) const
|
||||||
{
|
{
|
||||||
return index == other.index;
|
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
|
bool operator!=(const IdString &other) const
|
||||||
{
|
{
|
||||||
return index != other.index;
|
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 std::string &s) const { return str() != s; }
|
||||||
bool operator!=(const char *s) const { return str() != s; }
|
bool operator!=(const char *s) const { return str() != s; }
|
||||||
|
|
||||||
size_t size() const { return str().size(); }
|
size_t size() const { return str().size(); }
|
||||||
bool empty() const { return index == 0; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
NEXTPNR_NAMESPACE_END
|
NEXTPNR_NAMESPACE_END
|
||||||
@ -191,11 +214,29 @@ struct CellInfo
|
|||||||
|
|
||||||
struct Context : Arch
|
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, NetInfo *> nets;
|
||||||
std::unordered_map<IdString, CellInfo *> cells;
|
std::unordered_map<IdString, CellInfo *> cells;
|
||||||
|
|
||||||
Context(ArchArgs args) : Arch(args)
|
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);
|
||||||
|
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -124,7 +124,7 @@ BOOST_PYTHON_MODULE(MODULE_NAME)
|
|||||||
def("load_design", load_design_shim);
|
def("load_design", load_design_shim);
|
||||||
|
|
||||||
class_<IdString>("IdString")
|
class_<IdString>("IdString")
|
||||||
.def("__str__", &IdString::str,
|
.def("__str__", &IdString::global_str,
|
||||||
return_value_policy<copy_const_reference>())
|
return_value_policy<copy_const_reference>())
|
||||||
.def(self < self)
|
.def(self < self)
|
||||||
.def(self == self);
|
.def(self == self);
|
||||||
|
@ -26,7 +26,7 @@ Arch::Arch(ArchArgs) {}
|
|||||||
|
|
||||||
std::string Arch::getChipName() { return "Dummy"; }
|
std::string Arch::getChipName() { return "Dummy"; }
|
||||||
|
|
||||||
void IdString::initialize_chip() {}
|
void IdString::initialize_arch(Context *ctx) {}
|
||||||
|
|
||||||
// ---------------------------------------------------------------
|
// ---------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -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"
|
#include "portpins.inc"
|
||||||
#undef X
|
#undef X
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user