Browse files

fhdl/namer: number objects according to execution order

  • Loading branch information...
1 parent 51f9a2a commit f3e3a3eec7ec3568e48edf8abd0bfe116282255a @sbourdeauducq sbourdeauducq committed Sep 9, 2012
Showing with 40 additions and 39 deletions.
  1. +9 −32 migen/fhdl/namer.py
  2. +31 −7 migen/fhdl/tracer.py
View
41 migen/fhdl/namer.py
@@ -2,43 +2,20 @@
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)
status.append((signal, it, step, last))
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
38 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

0 comments on commit f3e3a3e

Please sign in to comment.