From c910846c5cefe03ce60d50418389f158846f8341 Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Tue, 19 Jun 2018 12:08:37 +0200 Subject: [PATCH] Refactor Arch/Context design hierarchy Signed-off-by: Clifford Wolf --- common/nextpnr.cc | 10 +++++----- common/nextpnr.h | 47 ++++++++++++++++++++++++++++++----------------- dummy/arch.cc | 2 +- dummy/arch.h | 17 ++++++++++++----- ice40/arch.cc | 2 +- ice40/arch.h | 41 +++++++++++++++++++---------------------- 6 files changed, 68 insertions(+), 51 deletions(-) diff --git a/common/nextpnr.cc b/common/nextpnr.cc index dbe2a6f7..b093d855 100644 --- a/common/nextpnr.cc +++ b/common/nextpnr.cc @@ -21,9 +21,9 @@ 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); 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); } -const char *IdString::c_str(const Context *ctx) const +const char *IdString::c_str(const BaseCtx *ctx) const { 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(int(ctx->idstring_idx_to_str->size()) == idx); diff --git a/common/nextpnr.h b/common/nextpnr.h index b8305247..08c941a5 100644 --- a/common/nextpnr.h +++ b/common/nextpnr.h @@ -41,27 +41,28 @@ NEXTPNR_NAMESPACE_BEGIN +struct BaseCtx; struct Context; struct IdString { int index = 0; - static Context *global_ctx; + static BaseCtx *global_ctx; - static void initialize_arch(const Context *ctx); - static void initialize_add(const Context *ctx, const char *s, int idx); + static void initialize_arch(const BaseCtx *ctx); + static void initialize_add(const BaseCtx *ctx, const char *s, int idx); 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 char *c_str(const Context *ctx) const; + const std::string &str(const BaseCtx *ctx) const; + const char *c_str(const BaseCtx *ctx) const; bool operator<(const IdString &other) const { return index < other.index; } @@ -184,7 +185,9 @@ struct GraphicElement NEXTPNR_NAMESPACE_END +#define NEXTPNR_ARCH_TOP #include "arch.h" +#undef NEXTPNR_ARCH_TOP NEXTPNR_NAMESPACE_BEGIN @@ -232,25 +235,22 @@ struct CellInfo std::unordered_map pins; }; -struct Context : Arch +struct BaseCtx { // -------------------------------------------------------------- mutable std::unordered_map *idstring_str_to_idx; mutable std::vector *idstring_idx_to_str; - IdString id(const std::string &s) const override - { - return IdString(this, s); - } - IdString id(const char *s) const override { return IdString(this, s); } + IdString id(const std::string &s) const { return IdString(this, s); } + IdString id(const char *s) const { return IdString(this, s); } // -------------------------------------------------------------- std::unordered_map nets; std::unordered_map cells; - Context(ArchArgs args) : Arch(args) + BaseCtx() { assert(IdString::global_ctx == nullptr); IdString::global_ctx = this; @@ -259,11 +259,24 @@ struct Context : Arch idstring_idx_to_str = new std::vector; IdString::initialize_add(this, "", 0); IdString::initialize_arch(this); - - // ... } }; 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 diff --git a/dummy/arch.cc b/dummy/arch.cc index 3ef6a4d3..fb54c74f 100644 --- a/dummy/arch.cc +++ b/dummy/arch.cc @@ -26,7 +26,7 @@ Arch::Arch(ArchArgs) {} std::string Arch::getChipName() { return "Dummy"; } -void IdString::initialize_arch(const Context *ctx) {} +void IdString::initialize_arch(const BaseCtx *ctx) {} // --------------------------------------------------------------- diff --git a/dummy/arch.h b/dummy/arch.h index 865536dc..02bec23a 100644 --- a/dummy/arch.h +++ b/dummy/arch.h @@ -17,13 +17,12 @@ * */ -#ifndef CHIP_H -#define CHIP_H - #ifndef NEXTPNR_H #error Include "arch.h" via "nextpnr.h" only. #endif +#ifdef NEXTPNR_ARCH_TOP + NEXTPNR_NAMESPACE_BEGIN typedef float delay_t; @@ -57,11 +56,19 @@ struct BelPin PortPin pin; }; +NEXTPNR_NAMESPACE_END + +#endif // NEXTPNR_ARCH_TOP + +#ifdef NEXTPNR_ARCH_BOTTOM + +NEXTPNR_NAMESPACE_BEGIN + struct ArchArgs { }; -struct Arch +struct Arch : BaseCtx { Arch(ArchArgs args); @@ -128,4 +135,4 @@ struct Arch NEXTPNR_NAMESPACE_END -#endif +#endif // NEXTPNR_ARCH_BOTTOM diff --git a/ice40/arch.cc b/ice40/arch.cc index 83a6e542..ba372410 100644 --- a/ice40/arch.cc +++ b/ice40/arch.cc @@ -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); #include "portpins.inc" diff --git a/ice40/arch.h b/ice40/arch.h index 2702e70e..0fdacfde 100644 --- a/ice40/arch.h +++ b/ice40/arch.h @@ -17,13 +17,12 @@ * */ -#ifndef CHIP_H -#define CHIP_H - #ifndef NEXTPNR_H #error Include "arch.h" via "nextpnr.h" only. #endif +#ifdef NEXTPNR_ARCH_TOP + NEXTPNR_NAMESPACE_BEGIN typedef int delay_t; @@ -258,7 +257,7 @@ template <> struct hash std::size_t operator()(const NEXTPNR_NAMESPACE_PREFIX BelId &bel) const noexcept { - return bel.index; + return hash()(bel.index); } }; @@ -267,18 +266,21 @@ template <> struct hash std::size_t operator()(const NEXTPNR_NAMESPACE_PREFIX WireId &wire) const noexcept { - return wire.index; + return hash()(wire.index); } }; template <> struct hash { - std::size_t operator()(const NEXTPNR_NAMESPACE_PREFIX PipId &wire) const + std::size_t operator()(const NEXTPNR_NAMESPACE_PREFIX PipId &pip) const noexcept { - return wire.index; + return hash()(pip.index); } }; + +template <> struct hash : hash {}; +template <> struct hash : hash {}; } // namespace std NEXTPNR_NAMESPACE_BEGIN @@ -435,8 +437,16 @@ struct PipRange PipIterator end() const { return e; } }; +NEXTPNR_NAMESPACE_END + +#endif // NEXTPNR_ARCH_TOP + // ----------------------------------------------------------------------- +#ifdef NEXTPNR_ARCH_BOTTOM + +NEXTPNR_NAMESPACE_BEGIN + struct ArchArgs { enum @@ -452,7 +462,7 @@ struct ArchArgs std::string package; }; -struct Arch +struct Arch : BaseCtx { const ChipInfoPOD *chip_info; const PackageInfoPOD *package_info; @@ -471,9 +481,6 @@ struct Arch 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; BelType belTypeFromId(IdString id) const; @@ -747,14 +754,4 @@ struct Arch NEXTPNR_NAMESPACE_END -namespace std { -template <> struct hash -{ - std::size_t operator()(NEXTPNR_NAMESPACE_PREFIX BelType bt) const noexcept - { - return std::hash()(int(bt)); - } -}; -} // namespace std - -#endif +#endif // NEXTPNR_ARCH_BOTTOM