fhdl/namer: number objects according to execution order
authorSebastien Bourdeauducq <sebastien@milkymist.org>
Sun, 9 Sep 2012 10:27:32 +0000 (12:27 +0200)
committerSebastien Bourdeauducq <sebastien@milkymist.org>
Sun, 9 Sep 2012 10:27:32 +0000 (12:27 +0200)
migen/fhdl/namer.py
migen/fhdl/tracer.py

index 433882bfc6f2583a80994c39bbf7864dcc8c11a1..e274e3c977d95e47f6e83266bb352259cda0a56f 100644 (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:
index 8a782a48fca886e63bc6ce9b745e2cec79ccb88e..030513152dcc1e1b66a568ce02ef9dcb95a1e75d 100644 (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:
+               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