From: Sebastien Bourdeauducq Date: Sun, 9 Sep 2012 10:27:32 +0000 (+0200) Subject: fhdl/namer: number objects according to execution order X-Git-Tag: 24jan2021_ls180~2099^2~844 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=f3e3a3eec7ec3568e48edf8abd0bfe116282255a;p=litex.git fhdl/namer: number objects according to execution order --- diff --git a/migen/fhdl/namer.py b/migen/fhdl/namer.py index 433882bf..e274e3c9 100644 --- a/migen/fhdl/namer.py +++ b/migen/fhdl/namer.py @@ -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: diff --git a/migen/fhdl/tracer.py b/migen/fhdl/tracer.py index 8a782a48..03051315 100644 --- a/migen/fhdl/tracer.py +++ b/migen/fhdl/tracer.py @@ -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: + if name is None: + name = get_var_name(frame) + if name is not None: + l.insert(0, name) + try: obj = frame.f_locals["self"] except KeyError: obj = None - if obj is not None and hasattr(obj, "__del__"): + if hasattr(obj, "__del__"): obj = None + if obj is None: modules = frame.f_globals["__name__"] modules = modules.split(".") - obj = modules[len(modules)-1] + 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) - if name is None: - name = get_var_name(frame) - if name is not None: - l.insert(0, name) - l.insert(0, obj) name = None frame = frame.f_back return l