From 16804acaa8fb997da981dfef751d858761d4759f Mon Sep 17 00:00:00 2001 From: Florent Kermarrec Date: Mon, 6 Nov 2023 13:19:19 +0100 Subject: [PATCH] gen/fhdl/namer: Add update_hierarchy_node function to reduce build_hierarchy_tree complexity. --- litex/gen/fhdl/namer.py | 43 +++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/litex/gen/fhdl/namer.py b/litex/gen/fhdl/namer.py index 4b3c8f688..e5c82756a 100644 --- a/litex/gen/fhdl/namer.py +++ b/litex/gen/fhdl/namer.py @@ -27,6 +27,34 @@ class HierarchyNode: self.use_number = False self.children = {} +def update_hierarchy_node(current, name, number, use_number, current_base): + """ + Updates or creates a hierarchy node based on the current position, name, and number. + If numbering is used, sorts and stores all numbers associated with the base node. + + Parameters: + curren t (HierarchyNode): The current node in the hierarchy. + name (str): The name of the current hierarchy level. + number (int): The number associated with the current hierarchy level. + use_number (bool): Flag indicating whether to use the number in the hierarchy. + current_base (HierarchyNode): The base node for number usage information. + + Returns: + HierarchyNode: The updated or created node. + """ + # Create the appropriate key for the node. + key = (name, number) if use_number else name + # Use setdefault to either get the existing child node or create a new one. + current = current.children.setdefault(key, HierarchyNode()) + # Add the number to the set of numbers associated with this node. + current.numbers.add(number) + # Increment the count of signals that have traversed this node. + current.signal_count += 1 + # If numbering is used, sort and store all numbers associated with the base node. + if use_number and current_base: + current.all_numbers = sorted(current_base.numbers) + return current + def build_hierarchy_tree(signals, base_tree=None): """ Constructs a hierarchical tree from signals, where each signal's backtrace contributes to the tree structure. @@ -52,19 +80,10 @@ def build_hierarchy_tree(signals, base_tree=None): use_number = False if current_base: current_base = current_base.children.get(name) - use_number = current_base.use_number if current_base else False + use_number = current_base.use_number if current_base else False - # Create the appropriate key for the node. - key = (name, number) if use_number else name - # Use setdefault to either get the existing child node or create a new one. - current = current.children.setdefault(key, HierarchyNode()) - # Add the number to the set of numbers associated with this node. - current.numbers.add(number) - # Increment the count of signals that have traversed this node. - current.signal_count += 1 - # If numbering is used, sort and store all numbers associated with the base node. - if use_number: - current.all_numbers = sorted(current_base.numbers) + # Update the current node in the hierarchy. + current = update_hierarchy_node(current, name, number, use_number, current_base) return root