aboutsummaryrefslogtreecommitdiffstats
path: root/json
diff options
context:
space:
mode:
authorDavid Shah <dave@ds0.me>2019-08-01 14:28:21 +0100
committerDavid Shah <dave@ds0.me>2019-08-05 14:52:15 +0100
commit1839a3a770a71c928b92bf876e04728d2649e425 (patch)
tree9de012efabb47f066860918b6360f3966b42f4da /json
parent1ecf271cb32f9f78ea082788c6534f2523144d01 (diff)
downloadnextpnr-1839a3a770a71c928b92bf876e04728d2649e425.tar.gz
nextpnr-1839a3a770a71c928b92bf876e04728d2649e425.tar.bz2
nextpnr-1839a3a770a71c928b92bf876e04728d2649e425.zip
Major Property improvements for common and iCE40
Signed-off-by: David Shah <dave@ds0.me>
Diffstat (limited to 'json')
-rw-r--r--json/jsonparse.cc58
-rw-r--r--json/jsonwrite.cc16
2 files changed, 40 insertions, 34 deletions
diff --git a/json/jsonparse.cc b/json/jsonparse.cc
index caedbb5b..b3bb491b 100644
--- a/json/jsonparse.cc
+++ b/json/jsonparse.cc
@@ -230,6 +230,25 @@ struct JsonNode
}
};
+inline Property json_parse_attr_param_value(JsonNode *node)
+{
+ Property value;
+
+ if (node->type == 'S') {
+ value = Property::from_string(node->data_string);
+ } else if (node->type == 'N') {
+ value = Property(node->data_number, 32);
+ } else if (node->type == 'A') {
+ log_error("JSON attribute or parameter value is an array.\n");
+ } else if (node->type == 'D') {
+ log_error("JSON attribute or parameter value is a dict.\n");
+ } else {
+ log_abort();
+ }
+
+ return value;
+}
+
void ground_net(Context *ctx, NetInfo *net)
{
std::unique_ptr<CellInfo> cell = std::unique_ptr<CellInfo>(new CellInfo);
@@ -323,18 +342,12 @@ void json_import_cell_params(Context *ctx, string &modname, CellInfo *cell, Json
param = param_node->data_dict.at(param_node->data_dict_keys[param_id]);
pId = ctx->id(param_node->data_dict_keys[param_id]);
- if (param->type == 'N') {
- (*dest)[pId].setNumber(param->data_number);
- } else if (param->type == 'S')
- (*dest)[pId].setString(param->data_string);
- else
- log_error("JSON parameter type of \"%s\' of cell \'%s\' not supported\n", pId.c_str(ctx),
- cell->name.c_str(ctx));
+ (*dest)[pId] = json_parse_attr_param_value(param);
if (json_debug)
log_info(" Added parameter \'%s\'=%s to cell \'%s\' "
"of module \'%s\'\n",
- pId.c_str(ctx), cell->params[pId].c_str(), cell->name.c_str(ctx), modname.c_str());
+ pId.c_str(ctx), cell->params[pId].as_string().c_str(), cell->name.c_str(ctx), modname.c_str());
}
void json_import_net_attrib(Context *ctx, string &modname, NetInfo *net, JsonNode *param_node,
@@ -347,16 +360,12 @@ void json_import_net_attrib(Context *ctx, string &modname, NetInfo *net, JsonNod
param = param_node->data_dict.at(param_node->data_dict_keys[param_id]);
pId = ctx->id(param_node->data_dict_keys[param_id]);
- if (param->type == 'N') {
- (*dest)[pId].setNumber(param->data_number);
- } else if (param->type == 'S')
- (*dest)[pId].setString(param->data_string);
- else
- log_error("JSON parameter type of \"%s\' of net \'%s\' not supported\n", pId.c_str(ctx), net->name.c_str(ctx));
+ (*dest)[pId] = json_parse_attr_param_value(param);
+
if (json_debug)
log_info(" Added parameter \'%s\'=%s to net \'%s\' "
"of module \'%s\'\n",
- pId.c_str(ctx), net->attrs[pId].c_str(), net->name.c_str(ctx), modname.c_str());
+ pId.c_str(ctx), net->attrs[pId].as_string().c_str(), net->name.c_str(ctx), modname.c_str());
}
void json_import_top_attrib(Context *ctx, string &modname, JsonNode *param_node,
@@ -369,14 +378,10 @@ void json_import_top_attrib(Context *ctx, string &modname, JsonNode *param_node,
param = param_node->data_dict.at(param_node->data_dict_keys[param_id]);
pId = ctx->id(param_node->data_dict_keys[param_id]);
- if (param->type == 'N') {
- (*dest)[pId].setNumber(param->data_number);
- } else if (param->type == 'S')
- (*dest)[pId].setString(param->data_string);
- else
- log_error("JSON parameter type of \"%s\' of module not supported\n", pId.c_str(ctx));
+ (*dest)[pId] = json_parse_attr_param_value(param);
+
if (json_debug)
- log_info(" Added parameter \'%s\'=%s module \'%s\'\n", pId.c_str(ctx), (*dest)[pId].c_str(),
+ log_info(" Added parameter \'%s\'=%s module \'%s\'\n", pId.c_str(ctx), (*dest)[pId].as_string().c_str(),
modname.c_str());
}
@@ -917,7 +922,7 @@ void json_import(Context *ctx, string modname, JsonNode *node)
}
}
check_all_nets_driven(ctx);
- ctx->settings[ctx->id("synth")] = "1";
+ ctx->settings[ctx->id("synth")] = 1;
}
}; // End Namespace JsonParser
@@ -986,12 +991,7 @@ bool load_json_settings(std::istream &f, std::string &filename, std::unordered_m
for (int attrid = 0; attrid < GetSize(attr_node->data_dict_keys); attrid++) {
JsonNode *param = attr_node->data_dict.at(attr_node->data_dict_keys[attrid]);
std::string pId = attr_node->data_dict_keys[attrid];
- if (param->type == 'N') {
- values[pId].setNumber(param->data_number);
- } else if (param->type == 'S')
- values[pId].setString(param->data_string);
- else
- log_error("JSON parameter type of \"%s\' of module not supported\n", pId.c_str());
+ values[pId] = json_parse_attr_param_value(param);
}
}
}
diff --git a/json/jsonwrite.cc b/json/jsonwrite.cc
index 0b7a5b25..30bee107 100644
--- a/json/jsonwrite.cc
+++ b/json/jsonwrite.cc
@@ -45,6 +45,15 @@ std::string get_string(std::string str)
std::string get_name(IdString name, Context *ctx) { return get_string(name.c_str(ctx)); }
+void write_parameter_value(std::ostream &f, const Property &value)
+{
+ if (value.size() == 32 && value.is_fully_def()) {
+ f << stringf("%d", value.as_int64());
+ } else {
+ f << get_string(value.as_string());
+ }
+}
+
void write_parameters(std::ostream &f, Context *ctx, const std::unordered_map<IdString, Property> &parameters,
bool for_module = false)
{
@@ -52,10 +61,7 @@ void write_parameters(std::ostream &f, Context *ctx, const std::unordered_map<Id
for (auto &param : parameters) {
f << stringf("%s\n", first ? "" : ",");
f << stringf(" %s%s: ", for_module ? "" : " ", get_name(param.first, ctx).c_str());
- if (param.second.isString())
- f << get_string(param.second);
- else
- f << param.second.num;
+ write_parameter_value(f, param.second);
first = false;
}
}
@@ -64,7 +70,7 @@ void write_module(std::ostream &f, Context *ctx)
{
auto val = ctx->attrs.find(ctx->id("module"));
if (val != ctx->attrs.end())
- f << stringf(" %s: {\n", get_string(val->second.str).c_str());
+ f << stringf(" %s: {\n", get_string(val->second.as_string()).c_str());
else
f << stringf(" %s: {\n", get_string("top").c_str());
f << stringf(" \"settings\": {");