From badba89686d55d709cb4fca93b623674d09bebe9 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sat, 5 Jan 2013 14:18:15 +0100 Subject: [PATCH] fhdl: support nested statement lists --- migen/fhdl/tools.py | 10 +++++++++- migen/fhdl/verilog.py | 2 +- migen/fhdl/visit.py | 5 +++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/migen/fhdl/tools.py b/migen/fhdl/tools.py index f6d11a09..ab9f9d25 100644 --- a/migen/fhdl/tools.py +++ b/migen/fhdl/tools.py @@ -2,6 +2,14 @@ from migen.fhdl.structure import * from migen.fhdl.structure import _Operator, _Slice, _Assign, _ArrayProxy from migen.fhdl.visit import NodeVisitor, NodeTransformer +def flat_iteration(l): + for element in l: + if isinstance(element, (list, tuple)): + for element2 in flat_iteration(element): + yield element2 + else: + yield element + class _SignalLister(NodeVisitor): def __init__(self): self.output_list = set() @@ -35,7 +43,7 @@ def list_targets(node): def group_by_targets(sl): groups = [] - for statement in sl: + for statement in flat_iteration(sl): targets = list_targets(statement) processed = False for g in groups: diff --git a/migen/fhdl/verilog.py b/migen/fhdl/verilog.py index a8f8f6e6..b26689d5 100644 --- a/migen/fhdl/verilog.py +++ b/migen/fhdl/verilog.py @@ -97,7 +97,7 @@ def _printnode(ns, at, level, node): else: assignment = " <= " return "\t"*level + _printexpr(ns, node.l)[0] + assignment + _printexpr(ns, node.r)[0] + ";\n" - elif isinstance(node, list): + elif isinstance(node, (list, tuple)): return "".join(list(map(partial(_printnode, ns, at, level), node))) elif isinstance(node, If): r = "\t"*level + "if (" + _printexpr(ns, node.cond)[0] + ") begin\n" diff --git a/migen/fhdl/visit.py b/migen/fhdl/visit.py index 3c397002..059daeef 100644 --- a/migen/fhdl/visit.py +++ b/migen/fhdl/visit.py @@ -25,7 +25,7 @@ class NodeVisitor: self.visit_Case(node) elif isinstance(node, Fragment): self.visit_Fragment(node) - elif isinstance(node, list): + elif isinstance(node, (list, tuple)): self.visit_statements(node) elif isinstance(node, dict): self.visit_clock_domains(node) @@ -115,7 +115,7 @@ class NodeTransformer: return self.visit_Case(node) elif isinstance(node, Fragment): return self.visit_Fragment(node) - elif isinstance(node, list): + elif isinstance(node, (list, tuple)): return self.visit_statements(node) elif isinstance(node, dict): return self.visit_clock_domains(node) @@ -164,6 +164,7 @@ class NodeTransformer: r.sync = self.visit(node.sync) return r + # NOTE: this will always return a list, even if node is a tuple def visit_statements(self, node): return [self.visit(statement) for statement in node] -- 2.30.2