fhdl/namer: number objects according to execution order

This commit is contained in:
Sebastien Bourdeauducq 2012-09-09 12:27:32 +02:00
parent 51f9a2a963
commit f3e3a3eec7
2 changed files with 45 additions and 44 deletions

View file

@ -2,29 +2,7 @@ from itertools import combinations
from migen.fhdl.structure import *
class _StepNamer:
def __init__(self):
self.name_to_ids = {}
def get_step_str(self, obj):
if isinstance(obj, str):
return obj
else:
n = obj.__class__.__name__.lower()
try:
l = self.name_to_ids[n]
except KeyError:
self.name_to_ids[n] = [id(obj)]
return n + "0"
else:
try:
idx = l.index(id(obj))
except ValueError:
idx = len(l)
l.append(id(obj))
return n + str(idx)
def _bin(sn, sig_iters):
def _bin(sig_iters):
status = []
for signal, it in sig_iters:
step, last = next(it)
@ -32,13 +10,12 @@ def _bin(sn, sig_iters):
terminals = []
bins = {}
for signal, it, step, last in status:
step_name = sn.get_step_str(step)
if last:
terminals.append((step_name, signal))
terminals.append((step, signal))
else:
if step_name not in bins:
bins[step_name] = []
bins[step_name].append((signal, it))
if step not in bins:
bins[step] = []
bins[step].append((signal, it))
return terminals, bins
def _sets_disjoint(l):
@ -47,9 +24,9 @@ def _sets_disjoint(l):
return False
return True
def _r_build_pnd(sn, sig_iters):
terminals, bins = _bin(sn, sig_iters)
bins_named = [(k, _r_build_pnd(sn, v)) for k, v in bins.items()]
def _r_build_pnd(sig_iters):
terminals, bins = _bin(sig_iters)
bins_named = [(k, _r_build_pnd(v)) for k, v in bins.items()]
name_sets = [set(sub_pnd.values()) for prefix, sub_pnd in bins_named]
r = {}
if not _sets_disjoint(name_sets):
@ -74,7 +51,7 @@ def last_flagged(seq):
def build_namespace(signals):
sig_iters = [(signal, last_flagged(signal.backtrace))
for signal in signals if signal.name_override is None]
pnd = _r_build_pnd(_StepNamer(), sig_iters)
pnd = _r_build_pnd(sig_iters)
ns = Namespace(pnd)
# register signals with name_override
for signal in signals:

View file

@ -25,26 +25,50 @@ def get_var_name(frame):
else:
return None
classname_to_objs = dict()
def index_id(l, obj):
for n, e in enumerate(l):
if id(e) == id(obj):
return n
raise ValueError
def trace_back(name=None):
l = []
frame = inspect.currentframe().f_back.f_back
while frame is not None:
try:
obj = frame.f_locals["self"]
except KeyError:
obj = None
if obj is not None and hasattr(obj, "__del__"):
obj = None
if obj is None:
modules = frame.f_globals["__name__"]
modules = modules.split(".")
obj = modules[len(modules)-1]
if name is None:
name = get_var_name(frame)
if name is not None:
l.insert(0, name)
l.insert(0, obj)
try:
obj = frame.f_locals["self"]
except KeyError:
obj = None
if hasattr(obj, "__del__"):
obj = None
if obj is None:
modules = frame.f_globals["__name__"]
modules = modules.split(".")
objname = modules[len(modules)-1]
else:
classname = obj.__class__.__name__.lower()
try:
objs = classname_to_objs[classname]
except KeyError:
classname_to_objs[classname] = [obj]
idx = 0
else:
try:
idx = index_id(objs, obj)
except ValueError:
idx = len(objs)
objs.append(obj)
objname = classname + str(idx)
l.insert(0, objname)
name = None
frame = frame.f_back
return l