gen/fhdl/namer: Simplify _invert_pnd_build_signal_groups/_build_pnd and add comments.

This commit is contained in:
Florent Kermarrec 2023-11-06 11:58:29 +01:00
parent 36e47052b2
commit a65d471ed2
1 changed files with 46 additions and 29 deletions

View File

@ -246,24 +246,29 @@ def _build_signal_groups(signals):
Returns: Returns:
list: A list of sets, each containing related signals. list: A list of sets, each containing related signals.
""" """
r = [] grouped_signals = []
# Create groups of related signals.
for signal in signals: for signal in signals:
# Build chain of related signals. chain = []
related_list = [] # Trace back the chain of related signals.
cur_signal = signal while signal is not None:
while cur_signal is not None: chain.insert(0, signal)
related_list.insert(0, cur_signal) signal = signal.related
cur_signal = cur_signal.related
# Add to groups. # Ensure there's a set for each level of relation.
for _ in range(len(related_list) - len(r)): while len(grouped_signals) < len(chain):
r.append(set()) grouped_signals.append(set())
for target_set, source_signal in zip(r, related_list):
target_set.add(source_signal) # Assign signals to their respective group.
# With the algorithm above and a list of all signals, a signal appears in all groups of a lower for group, sig in zip(grouped_signals, chain):
# number than its. Make signals appear only in their group of highest number. group.add(sig)
for s1, s2 in zip(r, r[1:]):
s1 -= s2 # Ensure signals only appear in their most specific group.
return r for i in range(len(grouped_signals) - 1):
grouped_signals[i] -= grouped_signals[i + 1]
return grouped_signals
def _build_pnd(signals): def _build_pnd(signals):
@ -276,19 +281,31 @@ def _build_pnd(signals):
Returns: Returns:
dict: A complete dictionary mapping signals to their hierarchical names. dict: A complete dictionary mapping signals to their hierarchical names.
""" """
# Group the signals based on their relationships.
groups = _build_signal_groups(signals) groups = _build_signal_groups(signals)
gpnds = [_build_pnd_for_group(n, gsignals) for n, gsignals in enumerate(groups)]
pnd = dict() # Generate a name mapping for each group.
for gn, gpnd in enumerate(gpnds): group_pnd_mappings = [_build_pnd_for_group(group_number, group_signals)
for signal, name in gpnd.items(): for group_number, group_signals in enumerate(groups)]
result = name
cur_gn = gn # Create the final signal-to-name mapping.
cur_signal = signal pnd = {}
while cur_signal.related is not None: for group_number, group_pnd in enumerate(group_pnd_mappings):
cur_signal = cur_signal.related for signal, name in group_pnd.items():
cur_gn -= 1 # Build the full hierarchical name for each signal.
result = gpnds[cur_gn][cur_signal] + "_" + result hierarchical_name = name
pnd[signal] = result current_group_number = group_number
current_signal = signal
# Traverse up the signal's group relationships to prepend parent names.
while current_signal.related is not None:
current_signal = current_signal.related
current_group_number -= 1
hierarchical_name = f"{group_pnd_mappings[current_group_number][current_signal]}_{hierarchical_name}"
# Map the signal to its full hierarchical name.
pnd[signal] = hierarchical_name
return pnd return pnd