common: Adding NPNR_ASSERT

Signed-off-by: David Shah <davey1576@gmail.com>
This commit is contained in:
David Shah 2018-07-04 12:04:26 +02:00
parent fd3c124f87
commit c9d1bce859
2 changed files with 45 additions and 13 deletions

View File

@ -21,6 +21,12 @@
NEXTPNR_NAMESPACE_BEGIN 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; std::unordered_set<BaseCtx *> IdString::global_ctx;
void IdString::set(const BaseCtx *ctx, const std::string &s) void IdString::set(const BaseCtx *ctx, const std::string &s)

View File

@ -20,6 +20,7 @@
#include <algorithm> #include <algorithm>
#include <assert.h> #include <assert.h>
#include <memory> #include <memory>
#include <stdexcept>
#include <stdint.h> #include <stdint.h>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
@ -60,6 +61,26 @@
NEXTPNR_NAMESPACE_BEGIN 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 BaseCtx;
struct Context; struct Context;
@ -68,6 +89,7 @@ struct IdString
int index = 0; int index = 0;
static void initialize_arch(const BaseCtx *ctx); static void initialize_arch(const BaseCtx *ctx);
static void initialize_add(const BaseCtx *ctx, const char *s, int idx); static void initialize_add(const BaseCtx *ctx, const char *s, int idx);
IdString() {} IdString() {}
@ -79,6 +101,7 @@ struct IdString
IdString(const BaseCtx *ctx, const char *s) { set(ctx, s); } IdString(const BaseCtx *ctx, const char *s) { set(ctx, s); }
const std::string &str(const BaseCtx *ctx) const; const std::string &str(const BaseCtx *ctx) const;
const char *c_str(const BaseCtx *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; }
@ -212,6 +235,7 @@ struct BaseCtx
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 { 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); } IdString id(const char *s) const { return IdString(this, s); }
// -------------------------------------------------------------- // --------------------------------------------------------------
@ -250,6 +274,7 @@ struct Context : Arch
bool force = false; bool force = false;
bool timing_driven = true; bool timing_driven = true;
float target_freq = 12e6; float target_freq = 12e6;
Context(ArchArgs args) : Arch(args) {} Context(ArchArgs args) : Arch(args) {}
// -------------------------------------------------------------- // --------------------------------------------------------------
@ -315,6 +340,7 @@ struct Context : Arch
} }
uint32_t checksum() const; uint32_t checksum() const;
void check() const; void check() const;
}; };