Skip to content

Commit

Permalink
add option to enforce pin (group) naming on creation
Browse files Browse the repository at this point in the history
  • Loading branch information
SJulianS committed Oct 23, 2023
1 parent 3082657 commit 4b35a0f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 44 deletions.
26 changes: 16 additions & 10 deletions include/hal_core/netlist/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,10 @@ namespace hal
* @param[in] net - The net that the pin is being assigned to.
* @param[in] type - The type of the pin. Defaults to `PinType::none`.
* @param[in] create_group - Set `true` to automatically create a pin group and assign the pin, `false` otherwise. Defaults to `true`.
* @param[in] force_name - Set `true` to enforce the name, `false` otherwise. If a pin with the same name already exists, the existing pin will be renamed. Defaults to `false`.
* @returns The module pin on success, an error message otherwise.
*/
Result<ModulePin*> create_pin(const u32 id, const std::string& name, Net* net, PinType type = PinType::none, bool create_group = true);
Result<ModulePin*> create_pin(const u32 id, const std::string& name, Net* net, PinType type = PinType::none, bool create_group = true, bool force_name = false);

/**
* Manually create a module pin and assign it to a net.
Expand All @@ -355,9 +356,10 @@ namespace hal
* @param[in] net - The net that the pin is being assigned to.
* @param[in] type - The type of the pin. Defaults to `PinType::none`.
* @param[in] create_group - Set `true` to automatically create a pin group and assign the pin, `false` otherwise. Defaults to `true`.
* @param[in] force_name - Set `true` to enforce the name, `false` otherwise. If a pin with the same name already exists, the existing pin will be renamed. Defaults to `false`.
* @returns The module pin on success, an error message otherwise.
*/
Result<ModulePin*> create_pin(const std::string& name, Net* net, PinType type = PinType::none, bool create_group = true);
Result<ModulePin*> create_pin(const std::string& name, Net* net, PinType type = PinType::none, bool create_group = true, bool force_name = false);

/**
* Get an ordered vector of all pins of the module.
Expand Down Expand Up @@ -459,10 +461,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`.
* @param[in] force_name - Set `true` to enforce the name, `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, const bool force = false);
bool set_pin_name(ModulePin* pin, const std::string& new_name, bool force_name = false);

/**
* Set the type of the given pin.
Expand All @@ -484,6 +486,7 @@ namespace hal
* @param[in] ascending - Set `true` for ascending pin order (from 0 to n-1), `false` otherwise (from n-1 to 0). Defaults to `true`.
* @param[in] start_index - The start index of the pin group. Defaults to `0`.
* @param[in] delete_empty_groups - Set `true` to delete groups that are empty after the pins have been assigned to the new group, `false` to keep empty groups. Defaults to `true`.
* @param[in] force_name - Set `true` to enforce the name, `false` otherwise. If a pin group with the same name already exists, the existing pin group will be renamed. Defaults to `false`.
* @returns The pin group on success, an error message otherwise.
*/
Result<PinGroup<ModulePin>*> create_pin_group(const u32 id,
Expand All @@ -493,7 +496,8 @@ namespace hal
PinType type = PinType::none,
bool ascending = true,
u32 start_index = 0,
bool delete_empty_groups = true);
bool delete_empty_groups = true,
bool force_name = false);

/**
* Create a new pin group with the given name.
Expand All @@ -506,6 +510,7 @@ namespace hal
* @param[in] ascending - Set `true` for ascending pin order (from 0 to n-1), `false` otherwise (from n-1 to 0). Defaults to `true`.
* @param[in] start_index - The start index of the pin group. Defaults to `0`.
* @param[in] delete_empty_groups - Set `true` to delete groups that are empty after the pins have been assigned to the new group, `false` to keep empty groups. Defaults to `true`.
* @param[in] force_name - Set `true` to enforce the name, `false` otherwise. If a pin group with the same name already exists, the existing pin group will be renamed. Defaults to `false`.
* @returns The pin group on success, an error message otherwise.
*/
Result<PinGroup<ModulePin>*> create_pin_group(const std::string& name,
Expand All @@ -514,7 +519,8 @@ namespace hal
PinType type = PinType::none,
bool ascending = true,
u32 start_index = 0,
bool delete_empty_groups = true);
bool delete_empty_groups = true,
bool force_name = false);

