diff options
author | David Shah <davey1576@gmail.com> | 2018-07-04 12:04:26 +0200 |
---|---|---|
committer | David Shah <davey1576@gmail.com> | 2018-07-04 12:04:26 +0200 |
commit | c9d1bce85994756f196a6eb22926ba4acaf42aec (patch) | |
tree | 90b09464435497e78806c3389a0e574be297f4b6 /common | |
parent | fd3c124f8738de3cbd380906b70135fad8c09d87 (diff) | |
download | nextpnr-c9d1bce85994756f196a6eb22926ba4acaf42aec.tar.gz nextpnr-c9d1bce85994756f196a6eb22926ba4acaf42aec.tar.bz2 nextpnr-c9d1bce85994756f196a6eb22926ba4acaf42aec.zip |
common: Adding NPNR_ASSERT
Signed-off-by: David Shah <davey1576@gmail.com>
Diffstat (limited to 'common')
-rw-r--r-- | common/nextpnr.cc | 6 | ||||
-rw-r--r-- | common/nextpnr.h | 52 |
2 files changed, 45 insertions, 13 deletions
diff --git a/common/nextpnr.cc b/common/nextpnr.cc index 1ca3766a..40eb2536 100644 --- a/common/nextpnr.cc +++ b/common/nextpnr.cc @@ -21,6 +21,12 @@ NEXTPNR_NAMESPACE_BEGIN +assertion_failure::assertion_failure(std::string msg, std::string expr_str, std::string filename, int line) + : runtime_error("Assertion failure: " + msg + " (" + filename + ":" + std::to_string(line) + ")"), msg(msg), + expr_str(expr_str), filename(filename), line(line) +{ +} + std::unordered_set<BaseCtx *> IdString::global_ctx; void IdString::set(const BaseCtx *ctx, const std::string &s) diff --git a/common/nextpnr.h b/common/nextpnr.h index 96511acf..1c7571cc 100644 --- a/common/nextpnr.h +++ b/common/nextpnr.h @@ -20,6 +20,7 @@ #include <algorithm> #include <assert.h> #include <memory> +#include <stdexcept> #include <stdint.h> #include <string> #include <unordered_map> @@ -42,24 +43,44 @@ #endif #if defined(__GNUC__) || defined(__clang__) -# define NPNR_ATTRIBUTE(...) __attribute__((__VA_ARGS__)) -# define NPNR_NORETURN -# define NPNR_DEPRECATED __attribute__((deprecated)) -# define NPNR_PACKED_STRUCT( ... ) __VA_ARGS__ __attribute__((packed)) +#define NPNR_ATTRIBUTE(...) __attribute__((__VA_ARGS__)) +#define NPNR_NORETURN +#define NPNR_DEPRECATED __attribute__((deprecated)) +#define NPNR_PACKED_STRUCT(...) __VA_ARGS__ __attribute__((packed)) #elif defined(_MSC_VER) -# define NPNR_ATTRIBUTE(...) -# define NPNR_NORETURN __declspec(noreturn) -# define NPNR_DEPRECATED __declspec(deprecated) -# define NPNR_PACKED_STRUCT( ... ) __pragma(pack(push, 1)) __VA_ARGS__ __pragma(pack(pop)) +#define NPNR_ATTRIBUTE(...) +#define NPNR_NORETURN __declspec(noreturn) +#define NPNR_DEPRECATED __declspec(deprecated) +#define NPNR_PACKED_STRUCT(...) __pragma(pack(push, 1)) __VA_ARGS__ __pragma(pack(pop)) #else -# define NPNR_ATTRIBUTE(...) -# define NPNR_NORETURN -# define NPNR_DEPRECATED -# define NPNR_PACKED_STRUCT( ... ) __VA_ARGS__ -#endif +#define NPNR_ATTRIBUTE(...) +#define NPNR_NORETURN +#define NPNR_DEPRECATED +#define NPNR_PACKED_STRUCT(...) __VA_ARGS__ +#endif NEXTPNR_NAMESPACE_BEGIN +class assertion_failure : std::runtime_error +{ + public: + assertion_failure(std::string msg, std::string expr_str, std::string filename, int line); + + std::string msg; + std::string expr_str; + std::string filename; + int line; +}; + +inline void except_assert_impl(bool expr, std::string message, std::string expr_str, std::string filename, int line) +{ + if (!expr) + throw assertion_failure(message, expr_str, filename, line); +} + +#define NPNR_ASSERT(cond) except_assert_impl((cond), #cond, #cond, __FILE__, __LINE__) +#define NPNR_ASSERT_MSG(cond, msg) except_assert_impl((cond), msg, #cond, __FILE__, __LINE__) + struct BaseCtx; struct Context; @@ -68,6 +89,7 @@ struct IdString int index = 0; static void initialize_arch(const BaseCtx *ctx); + static void initialize_add(const BaseCtx *ctx, const char *s, int idx); IdString() {} @@ -79,6 +101,7 @@ struct IdString IdString(const BaseCtx *ctx, const char *s) { set(ctx, s); } 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; } @@ -212,6 +235,7 @@ struct BaseCtx mutable std::vector<const std::string *> *idstring_idx_to_str; IdString id(const std::string &s) const { return IdString(this, s); } + IdString id(const char *s) const { return IdString(this, s); } // -------------------------------------------------------------- @@ -250,6 +274,7 @@ struct Context : Arch bool force = false; bool timing_driven = true; float target_freq = 12e6; + Context(ArchArgs args) : Arch(args) {} // -------------------------------------------------------------- @@ -315,6 +340,7 @@ struct Context : Arch } uint32_t checksum() const; + void check() const; }; |