From 858eae5572375fac376f6c2bb43e976abe73af0d Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Wed, 8 Nov 2023 17:10:40 +0100 Subject: [PATCH 1/5] verific_const: convert VHDL values to RTLIL consts --- frontends/verific/verific.cc | 64 +++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/frontends/verific/verific.cc b/frontends/verific/verific.cc index faa0e1bcdd3..6fc87ced645 100644 --- a/frontends/verific/verific.cc +++ b/frontends/verific/verific.cc @@ -223,7 +223,7 @@ RTLIL::IdString VerificImporter::new_verific_id(Verific::DesignObj *obj) // // Note: For signed values, verific uses 'sb and decimal values can // also be negative. -static const RTLIL::Const verific_const(const char *value, bool allow_string = true, bool output_signed = false) +static const RTLIL::Const verific_const(const char *value, bool from_vhdl, bool allow_string = true, bool output_signed = false) { size_t found; char *end; @@ -231,26 +231,44 @@ static const RTLIL::Const verific_const(const char *value, bool allow_string = t bool is_signed = false; RTLIL::Const c; std::string val = std::string(value); - if (allow_string && val.size()>1 && val[0]=='\"' && val.back()=='\"') { - c = RTLIL::Const(val.substr(1,val.size()-2)); - } else if ((found = val.find("'sb")) != std::string::npos) { - is_signed = output_signed; - c = RTLIL::Const::from_string(val.substr(found + 3)); - } else if ((found = val.find("'b")) != std::string::npos) { - c = RTLIL::Const::from_string(val.substr(found + 2)); - } else if ((value[0] == '-' || (value[0] >= '0' && value[0] <= '9')) && - ((decimal = std::strtol(value, &end, 10)), !end[0])) { - is_signed = output_signed; - c = RTLIL::Const((int)decimal, 32); - } else if (allow_string) { - c = RTLIL::Const(val); + if (from_vhdl) { + if (val.size()>1 && val[0]=='\"' && val.back()=='\"') { + std::string data = val.substr(1,val.size()-2); + bool isBinary = std::all_of(data.begin(), data.end(), [](char c) {return c=='1' || c=='0'; }); + if (isBinary) + c = RTLIL::Const::from_string(data); + else + c = RTLIL::Const(data); + } else if (val.size()==3 && val[0]=='\'' && val.back()=='\'') { + c = RTLIL::Const::from_string(val.substr(1,val.size()-2)); + } else if ((value[0] == '-' || (value[0] >= '0' && value[0] <= '9')) && + ((decimal = std::strtol(value, &end, 10)), !end[0])) { + is_signed = output_signed; + c = RTLIL::Const((int)decimal, 32); + } else { + log_error("non-expected '%s' constant found", value); + } } else { - log_error("expected numeric constant but found '%s'", value); - } - - if (is_signed) - c.flags |= RTLIL::CONST_FLAG_SIGNED; + if (allow_string && val.size()>1 && val[0]=='\"' && val.back()=='\"') { + c = RTLIL::Const(val.substr(1,val.size()-2)); + } else if ((found = val.find("'sb")) != std::string::npos) { + is_signed = output_signed; + c = RTLIL::Const::from_string(val.substr(found + 3)); + } else if ((found = val.find("'b")) != std::string::npos) { + c = RTLIL::Const::from_string(val.substr(found + 2)); + } else if ((value[0] == '-' || (value[0] >= '0' && value[0] <= '9')) && + ((decimal = std::strtol(value, &end, 10)), !end[0])) { + is_signed = output_signed; + c = RTLIL::Const((int)decimal, 32); + } else if (allow_string) { + c = RTLIL::Const(val); + } else { + log_error("expected numeric constant but found '%s'", value); + } + if (is_signed) + c.flags |= RTLIL::CONST_FLAG_SIGNED; + } return c; } @@ -276,7 +294,7 @@ void VerificImporter::import_attributes(dict &att FOREACH_ATTRIBUTE(obj, mi, attr) { if (attr->Key()[0] == ' ' || attr->Value() == nullptr) continue; - attributes[RTLIL::escape_id(attr->Key())] = verific_const(attr->Value()); + attributes[RTLIL::escape_id(attr->Key())] = verific_const(attr->Value(), obj->IsFromVhdl()); } if (nl) { @@ -298,7 +316,7 @@ void VerificImporter::import_attributes(dict &att const char *k, *v; FOREACH_MAP_ITEM(type_range->GetEnumIdMap(), mi, &k, &v) { if (nl->IsFromVerilog()) { - auto const value = verific_const(v, false); + auto const value = verific_const(v, nl->IsFromVhdl(), false); attributes.emplace(stringf("\\enum_value_%s", value.as_string().c_str()), RTLIL::escape_id(k)); } @@ -1304,7 +1322,7 @@ void VerificImporter::import_netlist(RTLIL::Design *design, Netlist *nl, std::ma MapIter mi; FOREACH_PARAMETER_OF_NETLIST(nl, mi, param_name, param_value) { module->avail_parameters(RTLIL::escape_id(param_name)); - module->parameter_default_values[RTLIL::escape_id(param_name)] = verific_const(param_value); + module->parameter_default_values[RTLIL::escape_id(param_name)] = verific_const(param_value, nl->IsFromVhdl()); } SetIter si; @@ -2004,7 +2022,7 @@ void VerificImporter::import_netlist(RTLIL::Design *design, Netlist *nl, std::ma const char *param_value ; if (is_blackbox(inst->View())) { FOREACH_PARAMETER_OF_INST(inst, mi2, param_name, param_value) { - cell->setParam(RTLIL::escape_id(param_name), verific_const(param_value)); + cell->setParam(RTLIL::escape_id(param_name), verific_const(param_value, nl->IsFromVhdl())); } } From 4279cea33a91aa934f65072e7b6c34830f55b037 Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Mon, 11 Mar 2024 09:30:58 +0100 Subject: [PATCH 2/5] improve handling VHDL constants --- frontends/verific/verific.cc | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/frontends/verific/verific.cc b/frontends/verific/verific.cc index 6fc87ced645..6dbad4c2ae6 100644 --- a/frontends/verific/verific.cc +++ b/frontends/verific/verific.cc @@ -223,7 +223,7 @@ RTLIL::IdString VerificImporter::new_verific_id(Verific::DesignObj *obj) // // Note: For signed values, verific uses 'sb and decimal values can // also be negative. -static const RTLIL::Const verific_const(const char *value, bool from_vhdl, bool allow_string = true, bool output_signed = false) +static const RTLIL::Const verific_const(const char *value, DesignObj *obj, bool allow_string = true, bool output_signed = false) { size_t found; char *end; @@ -231,7 +231,7 @@ static const RTLIL::Const verific_const(const char *value, bool from_vhdl, bool bool is_signed = false; RTLIL::Const c; std::string val = std::string(value); - if (from_vhdl) { + if (obj->IsFromVhdl()) { if (val.size()>1 && val[0]=='\"' && val.back()=='\"') { std::string data = val.substr(1,val.size()-2); bool isBinary = std::all_of(data.begin(), data.end(), [](char c) {return c=='1' || c=='0'; }); @@ -245,6 +245,10 @@ static const RTLIL::Const verific_const(const char *value, bool from_vhdl, bool ((decimal = std::strtol(value, &end, 10)), !end[0])) { is_signed = output_signed; c = RTLIL::Const((int)decimal, 32); + } else if (val == "false") { + c = RTLIL::Const::from_string("0"); + } else if (val == "true") { + c = RTLIL::Const::from_string("1"); } else { log_error("non-expected '%s' constant found", value); } @@ -294,7 +298,7 @@ void VerificImporter::import_attributes(dict &att FOREACH_ATTRIBUTE(obj, mi, attr) { if (attr->Key()[0] == ' ' || attr->Value() == nullptr) continue; - attributes[RTLIL::escape_id(attr->Key())] = verific_const(attr->Value(), obj->IsFromVhdl()); + attributes[RTLIL::escape_id(attr->Key())] = verific_const(attr->Value(), obj); } if (nl) { @@ -316,7 +320,7 @@ void VerificImporter::import_attributes(dict &att const char *k, *v; FOREACH_MAP_ITEM(type_range->GetEnumIdMap(), mi, &k, &v) { if (nl->IsFromVerilog()) { - auto const value = verific_const(v, nl->IsFromVhdl(), false); + auto const value = verific_const(v, nl, false); attributes.emplace(stringf("\\enum_value_%s", value.as_string().c_str()), RTLIL::escape_id(k)); } @@ -1322,7 +1326,7 @@ void VerificImporter::import_netlist(RTLIL::Design *design, Netlist *nl, std::ma MapIter mi; FOREACH_PARAMETER_OF_NETLIST(nl, mi, param_name, param_value) { module->avail_parameters(RTLIL::escape_id(param_name)); - module->parameter_default_values[RTLIL::escape_id(param_name)] = verific_const(param_value, nl->IsFromVhdl()); + module->parameter_default_values[RTLIL::escape_id(param_name)] = verific_const(param_value, nl); } SetIter si; @@ -2022,7 +2026,7 @@ void VerificImporter::import_netlist(RTLIL::Design *design, Netlist *nl, std::ma const char *param_value ; if (is_blackbox(inst->View())) { FOREACH_PARAMETER_OF_INST(inst, mi2, param_name, param_value) { - cell->setParam(RTLIL::escape_id(param_name), verific_const(param_value, nl->IsFromVhdl())); + cell->setParam(RTLIL::escape_id(param_name), verific_const(param_value, inst->View())); } } From 7c09fa572e2eb4cce2cfdfcfdbc7ca327382ad2a Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Thu, 14 Mar 2024 10:37:11 +0100 Subject: [PATCH 3/5] real number handling and default to string --- frontends/verific/verific.cc | 50 +++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/frontends/verific/verific.cc b/frontends/verific/verific.cc index 6dbad4c2ae6..a5bcd44c48e 100644 --- a/frontends/verific/verific.cc +++ b/frontends/verific/verific.cc @@ -214,6 +214,22 @@ RTLIL::IdString VerificImporter::new_verific_id(Verific::DesignObj *obj) return s; } +RTLIL::Const mkconst_str(const std::string &str) +{ + RTLIL::Const val; + std::vector data; + data.reserve(str.size() * 8); + for (size_t i = 0; i < str.size(); i++) { + unsigned char ch = str[str.size() - i - 1]; + for (int j = 0; j < 8; j++) { + data.push_back((ch & 1) ? State::S1 : State::S0); + ch = ch >> 1; + } + } + val.bits = data; + val.flags |= RTLIL::CONST_FLAG_STRING; + return val; +} // When used as attributes or parameter values Verific constants come already processed. // - Real string values are already under quotes // - Numeric values with specified width are always converted to binary @@ -223,7 +239,7 @@ RTLIL::IdString VerificImporter::new_verific_id(Verific::DesignObj *obj) // // Note: For signed values, verific uses 'sb and decimal values can // also be negative. -static const RTLIL::Const verific_const(const char *value, DesignObj *obj, bool allow_string = true, bool output_signed = false) +static const RTLIL::Const verific_const(const char* type_name, const char *value, DesignObj *obj, bool allow_string = true, bool output_signed = false) { size_t found; char *end; @@ -232,7 +248,10 @@ static const RTLIL::Const verific_const(const char *value, DesignObj *obj, bool RTLIL::Const c; std::string val = std::string(value); if (obj->IsFromVhdl()) { - if (val.size()>1 && val[0]=='\"' && val.back()=='\"') { + if (type_name && strcmp(type_name, "real")==0) { + c = mkconst_str(val); + c.flags |= RTLIL::CONST_FLAG_REAL; + } else if (val.size()>1 && val[0]=='\"' && val.back()=='\"') { std::string data = val.substr(1,val.size()-2); bool isBinary = std::all_of(data.begin(), data.end(), [](char c) {return c=='1' || c=='0'; }); if (isBinary) @@ -250,10 +269,14 @@ static const RTLIL::Const verific_const(const char *value, DesignObj *obj, bool } else if (val == "true") { c = RTLIL::Const::from_string("1"); } else { - log_error("non-expected '%s' constant found", value); + c = mkconst_str(val); + log_warning("encoding value '%s' of type '%s' as string found", value, type_name ? type_name : "unknown"); } } else { - if (allow_string && val.size()>1 && val[0]=='\"' && val.back()=='\"') { + if (type_name && strcmp(type_name, "real")==0) { + c = mkconst_str(val); + c.flags |= RTLIL::CONST_FLAG_REAL; + } else if (allow_string && val.size()>1 && val[0]=='\"' && val.back()=='\"') { c = RTLIL::Const(val.substr(1,val.size()-2)); } else if ((found = val.find("'sb")) != std::string::npos) { is_signed = output_signed; @@ -267,12 +290,13 @@ static const RTLIL::Const verific_const(const char *value, DesignObj *obj, bool } else if (allow_string) { c = RTLIL::Const(val); } else { - log_error("expected numeric constant but found '%s'", value); + c = mkconst_str(val); + log_warning("encoding value '%s' of type '%s' as string found", value, type_name ? type_name : "unknown"); } - - if (is_signed) - c.flags |= RTLIL::CONST_FLAG_SIGNED; } + + if (is_signed) + c.flags |= RTLIL::CONST_FLAG_SIGNED; return c; } @@ -298,7 +322,7 @@ void VerificImporter::import_attributes(dict &att FOREACH_ATTRIBUTE(obj, mi, attr) { if (attr->Key()[0] == ' ' || attr->Value() == nullptr) continue; - attributes[RTLIL::escape_id(attr->Key())] = verific_const(attr->Value(), obj); + attributes[RTLIL::escape_id(attr->Key())] = verific_const(nullptr, attr->Value(), obj); } if (nl) { @@ -320,7 +344,7 @@ void VerificImporter::import_attributes(dict &att const char *k, *v; FOREACH_MAP_ITEM(type_range->GetEnumIdMap(), mi, &k, &v) { if (nl->IsFromVerilog()) { - auto const value = verific_const(v, nl, false); + auto const value = verific_const(type_name, v, nl, false); attributes.emplace(stringf("\\enum_value_%s", value.as_string().c_str()), RTLIL::escape_id(k)); } @@ -1326,7 +1350,8 @@ void VerificImporter::import_netlist(RTLIL::Design *design, Netlist *nl, std::ma MapIter mi; FOREACH_PARAMETER_OF_NETLIST(nl, mi, param_name, param_value) { module->avail_parameters(RTLIL::escape_id(param_name)); - module->parameter_default_values[RTLIL::escape_id(param_name)] = verific_const(param_value, nl); + const TypeRange *tr = nl->GetTypeRange(param_name) ; + module->parameter_default_values[RTLIL::escape_id(param_name)] = verific_const(tr->GetTypeName(), param_value, nl); } SetIter si; @@ -2026,7 +2051,8 @@ void VerificImporter::import_netlist(RTLIL::Design *design, Netlist *nl, std::ma const char *param_value ; if (is_blackbox(inst->View())) { FOREACH_PARAMETER_OF_INST(inst, mi2, param_name, param_value) { - cell->setParam(RTLIL::escape_id(param_name), verific_const(param_value, inst->View())); + const TypeRange *tr = inst->View()->GetTypeRange(param_name) ; + cell->setParam(RTLIL::escape_id(param_name), verific_const(tr->GetTypeName(), param_value, inst->View())); } } From 9eebc801705c6f7c179f354774b9d87174b3ec3f Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Mon, 18 Mar 2024 10:35:01 +0100 Subject: [PATCH 4/5] handle standard types --- frontends/verific/verific.cc | 47 +++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/frontends/verific/verific.cc b/frontends/verific/verific.cc index a5bcd44c48e..62c070db589 100644 --- a/frontends/verific/verific.cc +++ b/frontends/verific/verific.cc @@ -248,9 +248,50 @@ static const RTLIL::Const verific_const(const char* type_name, const char *value RTLIL::Const c; std::string val = std::string(value); if (obj->IsFromVhdl()) { - if (type_name && strcmp(type_name, "real")==0) { - c = mkconst_str(val); - c.flags |= RTLIL::CONST_FLAG_REAL; + if (type_name) { + if (strcmp(type_name, "integer")==0 || strcmp(type_name, "natural")==0 || strcmp(type_name, "positive")==0) { + decimal = std::strtol(value, &end, 10); + c = RTLIL::Const((int)decimal, 32); + } else if (strcmp(type_name, "boolean")==0) { + if (val == "false") { + c = RTLIL::Const::from_string("0"); + } else if (val == "true") { + c = RTLIL::Const::from_string("1"); + } else + log_error("Error parsing boolean\n"); + } else if (strcmp(type_name, "bit")==0 || strcmp(type_name, "STD_LOGIC")==0 || strcmp(type_name, "STD_ULOGIC")==0) { + if (val.size()==3 && val[0]=='\'' && val.back()=='\'') { + c = RTLIL::Const::from_string(val.substr(1,val.size()-2)); + } else + log_error("Error parsing %s\n", type_name); + } else if (strcmp(type_name, "character")==0) { + if (val.size()>1 && val[0]=='\"' && val.back()=='\"') { + c = RTLIL::Const((int)val[1], 32); + } else + log_error("Error parsing character\n"); + } else if (strcmp(type_name, "bit_vector")==0 || strcmp(type_name, "STD_LOGIC_VECTOR")==0 || strcmp(type_name, "STD_ULOGIC_VECTOR")==0 || + strcmp(type_name, "UNSIGNED")==0 || strcmp(type_name, "SIGNED")==0) { + if (val.size()>1 && val[0]=='\"' && val.back()=='\"') { + c = RTLIL::Const::from_string(val.substr(1,val.size()-2)); + } else + log_error("Error parsing %s\n", type_name); + if (strcmp(type_name, "SIGNED")==0) + is_signed = true; + } else if (strcmp(type_name, "real")==0) { + c = mkconst_str(val); + c.flags |= RTLIL::CONST_FLAG_REAL; + } else if (strcmp(type_name, "string")==0) { + if (!(val.size()>1 && val[0]=='\"' && val.back()=='\"')) + log_error("Error parsing string\n"); + c = RTLIL::Const(val.substr(1,val.size()-2)); + } else { + if (val.size()>1 && val[0]=='\"' && val.back()=='\"') + c = RTLIL::Const(val.substr(1,val.size()-2)); + else if (val.size()==3 && val[0]=='\'' && val.back()=='\'') + c = RTLIL::Const(val.substr(1,val.size()-2)); + else + c = RTLIL::Const(val); + } } else if (val.size()>1 && val[0]=='\"' && val.back()=='\"') { std::string data = val.substr(1,val.size()-2); bool isBinary = std::all_of(data.begin(), data.end(), [](char c) {return c=='1' || c=='0'; }); From 4367e176fbfcfc3722dfa4a7a12c66cd430e3bf5 Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Tue, 19 Mar 2024 09:15:04 +0100 Subject: [PATCH 5/5] code split and cleanup --- frontends/verific/verific.cc | 231 +++++++++++++++++++++-------------- 1 file changed, 141 insertions(+), 90 deletions(-) diff --git a/frontends/verific/verific.cc b/frontends/verific/verific.cc index 62c070db589..96e0469f6f8 100644 --- a/frontends/verific/verific.cc +++ b/frontends/verific/verific.cc @@ -230,6 +230,126 @@ RTLIL::Const mkconst_str(const std::string &str) val.flags |= RTLIL::CONST_FLAG_STRING; return val; } + +static const RTLIL::Const extract_vhdl_boolean(std::string &val) +{ + if (val == "false") + return RTLIL::Const::from_string("0"); + if (val == "true") + return RTLIL::Const::from_string("1"); + log_error("Expecting VHDL boolean value.\n"); +} + +static const RTLIL::Const extract_vhdl_bit(std::string &val, std::string &typ) +{ + if (val.size()==3 && val[0]=='\'' && val.back()=='\'') + return RTLIL::Const::from_string(val.substr(1,val.size()-2)); + log_error("Error parsing VHDL %s.\n", typ.c_str()); +} + +static const RTLIL::Const extract_vhdl_bit_vector(std::string &val, std::string &typ) +{ + if (val.size()>1 && val[0]=='\"' && val.back()=='\"') { + RTLIL::Const c = RTLIL::Const::from_string(val.substr(1,val.size()-2)); + if (typ == "signed") + c.flags |= RTLIL::CONST_FLAG_SIGNED; + return c; + } + log_error("Error parsing VHDL %s.\n", typ.c_str()); +} + +static const RTLIL::Const extract_vhdl_integer(std::string &val) +{ + char *end; + return RTLIL::Const((int)std::strtol(val.c_str(), &end, 10), 32); +} + +static const RTLIL::Const extract_vhdl_char(std::string &val) +{ + if (val.size()==3 && val[0]=='\"' && val.back()=='\"') + return RTLIL::Const((int)val[1], 32); + log_error("Error parsing VHDL character.\n"); +} + +static const RTLIL::Const extract_real_value(std::string &val) +{ + RTLIL::Const c = mkconst_str(val); + c.flags |= RTLIL::CONST_FLAG_REAL; + return c; +} + +static const RTLIL::Const extract_vhdl_string(std::string &val) +{ + if (!(val.size()>1 && val[0]=='\"' && val.back()=='\"')) + log_error("Error parsing VHDL string.\n"); + return RTLIL::Const(val.substr(1,val.size()-2)); +} + +static const RTLIL::Const extract_vhdl_const(const char *value, bool output_signed) +{ + RTLIL::Const c; + char *end; + int decimal; + bool is_signed = false; + std::string val = std::string(value); + + if (val.size()>1 && val[0]=='\"' && val.back()=='\"') { + std::string data = val.substr(1,val.size()-2); + bool isBinary = std::all_of(data.begin(), data.end(), [](char c) {return c=='1' || c=='0'; }); + if (isBinary) + c = RTLIL::Const::from_string(data); + else + c = RTLIL::Const(data); + } else if (val.size()==3 && val[0]=='\'' && val.back()=='\'') { + c = RTLIL::Const::from_string(val.substr(1,val.size()-2)); + } else if ((value[0] == '-' || (value[0] >= '0' && value[0] <= '9')) && + ((decimal = std::strtol(value, &end, 10)), !end[0])) { + is_signed = output_signed; + c = RTLIL::Const((int)decimal, 32); + } else if (val == "false") { + c = RTLIL::Const::from_string("0"); + } else if (val == "true") { + c = RTLIL::Const::from_string("1"); + } else { + c = mkconst_str(val); + log_warning("encoding value '%s' as string.\n", value); + } + if (is_signed) + c.flags |= RTLIL::CONST_FLAG_SIGNED; + return c; +} + +static const RTLIL::Const extract_verilog_const(const char *value, bool allow_string, bool output_signed) +{ + RTLIL::Const c; + char *end; + int decimal; + bool is_signed = false; + size_t found; + std::string val = std::string(value); + + if (allow_string && val.size()>1 && val[0]=='\"' && val.back()=='\"') { + c = RTLIL::Const(val.substr(1,val.size()-2)); + } else if ((found = val.find("'sb")) != std::string::npos) { + is_signed = output_signed; + c = RTLIL::Const::from_string(val.substr(found + 3)); + } else if ((found = val.find("'b")) != std::string::npos) { + c = RTLIL::Const::from_string(val.substr(found + 2)); + } else if ((value[0] == '-' || (value[0] >= '0' && value[0] <= '9')) && + ((decimal = std::strtol(value, &end, 10)), !end[0])) { + is_signed = output_signed; + c = RTLIL::Const((int)decimal, 32); + } else if (allow_string) { + c = RTLIL::Const(val); + } else { + c = mkconst_str(val); + log_warning("encoding value '%s' as string.\n", value); + } + if (is_signed) + c.flags |= RTLIL::CONST_FLAG_SIGNED; + return c; +} + // When used as attributes or parameter values Verific constants come already processed. // - Real string values are already under quotes // - Numeric values with specified width are always converted to binary @@ -241,104 +361,35 @@ RTLIL::Const mkconst_str(const std::string &str) // also be negative. static const RTLIL::Const verific_const(const char* type_name, const char *value, DesignObj *obj, bool allow_string = true, bool output_signed = false) { - size_t found; - char *end; - int decimal; - bool is_signed = false; - RTLIL::Const c; std::string val = std::string(value); + // VHDL if (obj->IsFromVhdl()) { if (type_name) { - if (strcmp(type_name, "integer")==0 || strcmp(type_name, "natural")==0 || strcmp(type_name, "positive")==0) { - decimal = std::strtol(value, &end, 10); - c = RTLIL::Const((int)decimal, 32); - } else if (strcmp(type_name, "boolean")==0) { - if (val == "false") { - c = RTLIL::Const::from_string("0"); - } else if (val == "true") { - c = RTLIL::Const::from_string("1"); - } else - log_error("Error parsing boolean\n"); - } else if (strcmp(type_name, "bit")==0 || strcmp(type_name, "STD_LOGIC")==0 || strcmp(type_name, "STD_ULOGIC")==0) { - if (val.size()==3 && val[0]=='\'' && val.back()=='\'') { - c = RTLIL::Const::from_string(val.substr(1,val.size()-2)); - } else - log_error("Error parsing %s\n", type_name); - } else if (strcmp(type_name, "character")==0) { - if (val.size()>1 && val[0]=='\"' && val.back()=='\"') { - c = RTLIL::Const((int)val[1], 32); - } else - log_error("Error parsing character\n"); - } else if (strcmp(type_name, "bit_vector")==0 || strcmp(type_name, "STD_LOGIC_VECTOR")==0 || strcmp(type_name, "STD_ULOGIC_VECTOR")==0 || - strcmp(type_name, "UNSIGNED")==0 || strcmp(type_name, "SIGNED")==0) { - if (val.size()>1 && val[0]=='\"' && val.back()=='\"') { - c = RTLIL::Const::from_string(val.substr(1,val.size()-2)); - } else - log_error("Error parsing %s\n", type_name); - if (strcmp(type_name, "SIGNED")==0) - is_signed = true; - } else if (strcmp(type_name, "real")==0) { - c = mkconst_str(val); - c.flags |= RTLIL::CONST_FLAG_REAL; - } else if (strcmp(type_name, "string")==0) { - if (!(val.size()>1 && val[0]=='\"' && val.back()=='\"')) - log_error("Error parsing string\n"); - c = RTLIL::Const(val.substr(1,val.size()-2)); - } else { + std::string typ = std::string(type_name); + transform(typ.begin(), typ.end(), typ.begin(), ::tolower); + if (typ == "integer" || typ == "natural" || typ=="positive") return extract_vhdl_integer(val); + else if (typ =="boolean") return extract_vhdl_boolean(val); + else if (typ == "bit" || typ =="std_logic" || typ == "std_ulogic") return extract_vhdl_bit(val,typ); + else if (typ == "character") return extract_vhdl_char(val); + else if (typ == "bit_vector" || typ == "std_logic_vector" || typ == "std_ulogic_vector" || + typ == "unsigned" || typ == "signed") return extract_vhdl_bit_vector(val,typ); + else if (typ == "real") return extract_real_value(val); + else if (typ == "string") return extract_vhdl_string(val); + else { if (val.size()>1 && val[0]=='\"' && val.back()=='\"') - c = RTLIL::Const(val.substr(1,val.size()-2)); + return RTLIL::Const(val.substr(1,val.size()-2)); else if (val.size()==3 && val[0]=='\'' && val.back()=='\'') - c = RTLIL::Const(val.substr(1,val.size()-2)); + return RTLIL::Const(val.substr(1,val.size()-2)); else - c = RTLIL::Const(val); + return RTLIL::Const(val); } - } else if (val.size()>1 && val[0]=='\"' && val.back()=='\"') { - std::string data = val.substr(1,val.size()-2); - bool isBinary = std::all_of(data.begin(), data.end(), [](char c) {return c=='1' || c=='0'; }); - if (isBinary) - c = RTLIL::Const::from_string(data); - else - c = RTLIL::Const(data); - } else if (val.size()==3 && val[0]=='\'' && val.back()=='\'') { - c = RTLIL::Const::from_string(val.substr(1,val.size()-2)); - } else if ((value[0] == '-' || (value[0] >= '0' && value[0] <= '9')) && - ((decimal = std::strtol(value, &end, 10)), !end[0])) { - is_signed = output_signed; - c = RTLIL::Const((int)decimal, 32); - } else if (val == "false") { - c = RTLIL::Const::from_string("0"); - } else if (val == "true") { - c = RTLIL::Const::from_string("1"); - } else { - c = mkconst_str(val); - log_warning("encoding value '%s' of type '%s' as string found", value, type_name ? type_name : "unknown"); - } - } else { - if (type_name && strcmp(type_name, "real")==0) { - c = mkconst_str(val); - c.flags |= RTLIL::CONST_FLAG_REAL; - } else if (allow_string && val.size()>1 && val[0]=='\"' && val.back()=='\"') { - c = RTLIL::Const(val.substr(1,val.size()-2)); - } else if ((found = val.find("'sb")) != std::string::npos) { - is_signed = output_signed; - c = RTLIL::Const::from_string(val.substr(found + 3)); - } else if ((found = val.find("'b")) != std::string::npos) { - c = RTLIL::Const::from_string(val.substr(found + 2)); - } else if ((value[0] == '-' || (value[0] >= '0' && value[0] <= '9')) && - ((decimal = std::strtol(value, &end, 10)), !end[0])) { - is_signed = output_signed; - c = RTLIL::Const((int)decimal, 32); - } else if (allow_string) { - c = RTLIL::Const(val); - } else { - c = mkconst_str(val); - log_warning("encoding value '%s' of type '%s' as string found", value, type_name ? type_name : "unknown"); - } + } else extract_vhdl_const(value, output_signed); } - - if (is_signed) - c.flags |= RTLIL::CONST_FLAG_SIGNED; - return c; + // SystemVerilog + if (type_name && strcmp(type_name, "real")==0) { + return extract_real_value(val); + } else + return extract_verilog_const(value, allow_string, output_signed); } static const std::string verific_unescape(const char *value)