Skip to content

Commit d852e75

Browse files
authored
Merge pull request chipsalliance#509 from antmicro/kr/add_anonymous_enum_vars
systemverilog-plugin: add support for multiple variables declared in anonymous enum var
2 parents bbca3dd + da37374 commit d852e75

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

systemverilog-plugin/UhdmAst.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2474,6 +2474,17 @@ void UhdmAst::process_enum_typespec()
24742474
const auto *enum_object = (const UHDM::enum_typespec *)handle->object;
24752475
const auto *typespec = enum_object->Base_typespec();
24762476

2477+
if (current_node->str.empty()) {
2478+
// anonymous typespec, check if not already created
2479+
if (const auto enum_iter = shared.anonymous_enums.find(enum_object); enum_iter != shared.anonymous_enums.end()) {
2480+
// we already created typedef for this.
2481+
delete current_node;
2482+
current_node = make_node(AST::AST_WIRETYPE);
2483+
current_node->str = enum_iter->second;
2484+
return;
2485+
}
2486+
}
2487+
24772488
if (typespec && typespec->UhdmType() == UHDM::uhdmlogic_typespec) {
24782489
// If it's a logic_typespec, try to reduce expressions inside of it.
24792490
// The `reduceExpr` function needs the whole context of the enum typespec
@@ -2562,6 +2573,17 @@ void UhdmAst::process_enum_typespec()
25622573
if (range) {
25632574
delete range;
25642575
}
2576+
if (current_node->str.empty()) {
2577+
// anonymous typespec
2578+
std::string typedef_name = "$systemverilog_plugin$anonymous_enum" + std::to_string(shared.next_anonymous_enum_typedef_id());
2579+
current_node->str = typedef_name;
2580+
auto current_scope = find_ancestor({AST::AST_PACKAGE, AST::AST_MODULE, AST::AST_BLOCK, AST::AST_GENBLOCK});
2581+
uhdmast_assert(current_scope != nullptr);
2582+
move_type_to_new_typedef(current_scope, current_node);
2583+
current_node = make_node(AST::AST_WIRETYPE);
2584+
current_node->str = typedef_name;
2585+
shared.anonymous_enums[enum_object] = std::move(typedef_name);
2586+
}
25652587
}
25662588

25672589
void UhdmAst::process_enum_const()

systemverilog-plugin/uhdmastshared.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class UhdmAstShared
2525
// Used for generating unique names for anonymous types used as parameters.
2626
unsigned anonymous_type_count = 0;
2727

28+
// Used to generate unique names for anonymous enum typedefs
29+
unsigned anonymous_enum_typedef_count = 0;
30+
2831
public:
2932
~UhdmAstShared()
3033
{
@@ -45,6 +48,9 @@ class UhdmAstShared
4548
// Generate the next anonymous type ID (starting with 0).
4649
unsigned next_anonymous_type_id() { return anonymous_type_count++; }
4750

51+
// Generate the next anonymous enum typedef ID (starting with 0).
52+
unsigned next_anonymous_enum_typedef_id() { return anonymous_enum_typedef_count++; }
53+
4854
// Flag that determines whether debug info should be printed
4955
bool debug_flag = false;
5056

@@ -81,8 +87,12 @@ class UhdmAstShared
8187
std::unordered_map<std::string, ::Yosys::AST::AstNode *> param_types;
8288

8389
::Yosys::AST::AstNode *current_top_node = nullptr;
90+
8491
// Set of non-synthesizable objects to skip in current design;
8592
std::set<const UHDM::BaseClass *> nonSynthesizableObjects;
93+
94+
// Map of anonymous enum types to generated typedefs
95+
std::unordered_map<const UHDM::enum_typespec *, std::string> anonymous_enums;
8696
};
8797

8898
} // namespace systemverilog_plugin

0 commit comments

Comments
 (0)