From a139654980b2d2560667b12c886de7518ec97c40 Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Tue, 12 Jun 2018 15:08:01 +0200 Subject: Add IdString API Signed-off-by: Clifford Wolf --- common/nextpnr.h | 39 +++++++++++++++++++++++++++++++++++++-- common/place.cc | 4 ++-- common/rulecheck.cc | 13 ++++++------- frontend/json/jsonparse.cc | 10 +++++----- ice40/cells.cc | 6 +++--- ice40/chip.h | 9 ++++++--- ice40/pack.cc | 4 ++-- 7 files changed, 61 insertions(+), 24 deletions(-) diff --git a/common/nextpnr.h b/common/nextpnr.h index 0c74c1ad..bc92719f 100644 --- a/common/nextpnr.h +++ b/common/nextpnr.h @@ -41,8 +41,43 @@ NEXTPNR_NAMESPACE_BEGIN -// replace with proper IdString later -typedef std::string IdString; +struct IdString +{ + std::string data; + + IdString() {} + IdString(std::string s) : data(s) {} + IdString(const char *s) : data(s) {} + + const char *c_str() const { return data.c_str(); } + const std::string &str() const { return data; } + + operator const char *() const { return c_str(); } + operator const std::string &() const { return str(); } + + bool operator<(const IdString &other) const { return data < other.data; } + bool operator==(const IdString &other) const { return data == other.data; } + bool operator==(const std::string &s) const { return data == s; } + bool operator==(const char *s) const { return data == s; } + + size_t size() const { return data.size(); } + bool empty() const { return data.empty(); } +}; + +NEXTPNR_NAMESPACE_END + +namespace std { +template <> struct hash +{ + std::size_t operator()(const NEXTPNR_NAMESPACE_PREFIX IdString &obj) const + noexcept + { + return std::hash()(obj.data); + } +}; +} + +NEXTPNR_NAMESPACE_BEGIN struct GraphicElement { diff --git a/common/place.cc b/common/place.cc index e8fadd16..007fc748 100644 --- a/common/place.cc +++ b/common/place.cc @@ -100,7 +100,7 @@ void place_design(Design *design) if (cell->bel != BelId()) continue; // Only place one type of Bel at a time - if (cell->type.compare(bel_type_name) != 0) + if (cell->type != bel_type_name) continue; while ((bi != blist.end()) && @@ -115,7 +115,7 @@ void place_design(Design *design) design->chip.bindBel(cell->bel, cell->name); // Back annotate location - cell->attrs["BEL"] = design->chip.getBelName(cell->bel); + cell->attrs["BEL"] = design->chip.getBelName(cell->bel).str(); } } } diff --git a/common/rulecheck.cc b/common/rulecheck.cc index c0139a85..987eb602 100644 --- a/common/rulecheck.cc +++ b/common/rulecheck.cc @@ -24,7 +24,7 @@ bool check_all_nets_driven(Design *design) log_info(" Checking name of port \'%s\' " "against \'%s\'\n", port_entry.first.c_str(), port.name.c_str()); - assert(port.name.compare(port_entry.first) == 0); + assert(port.name == port_entry.first); assert(port.name.size() > 0); if (port.net == NULL) { @@ -45,10 +45,9 @@ bool check_all_nets_driven(Design *design) for (auto net_entry : design->nets) { NetInfo *net = net_entry.second; - assert(net->name.compare(net_entry.first) == 0); - if ((net->driver.cell != NULL) && - (net->driver.cell->type.compare("GND") != 0) && - (net->driver.cell->type.compare("VCC") != 0)) { + assert(net->name == net_entry.first); + if ((net->driver.cell != NULL) && (net->driver.cell->type != "GND") && + (net->driver.cell->type != "VCC")) { if (debug) log_info(" Checking for a driver cell named \'%s\'\n", @@ -57,8 +56,8 @@ bool check_all_nets_driven(Design *design) } for (auto user : net->users) { - if ((user.cell != NULL) && (user.cell->type.compare("GND") != 0) && - (user.cell->type.compare("VCC") != 0)) { + if ((user.cell != NULL) && (user.cell->type != "GND") && + (user.cell->type != "VCC")) { if (debug) log_info(" Checking for a user cell named \'%s\'\n", diff --git a/frontend/json/jsonparse.cc b/frontend/json/jsonparse.cc index 5f394217..3f965ce4 100644 --- a/frontend/json/jsonparse.cc +++ b/frontend/json/jsonparse.cc @@ -224,10 +224,10 @@ NetInfo *ground_net(NetInfo *net) PortInfo port_info; PortRef port_ref; - cell->name = string(net->name + ".GND"); + cell->name = string(net->name.str() + ".GND"); cell->type = string("GND"); - port_info.name = cell->name + "[]"; + port_info.name = cell->name.str() + "[]"; port_info.net = net; port_info.type = PORT_OUT; @@ -247,10 +247,10 @@ NetInfo *vcc_net(NetInfo *net) PortInfo port_info; PortRef port_ref; - cell->name = string(net->name + ".VCC"); + cell->name = string(net->name.str() + ".VCC"); cell->type = string("VCC"); - port_info.name = cell->name + "[]"; + port_info.name = cell->name.str() + "[]"; port_info.net = net; port_info.type = PORT_OUT; @@ -269,7 +269,7 @@ NetInfo *floating_net(NetInfo *net) PortInfo port_info; PortRef port_ref; - port_info.name = net->name + ".floating"; + port_info.name = net->name.str() + ".floating"; port_info.net = net; port_info.type = PORT_OUT; diff --git a/ice40/cells.cc b/ice40/cells.cc index 004bdb30..ad728d2c 100644 --- a/ice40/cells.cc +++ b/ice40/cells.cc @@ -33,8 +33,8 @@ CellInfo *create_ice_cell(Design *design, IdString type, IdString name) static int auto_idx = 0; CellInfo *new_cell = new CellInfo(); if (name == IdString()) { - new_cell->name = - IdString("$nextpnr_" + type + "_" + std::to_string(auto_idx++)); + new_cell->name = IdString("$nextpnr_" + type.str() + "_" + + std::to_string(auto_idx++)); } else { new_cell->name = name; } @@ -82,7 +82,7 @@ void lut_to_lc(CellInfo *lut, CellInfo *lc, bool no_dff) void dff_to_lc(CellInfo *dff, CellInfo *lc, bool pass_thru_lut) { lc->params["DFF_ENABLE"] = "1"; - std::string config = std::string(dff->type).substr(6); + std::string config = dff->type.str().substr(6); auto citer = config.begin(); replace_port(dff, "C", lc, "CLK"); diff --git a/ice40/chip.h b/ice40/chip.h index 96416c04..73e8d33b 100644 --- a/ice40/chip.h +++ b/ice40/chip.h @@ -217,7 +217,8 @@ NEXTPNR_NAMESPACE_END namespace std { template <> struct hash { - std::size_t operator()(const NEXTPNR_NAMESPACE_PREFIX BelId &bel) const noexcept + std::size_t operator()(const NEXTPNR_NAMESPACE_PREFIX BelId &bel) const + noexcept { return bel.index; } @@ -225,7 +226,8 @@ template <> struct hash template <> struct hash { - std::size_t operator()(const NEXTPNR_NAMESPACE_PREFIX WireId &wire) const noexcept + std::size_t operator()(const NEXTPNR_NAMESPACE_PREFIX WireId &wire) const + noexcept { return wire.index; } @@ -233,7 +235,8 @@ template <> struct hash template <> struct hash { - std::size_t operator()(const NEXTPNR_NAMESPACE_PREFIX PipId &wire) const noexcept + std::size_t operator()(const NEXTPNR_NAMESPACE_PREFIX PipId &wire) const + noexcept { return wire.index; } diff --git a/ice40/pack.cc b/ice40/pack.cc index a6e17378..a971dcc3 100644 --- a/ice40/pack.cc +++ b/ice40/pack.cc @@ -38,7 +38,7 @@ static void pack_lut_lutffs(Design *design) ci->type.c_str()); if (is_lut(ci)) { CellInfo *packed = create_ice_cell(design, "ICESTORM_LC", - std::string(ci->name) + "_LC"); + ci->name.str() + "_LC"); packed_cells.insert(ci->name); new_cells.push_back(packed); log_info("packed cell %s into %s\n", ci->name.c_str(), @@ -77,7 +77,7 @@ static void pack_nonlut_ffs(Design *design) CellInfo *ci = cell.second; if (is_ff(ci)) { CellInfo *packed = create_ice_cell(design, "ICESTORM_LC", - std::string(ci->name) + "_LC"); + ci->name.str() + "_LC"); packed_cells.insert(ci->name); new_cells.push_back(packed); dff_to_lc(ci, packed, true); -- cgit v1.2.3