HAL  v4.5.0-299-g9ee015483
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
configuration.cpp
Go to the documentation of this file.
2 
9 
10 namespace hal
11 {
12  namespace dataflow
13  {
15  {
16  }
17 
19  {
20  this->min_group_size = size;
21  return *this;
22  }
23 
24  Configuration& Configuration::with_expected_sizes(const std::vector<u32>& sizes)
25  {
26  this->expected_sizes = sizes;
27  return *this;
28  }
29 
30  Configuration& Configuration::with_known_structures(const std::vector<Module*>& structures, bool overwrite)
31  {
32  if (overwrite)
33  {
34  this->known_net_groups.clear();
35  }
36 
37  for (const auto* mod : structures)
38  {
39  for (const auto* pin_group : mod->get_pin_groups())
40  {
41  std::vector<Net*> nets;
42  for (const auto* pin : pin_group->get_pins())
43  {
44  nets.push_back(pin->get_net());
45  }
46  this->known_net_groups.push_back(nets);
47  }
48  }
49 
50  return *this;
51  }
52 
53  Configuration& Configuration::with_known_structures(const std::vector<std::pair<Module*, std::vector<PinGroup<ModulePin>*>>>& structures, bool overwrite)
54  {
55  if (overwrite)
56  {
57  this->known_net_groups.clear();
58  }
59 
60  for (const auto& [_, pin_groups] : structures)
61  {
62  for (const auto* pin_group : pin_groups)
63  {
64  std::vector<Net*> nets;
65  for (const auto* pin : pin_group->get_pins())
66  {
67  nets.push_back(pin->get_net());
68  }
69  this->known_net_groups.push_back(nets);
70  }
71  }
72 
73  return *this;
74  }
75 
76  Configuration& Configuration::with_known_structures(const std::vector<Gate*>& structures, bool overwrite)
77  {
78  if (overwrite)
79  {
80  this->known_net_groups.clear();
81  }
82 
83  for (const auto* gate : structures)
84  {
85  for (const auto* pin_group : gate->get_type()->get_pin_groups())
86  {
87  std::vector<Net*> nets;
88  for (const auto* pin : pin_group->get_pins())
89  {
90  if (pin->get_direction() == PinDirection::input)
91  {
92  nets.push_back(gate->get_fan_in_net(pin));
93  }
94  else if (pin->get_direction() == PinDirection::output)
95  {
96  nets.push_back(gate->get_fan_out_net(pin));
97  }
98  }
99  this->known_net_groups.push_back(nets);
100  }
101  }
102 
103  return *this;
104  }
105 
106  Configuration& Configuration::with_known_structures(const std::vector<std::pair<Gate*, std::vector<PinGroup<GatePin>*>>>& structures, bool overwrite)
107  {
108  if (overwrite)
109  {
110  this->known_net_groups.clear();
111  }
112 
113  for (const auto& [gate, pin_groups] : structures)
114  {
115  for (const auto* pin_group : pin_groups)
116  {
117  std::vector<Net*> nets;
118  for (const auto* pin : pin_group->get_pins())
119  {
120  if (pin->get_direction() == PinDirection::input)
121  {
122  nets.push_back(gate->get_fan_in_net(pin));
123  }
124  else if (pin->get_direction() == PinDirection::output)
125  {
126  nets.push_back(gate->get_fan_out_net(pin));
127  }
128  }
129  this->known_net_groups.push_back(nets);
130  }
131  }
132 
133  return *this;
134  }
135 
136  Configuration& Configuration::with_known_structures(const std::unordered_set<const GateType*>& structures, bool overwrite)
137  {
138  if (this->netlist == nullptr)
139  {
140  log_error("dataflow", "cannot add known structures by gate type, netlist is a nullptr");
141  return *this;
142  }
143 
144  if (overwrite)
145  {
146  this->known_net_groups.clear();
147  }
148 
149  for (const auto* gate : this->netlist->get_gates([structures](const Gate* g) { return structures.find(g->get_type()) != structures.end(); }))
150  {
151  for (const auto* pin_group : gate->get_type()->get_pin_groups())
152  {
153  std::vector<Net*> nets;
154  for (const auto* pin : pin_group->get_pins())
155  {
156  if (pin->get_direction() == PinDirection::input)
157  {
158  nets.push_back(gate->get_fan_in_net(pin));
159  }
160  else if (pin->get_direction() == PinDirection::output)
161  {
162  nets.push_back(gate->get_fan_out_net(pin));
163  }
164  }
165  this->known_net_groups.push_back(nets);
166  }
167  }
168 
169  return *this;
170  }
171 
172  Configuration& Configuration::with_known_structures(const std::unordered_map<const GateType*, std::vector<PinGroup<GatePin>*>>& structures, bool overwrite)
173  {
174  if (this->netlist == nullptr)
175  {
176  log_error("dataflow", "cannot add known structures by gate type, netlist is a nullptr");
177  return *this;
178  }
179 
180  if (overwrite)
181  {
182  this->known_net_groups.clear();
183  }
184 
185  for (const auto* gate : this->netlist->get_gates([structures](const Gate* g) { return structures.find(g->get_type()) != structures.end(); }))
186  {
187  for (const auto* pin_group : structures.at(gate->get_type()))
188  {
189  std::vector<Net*> nets;
190  for (const auto* pin : pin_group->get_pins())
191  {
192  if (pin->get_direction() == PinDirection::input)
193  {
194  nets.push_back(gate->get_fan_in_net(pin));
195  }
196  else if (pin->get_direction() == PinDirection::output)
197  {
198  nets.push_back(gate->get_fan_out_net(pin));
199  }
200  }
201  this->known_net_groups.push_back(nets);
202  }
203  }
204 
205  return *this;
206  }
207 
208  Configuration& Configuration::with_known_groups(const std::vector<Module*>& groups, bool overwrite)
209  {
210  if (overwrite)
211  {
212  this->known_gate_groups.clear();
213  }
214 
215  for (auto* mod : groups)
216  {
217  this->known_gate_groups.push_back(mod->get_gates());
218  }
219 
220  return *this;
221  }
222 
223  Configuration& Configuration::with_known_groups(const std::vector<std::vector<Gate*>>& groups, bool overwrite)
224  {
225  if (overwrite)
226  {
227  this->known_gate_groups = groups;
228  }
229  else
230  {
231  this->known_gate_groups.insert(this->known_gate_groups.end(), groups.begin(), groups.end());
232  }
233 
234  return *this;
235  }
236 
237  Configuration& Configuration::with_known_groups(const std::vector<std::vector<u32>>& groups, bool overwrite)
238  {
239  if (this->netlist == nullptr)
240  {
241  log_error("dataflow", "cannot add known groups by gate ID, netlist is a nullptr");
242  return *this;
243  }
244 
245  if (overwrite)
246  {
247  this->known_gate_groups.clear();
248  }
249 
250  for (const auto& gate_ids : groups)
251  {
252  std::vector<Gate*> gates;
253  std::transform(gate_ids.cbegin(), gate_ids.cend(), std::back_inserter(gates), [this](u32 gid) { return this->netlist->get_gate_by_id(gid); });
254  this->known_gate_groups.push_back(gates);
255  }
256 
257  return *this;
258  }
259 
260  Configuration& Configuration::with_known_groups(const std::unordered_map<u32, std::unordered_set<Gate*>>& groups, bool overwrite)
261  {
262  if (overwrite)
263  {
264  this->known_gate_groups.clear();
265  }
266 
267  for (const auto& [_, gates] : groups)
268  {
269  std::vector<Gate*> group(gates.cbegin(), gates.cend());
270  this->known_gate_groups.push_back(group);
271  }
272 
273  return *this;
274  }
275 
276  Configuration& Configuration::with_gate_types(const std::set<const GateType*>& types, bool overwrite)
277  {
278  if (overwrite)
279  {
280  this->gate_types = types;
281  }
282  else
283  {
284  this->gate_types.insert(types.begin(), types.end());
285  }
286 
287  return *this;
288  }
289 
290  Configuration& Configuration::with_gate_types(const std::set<GateTypeProperty>& gate_type_properties, bool overwrite)
291  {
292  if (this->netlist == nullptr)
293  {
294  log_error("dataflow", "cannot add gate types by property, netlist is a nullptr");
295  return *this;
296  }
297 
298  auto gate_types_map = this->netlist->get_gate_library()->get_gate_types([gate_type_properties](const GateType* gt) {
299  for (GateTypeProperty p : gate_type_properties)
300  {
301  if (gt->has_property(p))
302  {
303  return true;
304  }
305  }
306  return false;
307  });
308  std::set<const GateType*> types;
309  std::transform(gate_types_map.begin(), gate_types_map.end(), std::inserter(types, types.begin()), [](const auto& gt) { return gt.second; });
310  this->with_gate_types(types, overwrite);
311 
312  return *this;
313  }
314 
315  Configuration& Configuration::with_control_pin_types(const std::set<PinType>& types, bool overwrite)
316  {
317  if (overwrite)
318  {
319  this->control_pin_types = types;
320  }
321  else
322  {
323  this->control_pin_types.insert(types.begin(), types.end());
324  }
325 
326  return *this;
327  }
328 
330  {
331  this->with_gate_types({GateTypeProperty::ff}, true);
333 
334  return *this;
335  }
336 
338  {
339  this->enable_stages = enable;
340  return *this;
341  }
342 
344  {
345  this->enforce_type_consistency = enable;
346  return *this;
347  }
348  } // namespace dataflow
349 } // namespace hal
u32 size
Definition: gate.h:59
std::unordered_map< std::string, GateType * > get_gate_types(const std::function< bool(const GateType *)> &filter=nullptr) const
bool has_property(GateTypeProperty property) const
Definition: gate_type.cpp:101
const std::vector< Gate * > & get_gates() const
Definition: netlist.cpp:206
const GateLibrary * get_gate_library() const
Definition: netlist.cpp:134
This file contains the struct that holds all information on a dataflow analysis configuration.
uint32_t u32
Definition: defines.h:41
#define log_error(channel,...)
Definition: log.h:78
Definition: defines.h:45
Configuration of a dataflow analysis run.
Definition: configuration.h:61
Configuration & with_flip_flops()
Use the default detection configuration for flip-flops.
std::vector< std::vector< Net * > > known_net_groups
Groups of nets that have been identified as word-level datapathes beforehand. Defaults to an empty ve...
Definition: configuration.h:92
std::vector< u32 > expected_sizes
Expected group sizes. Groups of these sizes will be prioritized. Defaults to an empty vector.
Definition: configuration.h:82
Configuration & with_type_consistency(bool enable=true)
Enable type consistency as part of dataflow analysis when deciding whether two gates are allowed to m...
std::set< const GateType * > gate_types
The gate types to be grouped by dataflow analysis. Defaults to an empty set.
Definition: configuration.h:97
std::set< PinType > control_pin_types
The pin types of the pins to be considered control pins. Defaults to an empty set.
Configuration(Netlist *nl)
Construct a new dataflow analysis configuration for the given netlist.
Configuration & with_gate_types(const std::set< const GateType * > &types, bool overwrite=false)
Add the gate types to the set of gate types to be grouped by dataflow analysis.
Configuration & with_control_pin_types(const std::set< PinType > &types, bool overwrite=false)
Set the pin types of the pins to be considered control pins by dataflow analysis.
Configuration & with_stage_identification(bool enable=true)
Enable stage identification as part of dataflow analysis.
Configuration & with_known_groups(const std::vector< Module * > &groups, bool overwrite=false)
Add modules to the set of previously identified word-level groups.
bool enforce_type_consistency
Enforce gate type consistency inside of a group. Defaults to false.
u32 min_group_size
Minimum size of a group. Smaller groups will be penalized during analysis. Defaults to 8.
Definition: configuration.h:77
Configuration & with_expected_sizes(const std::vector< u32 > &sizes)
Set the expected group sizes. Groups of these sizes will be prioritized.
std::vector< std::vector< Gate * > > known_gate_groups
Groups of gates that have already been identified as word-level groups beforehand....
Definition: configuration.h:87
Configuration & with_min_group_size(u32 size)
Set the minimum size of a group. Smaller groups will be penalized during analysis.
bool enable_stages
Enable stage identification as part of dataflow analysis. Defaults to false.
Netlist * netlist
The netlist to be analyzed.
Definition: configuration.h:72
Configuration & with_known_structures(const std::vector< Module * > &structures, bool overwrite=false)
Add modules to the set of previously identified word-level structures.