aboutsummaryrefslogtreecommitdiffstats
path: root/testsuite/pyunit/units01/show_units.py
diff options
context:
space:
mode:
Diffstat (limited to 'testsuite/pyunit/units01/show_units.py')
-rwxr-xr-xtestsuite/pyunit/units01/show_units.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/testsuite/pyunit/units01/show_units.py b/testsuite/pyunit/units01/show_units.py
new file mode 100755
index 000000000..43baf9aed
--- /dev/null
+++ b/testsuite/pyunit/units01/show_units.py
@@ -0,0 +1,55 @@
+#!/usr/bin/env python
+import libghdl
+from libghdl.thin import name_table
+from libghdl.thin import files_map
+from libghdl.thin.vhdl import nodes
+from libghdl.thin.vhdl import sem_lib
+from libghdl.thin import errorout_console
+
+
+def init():
+ """Initialization: set options and then load libaries"""
+ # Print error messages on the console
+ errorout_console.Install_Handler()
+ # Set options. This must be done before analyze_init()
+ libghdl.set_option(b"--std=08")
+ # Finish initialization. This will load the standard package
+ if libghdl.analyze_init_status() != 0:
+ raise Exception("libghdl initialization error")
+
+def get_identifier_ptr(n):
+ """Return the python string from node :param n: identifier"""
+ return name_table.Get_Name_Ptr(nodes.Get_Identifier(n)).decode("utf-8")
+
+
+def list_units(filename):
+ # Load the file
+ file_id = name_table.Get_Identifier(filename.encode("utf_8"))
+ sfe = files_map.Read_Source_File(name_table.Null_Identifier, file_id)
+ if sfe == files_map.No_Source_File_Entry:
+ print("cannot open file '%s'" % filename)
+ return
+
+ # Parse
+ file = sem_lib.Load_File(sfe)
+
+ # Display all design units
+ unit = nodes.Get_First_Design_Unit(file)
+ while unit != nodes.Null_Iir:
+ lib_unit = nodes.Get_Library_Unit(unit)
+ if nodes.Get_Kind(lib_unit) == nodes.Iir_Kind.Entity_Declaration:
+ print("entity %s" % get_identifier_ptr(lib_unit))
+ elif nodes.Get_Kind(lib_unit) == nodes.Iir_Kind.Architecture_Body:
+ print("architecture %s" % get_identifier_ptr(lib_unit))
+ else:
+ print("unknown unit!")
+ unit = nodes.Get_Chain(unit)
+
+
+def main():
+ init()
+ list_units("demo.vhdl")
+
+
+if __name__ == "__main__":
+ main()