/**
* Delete the given pin group.
Expand All @@ -539,10 +545,10 @@ namespace hal
*
* @param[in] pin_group - The pin group.
* @param[in] new_name - The name to be assigned to the pin group.
* @param[in] force - Set `true` to enforce renaming, `false` otherwise. If a pin group with the same name already exists, that existing pin group will be renamed. Defaults to `false`.
* @param[in] force_name - Set `true` to enforce the name, `false` otherwise. If a pin group with the same name already exists, the existing pin group will be renamed. Defaults to `false`.
* @returns `true` on success, `false` otherwise.
*/
bool set_pin_group_name(PinGroup<ModulePin>* pin_group, const std::string& new_name, const bool force = false);
bool set_pin_group_name(PinGroup<ModulePin>* pin_group, const std::string& new_name, bool force_name = false);

/**
* Set the type of the given pin group.
Expand Down Expand Up @@ -737,9 +743,9 @@ namespace hal
Result<std::monostate> check_net(Net* net, bool recursive = false);
Result<ModulePin*> assign_pin_net(const u32 pin_id, Net* net, PinDirection direction, const std::string& name = "", PinType type = PinType::none);
Result<std::monostate> remove_pin_net(Net* net);
Result<ModulePin*> create_pin_internal(const u32 id, const std::string& name, Net* net, PinDirection direction, PinType type);
Result<ModulePin*> create_pin_internal(const u32 id, const std::string& name, Net* net, PinDirection direction, PinType type, bool force_name);
Result<std::monostate> delete_pin_internal(ModulePin* pin);
Result<PinGroup<ModulePin>*> create_pin_group_internal(const u32 id, const std::string& name, PinDirection direction, PinType type, bool ascending, u32 start_index);
Result<PinGroup<ModulePin>*> create_pin_group_internal(const u32 id, const std::string& name, PinDirection direction, PinType type, bool ascending, u32 start_index, bool force_name);
Result<std::monostate> delete_pin_group_internal(PinGroup<ModulePin>* pin_group);
};
} // namespace hal
73 changes: 51 additions & 22 deletions src/netlist/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ namespace hal
return m_next_pin_group_id;
}

Result<ModulePin*> Module::create_pin(const u32 id, const std::string& name, Net* net, PinType type, bool create_group)
Result<ModulePin*> Module::create_pin(const u32 id, const std::string& name, Net* net, PinType type, bool create_group, bool force_name)
{
if (name.empty())
{
Expand Down Expand Up @@ -825,15 +825,15 @@ namespace hal
+ " is neither an input nor an output");
}

if (auto pin_res = create_pin_internal(id, name, net, direction, type); pin_res.is_error())
if (auto pin_res = create_pin_internal(id, name, net, direction, type, force_name); pin_res.is_error())
{
return ERR_APPEND(pin_res.get_error(), "could not create pin '" + name + "' for module '" + m_name + "' with ID " + std::to_string(m_id));
}
else
{
if (create_group)
{
if (const auto group_res = create_pin_group_internal(get_unique_pin_group_id(), name, direction, type, true, 0); group_res.is_error())
if (const auto group_res = create_pin_group_internal(get_unique_pin_group_id(), name, direction, type, true, 0, force_name); group_res.is_error())
{
assert(delete_pin_internal(pin_res.get()).is_ok());
return ERR_APPEND(group_res.get_error(), "could not create pin '" + name + "' for module '" + m_name + "' with ID " + std::to_string(m_id) + ": failed to create pin group");
Expand All @@ -854,9 +854,9 @@ namespace hal
}
}

Result<ModulePin*> Module::create_pin(const std::string& name, Net* net, PinType type, bool create_group)
Result<ModulePin*> Module::create_pin(const std::string& name, Net* net, PinType type, bool create_group, bool force_name)
{
return create_pin(get_unique_pin_id(), name, net, type, create_group);
return create_pin(get_unique_pin_id(), name, net, type, create_group, force_name);
}

std::vector<ModulePin*> Module::get_pins(const std::function<bool(ModulePin*)>& filter) const
Expand Down Expand Up @@ -1056,7 +1056,7 @@ namespace hal
return nullptr;
}

bool Module::set_pin_name(ModulePin* pin, const std::string& new_name, const bool force)
bool Module::set_pin_name(ModulePin* pin, const std::string& new_name, bool force_name)
{
if (pin == nullptr)
{
Expand All @@ -1078,7 +1078,7 @@ namespace hal

if (const auto pin_it = m_pin_names_map.find(new_name); pin_it != m_pin_names_map.end())
{
if (force)
if (force_name)
{
u32 ctr = 2;
while (!this->set_pin_name(pin_it->second, new_name + "__" + std::to_string(ctr) + "__"))
Expand Down Expand Up @@ -1133,7 +1133,7 @@ namespace hal
return true;
}

bool Module::set_pin_group_name(PinGroup<ModulePin>* pin_group, const std::string& new_name, const bool force)
bool Module::set_pin_group_name(PinGroup<ModulePin>* pin_group, const std::string& new_name, bool force_name)
{
if (pin_group == nullptr)
{
Expand All @@ -1157,7 +1157,7 @@ namespace hal

if (const auto pin_group_it = m_pin_group_names_map.find(new_name); pin_group_it != m_pin_group_names_map.end())
{
if (force)
if (force_name)
{
u32 ctr = 2;
while (!this->set_pin_group_name(pin_group_it->second, new_name + "__" + std::to_string(ctr) + "__"))
Expand Down Expand Up @@ -1246,15 +1246,16 @@ namespace hal
PinType type,
bool ascending,
u32 start_index,
bool delete_empty_groups)
bool delete_empty_groups,
bool force_name)
{
if (name.empty())
{
return ERR("could not create pin group for module '" + m_name + "' with ID " + std::to_string(m_id) + ": empty string passed as name");
}

PinGroup<ModulePin>* pin_group;
if (auto res = create_pin_group_internal(id, name, direction, type, ascending, start_index); res.is_error())
if (auto res = create_pin_group_internal(id, name, direction, type, ascending, start_index, force_name); res.is_error())
{
return ERR_APPEND(res.get_error(), "could not create pin group '" + name + "' for module '" + m_name + "' with ID " + std::to_string(m_id));
}
Expand All @@ -1276,10 +1277,16 @@ namespace hal
return OK(pin_group);
}

Result<PinGroup<ModulePin>*>
Module::create_pin_group(const std::string& name, const std::vector<ModulePin*> pins, PinDirection direction, PinType type, bool ascending, u32 start_index, bool delete_empty_groups)
Result<PinGroup<ModulePin>*> Module::create_pin_group(const std::string& name,
const std::vector<ModulePin*> pins,
PinDirection direction,
PinType type,
bool ascending,
u32 start_index,
bool delete_empty_groups,
bool force_name)
{
return create_pin_group(get_unique_pin_group_id(), name, pins, direction, type, ascending, start_index, delete_empty_groups);
return create_pin_group(get_unique_pin_group_id(), name, pins, direction, type, ascending, start_index, delete_empty_groups, force_name);
}

Result<std::monostate> Module::delete_pin_group(PinGroup<ModulePin>* pin_group)
Expand Down Expand Up @@ -1524,7 +1531,7 @@ namespace hal

// create pin
ModulePin* pin;
if (auto res = create_pin_internal(pin_id, name_internal, net, direction, type); res.is_error())
if (auto res = create_pin_internal(pin_id, name_internal, net, direction, type, false); res.is_error())
{
return ERR_APPEND(res.get_error(), "could not assign pin '" + name_internal + "' to net: failed to create pin");
}
Expand All @@ -1533,7 +1540,7 @@ namespace hal
pin = res.get();
}

if (const auto group_res = create_pin_group_internal(get_unique_pin_group_id(), name_internal, pin->get_direction(), pin->get_type(), true, 0); group_res.is_error())
if (const auto group_res = create_pin_group_internal(get_unique_pin_group_id(), name_internal, pin->get_direction(), pin->get_type(), true, 0, false); group_res.is_error())
{
return ERR_APPEND(group_res.get_error(), "could not assign pin '" + name_internal + "' to net: failed to create pin group");
}
Expand Down Expand Up @@ -1588,7 +1595,7 @@ namespace hal
return OK({});
}

Result<ModulePin*> Module::create_pin_internal(const u32 id, const std::string& name, Net* net, PinDirection direction, PinType type)
Result<ModulePin*> Module::create_pin_internal(const u32 id, const std::string& name, Net* net, PinDirection direction, PinType type, bool force_name)
{
// some sanity checks
if (id == 0)
Expand All @@ -1599,9 +1606,20 @@ namespace hal
{
return ERR("could not create pin '" + name + "' for module '" + m_name + "' with ID " + std::to_string(m_id) + ": ID " + std::to_string(id) + " is already taken");
}
if (m_pin_names_map.find(name) != m_pin_names_map.end())
if (const auto pin_it = m_pin_names_map.find(name); pin_it != m_pin_names_map.end())
{
return ERR("could not create pin '" + name + "' for module '" + m_name + "' with ID " + std::to_string(m_id) + ": name '" + name + "' is already taken");
if (force_name)
{
u32 ctr = 2;
while (!this->set_pin_name(pin_it->second, name + "__" + std::to_string(ctr) + "__"))
{
ctr++;
}
}
else
{
return ERR("could not create pin '" + name + "' for module '" + m_name + "' with ID " + std::to_string(m_id) + ": name '" + name + "' is already taken");
}
}
if (net == nullptr)
{
Expand Down Expand Up @@ -1656,7 +1674,7 @@ namespace hal
return OK({});
}

Result<PinGroup<ModulePin>*> Module::create_pin_group_internal(const u32 id, const std::string& name, PinDirection direction, PinType type, bool ascending, u32 start_index)
Result<PinGroup<ModulePin>*> Module::create_pin_group_internal(const u32 id, const std::string& name, PinDirection direction, PinType type, bool ascending, u32 start_index, bool force_name)
{
// some sanity checks
if (id == 0)
Expand All @@ -1667,9 +1685,20 @@ namespace hal
{
return ERR("could not create pin group '" + name + "' for module '" + m_name + "' with ID " + std::to_string(m_id) + ": ID " + std::to_string(id) + " is already taken");
}
if (m_pin_group_names_map.find(name) != m_pin_group_names_map.end())
if (const auto pin_group_it = m_pin_group_names_map.find(name); pin_group_it != m_pin_group_names_map.end())
{
return ERR("could not create pin group '" + name + "' for module '" + m_name + "' with ID " + std::to_string(m_id) + ": name '" + name + "' is already taken");
if (force_name)
{
u32 ctr = 2;
while (!this->set_pin_group_name(pin_group_it->second, name + "__" + std::to_string(ctr) + "__"))
{
ctr++;
}
}
else
{
return ERR("could not create pin group '" + name + "' for module '" + m_name + "' with ID " + std::to_string(m_id) + ": name '" + name + "' is already taken");
}
}

// create pin group
Expand Down
Loading

0 comments on commit 4b35a0f

Please sign in to comment.