Skip to content

Commit be060ad

Browse files
committed
module::delete_net() implemented
1 parent 54faa98 commit be060ad

File tree

5 files changed

+88
-57
lines changed

5 files changed

+88
-57
lines changed

include/hal_core/netlist/module.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,8 @@ namespace hal
747747
EventHandler* m_event_handler;
748748

749749
NetConnectivity check_net_endpoints(const Net* net) const;
750-
Result<std::monostate> check_net(Net* net, bool recursive = false);
750+
bool check_net(Net* net, bool recursive = false);
751+
bool delete_net(Net* net, bool recursive = false);
751752
bool assign_pin_net(const u32 pin_id, Net* net, PinDirection direction);
752753
bool remove_pin_net(Net* net);
753754
Result<ModulePin*> create_pin_internal(const u32 id, const std::string& name, Net* net, PinDirection direction, PinType type, bool force_name);

include/hal_core/netlist/netlist_internal_manager.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@ namespace hal
7474
Net* create_net(u32 id, const std::string& name);
7575
bool delete_net(Net* net);
7676
Endpoint* net_add_source(Net* net, Gate* gate, GatePin* pin);
77-
bool net_remove_source(Net* net, Endpoint* ep);
7877
Endpoint* net_add_destination(Net* net, Gate* gate, GatePin* pin);
78+
bool net_remove_source(Net* net, Endpoint* ep);
79+
bool net_remove_source_internal(Net* net, Endpoint* ep);
7980
bool net_remove_destination(Net* net, Endpoint* ep);
81+
bool net_remove_destination_internal(Net* net, Endpoint* ep);
8082

8183
// module functions
8284
Module* create_module(u32 id, Module* parent, const std::string& name);

src/netlist/module.cpp

+45-20
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,7 @@ namespace hal
204204
{
205205
for (Net* net : get_nets(nullptr, true))
206206
{
207-
if (auto res = m_parent->check_net(net, true); res.is_error())
208-
{
209-
log_error("module", "{}", res.get_error().get());
210-
}
207+
m_parent->check_net(net, true);
211208
}
212209
}
213210

@@ -222,10 +219,7 @@ namespace hal
222219
{
223220
for (Net* net : get_nets(nullptr, true))
224221
{
225-
if (auto res = m_parent->check_net(net, true); res.is_error())
226-
{
227-
log_error("module", "{}", res.get_error().get());
228-
}
222+
m_parent->check_net(net, true);
229223
}
230224
}
231225

@@ -241,16 +235,19 @@ namespace hal
241235
{
242236
return false;
243237
}
244-
for (auto sm : m_submodules)
238+
239+
Module* parent_module = module->m_parent;
240+
while (parent_module)
245241
{
246-
if (sm == module)
242+
if (parent_module == this)
247243
{
248244
return true;
249245
}
250-
else if (recursive && sm->is_parent_module_of(module, true))
246+
if (!recursive)
251247
{
252-
return true;
248+
break;
253249
}
250+
parent_module = parent_module->m_parent;
254251
}
255252

256253
return false;
@@ -630,7 +627,31 @@ namespace hal
630627
return res;
631628
}
632629

633-
Result<std::monostate> Module::check_net(Net* net, bool recursive)
630+
bool Module::delete_net(Net* net, bool recursive)
631+
{
632+
if (!m_internal_manager->m_net_checks_enabled)
633+
{
634+
return true; // do nothing, no error either to reproduce check_net() behavior
635+
}
636+
637+
m_internal_nets.erase(net);
638+
m_nets.erase(net);
639+
m_input_nets.erase(net);
640+
m_output_nets.erase(net);
641+
642+
if (m_internal_manager->m_net_checks_enabled && recursive && m_parent != nullptr)
643+
{
644+
if (!m_parent->delete_net(net, recursive))
645+
{
646+
return false;
647+
}
648+
}
649+
650+
return true;
651+
652+
}
653+
654+
bool Module::check_net(Net* net, bool recursive)
634655
{
635656
NetConnectivity con = check_net_endpoints(net);
636657
if (con.has_internal_source && con.has_internal_destination)
@@ -673,7 +694,8 @@ namespace hal
673694
{
674695
if (!assign_pin_net(get_unique_pin_id(), net, PinDirection::inout))
675696
{
676-
return ERR("could not assign inout pin to net ID " + std::to_string(net->get_id()) + ": failed to create pin");
697+
log_warning("module", "could not assign inout pin to net ID {}: failed to create pin", net->get_id() );
698+
return false;
677699
}
678700
}
679701
}
@@ -695,7 +717,8 @@ namespace hal
695717
m_input_nets.insert(net);
696718
if (!assign_pin_net(get_unique_pin_id(), net, PinDirection::input))
697719
{
698-
return ERR("could not assign input pin to net ID " + std::to_string(net->get_id()) + ": failed to create pin");
720+
log_warning("module", "could not assign input pin to net ID {}: failed to create pin", net->get_id() );
721+
return false;
699722
}
700723
}
701724
}
@@ -717,7 +740,8 @@ namespace hal
717740
m_output_nets.insert(net);
718741
if (!assign_pin_net(get_unique_pin_id(), net, PinDirection::output))
719742
{
720-
return ERR("could not assign output pin to net ID " + std::to_string(net->get_id()) + ": failed to create pin");
743+
log_warning("module", "could not assign output pin to net ID {}: failed to create pin", net->get_id() );
744+
return false;
721745
}
722746
}
723747
}
@@ -736,20 +760,21 @@ namespace hal
736760
}
737761
if (!remove_pin_net(net))
738762
{
739-
return ERR("Remove pin net failed");
763+
log_warning("module", "Remove pin net failed");
764+
return false;
740765
}
741766
}
742767
}
743768

