Skip to content

Commit

Permalink
reduced code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
SJulianS committed Mar 1, 2021
1 parent 85197ca commit ed5242d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 47 deletions.
1 change: 0 additions & 1 deletion include/hal_core/netlist/netlist_internal_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ namespace hal
Module* create_module(u32 id, Module* parent, const std::string& name);
bool delete_module(Module* module);
bool module_assign_gate(Module* m, Gate* g);
bool module_remove_gate(Module* m, Gate* g);

// grouping functions
Grouping* create_grouping(u32 id, const std::string name);
Expand Down
21 changes: 14 additions & 7 deletions src/netlist/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,25 @@ namespace hal

bool Module::assign_gate(Gate* gate)
{
m_input_nets_dirty = true;
m_output_nets_dirty = true;
m_internal_nets_dirty = true;
return m_internal_manager->module_assign_gate(this, gate);
}

bool Module::remove_gate(Gate* gate)
{
m_input_nets_dirty = true;
m_output_nets_dirty = true;
m_internal_nets_dirty = true;
return m_internal_manager->module_remove_gate(this, gate);
if (contains_gate(gate))
{
return m_internal_manager->module_assign_gate(m_internal_manager->m_netlist->get_top_module(), gate);
}

if (gate == nullptr)
{
log_error("module", "gate cannot be a nullptr.");
return false;
}

log_error(
"module", "gate '{}' with ID {} does not belong to module '{}' with ID {} in netlist with ID {}.", gate->get_name(), gate->get_id(), m_name, m_id, m_internal_manager->m_netlist->get_id());
return false;
}

bool Module::contains_gate(Gate* gate, bool recursive) const
Expand Down
77 changes: 38 additions & 39 deletions src/netlist/netlist_internal_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,63 +627,62 @@ namespace hal
{
if (g == nullptr)
{
log_error("module", "gate cannot be a nullptr.");
return false;
}
if (g->m_module == m)
{
return false;
}
auto prev_module = g->m_module;

prev_module->m_input_nets_dirty = true;
prev_module->m_output_nets_dirty = true;
prev_module->m_internal_nets_dirty = true;
prev_module->m_gates_map.erase(prev_module->m_gates_map.find(g->get_id()));
unordered_vector_erase(prev_module->m_gates, g);

m->m_gates_map[g->get_id()] = g;
m->m_gates.push_back(g);

g->m_module = m;

module_event_handler::notify(module_event_handler::event::gate_removed, prev_module, g->get_id());
module_event_handler::notify(module_event_handler::event::gate_assigned, m, g->get_id());
return true;
}

bool NetlistInternalManager::module_remove_gate(Module* m, Gate* g)
{
if (g == nullptr)
if (m == nullptr)
{
log_error("module", "module cannot be a nullptr.");
return false;
}

if (m == m_netlist->m_top_module)
{
log_error(
"module", "cannot remove gate '{}' with ID {} from top module '{}' with ID {} in netlist with ID {}.", g->get_name(), g->get_id(), m->get_name(), m->get_id(), m_netlist->m_netlist_id);
auto prev_module = g->m_module;
if (prev_module == m)
{
log_error("module",
"gate '{}' with ID {} is already contained in module '{}' with ID {} in netlist with ID {}.",
g->get_name(),
g->get_id(),
m->get_name(),
m->get_id(),
m_netlist->m_netlist_id);
return false;
}

auto it = m->m_gates_map.find(g->get_id());
// mark caches as dirty
m->m_input_nets_dirty = true;
m->m_output_nets_dirty = true;
m->m_internal_nets_dirty = true;
prev_module->m_input_nets_dirty = true;
prev_module->m_output_nets_dirty = true;
prev_module->m_internal_nets_dirty = true;

// remove gate from old module
auto it = prev_module->m_gates_map.find(g->get_id());
if (it == m->m_gates_map.end())
{
log_error(
"module", "gate '{}' with ID {} does not belong to module '{}' with ID {} in netlist with ID {}.", g->get_name(), g->get_id(), m->get_name(), m->get_id(), m_netlist->m_netlist_id);
log_error("module",
"gate '{}' with ID {} does not belong to module '{}' with ID {} in netlist with ID {}.",
g->get_name(),
g->get_id(),
prev_module->get_name(),
prev_module->get_id(),
m_netlist->m_netlist_id);
return false;
}

m->m_gates_map.erase(it);
unordered_vector_erase(m->m_gates, g);

m_netlist->m_top_module->m_gates_map[g->get_id()] = g;
m_netlist->m_top_module->m_gates.push_back(g);
g->m_module = m_netlist->m_top_module;
prev_module->m_gates_map.erase(it);
unordered_vector_erase(prev_module->m_gates, g);

module_event_handler::notify(module_event_handler::event::gate_removed, m, g->get_id());
module_event_handler::notify(module_event_handler::event::gate_assigned, m_netlist->m_top_module, g->get_id());
// move gate to new module
m->m_gates_map[g->get_id()] = g;
m->m_gates.push_back(g);
g->m_module = m;

// notify event handlers
module_event_handler::notify(module_event_handler::event::gate_removed, prev_module, g->get_id());
module_event_handler::notify(module_event_handler::event::gate_assigned, m, g->get_id());
return true;
}

Expand Down

0 comments on commit ed5242d

Please sign in to comment.