fhdl/verilog: recursive Special lowering
authorSebastien Bourdeauducq <sebastien@milkymist.org>
Thu, 25 Apr 2013 12:56:26 +0000 (14:56 +0200)
committerSebastien Bourdeauducq <sebastien@milkymist.org>
Thu, 25 Apr 2013 12:56:26 +0000 (14:56 +0200)
migen/fhdl/verilog.py

index 748db5b229a9e9c54e54ceb72124f06ac23ba67d..6708bdffef722e4fc858fc3d37500ff9a5c6269c 100644 (file)
@@ -230,7 +230,7 @@ def _call_special_classmethod(overrides, obj, method, *args, **kwargs):
        else:
                return None
 
-def _lower_specials(overrides, specials):
+def _lower_specials_step(overrides, specials):
        f = Fragment()
        lowered_specials = set()
        for special in sorted(specials, key=lambda x: x.huid):
@@ -240,6 +240,24 @@ def _lower_specials(overrides, specials):
                        lowered_specials.add(special)
        return f, lowered_specials
 
+def _can_lower(overrides, specials):
+       for special in specials:
+               cl = special.__class__
+               if cl in overrides:
+                       cl = overrides[cl]
+               if hasattr(cl, "lower"):
+                       return True
+       return False
+
+def _lower_specials(overrides, specials):
+       f, lowered_specials = _lower_specials_step(overrides, specials)
+       while _can_lower(overrides, f.specials):
+               f2, lowered_specials2 = _lower_specials_step(overrides, f.specials)
+               f += f2
+               lowered_specials |= lowered_specials2
+               f.specials -= lowered_specials2
+       return f, lowered_specials
+
 def _printspecials(overrides, specials, ns):
        r = ""
        for special in sorted(specials, key=lambda x: x.huid):