744769
if (m_internal_manager->m_net_checks_enabled && recursive && m_parent != nullptr)
745770
{
746-
if (auto res = m_parent->check_net(net, true); res.is_error())
771+
if (!m_parent->check_net(net, true))
747772
{
748-
return res;
773+
return false;
749774
}
750775
}
751776

752-
return OK({});
777+
return true;
753778
}
754779

755780
/*

src/netlist/netlist.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "hal_core/netlist/net.h"
88
#include "hal_core/netlist/netlist_internal_manager.h"
99
#include "hal_core/utilities/log.h"
10+
#include <valgrind/callgrind.h>
1011

1112
namespace hal
1213
{
@@ -342,7 +343,11 @@ namespace hal
342343

343344
bool Netlist::delete_net(Net* n)
344345
{
346+
CALLGRIND_START_INSTRUMENTATION;
347+
CALLGRIND_TOGGLE_COLLECT;
345348
return m_manager->delete_net(n);
349+
CALLGRIND_TOGGLE_COLLECT;
350+
CALLGRIND_STOP_INSTRUMENTATION;
346351
}
347352

348353
bool Netlist::is_net_in_netlist(const Net* n) const

src/netlist/netlist_internal_manager.cpp

+33-35
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ namespace hal
445445
auto dsts = net->m_destinations_raw;
446446
for (auto dst : dsts)
447447
{
448-
if (!this->net_remove_destination(net, dst))
448+
if (!this->net_remove_destination_internal(net, dst))
449449
{
450450
return false;
451451
}
@@ -454,7 +454,7 @@ namespace hal
454454
auto srcs = net->m_sources_raw;
455455
for (auto src : srcs)
456456
{
457-
if (!this->net_remove_source(net, src))
457+
if (!this->net_remove_source_internal(net, src))
458458
{
459459
return false;
460460
}
@@ -540,17 +540,15 @@ namespace hal
540540
// update internal nets and port nets
541541
if (m_net_checks_enabled)
542542
{
543-
if (const auto res = gate->get_module()->check_net(net, true); res.is_error())
543+
if (!gate->get_module()->check_net(net, true))
544544
{
545-
log_error("net", "{}", res.get_error().get());
546545
return nullptr;
547546
}
548547

549548
for (Endpoint* ep : net->get_destinations())
550549
{
551-
if (const auto res = ep->get_gate()->get_module()->check_net(net, true); res.is_error())
550+
if (!ep->get_gate()->get_module()->check_net(net, true))
552551
{
553-
log_error("net", "{}", res.get_error().get());
554552
return nullptr;
555553
}
556554
}
@@ -561,7 +559,7 @@ namespace hal
561559
return new_endpoint_raw;
562560
}
563561

564-
bool NetlistInternalManager::net_remove_source(Net* net, Endpoint* ep)
562+
bool NetlistInternalManager::net_remove_source_internal(Net* net, Endpoint* ep)
565563
{
566564
auto gate = ep->get_gate();
567565

@@ -570,7 +568,6 @@ namespace hal
570568
return false;
571569
}
572570

573-
bool removed = false;
574571
for (u32 i = 0; i < net->m_sources.size(); ++i)
575572
{
576573
if (net->m_sources_raw[i] == ep)
@@ -582,12 +579,18 @@ namespace hal
582579
net->m_sources_raw[i] = net->m_sources_raw.back();
583580
net->m_sources_raw.pop_back();
584581
m_event_handler->notify(NetEvent::event::src_removed, net, gate->get_id());
585-
removed = true;
586-
break;
582+
return true;
587583
}
588584
}
589585

590-
if (!removed)
586+
return false;
587+
}
588+
589+
bool NetlistInternalManager::net_remove_source(Net* net, Endpoint* ep)
590+
{
591+
auto gate = ep->get_gate();
592+
593+
if (!net_remove_source_internal(net, ep))
591594
{
592595
log_warning("net",
593596
"output pin '{}' of gate '{}' with ID {} is not a source of net '{}' with ID {} in netlist with ID {}",
@@ -603,17 +606,15 @@ namespace hal
603606
// update internal nets and port nets
604607
if (m_net_checks_enabled)
605608
{
606-
if (const auto res = gate->get_module()->check_net(net, true); res.is_error())
609+
if (!gate->get_module()->check_net(net, true))
607610
{
608-
log_error("net", "{}", res.get_error().get());
609611
return false;
610612
}
611613

612614
for (Endpoint* dst : net->get_destinations())
613615
{
614-
if (const auto res = dst->get_gate()->get_module()->check_net(net, true); res.is_error())
616+
if (!dst->get_gate()->get_module()->check_net(net, true))
615617
{
616-
log_error("net", "{}", res.get_error().get());
617618
return false;
618619
}
619620
}
@@ -678,17 +679,15 @@ namespace hal
678679
// update internal nets and port nets
679680
if (m_net_checks_enabled)
680681
{
681-
if (const auto res = gate->get_module()->check_net(net, true); res.is_error())
682+
if (!gate->get_module()->check_net(net, true))
682683
{
683-
log_error("net", "{}", res.get_error().get());
684684
return nullptr;
685685
}
686686

687687
for (Endpoint* ep : net->get_sources())
688688
{
689-
if (const auto res = ep->get_gate()->get_module()->check_net(net, true); res.is_error())
689+
if (!ep->get_gate()->get_module()->check_net(net, true))
690690
{
691-
log_error("net", "{}", res.get_error().get());
692691
return nullptr;
693692
}
694693
}
@@ -699,15 +698,15 @@ namespace hal
699698
return new_endpoint_raw;
700699
}
701700

702-
bool NetlistInternalManager::net_remove_destination(Net* net, Endpoint* ep)
701+
bool NetlistInternalManager::net_remove_destination_internal(Net* net, Endpoint* ep)
703702
{
704703
auto gate = ep->get_gate();
704+
705705
if (!m_netlist->is_net_in_netlist(net) || !m_netlist->is_gate_in_netlist(gate) || !net->is_a_destination(ep))
706706
{
707707
return false;
708708
}
709709

710-
bool removed = false;
711710
for (u32 i = 0; i < net->m_destinations.size(); ++i)
712711
{
713712
if (net->m_destinations_raw[i] == ep)
@@ -719,12 +718,18 @@ namespace hal
719718
net->m_destinations_raw[i] = net->m_destinations_raw.back();
720719
net->m_destinations_raw.pop_back();
721720
m_event_handler->notify(NetEvent::event::dst_removed, net, gate->get_id());
722-
removed = true;
723-
break;
721+
return true;
724722
}
725723
}
726724

727-
if (!removed)
725+
return false;
726+
}
727+
728+
bool NetlistInternalManager::net_remove_destination(Net* net, Endpoint* ep)
729+
{
730+
auto gate = ep->get_gate();
731+
732+
if (!net_remove_destination_internal(net,ep))
728733
{
729734
log_warning("net",
730735
"input pin '{}' of gate '{}' with ID {} is not a destination of net '{}' with ID {} in netlist with ID {}",
@@ -739,17 +744,15 @@ namespace hal
739744
{ // update internal nets and port nets
740745
if (m_net_checks_enabled)
741746
{
742-
if (const auto res = gate->get_module()->check_net(net, true); res.is_error())
747+
if (!gate->get_module()->check_net(net, true))
743748
{
744-
log_error("net", "{}", res.get_error().get());
745749
return false;
746750
}
747751

748752
for (Endpoint* src : net->get_sources())
749753
{
750-
if (const auto res = src->get_gate()->get_module()->check_net(net, true); res.is_error())
754+
if (!src->get_gate()->get_module()->check_net(net, true))
751755
{
752-
log_error("net", "{}", res.get_error().get());
753756
return false;
754757
}
755758
}
@@ -977,9 +980,8 @@ namespace hal
977980
{
978981
for (Net* net : nets)
979982
{
980-
if (const auto res = affected_module->check_net(net, true); res.is_error())
983+
if (!affected_module->check_net(net, true))
981984
{
982-
log_error("module", "{}", res.get_error().get());
983985
return false;
984986
}
985987
}
@@ -997,11 +999,7 @@ namespace hal
997999

9981000
bool NetlistInternalManager::module_check_net(Module* module, Net* net, bool recursive)
9991001
{
1000-
if (const auto res = module->check_net(net, recursive); res.is_error())
1001-
{
1002-
return false;
1003-
}
1004-
return true;
1002+
return module->check_net(net, recursive);
10051003
}
10061004

10071005
//######################################################################

0 commit comments

Comments
 (0)