Don't visit vpiFullName Signed-off-by: Kamil Rakoczy <krakoczy@antmicro.com>
diff --git a/systemverilog-plugin/UhdmAst.cc b/systemverilog-plugin/UhdmAst.cc index 1b6fdf0..a13c8de 100644 --- a/systemverilog-plugin/UhdmAst.cc +++ b/systemverilog-plugin/UhdmAst.cc
@@ -182,93 +182,6 @@ } } -static std::string get_parent_name(vpiHandle parent_h) -{ - std::string parent_name; - if (auto p = vpi_get_str(vpiFullName, parent_h)) { - parent_name = p; - } else if (auto p = vpi_get_str(vpiName, parent_h)) { - parent_name = p; - } else if (auto p = vpi_get_str(vpiDefName, parent_h)) { - parent_name = p; - } - return parent_name; -} - -// Warning: Takes ownership of `parent_h` and releases it. -static void find_ancestor_name(vpiHandle parent_h, std::string &name, std::string &parent_name) -{ - - while (!parent_name.empty()) { - parent_name = parent_name + "."; - if ((name.rfind(parent_name) != std::string::npos)) { - name = name.substr(name.rfind(parent_name) + parent_name.size()); - break; - } else { - auto old_parent_h = parent_h; - parent_h = vpi_handle(vpiParent, parent_h); - vpi_release_handle(old_parent_h); - - if (parent_h) { - parent_name = get_parent_name(parent_h); - } else { - parent_name.clear(); - } - } - } - vpi_release_handle(parent_h); -} - -static std::string get_name(vpiHandle obj_h, bool prefer_full_name = false) -{ - auto first_check = prefer_full_name ? vpiFullName : vpiName; - auto last_check = prefer_full_name ? vpiName : vpiFullName; - std::string name; - if (auto s = vpi_get_str(first_check, obj_h)) { - name = s; - } else if (auto s = vpi_get_str(vpiDefName, obj_h)) { - name = s; - } else if (auto s = vpi_get_str(last_check, obj_h)) { - name = s; - } - // We are looking for the ancestor name to use it as a delimeter - // when stripping the name of the current node. - // We used to strip the name by searching for "." in it, but this - // approach didn't work for the names whith "." as an escaped - // character. - vpiHandle parent_h = vpi_handle(vpiParent, obj_h); - - if (parent_h) { - std::string parent_name; - parent_name = get_parent_name(parent_h); - - if (parent_name.empty()) { - // Nodes of certain types, like param_assign, don't have - // a name, so we need to look further for the ancestor. - auto old_parent_h = parent_h; - parent_h = vpi_handle(vpiParent, parent_h); - vpi_release_handle(old_parent_h); - - if (parent_h) { - parent_name = get_parent_name(parent_h); - } - } - find_ancestor_name(parent_h, name, parent_name); - } - sanitize_symbol_name(name); - return name; -} - -static std::string strip_package_name(std::string name) -{ - auto sep_index = name.find("::"); - if (sep_index != string::npos) { - name = name.substr(sep_index + 1); - name[0] = '\\'; - } - return name; -} - static std::string get_object_name(vpiHandle obj_h, const std::vector<int> &name_fields = {vpiName}) { std::string objectName; @@ -282,6 +195,18 @@ return objectName; } +static std::string get_name(vpiHandle obj_h) { return get_object_name(obj_h, {vpiName, vpiDefName}); } + +static std::string strip_package_name(std::string name) +{ + auto sep_index = name.find("::"); + if (sep_index != string::npos) { + name = name.substr(sep_index + 1); + name[0] = '\\'; + } + return name; +} + static AST::AstNode *mkconst_real(double d) { AST::AstNode *node = new AST::AstNode(AST::AST_REALVALUE); @@ -1796,9 +1721,9 @@ } } -void UhdmAst::apply_name_from_current_obj(AST::AstNode &target_node, bool prefer_full_name) const +void UhdmAst::apply_name_from_current_obj(AST::AstNode &target_node) const { - target_node.str = get_name(obj_h, prefer_full_name); + target_node.str = get_name(obj_h); auto it = node_renames.find(target_node.str); if (it != node_renames.end()) target_node.str = it->second; @@ -1811,11 +1736,11 @@ return AstNodeBuilder(std::move(node)); }; -AstNodeBuilder UhdmAst::make_named_node(AST::AstNodeType type, bool prefer_full_name) const +AstNodeBuilder UhdmAst::make_named_node(AST::AstNodeType type) const { auto node = std::make_unique<AST::AstNode>(type); apply_location_from_current_obj(*node); - apply_name_from_current_obj(*node, prefer_full_name); + apply_name_from_current_obj(*node); return AstNodeBuilder(std::move(node)); }; @@ -1835,10 +1760,10 @@ return make_node(AST::AST_CONSTANT).value(value, false, width); }; -AST::AstNode *UhdmAst::make_ast_node(AST::AstNodeType type, std::vector<AST::AstNode *> children, bool prefer_full_name) +AST::AstNode *UhdmAst::make_ast_node(AST::AstNodeType type, std::vector<AST::AstNode *> children) { auto node = new AST::AstNode(type); - apply_name_from_current_obj(*node, prefer_full_name); + apply_name_from_current_obj(*node); apply_location_from_current_obj(*node); node->children = children; return node; @@ -4295,8 +4220,6 @@ void UhdmAst::process_hier_path() { - current_node = make_ast_node(AST::AST_IDENTIFIER); - current_node->str = "\\"; AST::AstNode *top_node = nullptr; visit_one_to_many({vpiActual}, obj_h, [&](AST::AstNode *node) { if (node) { @@ -4304,13 +4227,10 @@ node->str = node->str.substr(0, node->str.find('[')); // for first node, just set correct string and move any children if (!top_node) { - current_node->str += node->str.substr(1); - current_node->children = std::move(node->children); - node->children.clear(); + current_node = node; top_node = current_node; - delete node; } else { - if (node->type == AST::AST_IDENTIFIER) { + if (node->type == AST::AST_IDENTIFIER && !node->str.empty()) { node->type = static_cast<AST::AstNodeType>(AST::Extended::AST_DOT); top_node->children.push_back(node); top_node = node; @@ -4477,13 +4397,6 @@ current_node->children.push_back(node); } }); - // Prefer fully qualified name of a function (prefixed with a scope). - // This is important when a single function which has been imported from a package - // calls another function that is not imported in the calling scope. - if (vpiHandle function_h = vpi_handle(vpiFunction, obj_h)) { - current_node->str = get_name(function_h, true); - vpi_release_handle(function_h); - } } void UhdmAst::process_immediate_assert() @@ -4823,7 +4736,7 @@ void UhdmAst::process_parameter() { auto type = vpi_get(vpiLocalParam, obj_h) == 1 ? AST::AST_LOCALPARAM : AST::AST_PARAMETER; - current_node = make_ast_node(type, {}, true); + current_node = make_ast_node(type); std::vector<AST::AstNode *> packed_ranges; // comes before wire name std::vector<AST::AstNode *> unpacked_ranges; // comes after wire name visit_one_to_many({vpiRange}, obj_h, [&](AST::AstNode *node) { unpacked_ranges.push_back(node); });
diff --git a/systemverilog-plugin/UhdmAst.h b/systemverilog-plugin/UhdmAst.h index bf2d1b5..d60e827 100644 --- a/systemverilog-plugin/UhdmAst.h +++ b/systemverilog-plugin/UhdmAst.h
@@ -39,12 +39,12 @@ // Reads location info (start/end line/column numbers, file name) from `obj_h` and sets them on `target_node`. void apply_location_from_current_obj(::Yosys::AST::AstNode &target_node) const; // Reads object name from `obj_h` and assigns it to `target_node`. - void apply_name_from_current_obj(::Yosys::AST::AstNode &target_node, bool prefer_full_name = false) const; + void apply_name_from_current_obj(::Yosys::AST::AstNode &target_node) const; // Creates node of specified `type` with location properties read from `obj_h`. AstNodeBuilder make_node(::Yosys::AST::AstNodeType type) const; // Creates node of specified `type` with location properties and name read from `obj_h`. - AstNodeBuilder make_named_node(::Yosys::AST::AstNodeType type, bool prefer_full_name = false) const; + AstNodeBuilder make_named_node(::Yosys::AST::AstNodeType type) const; // Creates AST_IDENTIFIER node with specified `id` and location properties read from `obj_h`. AstNodeBuilder make_ident(std::string id) const; // Creates signed AST_CONSTANT node with specified `value` and location properties read from `obj_h`. @@ -55,8 +55,7 @@ // Create an AstNode of the specified type with metadata extracted from // the given vpiHandle. // OBSOLETE: use `make_node` or `make_named_node` instead. - ::Yosys::AST::AstNode *make_ast_node(::Yosys::AST::AstNodeType type, std::vector<::Yosys::AST::AstNode *> children = {}, - bool prefer_full_name = false); + ::Yosys::AST::AstNode *make_ast_node(::Yosys::AST::AstNodeType type, std::vector<::Yosys::AST::AstNode *> children = {}); // Create an identifier AstNode // OBSOLETE: use `make_ident` instead.