Skip to content

Commit

Permalink
add option to force pin renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
SJulianS committed Oct 23, 2023
1 parent 6fadad1 commit 6631b70
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
3 changes: 2 additions & 1 deletion include/hal_core/netlist/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,10 @@ namespace hal
*
* @param[in] pin - The pin.
* @param[in] new_name - The name to be assigned to the pin.
* @param[in] force - Set `true` to enforce renaming, `false` otherwise. If a pin with the same name already exists, that existing pin will be renamed. Defaults to `false`.
* @returns `true` on success, `false` otherwise.
*/
bool set_pin_name(ModulePin* pin, const std::string& new_name);
bool set_pin_name(ModulePin* pin, const std::string& new_name, const bool force = false);

/**
* Set the type of the given pin.
Expand Down
31 changes: 21 additions & 10 deletions src/netlist/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ namespace hal
return nullptr;
}

bool Module::set_pin_name(ModulePin* pin, const std::string& new_name)
bool Module::set_pin_name(ModulePin* pin, const std::string& new_name, const bool force)
{
if (pin == nullptr)
{
Expand All @@ -1076,16 +1076,27 @@ namespace hal
return false;
}

if (m_pin_names_map.find(new_name) != m_pin_names_map.end())
if (const auto pin_it = m_pin_names_map.find(new_name); pin_it != m_pin_names_map.end())
{
log_warning("module",
"could not set name for pin '{}' with ID {} of module '{}' with ID {}: a pin with name '{}' already exists within the module",
pin->get_name(),
pin->get_id(),
m_name,
m_id,
new_name);
return false;
if (force)
{
u32 ctr = 2;
while (!this->set_pin_name(pin_it->second, new_name + "__" + std::to_string(ctr) + "__"))
{
ctr++;
}
}
else
{
log_warning("module",
"could not set name for pin '{}' with ID {} of module '{}' with ID {}: a pin with name '{}' already exists within the module",
pin->get_name(),
pin->get_id(),
m_name,
m_id,
new_name);
return false;
}
}

if (const std::string& old_name = pin->get_name(); old_name != new_name)
Expand Down
3 changes: 2 additions & 1 deletion src/python_bindings/bindings/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,11 +633,12 @@ namespace hal
:rtype: hal_py.ModulePinGroup or None
)");

py_module.def("set_pin_name", &Module::set_pin_name, py::arg("pin"), py::arg("new_name"), R"(
py_module.def("set_pin_name", &Module::set_pin_name, py::arg("pin"), py::arg("new_name"), py::arg("force") = false, R"(
Set the name of the given pin.
:param hal_py.ModulePin pin: The pin.
:param str new_name: The name to be assigned to the pin.
:param bool force: Set ``True`` to enforce renaming, ``False`` otherwise. If a pin with the same name already exists, that existing pin will be renamed. Defaults to ``False``.
:returns: ``True`` on success, ``False`` otherwise.
:rtype: bool
)");
Expand Down

0 comments on commit 6631b70

Please sign in to comment.