Skip to content

Commit 1303e93

Browse files
author
Shareef Jalloq
committed
feature: add Module.print_hierarchy
Adding a new Module method that prints the hierarchy. Useful for visualising the structure of a module.
1 parent fd0bf58 commit 1303e93

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

migen/fhdl/module.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,27 @@ def do_finalize(self):
183183
def do_exit(self, *args, **kwargs):
184184
for name, submodule in self._submodules:
185185
submodule.do_exit(*args, **kwargs)
186+
187+
def print_hierarchy(self, indent=4, include_anon=False):
188+
"""Prints a hierarchy tree for the Module.
189+
190+
This method iterates over each submodule in the design and prints out an
191+
indented hierarchy. By default it ignores any anonymous modules but this can be
192+
overridden by passing include_anon=True.
193+
194+
Note that by default, with include_anon=False, all hierarchy below an anonymous
195+
module will be skipped. It is assumed that the user will only use anonymous
196+
instantiations for leaf cells such as CSRs, synchronisers, etc where we don't
197+
particularly care about printing them in a hierarchy view.
198+
199+
"""
200+
if indent == 4:
201+
print(self.__class__.__name__)
202+
for name, submodule in self._submodules:
203+
if name is None:
204+
if not include_anon:
205+
# all hierarchy below an anonymous module is skipped
206+
continue
207+
name = "anon"
208+
print("{}{}:{}".format(" " * indent, name, submodule.__class__.__name__))
209+
submodule.print_hierarchy(indent+4, include_anon)

0 commit comments

Comments
 (0)