Refactor Arch/Context design hierarchy

Signed-off-by: Clifford Wolf <clifford@clifford.at>
This commit is contained in:
Clifford Wolf 2018-06-19 12:08:37 +02:00
parent e3519ddfcd
commit c910846c5c
6 changed files with 68 additions and 51 deletions

View File

@ -21,9 +21,9 @@
NEXTPNR_NAMESPACE_BEGIN NEXTPNR_NAMESPACE_BEGIN
Context *IdString::global_ctx = nullptr; BaseCtx *IdString::global_ctx = nullptr;
void IdString::set(const Context *ctx, const std::string &s) void IdString::set(const BaseCtx *ctx, const std::string &s)
{ {
auto it = ctx->idstring_str_to_idx->find(s); auto it = ctx->idstring_str_to_idx->find(s);
if (it == ctx->idstring_str_to_idx->end()) { if (it == ctx->idstring_str_to_idx->end()) {
@ -35,17 +35,17 @@ void IdString::set(const Context *ctx, const std::string &s)
} }
} }
const std::string &IdString::str(const Context *ctx) const const std::string &IdString::str(const BaseCtx *ctx) const
{ {
return *ctx->idstring_idx_to_str->at(index); return *ctx->idstring_idx_to_str->at(index);
} }
const char *IdString::c_str(const Context *ctx) const const char *IdString::c_str(const BaseCtx *ctx) const
{ {
return str(ctx).c_str(); return str(ctx).c_str();
} }
void IdString::initialize_add(const Context *ctx, const char *s, int idx) void IdString::initialize_add(const BaseCtx *ctx, const char *s, int idx)
{ {
assert(ctx->idstring_str_to_idx->count(s) == 0); assert(ctx->idstring_str_to_idx->count(s) == 0);
assert(int(ctx->idstring_idx_to_str->size()) == idx); assert(int(ctx->idstring_idx_to_str->size()) == idx);

View File

@ -41,27 +41,28 @@
NEXTPNR_NAMESPACE_BEGIN NEXTPNR_NAMESPACE_BEGIN
struct BaseCtx;
struct Context; struct Context;
struct IdString struct IdString
{ {
int index = 0; int index = 0;
static Context *global_ctx; static BaseCtx *global_ctx;
static void initialize_arch(const Context *ctx); static void initialize_arch(const BaseCtx *ctx);
static void initialize_add(const Context *ctx, const char *s, int idx); static void initialize_add(const BaseCtx *ctx, const char *s, int idx);
IdString() {} IdString() {}
void set(const Context *ctx, const std::string &s); void set(const BaseCtx *ctx, const std::string &s);
IdString(const Context *ctx, const std::string &s) { set(ctx, s); } IdString(const BaseCtx *ctx, const std::string &s) { set(ctx, s); }
IdString(const Context *ctx, const char *s) { set(ctx, s); } IdString(const BaseCtx *ctx, const char *s) { set(ctx, s); }
const std::string &str(const Context *ctx) const; const std::string &str(const BaseCtx *ctx) const;
const char *c_str(const Context *ctx) const; const char *c_str(const BaseCtx *ctx) const;
bool operator<(const IdString &other) const { return index < other.index; } bool operator<(const IdString &other) const { return index < other.index; }
@ -184,7 +185,9 @@ struct GraphicElement
NEXTPNR_NAMESPACE_END NEXTPNR_NAMESPACE_END
#define NEXTPNR_ARCH_TOP
#include "arch.h" #include "arch.h"
#undef NEXTPNR_ARCH_TOP
NEXTPNR_NAMESPACE_BEGIN NEXTPNR_NAMESPACE_BEGIN
@ -232,25 +235,22 @@ struct CellInfo
std::unordered_map<IdString, IdString> pins; std::unordered_map<IdString, IdString> pins;
}; };
struct Context : Arch struct BaseCtx
{ {
// -------------------------------------------------------------- // --------------------------------------------------------------
mutable std::unordered_map<std::string, int> *idstring_str_to_idx; mutable std::unordered_map<std::string, int> *idstring_str_to_idx;
mutable std::vector<const std::string *> *idstring_idx_to_str; mutable std::vector<const std::string *> *idstring_idx_to_str;
IdString id(const std::string &s) const override IdString id(const std::string &s) const { return IdString(this, s); }
{ IdString id(const char *s) const { return IdString(this, s); }
return IdString(this, s);
}
IdString id(const char *s) const override { 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) BaseCtx()
{ {
assert(IdString::global_ctx == nullptr); assert(IdString::global_ctx == nullptr);
IdString::global_ctx = this; IdString::global_ctx = this;
@ -259,11 +259,24 @@ struct Context : Arch
idstring_idx_to_str = new std::vector<const std::string *>; idstring_idx_to_str = new std::vector<const std::string *>;
IdString::initialize_add(this, "", 0); IdString::initialize_add(this, "", 0);
IdString::initialize_arch(this); IdString::initialize_arch(this);
// ...
} }
}; };
NEXTPNR_NAMESPACE_END NEXTPNR_NAMESPACE_END
#define NEXTPNR_ARCH_BOTTOM
#include "arch.h"
#undef NEXTPNR_ARCH_BOTTOM
NEXTPNR_NAMESPACE_BEGIN
struct Context : Arch
{
bool verbose = false;
Context(ArchArgs args) : Arch(args) {}
};
NEXTPNR_NAMESPACE_END
#endif #endif

View File

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

View File

@ -17,13 +17,12 @@
* *
*/ */
#ifndef CHIP_H
#define CHIP_H
#ifndef NEXTPNR_H #ifndef NEXTPNR_H
#error Include "arch.h" via "nextpnr.h" only. #error Include "arch.h" via "nextpnr.h" only.
#endif #endif
#ifdef NEXTPNR_ARCH_TOP
NEXTPNR_NAMESPACE_BEGIN NEXTPNR_NAMESPACE_BEGIN
typedef float delay_t; typedef float delay_t;
@ -57,11 +56,19 @@ struct BelPin
PortPin pin; PortPin pin;
}; };
NEXTPNR_NAMESPACE_END
#endif // NEXTPNR_ARCH_TOP
#ifdef NEXTPNR_ARCH_BOTTOM
NEXTPNR_NAMESPACE_BEGIN
struct ArchArgs struct ArchArgs
{ {
}; };
struct Arch struct Arch : BaseCtx
{ {
Arch(ArchArgs args); Arch(ArchArgs args);
@ -128,4 +135,4 @@ struct Arch
NEXTPNR_NAMESPACE_END NEXTPNR_NAMESPACE_END
#endif #endif // NEXTPNR_ARCH_BOTTOM

View File

@ -54,7 +54,7 @@ BelType Arch::belTypeFromId(IdString type) const
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
void IdString::initialize_arch(const Context *ctx) void IdString::initialize_arch(const BaseCtx *ctx)
{ {
#define X(t) initialize_add(ctx, #t, PIN_##t); #define X(t) initialize_add(ctx, #t, PIN_##t);
#include "portpins.inc" #include "portpins.inc"

View File

@ -17,13 +17,12 @@
* *
*/ */
#ifndef CHIP_H
#define CHIP_H
#ifndef NEXTPNR_H #ifndef NEXTPNR_H
#error Include "arch.h" via "nextpnr.h" only. #error Include "arch.h" via "nextpnr.h" only.
#endif #endif
#ifdef NEXTPNR_ARCH_TOP
NEXTPNR_NAMESPACE_BEGIN NEXTPNR_NAMESPACE_BEGIN
typedef int delay_t; typedef int delay_t;
@ -258,7 +257,7 @@ template <> struct hash<NEXTPNR_NAMESPACE_PREFIX BelId>
std::size_t operator()(const NEXTPNR_NAMESPACE_PREFIX BelId &bel) const std::size_t operator()(const NEXTPNR_NAMESPACE_PREFIX BelId &bel) const
noexcept noexcept
{ {
return bel.index; return hash<int>()(bel.index);
} }
}; };
@ -267,18 +266,21 @@ template <> struct hash<NEXTPNR_NAMESPACE_PREFIX WireId>
std::size_t operator()(const NEXTPNR_NAMESPACE_PREFIX WireId &wire) const std::size_t operator()(const NEXTPNR_NAMESPACE_PREFIX WireId &wire) const
noexcept noexcept
{ {
return wire.index; return hash<int>()(wire.index);
} }
}; };
template <> struct hash<NEXTPNR_NAMESPACE_PREFIX PipId> template <> struct hash<NEXTPNR_NAMESPACE_PREFIX PipId>
{ {
std::size_t operator()(const NEXTPNR_NAMESPACE_PREFIX PipId &wire) const std::size_t operator()(const NEXTPNR_NAMESPACE_PREFIX PipId &pip) const
noexcept noexcept
{ {
return wire.index; return hash<int>()(pip.index);
} }
}; };
template <> struct hash<NEXTPNR_NAMESPACE_PREFIX BelType> : hash<int> {};
template <> struct hash<NEXTPNR_NAMESPACE_PREFIX PortPin> : hash<int> {};
} // namespace std } // namespace std
NEXTPNR_NAMESPACE_BEGIN NEXTPNR_NAMESPACE_BEGIN
@ -435,8 +437,16 @@ struct PipRange
PipIterator end() const { return e; } PipIterator end() const { return e; }
}; };
NEXTPNR_NAMESPACE_END
#endif // NEXTPNR_ARCH_TOP
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
#ifdef NEXTPNR_ARCH_BOTTOM
NEXTPNR_NAMESPACE_BEGIN
struct ArchArgs struct ArchArgs
{ {
enum enum
@ -452,7 +462,7 @@ struct ArchArgs
std::string package; std::string package;
}; };
struct Arch struct Arch : BaseCtx
{ {
const ChipInfoPOD *chip_info; const ChipInfoPOD *chip_info;
const PackageInfoPOD *package_info; const PackageInfoPOD *package_info;
@ -471,9 +481,6 @@ struct Arch
std::string getChipName(); std::string getChipName();
virtual IdString id(const std::string &s) const { abort(); }
virtual IdString id(const char *s) const { abort(); }
IdString belTypeToId(BelType type) const; IdString belTypeToId(BelType type) const;
BelType belTypeFromId(IdString id) const; BelType belTypeFromId(IdString id) const;
@ -747,14 +754,4 @@ struct Arch
NEXTPNR_NAMESPACE_END NEXTPNR_NAMESPACE_END
namespace std { #endif // NEXTPNR_ARCH_BOTTOM
template <> struct hash<NEXTPNR_NAMESPACE_PREFIX BelType>
{
std::size_t operator()(NEXTPNR_NAMESPACE_PREFIX BelType bt) const noexcept
{
return std::hash<int>()(int(bt));
}
};
} // namespace std
#endif