From c33a039ac388bfcb5e068a04a7cb1b05ebec7d7f Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Thu, 21 Jun 2018 18:08:28 +0200 Subject: Added return code to json parsing and pcf reading --- frontend/json/jsonparse.cc | 33 +++++++++++--------- frontend/json/jsonparse.h | 2 +- gui/ice40/worker.cc | 4 +-- ice40/main.cc | 6 ++-- ice40/pcf.cc | 75 +++++++++++++++++++++++++--------------------- ice40/pcf.h | 2 +- 6 files changed, 68 insertions(+), 54 deletions(-) diff --git a/frontend/json/jsonparse.cc b/frontend/json/jsonparse.cc index 7d9e9dcf..a832e9e5 100644 --- a/frontend/json/jsonparse.cc +++ b/frontend/json/jsonparse.cc @@ -797,27 +797,32 @@ void json_import(Context *ctx, string modname, JsonNode *node) } }; // End Namespace JsonParser -void parse_json_file(std::istream &f, std::string &filename, Context *ctx) +bool parse_json_file(std::istream &f, std::string &filename, Context *ctx) { - using namespace JsonParser; + try { + using namespace JsonParser; - JsonNode root(f); + JsonNode root(f); - if (root.type != 'D') - log_error("JSON root node is not a dictionary.\n"); + if (root.type != 'D') + log_error("JSON root node is not a dictionary.\n"); - if (root.data_dict.count("modules") != 0) { - JsonNode *modules = root.data_dict.at("modules"); + if (root.data_dict.count("modules") != 0) { + JsonNode *modules = root.data_dict.at("modules"); - if (modules->type != 'D') - log_error("JSON modules node is not a dictionary.\n"); + if (modules->type != 'D') + log_error("JSON modules node is not a dictionary.\n"); - for (auto &it : modules->data_dict) - json_import(ctx, it.first, it.second); - } + for (auto &it : modules->data_dict) + json_import(ctx, it.first, it.second); + } - log_info("Checksum: 0x%08x\n", ctx->checksum()); - log_break(); + log_info("Checksum: 0x%08x\n", ctx->checksum()); + log_break(); + return true; + } catch (log_execution_error_exception) { + return false; + } } NEXTPNR_NAMESPACE_END diff --git a/frontend/json/jsonparse.h b/frontend/json/jsonparse.h index 351b6558..fe71444f 100644 --- a/frontend/json/jsonparse.h +++ b/frontend/json/jsonparse.h @@ -26,7 +26,7 @@ NEXTPNR_NAMESPACE_BEGIN -extern void parse_json_file(std::istream &, std::string &, Context *); +extern bool parse_json_file(std::istream &, std::string &, Context *); NEXTPNR_NAMESPACE_END diff --git a/gui/ice40/worker.cc b/gui/ice40/worker.cc index f86ec55e..5702137b 100644 --- a/gui/ice40/worker.cc +++ b/gui/ice40/worker.cc @@ -20,7 +20,8 @@ void Worker::parsejson(const std::string &filename) std::string fn = filename; std::ifstream f(fn); try { - parse_json_file(f, fn, ctx); + if (!parse_json_file(f, fn, ctx)) + log_error("Loading design failed.\n"); if (!pack_design(ctx)) log_error("Packing design failed.\n"); double freq = 50e6; @@ -33,7 +34,6 @@ void Worker::parsejson(const std::string &filename) log_error("Routing design failed.\n"); Q_EMIT log("done"); } catch (log_execution_error_exception) { - Q_EMIT log("failed"); } } diff --git a/ice40/main.cc b/ice40/main.cc index 67d71dc1..067637e8 100644 --- a/ice40/main.cc +++ b/ice40/main.cc @@ -288,11 +288,13 @@ int main(int argc, char *argv[]) if (vm.count("json")) { std::string filename = vm["json"].as(); std::ifstream f(filename); - parse_json_file(f, filename, &ctx); + if (!parse_json_file(f, filename, &ctx)) + log_error("Loading design failed.\n"); if (vm.count("pcf")) { std::ifstream pcf(vm["pcf"].as()); - apply_pcf(&ctx, pcf); + if (!apply_pcf(&ctx, pcf)) + log_error("Loading PCF failed.\n"); } if (!pack_design(&ctx) && !ctx.force) diff --git a/ice40/pcf.cc b/ice40/pcf.cc index 756aba4a..87d27ff1 100644 --- a/ice40/pcf.cc +++ b/ice40/pcf.cc @@ -27,44 +27,51 @@ NEXTPNR_NAMESPACE_BEGIN // Read a w // Apply PCF constraints to a pre-packing design -void apply_pcf(Context *ctx, std::istream &in) +bool apply_pcf(Context *ctx, std::istream &in) { - if (!in) - log_error("failed to open PCF file"); - std::string line; - while (std::getline(in, line)) { - size_t cstart = line.find("#"); - if (cstart != std::string::npos) - line = line.substr(0, cstart); - std::stringstream ss(line); - std::vector words; - std::string tmp; - while (ss >> tmp) - words.push_back(tmp); - if (words.size() == 0) - continue; - std::string cmd = words.at(0); - if (cmd == "set_io") { - size_t args_end = 1; - while (args_end < words.size() && words.at(args_end).at(0) == '-') - args_end++; - std::string cell = words.at(args_end); - std::string pin = words.at(args_end + 1); - auto fnd_cell = ctx->cells.find(cell); - if (fnd_cell == ctx->cells.end()) { - log_warning("unmatched pcf constraint %s\n", cell.c_str()); + try { + if (!in) + log_error("failed to open PCF file"); + std::string line; + while (std::getline(in, line)) { + size_t cstart = line.find("#"); + if (cstart != std::string::npos) + line = line.substr(0, cstart); + std::stringstream ss(line); + std::vector words; + std::string tmp; + while (ss >> tmp) + words.push_back(tmp); + if (words.size() == 0) + continue; + std::string cmd = words.at(0); + if (cmd == "set_io") { + size_t args_end = 1; + while (args_end < words.size() && + words.at(args_end).at(0) == '-') + args_end++; + std::string cell = words.at(args_end); + std::string pin = words.at(args_end + 1); + auto fnd_cell = ctx->cells.find(cell); + if (fnd_cell == ctx->cells.end()) { + log_warning("unmatched pcf constraint %s\n", cell.c_str()); + } else { + BelId pin_bel = ctx->getPackagePinBel(pin); + if (pin_bel == BelId()) + log_error("package does not have a pin named %s\n", + pin.c_str()); + fnd_cell->second->attrs["BEL"] = + ctx->getBelName(pin_bel).str(); + log_info("constrained '%s' to bel '%s'\n", cell.c_str(), + fnd_cell->second->attrs["BEL"].c_str()); + } } else { - BelId pin_bel = ctx->getPackagePinBel(pin); - if (pin_bel == BelId()) - log_error("package does not have a pin named %s\n", - pin.c_str()); - fnd_cell->second->attrs["BEL"] = ctx->getBelName(pin_bel).str(); - log_info("constrained '%s' to bel '%s'\n", cell.c_str(), - fnd_cell->second->attrs["BEL"].c_str()); + log_error("unsupported pcf command '%s'\n", cmd.c_str()); } - } else { - log_error("unsupported pcf command '%s'\n", cmd.c_str()); } + return true; + } catch (log_execution_error_exception) { + return false; } } diff --git a/ice40/pcf.h b/ice40/pcf.h index e0816075..b86a7609 100644 --- a/ice40/pcf.h +++ b/ice40/pcf.h @@ -27,7 +27,7 @@ NEXTPNR_NAMESPACE_BEGIN // Apply PCF constraints to a pre-packing design -void apply_pcf(Context *ctx, std::istream &in); +bool apply_pcf(Context *ctx, std::istream &in); NEXTPNR_NAMESPACE_END -- cgit v1.2.3