HAL  v4.5.0-124-g47ab54673
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
bit_order.cpp
Go to the documentation of this file.
2 
4 #include "hal_core/netlist/net.h"
5 
6 #include <algorithm>
7 
8 namespace hal
9 {
10  namespace bitorder_propagation
11  {
12  BitOrder::BitOrder(Module* module, PinGroup<ModulePin>* pin_group, std::vector<std::pair<Net*, u32>> order)
13  : m_module(module), m_pin_group(pin_group), m_order(std::move(order))
14  {
15  std::sort(m_order.begin(), m_order.end(), [](const auto& lhs, const auto& rhs) { return lhs.second < rhs.second; });
16  }
17 
19  {
20  return m_module;
21  }
22 
24  {
25  return m_pin_group;
26  }
27 
28  const std::vector<std::pair<Net*, u32>>& BitOrder::get_order() const
29  {
30  return m_order;
31  }
32 
33  std::optional<u32> BitOrder::get_index(const Net* net) const
34  {
35  const auto it = std::find_if(m_order.begin(), m_order.end(), [net](const auto& entry) { return entry.first == net; });
36  return (it == m_order.end()) ? std::nullopt : std::optional<u32>(it->second);
37  }
38 
40  {
41  const auto it = std::find_if(m_order.begin(), m_order.end(), [index](const auto& entry) { return entry.second == index; });
42  return (it == m_order.end()) ? nullptr : it->first;
43  }
44 
46  {
47  return m_order.size();
48  }
49 
51  {
52  // m_order is sorted by index, so it suffices that the indices are 0, 1, ... without repetition.
53  for (u32 i = 0; i < m_order.size(); i++)
54  {
55  if (m_order.at(i).second != i)
56  {
57  return false;
58  }
59  }
60  return true;
61  }
62 
63  bool BitOrder::operator==(const BitOrder& other) const
64  {
65  return (m_module == other.m_module) && (m_pin_group == other.m_pin_group) && (m_order == other.m_order);
66  }
67 
68  bool BitOrder::operator!=(const BitOrder& other) const
69  {
70  return !(*this == other);
71  }
72 
73  namespace
74  {
76  bool precedes(const BitOrder& lhs, const Module* module, const PinGroup<ModulePin>* pin_group)
77  {
78  const u32 lhs_module = lhs.get_module()->get_id();
79  const u32 rhs_module = module->get_id();
80  if (lhs_module != rhs_module)
81  {
82  return lhs_module < rhs_module;
83  }
84  return lhs.get_pin_group()->get_id() < pin_group->get_id();
85  }
86  } // namespace
87 
88  BitOrderResult::BitOrderResult(std::vector<BitOrder> bit_orders)
89  {
90  for (auto& bit_order : bit_orders)
91  {
92  add(std::move(bit_order));
93  }
94  }
95 
96  void BitOrderResult::add(BitOrder bit_order)
97  {
98  const auto it = std::lower_bound(m_bit_orders.begin(), m_bit_orders.end(), bit_order, [](const BitOrder& lhs, const BitOrder& rhs) {
99  return precedes(lhs, rhs.get_module(), rhs.get_pin_group());
100  });
101 
102  if ((it != m_bit_orders.end()) && (it->get_module() == bit_order.get_module()) && (it->get_pin_group() == bit_order.get_pin_group()))
103  {
104  *it = std::move(bit_order);
105  return;
106  }
107 
108  m_bit_orders.insert(it, std::move(bit_order));
109  }
110 
111  const std::vector<BitOrder>& BitOrderResult::get_bit_orders() const
112  {
113  return m_bit_orders;
114  }
115 
116  const BitOrder* BitOrderResult::get(const Module* module, const PinGroup<ModulePin>* pin_group) const
117  {
118  const auto it = std::lower_bound(m_bit_orders.begin(), m_bit_orders.end(), 0, [module, pin_group](const BitOrder& lhs, int) {
119  return precedes(lhs, module, pin_group);
120  });
121 
122  if ((it != m_bit_orders.end()) && (it->get_module() == module) && (it->get_pin_group() == pin_group))
123  {
124  return &(*it);
125  }
126  return nullptr;
127  }
128 
129  bool BitOrderResult::contains(const Module* module, const PinGroup<ModulePin>* pin_group) const
130  {
131  return get(module, pin_group) != nullptr;
132  }
133 
135  {
136  return m_bit_orders.size();
137  }
138 
140  {
141  return m_bit_orders.empty();
142  }
143 
144  std::vector<BitOrder>::const_iterator BitOrderResult::begin() const
145  {
146  return m_bit_orders.begin();
147  }
148 
149  std::vector<BitOrder>::const_iterator BitOrderResult::end() const
150  {
151  return m_bit_orders.end();
152  }
153  } // namespace bitorder_propagation
154 } // namespace hal
This file contains the bit order of a module pin group and the collection of bit orders that a propag...
u32 get_id() const
Definition: module.cpp:82
Definition: net.h:58
u32 get_id() const
Definition: pin_group.h:133
bool operator==(const BitOrder &other) const
Definition: bit_order.cpp:63
bool operator!=(const BitOrder &other) const
Definition: bit_order.cpp:68
Net * get_net_at(u32 index) const
Definition: bit_order.cpp:39
std::optional< u32 > get_index(const Net *net) const
Definition: bit_order.cpp:33
const std::vector< std::pair< Net *, u32 > > & get_order() const
Definition: bit_order.cpp:28
PinGroup< ModulePin > * get_pin_group() const
Definition: bit_order.cpp:23
BitOrder(Module *module, PinGroup< ModulePin > *pin_group, std::vector< std::pair< Net *, u32 >> order)
Definition: bit_order.cpp:12
bool contains(const Module *module, const PinGroup< ModulePin > *pin_group) const
Definition: bit_order.cpp:129
const BitOrder * get(const Module *module, const PinGroup< ModulePin > *pin_group) const
Definition: bit_order.cpp:116
std::vector< BitOrder >::const_iterator begin() const
Definition: bit_order.cpp:144
std::vector< BitOrder >::const_iterator end() const
Definition: bit_order.cpp:149
const std::vector< BitOrder > & get_bit_orders() const
Definition: bit_order.cpp:111
uint32_t u32
Definition: defines.h:41
const Module * module(const Gate *g, const NodeBoxes &boxes)
Definition: defines.h:45
Net * net