Skip to content

Commit

Permalink
feature: add Module.print_hierarchy
Browse files Browse the repository at this point in the history
Adding a new Module method that prints the hierarchy. Useful for visualising
the structure of a module.
  • Loading branch information
shareefj committed Nov 8, 2023
1 parent fd0bf58 commit 64f1182
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions migen/fhdl/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,23 @@ def do_finalize(self):
def do_exit(self, *args, **kwargs):
for name, submodule in self._submodules:
submodule.do_exit(*args, **kwargs)

def print_hierarchy(self, include_anon=False):
"""Prints a hierarchy tree for the Module.
This method iterates over each submodule in the design and prints out an
indented hierarchy. By default it ignores any anonymous modules but this can be
overridden by passing include_anon=True.
"""
print(self.__class__.__name__)
self._iter_submodules(self, indent=4, include_anon=include_anon)

def _iter_submodules(self, module, indent, include_anon):
for name, submodule in module._submodules:
if name is None:
if not include_anon:
continue
name = "anon"
print("{}{}:{}".format(" " * indent, name, submodule.__class__.__name__))
self._iter_submodules(submodule, indent=indent+4)

0 comments on commit 64f1182

Please sign in to comment.