Skip to content

Commit

Permalink
slang-reflect fixes on data assignment and namespaces (#844)
Browse files Browse the repository at this point in the history
* [slang-reflect] Correctly assign members greater than 64 bits

We were not checking if a struct member was greater than 64 bits
when giving it a value in the struct constructor, and always using the
to_uint64() function loosing data in the conversion.

* [slang-reflect] Provide type namespace if necessary in get_* functions

When returning a non-primitive type from a get_* function, we were
assuming that the type was part of the current namespace, which is not
always true. This commit fixes this issue by adding the namespace
when necessary.
  • Loading branch information
Sustrak authored Nov 3, 2023
1 parent a682452 commit d584282
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions tools/reflect/src/SvStruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ void SvStruct::toCpp(HppFile& hppFile, std::string_view _namespace, const SvAlia
for (const auto& member : members) {
if (member.second.size == 1)
values.emplace_back(fmt::format("__data.get_bit({0}_s)", member.first));
else if (member.second.size > 64)
values.emplace_back(
fmt::format("__data.range({0}_s + {0}_w - 1, {0}_s)", member.first));
else
values.emplace_back(fmt::format(
"__data.range({0}_s + {0}_w - 1, {0}_s).to_uint64()", member.first));
Expand Down Expand Up @@ -228,15 +231,30 @@ void SvStruct::toCpp(HppFile& hppFile, std::string_view _namespace, const SvAlia

//* STATIC GET FUNCTIONS *//
for (const auto& member : members) {
hppFile.addWithIndent(fmt::format("static {} get_{} (const {}& __data) {{\n",
member.second.toString(), member.first, cppTypeStr));
if (member.second.isStructOrEnum() && _namespace != member.second._namespace) {
hppFile.addWithIndent(fmt::format("static {}::{} get_{} (const {}& __data) {{\n",
member.second._namespace, member.second.toString(),
member.first, cppTypeStr));
}
else {
hppFile.addWithIndent(fmt::format("static {} get_{} (const {}& __data) {{\n",
member.second.toString(), member.first, cppTypeStr));
}
hppFile.increaseIndent();
std::string value;
if (cppType == CppType::SC_BV)
value = fmt::format("__data.range({0}_s + {0}_w - 1, {0}_s).to_uint64()", member.first);
else
if (cppType == CppType::SC_BV) {
if (member.second.size == 1)
value = fmt::format("__data.get_bit({0}_s)", member.first);
else if (member.second.size > 64)
value = fmt::format("__data.range({0}_s + {0}_w - 1, {0}_s)", member.first);
else
value = fmt::format("__data.range({0}_s + {0}_w - 1, {0}_s).to_uint64()",
member.first);
}
else {
value = fmt::format("(__data >> {0}_s) & (~0ULL >> (64 - {1}))", member.first,
member.second.size);
}

if (member.second.isStructOrEnum())
hppFile.addWithIndent(fmt::format("return {}({});\n", member.second.toString(), value));
Expand Down

0 comments on commit d584282

Please sign in to comment.