From 64f11820e6f7c7684448ecbfdfdc833891b579f4 Mon Sep 17 00:00:00 2001 From: Shareef Jalloq Date: Thu, 2 Nov 2023 01:19:37 +0000 Subject: [PATCH] feature: add Module.print_hierarchy Adding a new Module method that prints the hierarchy. Useful for visualising the structure of a module. --- migen/fhdl/module.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/migen/fhdl/module.py b/migen/fhdl/module.py index fb9ee7f95..679d6cde7 100644 --- a/migen/fhdl/module.py +++ b/migen/fhdl/module.py @@ -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) \ No newline at end of file