From 75ee2fc4e6ede1ad731b9451fd606c61f5892801 Mon Sep 17 00:00:00 2001 From: "D. Shah" Date: Wed, 27 Jan 2021 20:43:01 +0000 Subject: [PATCH] Move RelPtr/RelSlice out of arches into common The bba approach seems widely used enough that it's reasonable for this to become part of common code. Signed-off-by: D. Shah --- common/relptr.h | 39 +++++++++++++++++++++++++++++++++++++++ ecp5/arch.h | 38 +------------------------------------- ice40/arch.h | 43 +------------------------------------------ nexus/arch.h | 40 +--------------------------------------- 4 files changed, 42 insertions(+), 118 deletions(-) create mode 100644 common/relptr.h diff --git a/common/relptr.h b/common/relptr.h new file mode 100644 index 00000000..f8f1d429 --- /dev/null +++ b/common/relptr.h @@ -0,0 +1,39 @@ +// This is intended to be included inside arch.h only. + +template struct RelPtr +{ + int32_t offset; + + const T *get() const { return reinterpret_cast(reinterpret_cast(this) + offset); } + + const T &operator[](size_t index) const { return get()[index]; } + + const T &operator*() const { return *(get()); } + + const T *operator->() const { return get(); } + + RelPtr(const RelPtr &) = delete; + RelPtr &operator=(const RelPtr &) = delete; +}; + +NPNR_PACKED_STRUCT(template struct RelSlice { + int32_t offset; + uint32_t length; + + const T *get() const { return reinterpret_cast(reinterpret_cast(this) + offset); } + + const T &operator[](size_t index) const + { + NPNR_ASSERT(index < length); + return get()[index]; + } + + const T *begin() const { return get(); } + const T *end() const { return get() + length; } + + const size_t size() const { return length; } + + const T &operator*() const { return *(get()); } + + const T *operator->() const { return get(); } +}); diff --git a/ecp5/arch.h b/ecp5/arch.h index 458ff935..58373157 100644 --- a/ecp5/arch.h +++ b/ecp5/arch.h @@ -29,43 +29,7 @@ NEXTPNR_NAMESPACE_BEGIN /**** Everything in this section must be kept in sync with chipdb.py ****/ -template struct RelPtr -{ - int32_t offset; - - const T *get() const { return reinterpret_cast(reinterpret_cast(this) + offset); } - - const T &operator[](size_t index) const { return get()[index]; } - - const T &operator*() const { return *(get()); } - - const T *operator->() const { return get(); } - - RelPtr(const RelPtr &) = delete; - RelPtr &operator=(const RelPtr &) = delete; -}; - -NPNR_PACKED_STRUCT(template struct RelSlice { - int32_t offset; - uint32_t length; - - const T *get() const { return reinterpret_cast(reinterpret_cast(this) + offset); } - - const T &operator[](size_t index) const - { - NPNR_ASSERT(index < length); - return get()[index]; - } - - const T *begin() const { return get(); } - const T *end() const { return get() + length; } - - const size_t size() const { return length; } - - const T &operator*() const { return *(get()); } - - const T *operator->() const { return get(); } -}); +#include "relptr.h" NPNR_PACKED_STRUCT(struct BelWirePOD { LocationPOD rel_wire_loc; diff --git a/ice40/arch.h b/ice40/arch.h index c9632f3e..1de39c33 100644 --- a/ice40/arch.h +++ b/ice40/arch.h @@ -25,48 +25,7 @@ NEXTPNR_NAMESPACE_BEGIN /**** Everything in this section must be kept in sync with chipdb.py ****/ -template struct RelPtr -{ - int32_t offset; - - // void set(const T *ptr) { - // offset = reinterpret_cast(ptr) - - // reinterpret_cast(this); - // } - - const T *get() const { return reinterpret_cast(reinterpret_cast(this) + offset); } - - const T &operator[](size_t index) const { return get()[index]; } - - const T &operator*() const { return *(get()); } - - const T *operator->() const { return get(); } - - RelPtr(const RelPtr &) = delete; - RelPtr &operator=(const RelPtr &) = delete; -}; - -NPNR_PACKED_STRUCT(template struct RelSlice { - int32_t offset; - uint32_t length; - - const T *get() const { return reinterpret_cast(reinterpret_cast(this) + offset); } - - const T &operator[](size_t index) const - { - NPNR_ASSERT(index < length); - return get()[index]; - } - - const T *begin() const { return get(); } - const T *end() const { return get() + length; } - - const size_t size() const { return length; } - - const T &operator*() const { return *(get()); } - - const T *operator->() const { return get(); } -}); +#include "relptr.h" NPNR_PACKED_STRUCT(struct BelWirePOD { int32_t port; diff --git a/nexus/arch.h b/nexus/arch.h index 00934797..e9984ee6 100644 --- a/nexus/arch.h +++ b/nexus/arch.h @@ -28,45 +28,7 @@ NEXTPNR_NAMESPACE_BEGIN -template struct RelPtr -{ - int32_t offset; - - // void set(const T *ptr) { - // offset = reinterpret_cast(ptr) - - // reinterpret_cast(this); - // } - - const T *get() const { return reinterpret_cast(reinterpret_cast(this) + offset); } - - const T &operator[](size_t index) const { return get()[index]; } - - const T &operator*() const { return *(get()); } - - const T *operator->() const { return get(); } -}; - -NPNR_PACKED_STRUCT(template struct RelSlice { - int32_t offset; - uint32_t length; - - const T *get() const { return reinterpret_cast(reinterpret_cast(this) + offset); } - - const T &operator[](size_t index) const - { - NPNR_ASSERT(index < length); - return get()[index]; - } - - const T *begin() const { return get(); } - const T *end() const { return get() + length; } - - const size_t size() const { return length; } - - const T &operator*() const { return *(get()); } - - const T *operator->() const { return get(); } -}); +#include "relptr.h" /* Fully deduplicated database