From 1839a3a770a71c928b92bf876e04728d2649e425 Mon Sep 17 00:00:00 2001 From: David Shah Date: Thu, 1 Aug 2019 14:28:21 +0100 Subject: Major Property improvements for common and iCE40 Signed-off-by: David Shah --- common/command.cc | 20 ++++----- common/nextpnr.cc | 100 +++++++++++++++++++++++++++++++++---------- common/nextpnr.h | 115 ++++++++++++++++++++++++++++++++++++++------------ common/placer1.cc | 2 +- common/placer_heap.cc | 2 +- common/pybindings.cc | 9 +++- common/util.h | 24 +++++++++++ 7 files changed, 210 insertions(+), 62 deletions(-) (limited to 'common') diff --git a/common/command.cc b/common/command.cc index 8acbafd2..ad5b6c54 100644 --- a/common/command.cc +++ b/common/command.cc @@ -184,9 +184,9 @@ void CommandHandler::setupContext(Context *ctx) } if (vm.count("slack_redist_iter")) { - ctx->settings[ctx->id("slack_redist_iter")] = vm["slack_redist_iter"].as(); + ctx->settings[ctx->id("slack_redist_iter")] = vm["slack_redist_iter"].as(); if (vm.count("freq") && vm["freq"].as() == 0) { - ctx->settings[ctx->id("auto_freq")] = std::to_string(true); + ctx->settings[ctx->id("auto_freq")] = true; #ifndef NO_GUI if (!vm.count("gui")) #endif @@ -195,11 +195,11 @@ void CommandHandler::setupContext(Context *ctx) } if (vm.count("ignore-loops")) { - ctx->settings[ctx->id("timing/ignoreLoops")] = std::to_string(true); + ctx->settings[ctx->id("timing/ignoreLoops")] = true; } if (vm.count("timing-allow-fail")) { - ctx->settings[ctx->id("timing/allowFail")] = std::to_string(true); + ctx->settings[ctx->id("timing/allowFail")] = true; } if (vm.count("placer")) { @@ -219,7 +219,7 @@ void CommandHandler::setupContext(Context *ctx) } if (vm.count("placer-budgets")) { - ctx->settings[ctx->id("placer1/budgetBased")] = std::to_string(true); + ctx->settings[ctx->id("placer1/budgetBased")] = true; } if (vm.count("freq")) { auto freq = vm["freq"].as(); @@ -228,23 +228,23 @@ void CommandHandler::setupContext(Context *ctx) } if (vm.count("no-tmdriv")) - ctx->settings[ctx->id("timing_driven")] = std::to_string(false); + ctx->settings[ctx->id("timing_driven")] = false; // Setting default values if (ctx->settings.find(ctx->id("target_freq")) == ctx->settings.end()) ctx->settings[ctx->id("target_freq")] = std::to_string(12e6); if (ctx->settings.find(ctx->id("timing_driven")) == ctx->settings.end()) - ctx->settings[ctx->id("timing_driven")] = std::to_string(true); + ctx->settings[ctx->id("timing_driven")] = true; if (ctx->settings.find(ctx->id("slack_redist_iter")) == ctx->settings.end()) - ctx->settings[ctx->id("slack_redist_iter")] = "0"; + ctx->settings[ctx->id("slack_redist_iter")] = 0; if (ctx->settings.find(ctx->id("auto_freq")) == ctx->settings.end()) - ctx->settings[ctx->id("auto_freq")] = std::to_string(false); + ctx->settings[ctx->id("auto_freq")] = false; if (ctx->settings.find(ctx->id("placer")) == ctx->settings.end()) ctx->settings[ctx->id("placer")] = Arch::defaultPlacer; ctx->settings[ctx->id("arch.name")] = std::string(ctx->archId().c_str(ctx)); ctx->settings[ctx->id("arch.type")] = std::string(ctx->archArgsToId(ctx->archArgs()).c_str(ctx)); - ctx->settings[ctx->id("seed")] = std::to_string(ctx->rngstate); + ctx->settings[ctx->id("seed")] = ctx->rngstate; } int CommandHandler::executeMain(std::unique_ptr ctx) diff --git a/common/nextpnr.cc b/common/nextpnr.cc index d4cc4917..8f172422 100644 --- a/common/nextpnr.cc +++ b/common/nextpnr.cc @@ -131,6 +131,60 @@ TimingConstrObjectId BaseCtx::timingPortObject(CellInfo *cell, IdString port) } } +Property::Property() : is_string(false), str(""), intval(0) {} + +Property::Property(int64_t intval, int width) : is_string(false), intval(intval) +{ + str.resize(width); + for (int i = 0; i < width; i++) + str.push_back((intval & (1ULL << i)) ? S1 : S0); +} + +Property::Property(const std::string &strval) : is_string(true), str(strval), intval(0xDEADBEEF) {} + +Property::Property(State bit) : is_string(false), str(std::string("") + char(bit)), intval(bit == S1) {} + +std::string Property::to_string() const +{ + if (is_string) { + std::string result = str; + int state = 0; + for (char c : str) { + if (state == 0) { + if (c == '0' || c == '1' || c == 'x' || c == 'z') + state = 0; + else if (c == ' ') + state = 1; + else + state = 2; + } else if (state == 1 && c != ' ') + state = 2; + } + if (state < 2) + result += " "; + return result; + } else { + return std::string(str.rbegin(), str.rend()); + } +} + +Property Property::from_string(const std::string &s) +{ + Property p; + + size_t cursor = s.find_first_not_of("01xz"); + if (cursor == std::string::npos) { + p.str = std::string(s.rbegin(), s.rend()); + p.is_string = false; + p.update_intval(); + } else if (s.find_first_not_of(' ', cursor) == std::string::npos) { + p = Property(s.substr(0, s.size() - 1)); + } else { + p = Property(s); + } + return p; +} + void BaseCtx::addConstraint(std::unique_ptr constr) { for (auto fromObj : constr->from) @@ -285,8 +339,8 @@ uint32_t Context::checksum() const for (auto &a : ni.attrs) { uint32_t attr_x = 123456789; attr_x = xorshift32(attr_x + xorshift32(a.first.index)); - for (uint8_t ch : a.second) - attr_x = xorshift32(attr_x + xorshift32(ch)); + for (char ch : a.second.str) + attr_x = xorshift32(attr_x + xorshift32((int)ch)); attr_x_sum += attr_x; } x = xorshift32(x + xorshift32(attr_x_sum)); @@ -329,8 +383,8 @@ uint32_t Context::checksum() const for (auto &a : ci.attrs) { uint32_t attr_x = 123456789; attr_x = xorshift32(attr_x + xorshift32(a.first.index)); - for (uint8_t ch : a.second) - attr_x = xorshift32(attr_x + xorshift32(ch)); + for (char ch : a.second.str) + attr_x = xorshift32(attr_x + xorshift32((int)ch)); attr_x_sum += attr_x; } x = xorshift32(x + xorshift32(attr_x_sum)); @@ -339,8 +393,8 @@ uint32_t Context::checksum() const for (auto &p : ci.params) { uint32_t param_x = 123456789; param_x = xorshift32(param_x + xorshift32(p.first.index)); - for (uint8_t ch : p.second) - param_x = xorshift32(param_x + xorshift32(ch)); + for (char ch : p.second.str) + param_x = xorshift32(param_x + xorshift32((int)ch)); param_x_sum += param_x; } x = xorshift32(x + xorshift32(param_x_sum)); @@ -462,19 +516,19 @@ void BaseCtx::archInfoToAttributes() if (ci->attrs.find(id("BEL")) != ci->attrs.end()) { ci->attrs.erase(ci->attrs.find(id("BEL"))); } - ci->attrs[id("NEXTPNR_BEL")] = getCtx()->getBelName(ci->bel).c_str(this); - ci->attrs[id("BEL_STRENGTH")] = std::to_string((int)ci->belStrength); + ci->attrs[id("NEXTPNR_BEL")] = getCtx()->getBelName(ci->bel).str(this); + ci->attrs[id("BEL_STRENGTH")] = (int)ci->belStrength; } if (ci->constr_x != ci->UNCONSTR) - ci->attrs[id("CONSTR_X")] = std::to_string(ci->constr_x); + ci->attrs[id("CONSTR_X")] = ci->constr_x; if (ci->constr_y != ci->UNCONSTR) - ci->attrs[id("CONSTR_Y")] = std::to_string(ci->constr_y); + ci->attrs[id("CONSTR_Y")] = ci->constr_y; if (ci->constr_z != ci->UNCONSTR) { - ci->attrs[id("CONSTR_Z")] = std::to_string(ci->constr_z); - ci->attrs[id("CONSTR_ABS_Z")] = std::to_string(ci->constr_abs_z ? 1 : 0); + ci->attrs[id("CONSTR_Z")] = ci->constr_z; + ci->attrs[id("CONSTR_ABS_Z")] = ci->constr_abs_z ? 1 : 0; } if (ci->constr_parent != nullptr) - ci->attrs[id("CONSTR_PARENT")] = ci->constr_parent->name.c_str(this); + ci->attrs[id("CONSTR_PARENT")] = ci->constr_parent->name.str(this); if (!ci->constr_children.empty()) { std::string constr = ""; for (auto &item : ci->constr_children) { @@ -512,37 +566,38 @@ void BaseCtx::attributesToArchInfo() auto str = ci->attrs.find(id("BEL_STRENGTH")); PlaceStrength strength = PlaceStrength::STRENGTH_USER; if (str != ci->attrs.end()) - strength = (PlaceStrength)std::stoi(str->second.str); + strength = (PlaceStrength)str->second.as_int64(); - BelId b = getCtx()->getBelByName(id(val->second.str)); + BelId b = getCtx()->getBelByName(id(val->second.as_string())); getCtx()->bindBel(b, ci, strength); } val = ci->attrs.find(id("CONSTR_X")); if (val != ci->attrs.end()) - ci->constr_x = std::stoi(val->second.str); + ci->constr_x = val->second.as_int64(); val = ci->attrs.find(id("CONSTR_Y")); if (val != ci->attrs.end()) - ci->constr_y = std::stoi(val->second.str); + ci->constr_y = val->second.as_int64(); val = ci->attrs.find(id("CONSTR_Z")); if (val != ci->attrs.end()) - ci->constr_z = std::stoi(val->second.str); + ci->constr_z = val->second.as_int64(); val = ci->attrs.find(id("CONSTR_ABS_Z")); if (val != ci->attrs.end()) - ci->constr_abs_z = std::stoi(val->second.str) == 1; + ci->constr_abs_z = val->second.as_int64() == 1; val = ci->attrs.find(id("CONSTR_PARENT")); if (val != ci->attrs.end()) { - auto parent = cells.find(id(val->second.str)); + auto parent = cells.find(id(val->second.as_string())); if (parent != cells.end()) ci->constr_parent = parent->second.get(); } val = ci->attrs.find(id("CONSTR_CHILDREN")); if (val != ci->attrs.end()) { std::vector strs; - boost::split(strs, val->second.str, boost::is_any_of(";")); + auto children = val->second.as_string(); + boost::split(strs, children, boost::is_any_of(";")); for (auto val : strs) { ci->constr_children.push_back(cells.find(id(val.c_str()))->second.get()); } @@ -553,7 +608,8 @@ void BaseCtx::attributesToArchInfo() auto val = ni->attrs.find(id("ROUTING")); if (val != ni->attrs.end()) { std::vector strs; - boost::split(strs, val->second.str, boost::is_any_of(";")); + auto routing = val->second.as_string(); + boost::split(strs, routing, boost::is_any_of(";")); for (size_t i = 0; i < strs.size() / 3; i++) { std::string wire = strs[i * 3]; std::string pip = strs[i * 3 + 1]; diff --git a/common/nextpnr.h b/common/nextpnr.h index 1f22e65a..efa326c0 100644 --- a/common/nextpnr.h +++ b/common/nextpnr.h @@ -32,6 +32,7 @@ #include #include +#include #include #ifndef NEXTPNR_H @@ -289,42 +290,98 @@ struct PipMap struct Property { + enum State : char + { + S0 = '0', + S1 = '1', + Sx = 'x', + Sz = 'z' + }; + + Property(); + Property(int64_t intval, int width = 32); + Property(const std::string &strval); + Property(State bit); + Property &operator=(const Property &other) = default; + bool is_string; + // The string literal (for string values), or a string of [01xz] (for numeric values) std::string str; - int num; - - std::string::iterator begin() { return str.begin(); } - std::string::iterator end() { return str.end(); } + // The lower 64 bits (for numeric values), unused for string values + int64_t intval; - bool isString() const { return is_string; } + void update_intval() + { + intval = 0; + for (int i = 0; i < int(str.size()); i++) { + NPNR_ASSERT(str[i] == S0 || str[i] == S1 || str[i] == Sx || str[i] == Sz); + if ((str[i] == S1) && i < 64) + intval |= (1ULL << i); + } + } - void setNumber(int val) + int64_t as_int64() const { - is_string = false; - num = val; - str = std::to_string(val); + NPNR_ASSERT(!is_string); + return intval; } - void setString(std::string val) + std::vector as_bits() const { - is_string = true; - str = val; + std::vector result; + result.reserve(str.size()); + NPNR_ASSERT(!is_string); + for (auto c : str) + result.push_back(c == S1); } - - const char *c_str() const { return str.c_str(); } - operator std::string() const { return str; } - - bool operator==(const std::string other) const { return str == other; } - bool operator!=(const std::string other) const { return str != other; } - - Property &operator=(std::string other) + std::string as_string() const + { + NPNR_ASSERT(is_string); + return str; + } + const char *c_str() const { - is_string = true; - str = other; - return *this; + NPNR_ASSERT(is_string); + return str.c_str(); } + size_t size() const { return is_string ? 8 * str.size() : str.size(); } + double as_double() const + { + NPNR_ASSERT(is_string); + return std::stod(str); + } + bool as_bool() const + { + if (int(str.size()) <= 64) + return intval != 0; + else + return std::any_of(str.begin(), str.end(), [](char c) { return c == S1; }); + } + bool is_fully_def() const + { + return !is_string && std::all_of(str.begin(), str.end(), [](char c) { return c == S0 || c == S1; }); + } + Property extract(int offset, int len, State padding = State::S0) const + { + Property ret; + ret.is_string = false; + ret.str.reserve(len); + for (int i = offset; i < offset + len; i++) + ret.str.push_back(i < int(str.size()) ? str[i] : padding); + ret.update_intval(); + return ret; + } + // Convert to a string representation, escaping literal strings matching /^[01xz]* *$/ by adding a space at the end, + // to disambiguate from binary strings + std::string to_string() const; + // Convert a string of four-value binary [01xz], or a literal string escaped according to the above rule + // to a Property + static Property from_string(const std::string &s); }; +inline bool operator==(const Property &a, const Property &b) { return a.is_string == b.is_string && a.str == b.str; } +inline bool operator!=(const Property &a, const Property &b) { return a.is_string != b.is_string || a.str != b.str; } + struct ClockConstraint; struct NetInfo : ArchNetInfo @@ -731,8 +788,10 @@ struct Context : Arch, DeterministicRNG template T setting(const char *name, T defaultValue) { IdString new_id = id(name); - if (settings.find(new_id) != settings.end()) - return boost::lexical_cast(settings.find(new_id)->second.str); + auto found = settings.find(new_id); + if (found != settings.end()) + return boost::lexical_cast(found->second.is_string ? found->second.as_string() + : std::to_string(found->second.as_int64())); else settings[id(name)] = std::to_string(defaultValue); @@ -742,8 +801,10 @@ struct Context : Arch, DeterministicRNG template T setting(const char *name) const { IdString new_id = id(name); - if (settings.find(new_id) != settings.end()) - return boost::lexical_cast(settings.find(new_id)->second.str); + auto found = settings.find(new_id); + if (found != settings.end()) + return boost::lexical_cast(found->second.is_string ? found->second.as_string() + : std::to_string(found->second.as_int64())); else throw std::runtime_error("settings does not exists"); } diff --git a/common/placer1.cc b/common/placer1.cc index cb7ae847..6683ddf7 100644 --- a/common/placer1.cc +++ b/common/placer1.cc @@ -159,7 +159,7 @@ class SAPlacer CellInfo *cell = cell_entry.second.get(); auto loc = cell->attrs.find(ctx->id("BEL")); if (loc != cell->attrs.end()) { - std::string loc_name = loc->second; + std::string loc_name = loc->second.as_string(); BelId bel = ctx->getBelByName(ctx->id(loc_name)); if (bel == BelId()) { log_error("No Bel named \'%s\' located for " diff --git a/common/placer_heap.cc b/common/placer_heap.cc index d1f5a974..e9fc2fb2 100644 --- a/common/placer_heap.cc +++ b/common/placer_heap.cc @@ -348,7 +348,7 @@ class HeAPPlacer CellInfo *cell = cell_entry.second.get(); auto loc = cell->attrs.find(ctx->id("BEL")); if (loc != cell->attrs.end()) { - std::string loc_name = loc->second; + std::string loc_name = loc->second.as_string(); BelId bel = ctx->getBelByName(ctx->id(loc_name)); if (bel == BelId()) { log_error("No Bel named \'%s\' located for " diff --git a/common/pybindings.cc b/common/pybindings.cc index 52dd9717..3f2cb811 100644 --- a/common/pybindings.cc +++ b/common/pybindings.cc @@ -81,6 +81,13 @@ template <> struct string_converter } }; +template <> struct string_converter +{ + inline Property from_str(Context *ctx, std::string s) { return Property::from_string(s); } + + inline std::string to_str(Context *ctx, Property p) { return p.to_string(); } +}; + } // namespace PythonConversion BOOST_PYTHON_MODULE(MODULE_NAME) @@ -207,7 +214,7 @@ BOOST_PYTHON_MODULE(MODULE_NAME) readonly_wrapper>::def_wrap(region_cls, "wires"); - WRAP_MAP(AttrMap, pass_through, "AttrMap"); + WRAP_MAP(AttrMap, conv_to_str, "AttrMap"); WRAP_MAP(PortMap, wrap_context, "PortMap"); WRAP_MAP(PinMap, conv_to_str, "PinMap"); WRAP_MAP(WireMap, wrap_context, "WireMap"); diff --git a/common/util.h b/common/util.h index 8f361dc8..81d7e47d 100644 --- a/common/util.h +++ b/common/util.h @@ -51,6 +51,16 @@ std::string str_or_default(const Container &ct, const KeyType &key, std::string return found->second; }; +template +std::string str_or_default(const std::unordered_map &ct, const KeyType &key, std::string def = "") +{ + auto found = ct.find(key); + if (found == ct.end()) + return def; + else + return found->second.as_string(); +}; + // Get a value from a map-style container, converting to int, and returning // default if value is not found template int int_or_default(const Container &ct, const KeyType &key, int def = 0) @@ -62,6 +72,20 @@ template int int_or_default(const Contain return std::stoi(found->second); }; +template +int int_or_default(const std::unordered_map &ct, const KeyType &key, int def = 0) +{ + auto found = ct.find(key); + if (found == ct.end()) + return def; + else { + if (found->second.is_string) + return std::stoi(found->second.as_string()); + else + return found->second.as_int64(); + } +}; + // As above, but convert to bool template bool bool_or_default(const Container &ct, const KeyType &key, bool def = false) -- cgit v1.2.3