2013-08-07 11:13:52 -04:00
|
|
|
from collections import OrderedDict
|
2012-01-16 12:09:52 -05:00
|
|
|
from itertools import combinations
|
2012-01-28 17:17:44 -05:00
|
|
|
|
2012-03-06 12:33:44 -05:00
|
|
|
from migen.fhdl.structure import *
|
2012-01-16 12:09:52 -05:00
|
|
|
|
2013-08-07 11:13:52 -04:00
|
|
|
class _Node:
|
|
|
|
def __init__(self):
|
|
|
|
self.use_name = False
|
|
|
|
self.children = OrderedDict()
|
2012-01-16 12:09:52 -05:00
|
|
|
|
2013-08-07 11:13:52 -04:00
|
|
|
def _build_tree(signals):
|
|
|
|
root = _Node()
|
|
|
|
for signal in signals:
|
|
|
|
current = root
|
|
|
|
for name, number in signal.backtrace:
|
|
|
|
try:
|
|
|
|
current = current.children[name]
|
|
|
|
except KeyError:
|
|
|
|
new = _Node()
|
|
|
|
current.children[name] = new
|
|
|
|
current = new
|
|
|
|
return root
|
2012-09-09 11:31:35 -04:00
|
|
|
|
2013-08-07 11:13:52 -04:00
|
|
|
def _set_use_name(node, node_name=""):
|
|
|
|
if not node.children:
|
|
|
|
node.use_name = True
|
|
|
|
return {(node_name, )}
|
2012-01-19 12:25:25 -05:00
|
|
|
else:
|
2013-08-07 11:13:52 -04:00
|
|
|
cnames = [(k, _set_use_name(v, k)) for k, v in node.children.items()]
|
|
|
|
for (c1_prefix, c1_names), (c2_prefix, c2_names) in combinations(cnames, 2):
|
|
|
|
if not c1_names.isdisjoint(c2_names):
|
|
|
|
node.children[c1_prefix].use_name = True
|
|
|
|
node.children[c2_prefix].use_name = True
|
|
|
|
r = set()
|
|
|
|
for c_prefix, c_names in cnames:
|
|
|
|
if node.children[c_prefix].use_name:
|
|
|
|
for c_name in c_names:
|
|
|
|
r.add((c_prefix, ) + c_name)
|
2012-09-09 11:31:35 -04:00
|
|
|
else:
|
2013-08-07 11:13:52 -04:00
|
|
|
r |= c_names
|
|
|
|
return r
|
|
|
|
|
|
|
|
def _display_tree(tree):
|
|
|
|
from migen.graph.treeviz import RenderNode
|
2012-09-09 11:31:35 -04:00
|
|
|
|
2013-08-07 11:13:52 -04:00
|
|
|
def _to_render_node(name, node):
|
|
|
|
children = [_to_render_node(k, v) for k, v in node.children.items()]
|
|
|
|
if node.use_name:
|
|
|
|
color = (0.8, 0.5, 0.9)
|
|
|
|
else:
|
|
|
|
color = (0.8, 0.8, 0.8)
|
|
|
|
return RenderNode(name, children, color=color)
|
|
|
|
|
|
|
|
top = _to_render_node("top", tree)
|
|
|
|
top.to_svg("names.svg")
|
2012-01-19 12:25:25 -05:00
|
|
|
|
2013-08-07 11:13:52 -04:00
|
|
|
def _name_signal(tree, signal):
|
|
|
|
elements = []
|
|
|
|
treepos = tree
|
|
|
|
for step_name, step_n in signal.backtrace:
|
|
|
|
treepos = treepos.children[step_name]
|
|
|
|
if treepos.use_name:
|
|
|
|
elements.append(step_name)
|
|
|
|
return "_".join(elements)
|
2012-01-19 12:42:43 -05:00
|
|
|
|
2013-08-07 11:13:52 -04:00
|
|
|
def _build_pnd(tree, signals):
|
|
|
|
return dict((signal, _name_signal(tree, signal)) for signal in signals)
|
|
|
|
|
2012-01-20 16:20:32 -05:00
|
|
|
def build_namespace(signals):
|
2013-08-07 11:13:52 -04:00
|
|
|
tree = _build_tree(signals)
|
|
|
|
_set_use_name(tree)
|
|
|
|
_display_tree(tree)
|
|
|
|
pnd = _build_pnd(tree, signals)
|
|
|
|
|
2012-01-20 16:20:32 -05:00
|
|
|
ns = Namespace(pnd)
|
|
|
|
# register signals with name_override
|
|
|
|
for signal in signals:
|
|
|
|
if signal.name_override is not None:
|
|
|
|
ns.get_name(signal)
|
|
|
|
return ns
|
2012-01-16 12:09:52 -05:00
|
|
|
|
|
|
|
class Namespace:
|
2012-01-19 12:25:25 -05:00
|
|
|
def __init__(self, pnd):
|
2012-01-16 12:09:52 -05:00
|
|
|
self.counts = {}
|
|
|
|
self.sigs = {}
|
2012-01-19 12:25:25 -05:00
|
|
|
self.pnd = pnd
|
2012-01-16 12:09:52 -05:00
|
|
|
|
|
|
|
def get_name(self, sig):
|
2013-02-22 11:56:35 -05:00
|
|
|
if sig.name_override is not None:
|
|
|
|
sig_name = sig.name_override
|
2012-01-19 12:25:25 -05:00
|
|
|
else:
|
2013-02-22 11:56:35 -05:00
|
|
|
sig_name = self.pnd[sig]
|
2012-01-16 12:09:52 -05:00
|
|
|
try:
|
|
|
|
n = self.sigs[sig]
|
|
|
|
except KeyError:
|
|
|
|
try:
|
|
|
|
n = self.counts[sig_name]
|
|
|
|
except KeyError:
|
|
|
|
n = 0
|
|
|
|
self.sigs[sig] = n
|
|
|
|
self.counts[sig_name] = n + 1
|
2012-01-19 12:25:25 -05:00
|
|
|
if n:
|
|
|
|
return sig_name + "_" + str(n)
|
|
|
|
else:
|
|
|
|
return sig_name
|