HAL  v4.5.0-299-g9ee015483
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
clock_tree.cpp
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4 // Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5 // Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6 // Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7 // Copyright (c) 2025-2026 Sascha Tommasone. All rights reserved.
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in all
17 // copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 // SOFTWARE.
26 
28 
31 #include "hal_core/netlist/gate.h"
35 #include "hal_core/netlist/net.h"
37 #include "hal_core/utilities/log.h"
39 
40 #include <algorithm>
41 #include <cstddef>
42 #include <igraph/igraph.h>
43 #include <queue>
44 #include <string>
45 #include <tuple>
46 #include <unordered_map>
47 #include <unordered_set>
48 #include <utility>
49 #include <vector>
50 
51 namespace hal::cte
52 {
53  namespace
54  {
55  std::unordered_set<const Gate *> get_toggle_ffs( const Netlist *netlist )
56  {
57  const std::vector<Gate *> ffs = netlist->get_gates( is_ff );
58 
59  std::unordered_set<const Gate *> result;
60  for( const Gate *ff : ffs )
61  {
62  const std::vector<Endpoint *> successor_endpoints = ff->get_successors();
63  const std::size_t successor_endpoints_size = successor_endpoints.size();
64 
65  if( successor_endpoints_size == 0 )
66  {
67  continue;
68  }
69 
70  std::vector<const Gate *> successors;
71  successors.reserve( successor_endpoints_size );
72 
73  std::transform( successor_endpoints.begin(),
74  successor_endpoints.end(),
75  std::back_inserter( successors ),
76  []( const Endpoint *ep ) { return ep->get_gate(); } );
77 
78  if( std::find( successors.begin(), successors.end(), ff ) != successors.end() )
79  {
80  result.insert( ff );
81  }
82  }
83 
84  return result;
85  }
86 
87  igraph_error_t in_callback( const igraph_t *graph, igraph_int_t vid, igraph_int_t dist, void *extra )
88  {
89  return igraph_vector_int_push_back( static_cast<igraph_vector_int_t *>( extra ), vid );
90  }
91 
100  class ScopedIgraphErrorHandler
101  {
102  public:
103  ScopedIgraphErrorHandler()
104  : m_previous( igraph_set_error_handler( &handle_error ) )
105  {
106  }
107 
108  ~ScopedIgraphErrorHandler()
109  {
110  igraph_set_error_handler( m_previous );
111  }
112 
113  ScopedIgraphErrorHandler( const ScopedIgraphErrorHandler & ) = delete;
114  ScopedIgraphErrorHandler &operator=( const ScopedIgraphErrorHandler & ) = delete;
115 
116  private:
117  static void handle_error( const char *reason, const char *file, int line, igraph_error_t igraph_errno )
118  {
119  // igraph calls the handler once per level an error passes through, so this may log several times
120  // per error. log first: IGRAPH_FINALLY_FREE may invalidate reason
121  log_debug( "clock_tree_extractor",
122  "igraph error at {}:{}: {} ({})",
123  file,
124  line,
125  reason,
126  igraph_strerror( igraph_errno ) );
127 
128  // required of every handler that does not abort: frees igraph's temporary allocations
129  IGRAPH_FINALLY_FREE();
130  }
131 
132  igraph_error_handler_t *m_previous;
133  };
134  } // namespace
135 
136  ClockTree::ClockTree( const Netlist *netlist,
137  igraph_t &&igraph,
138  std::unordered_set<igraph_int_t> &&roots,
139  std::unordered_map<igraph_int_t, const void *> &&vertices_to_ptrs,
140  std::unordered_map<const void *, PtrType> &&ptrs_to_types )
141  : m_netlist( netlist )
142  , m_igraph( igraph )
143  , m_roots( std::move( roots ) )
144  , m_vertices_to_ptrs( std::move( vertices_to_ptrs ) )
145  , m_ptrs_to_types( std::move( ptrs_to_types ) )
146  {
147  for( const auto &[vertex, ptr] : m_vertices_to_ptrs )
148  {
149  m_ptrs_to_vertices[ptr] = vertex;
150  }
151  }
152 
153  ClockTree::~ClockTree()
154  {
155  igraph_destroy( &m_igraph );
156  }
157 
158  Result<std::unique_ptr<ClockTree>> ClockTree::from_netlist( const Netlist *netlist )
159  {
160  if( netlist == nullptr )
161  {
162  return ERR( "no netlist provided" );
163  }
164 
165  std::vector<Diagnostic> diagnostics;
166  const auto report = [&diagnostics]( Severity severity,
167  DiagnosticCode code,
168  PtrType type,
169  const void *ptr,
170  u32 id,
171  std::string message ) {
172  if( severity == Severity::error )
173  {
174  log_error( "clock_tree_extractor", "{}", message );
175  }
176  else
177  {
178  log_warning( "clock_tree_extractor", "{}", message );
179  }
180  diagnostics.push_back( { severity, code, type, ptr, id, std::move( message ) } );
181  };
182 
183  std::unordered_set<const void *> vertices;
184  std::unordered_set<std::pair<const void *, const void *>, VoidPtrHash> edges;
185  std::unordered_map<const void *, PtrType> ptrs_to_type;
186 
187  std::queue<std::tuple<const Gate *, const Gate *, std::vector<const Gate *>>> queue;
188  std::unordered_set<std::pair<const Gate *, const Gate *>, VoidPtrHash> visited;
189  std::unordered_set<const Gate *> expanded;
190 
191  for( const Gate *ff : netlist->get_gates( is_ff ) )
192  {
193  vertices.insert( ff );
194  ptrs_to_type[ff] = GATE;
195 
196  const std::vector<GatePin *> clock_pins = ff->get_type()->get_pins( []( const auto &p ) {
197  return ( p->get_direction() == PinDirection::input ) && ( p->get_type() == PinType::clock );
198  } );
199 
200  if( clock_pins.size() != 1 )
201  {
202  report( Severity::error,
203  DiagnosticCode::invalid_clock_pin_count,
204  GATE,
205  ff,
206  ff->get_id(),
207  "invalid number of input clock pins at gate '" + ff->get_name() + "' with ID "
208  + std::to_string( ff->get_id() ) );
209  continue;
210  }
211 
212  const Net *clk = ff->get_fan_in_net( clock_pins.front() );
213  if( clk == nullptr )
214  {
215  report( Severity::error,
216  DiagnosticCode::unconnected_clock_pin,
217  GATE,
218  ff,
219  ff->get_id(),
220  "no net connected to clock pin at gate '" + ff->get_name() + "' with ID "
221  + std::to_string( ff->get_id() ) );
222  continue;
223  }
224 
225  if( clk->get_num_of_sources() > 1 )
226  {
227  bool valid = true;
228  for( const Endpoint *source_ep : clk->get_sources() )
229  {
230  const Gate *gate = source_ep->get_gate();
231  if( !( is_buffer( gate ) || is_inverter( gate ) ) )
232  {
233  // In theory, it should be either all buffers or all inverters. But depending on
234  // extraction results, e.g., it could happen that a buffer is split into two inverters.
235  // So I just assume it is fine if all the sources are either a buffer or an inverter
236  // without enforcing strict buffer only or inverter only.
237  valid = false;
238  break;
239  }
240  }
241  if( !valid )
242  {
243  report( Severity::error,
244  DiagnosticCode::invalid_clock_net_sources,
245  NET,
246  clk,
247  clk->get_id(),
248  "invalid number of sources for clock net with ID " + std::to_string( clk->get_id() ) );
249  continue;
250  }
251  }
252  else if( clk->is_global_input_net() )
253  {
254  // the flip-flop is clocked straight from the outside: the net is the root and the flip-flop its only child
255  vertices.insert( clk );
256  ptrs_to_type[clk] = NET;
257  edges.insert( { clk, ff } );
258  continue;
259  }
260  else if( clk->get_num_of_sources() == 0 )
261  {
262  report( Severity::warning,
263  DiagnosticCode::unrouted_clock_net,
264  NET,
265  clk,
266  clk->get_id(),
267  "unrouted clock net with ID " + std::to_string( clk->get_id() ) + " ignored" );
268  continue;
269  }
270 
271  for( const Endpoint *source_ep : clk->get_sources() )
272  {
273  const Gate *gate = source_ep->get_gate();
274  queue.emplace( ff, gate, std::vector<const Gate *>{ ff } );
275  }
276  }
277 
278  const std::unordered_set<const Gate *> toggle_ffs = get_toggle_ffs( netlist );
279 
280  while( !queue.empty() )
281  {
282  const Gate *reference = std::get<0>( queue.front() );
283  const Gate *source = std::get<1>( queue.front() );
284  std::vector<const Gate *> path = std::move( std::get<2>( queue.front() ) );
285  queue.pop();
286 
287  path.push_back( source );
288 
289  if( is_latch( source ) )
290  {
291  // Ignore latches
292  continue;
293  }
294  else if( is_buffer( source ) || is_inverter( source ) || is_delay( source ) || is_ff( source ) )
295  {
296  if( is_ff( source ) && toggle_ffs.find( source ) == toggle_ffs.end() )
297  {
298  // Include only toggle flip-flops for now
299  continue;
300  }
301 
302  for( const Gate *gate : path )
303  {
304  vertices.insert( gate );
305  ptrs_to_type[gate] = GATE;
306  }
307 
308  for( std::size_t idx = 0; idx < path.size() - 1; idx++ )
309  {
310  edges.insert( { path[idx + 1], path[idx] } );
311  }
312 
313  path.clear();
314  path.push_back( source );
315 
316  if( is_ff( source ) )
317  {
318  continue;
319  }
320 
321  // prevent runtime explosion
322  if( !expanded.insert( source ).second )
323  {
324  // the path is merged now, and the fan-in of this gate has already been walked
325  continue;
326  }
327 
328  reference = source;
329  }
330 
331  visited.insert( std::make_pair( reference, source ) );
332 
333  for( const Endpoint *ep : source->get_fan_in_endpoints() )
334  {
335  if( is_connected_to_control_pin( ep ) )
336  {
337  // Don't traverse control signals of clock gates
338  continue;
339  }
340 
341  const Net *net = ep->get_net();
342  if( net->get_name() == "'0'" || net->get_name() == "'1'" )
343  {
344  // Don't traverse power/ground signals
345  continue;
346  }
347 
348  if( net->is_global_input_net() )
349  {
350  for( const Gate *gate : path )
351  {
352  vertices.insert( gate );
353  ptrs_to_type[gate] = GATE;
354  }
355 
356  for( std::size_t idx = 0; idx < path.size() - 1; idx++ )
357  {
358  edges.insert( { path[idx + 1], path[idx] } );
359  }
360 
361  vertices.insert( net );
362  ptrs_to_type[net] = NET;
363 
364  edges.insert( { net, path.back() } );
365 
366  path.clear();
367  path.push_back( source );
368 
369  continue;
370  }
371 
372  if( net->get_num_of_sources() == 0 )
373  {
374  report( Severity::warning,
375  DiagnosticCode::unrouted_clock_net,
376  NET,
377  net,
378  net->get_id(),
379  "unrouted clock net with ID " + std::to_string( net->get_id() ) + " ignored" );
380  continue;
381  }
382  if( net->get_num_of_sources() > 1 )
383  {
384  report( Severity::warning,
385  DiagnosticCode::multi_driven_clock_net,
386  NET,
387  net,
388  net->get_id(),
389  "multi-driven clock net with ID " + std::to_string( net->get_id() ) + " ignored" );
390  continue;
391  }
392 
393  const Gate *new_source = net->get_sources().front()->get_gate();
394  if( visited.find( { reference, new_source } ) == visited.end() )
395  {
396  queue.emplace( reference, new_source, path );
397  }
398  }
399  }
400 
401  const ScopedIgraphErrorHandler igraph_error_handler;
402 
403  // everything is built in locals first and only moved into a ClockTree once it all succeeded, so an
404  // early return never destroys a ClockTree whose graph was not initialized
405  std::unordered_map<igraph_int_t, const void *> vertices_to_ptrs;
406  std::unordered_map<const void *, igraph_int_t> ptrs_to_vertices;
407 
408  igraph_int_t idx = 0;
409  for( const void *vertex : vertices )
410  {
411  const igraph_int_t vertex_id = idx++;
412 
413  vertices_to_ptrs[vertex_id] = vertex;
414  ptrs_to_vertices[vertex] = vertex_id;
415  }
416 
417  igraph_error_t ierror;
418  igraph_vector_int_t iedges;
419  if( ( ierror = igraph_vector_int_init( &iedges, 2 * edges.size() ) ) != IGRAPH_SUCCESS )
420  {
421  return ERR( igraph_strerror( ierror ) );
422  }
423 
424  idx = 0;
425  for( const auto &[src, dst] : edges )
426  {
427  VECTOR( iedges )[idx++] = ptrs_to_vertices.at( src );
428  VECTOR( iedges )[idx++] = ptrs_to_vertices.at( dst );
429  }
430 
431  igraph_t igraph;
432  if( ( ierror = igraph_create( &igraph, &iedges, vertices.size(), IGRAPH_DIRECTED ) ) != IGRAPH_SUCCESS )
433  {
434  igraph_vector_int_destroy( &iedges );
435  return ERR( igraph_strerror( ierror ) );
436  }
437 
438  igraph_vector_int_destroy( &iedges );
439 
440  igraph_vector_int_t indegrees;
441  if( ( ierror = igraph_vector_int_init( &indegrees, 0 ) ) != IGRAPH_SUCCESS )
442  {
443  igraph_destroy( &igraph );
444  return ERR( igraph_strerror( ierror ) );
445  }
446 
447  if( ( ierror = igraph_degree( &igraph, &indegrees, igraph_vss_all(), IGRAPH_IN, IGRAPH_NO_LOOPS ) )
448  != IGRAPH_SUCCESS )
449  {
450  igraph_destroy( &igraph );
451  igraph_vector_int_destroy( &indegrees );
452  return ERR( igraph_strerror( ierror ) );
453  }
454 
455  std::unordered_set<igraph_int_t> roots;
456  for( idx = 0; idx < igraph_vector_int_size( &indegrees ); idx++ )
457  {
458  if( VECTOR( indegrees )[idx] != 0 )
459  {
460  continue;
461  }
462  roots.insert( idx );
463  }
464 
465  igraph_vector_int_destroy( &indegrees );
466 
467  // the ClockTree takes ownership of igraph from here on and destroys it in its destructor.
468  // plain new instead of make_unique, since the constructor is private
469  auto clock_tree = std::unique_ptr<ClockTree>( new ClockTree( netlist,
470  std::move( igraph ),
471  std::move( roots ),
472  std::move( vertices_to_ptrs ),
473  std::move( ptrs_to_type ) ) );
474  clock_tree->m_diagnostics = std::move( diagnostics );
475 
476  return OK( std::move( clock_tree ) );
477  }
478 
479  Result<std::unique_ptr<ClockTree>> ClockTree::get_subtree( const void *ptr, const bool parent ) const
480  {
481  const ScopedIgraphErrorHandler igraph_error_handler;
482 
483  auto it = m_ptrs_to_vertices.find( ptr );
484  if( it == m_ptrs_to_vertices.end() )
485  {
486  return ERR( "object is not part of clock tree" );
487  }
488 
489  igraph_error_t ierror;
490  igraph_int_t root = it->second;
491  if( parent )
492  {
493  igraph_vector_int_t parents;
494  if( ( ierror = igraph_vector_int_init( &parents, 0 ) ) != IGRAPH_SUCCESS )
495  {
496  return ERR( igraph_strerror( ierror ) );
497  }
498 
499  if( ( ierror =
500  igraph_neighbors( &m_igraph, &parents, root, IGRAPH_IN, IGRAPH_NO_LOOPS, IGRAPH_NO_MULTIPLE ) )
501  != IGRAPH_SUCCESS )
502  {
503  igraph_vector_int_destroy( &parents );
504  return ERR( igraph_strerror( ierror ) );
505  }
506 
507  // Only accept, if there is only one parent vertex for now.
508  if( igraph_vector_int_size( &parents ) == 1 )
509  {
510  root = VECTOR( parents )[0];
511  }
512 
513  igraph_vector_int_destroy( &parents );
514  }
515 
516  igraph_vector_int_t vertices;
517  if( ( ierror = igraph_vector_int_init( &vertices, 0 ) ) != IGRAPH_SUCCESS )
518  {
519  return ERR( igraph_strerror( ierror ) );
520  }
521 
522  if( ( ierror = igraph_dfs( &m_igraph,
523  root,
524  IGRAPH_OUT,
525  false,
526  nullptr,
527  nullptr,
528  nullptr,
529  nullptr,
530  in_callback,
531  nullptr,
532  &vertices ) )
533  != IGRAPH_SUCCESS )
534  {
535  igraph_vector_int_destroy( &vertices );
536  return ERR( igraph_strerror( ierror ) );
537  }
538 
539  igraph_vs_t vs;
540  if( ( ierror = igraph_vs_vector( &vs, &vertices ) ) != IGRAPH_SUCCESS )
541  {
542  igraph_vector_int_destroy( &vertices );
543  return ERR( igraph_strerror( ierror ) );
544  }
545 
546  igraph_vector_int_t map;
547  if( ( ierror = igraph_vector_int_init( &map, igraph_vcount( &m_igraph ) ) ) != IGRAPH_SUCCESS )
548  {
549  igraph_vs_destroy( &vs );
550  igraph_vector_int_destroy( &vertices );
551  return ERR( igraph_strerror( ierror ) );
552  }
553 
554  igraph_t igraph;
555  if( ( ierror = igraph_induced_subgraph_map( &m_igraph, &igraph, vs, IGRAPH_SUBGRAPH_AUTO, &map, nullptr ) )
556  != IGRAPH_SUCCESS )
557  {
558  igraph_vs_destroy( &vs );
559  igraph_vector_int_destroy( &map );
560  igraph_vector_int_destroy( &vertices );
561  return ERR( igraph_strerror( ierror ) );
562  }
563 
564  igraph_vs_destroy( &vs );
565  igraph_vector_int_destroy( &vertices );
566 
567  std::unordered_set<igraph_int_t> roots;
568  std::unordered_map<const void *, PtrType> ptrs_to_types;
569  std::unordered_map<igraph_int_t, const void *> vertices_to_ptrs;
570 
571  for( igraph_int_t idx = 0; idx < igraph_vector_int_size( &map ); idx++ )
572  {
573  const igraph_int_t vertex = VECTOR( map )[idx];
574  if( vertex == -1 )
575  {
576  continue;
577  }
578 
579  const void *ptr = m_vertices_to_ptrs.at( idx );
580 
581  vertices_to_ptrs[vertex] = ptr;
582  ptrs_to_types[ptr] = m_ptrs_to_types.at( ptr );
583  }
584 
585  igraph_vector_int_destroy( &map );
586 
587  igraph_vector_int_t indegrees;
588  if( ( ierror = igraph_vector_int_init( &indegrees, igraph_vcount( &igraph ) ) ) != IGRAPH_SUCCESS )
589  {
590  igraph_destroy( &igraph );
591  return ERR( igraph_strerror( ierror ) );
592  }
593 
594  if( ( ierror = igraph_degree( &igraph, &indegrees, igraph_vss_all(), IGRAPH_IN, IGRAPH_NO_LOOPS ) )
595  != IGRAPH_SUCCESS )
596  {
597  igraph_destroy( &igraph );
598  igraph_vector_int_destroy( &indegrees );
599  return ERR( igraph_strerror( ierror ) );
600  }
601 
602  for( igraph_int_t idx = 0; idx < igraph_vector_int_size( &indegrees ); idx++ )
603  {
604  if( VECTOR( indegrees )[idx] != 0 )
605  {
606  continue;
607  }
608  roots.insert( idx );
609  }
610 
611  igraph_vector_int_destroy( &indegrees );
612 
613  return OK( std::unique_ptr<ClockTree>( new ClockTree( m_netlist,
614  std::move( igraph ),
615  std::move( roots ),
616  std::move( vertices_to_ptrs ),
617  std::move( ptrs_to_types ) ) ) );
618  }
619 
620  Result<igraph_int_t> ClockTree::get_vertex_from_ptr( const void *ptr ) const
621  {
622  auto it = m_ptrs_to_vertices.find( ptr );
623  if( it == m_ptrs_to_vertices.end() )
624  {
625  return ERR( "object is not part of clock tree" );
626  }
627 
628  return OK( it->second );
629  }
630 
631  Result<std::pair<const void *, PtrType>> ClockTree::get_ptr_from_vertex( const igraph_int_t vertex ) const
632  {
633  auto it = m_vertices_to_ptrs.find( vertex );
634  if( it == m_vertices_to_ptrs.end() )
635  {
636  return ERR( "object is not part of clock tree" );
637  }
638 
639  return OK( std::make_pair( it->second, m_ptrs_to_types.at( it->second ) ) );
640  }
641 
642  Result<std::vector<igraph_int_t>> ClockTree::get_vertices_from_ptrs( const std::vector<const void *> &ptrs ) const
643  {
644  std::vector<igraph_int_t> result;
645 
646  for( const void *ptr : ptrs )
647  {
648  auto res = get_vertex_from_ptr( ptr );
649  if( res.is_error() )
650  {
651  return ERR( res.get_error().get() );
652  }
653 
654  result.push_back( res.get() );
655  }
656 
657  return OK( result );
658  }
659 
661  ClockTree::get_ptrs_from_vertices( const std::vector<igraph_int_t> &vertices ) const
662  {
663  std::vector<std::pair<const void *, PtrType>> result;
664 
665  for( const igraph_int_t vertex : vertices )
666  {
667  auto res = get_ptr_from_vertex( vertex );
668  if( res.is_error() )
669  {
670  return ERR( res.get_error().get() );
671  }
672 
673  result.push_back( res.get() );
674  }
675 
676  return OK( result );
677  }
678 
679  std::vector<const Gate *> ClockTree::get_gates() const
680  {
681  std::vector<const Gate *> result;
682 
683  for( const auto &[ptr, type] : m_ptrs_to_types )
684  {
685  if( type == PtrType::GATE )
686  {
687  result.push_back( static_cast<const Gate *>( ptr ) );
688  }
689  }
690 
691  return result;
692  }
693 
694  std::vector<const Net *> ClockTree::get_nets() const
695  {
696  std::vector<const Net *> result;
697 
698  for( const auto &[ptr, type] : m_ptrs_to_types )
699  {
700  if( type == PtrType::NET )
701  {
702  result.push_back( static_cast<const Net *>( ptr ) );
703  }
704  }
705 
706  return result;
707  }
708 
709  const std::unordered_map<const void *, PtrType> &ClockTree::get_all() const
710  {
711  return m_ptrs_to_types;
712  }
713 
714  const Netlist *ClockTree::get_netlist() const
715  {
716  return m_netlist;
717  }
718 
719  const std::vector<Diagnostic> &ClockTree::get_diagnostics() const
720  {
721  return m_diagnostics;
722  }
723 
724  const std::unordered_set<igraph_int_t> &ClockTree::get_roots() const
725  {
726  return m_roots;
727  }
728 
729  const igraph_t *ClockTree::get_igraph() const
730  {
731  return &m_igraph;
732  }
733 
735  ClockTree::get_neighbors( const void *ptr, const igraph_neimode_t direction ) const
736  {
737  const ScopedIgraphErrorHandler igraph_error_handler;
738 
739  auto it = m_ptrs_to_vertices.find( ptr );
740  if( it == m_ptrs_to_vertices.end() )
741  {
742  return ERR( "object is not part of clock tree" );
743  }
744 
745  igraph_error_t ierror;
746  igraph_vector_int_t neighbors;
747 
748  if( ( ierror = igraph_vector_int_init( &neighbors, 0 ) ) != IGRAPH_SUCCESS )
749  {
750  return ERR( igraph_strerror( ierror ) );
751  }
752 
753  if( ( ierror = igraph_neighbors(
754  &m_igraph, &neighbors, it->second, direction, IGRAPH_NO_LOOPS, IGRAPH_NO_MULTIPLE ) )
755  != IGRAPH_SUCCESS )
756  {
757  igraph_vector_int_destroy( &neighbors );
758  return ERR( igraph_strerror( ierror ) );
759  }
760 
761  std::vector<std::pair<const void *, PtrType>> result;
762  for( igraph_int_t idx = 0; idx < igraph_vector_int_size( &neighbors ); idx++ )
763  {
764  const void *n_ptr = m_vertices_to_ptrs.at( VECTOR( neighbors )[idx] );
765  result.emplace_back( n_ptr, m_ptrs_to_types.at( n_ptr ) );
766  }
767 
768  igraph_vector_int_destroy( &neighbors );
769 
770  return OK( result );
771  }
772 } // namespace hal::cte
Definition: gate.h:59
const std::vector< Endpoint * > & get_fan_in_endpoints() const
Definition: gate.cpp:888
Definition: net.h:58
u32 get_num_of_sources(const std::function< bool(Endpoint *ep)> &filter=nullptr) const
Definition: net.cpp:258
u32 get_id() const
Definition: net.cpp:88
bool is_global_input_net() const
Definition: net.cpp:504
std::vector< Endpoint * > get_sources(const std::function< bool(Endpoint *ep)> &filter=nullptr) const
Definition: net.cpp:276
const std::vector< Gate * > & get_gates() const
Definition: netlist.cpp:206
ClockTree(const ClockTree &)=delete
uint32_t u32
Definition: defines.h:41
#define log_error(channel,...)
Definition: log.h:78
#define log_debug(channel,...)
Definition: log.h:74
#define log_warning(channel,...)
Definition: log.h:76
#define ERR(message)
Definition: result.h:60
#define OK(...)
Definition: result.h:56
bool is_delay(const Gate *gate)
Definition: utils.cpp:63
bool is_connected_to_control_pin(const Endpoint *endpoint)
Definition: utils.cpp:89
bool is_inverter(const Gate *gate)
Definition: utils.cpp:58
DiagnosticCode
Definition: clock_tree.h:52
bool is_buffer(const Gate *gate)
Definition: utils.cpp:53
bool is_latch(const Gate *gate)
Definition: utils.cpp:48
bool is_ff(const Gate *gate)
Definition: utils.cpp:43
PinType type
Net * net
PinDirection direction
i32 id