aboutsummaryrefslogtreecommitdiffstats
path: root/testsuite/pyunit
diff options
context:
space:
mode:
authorumarcor <unai.martinezcorral@ehu.eus>2020-12-28 20:49:18 +0100
committerumarcor <unai.martinezcorral@ehu.eus>2020-12-28 23:48:09 +0100
commit147375c38a56820595df54047ad904cdc21d94ec (patch)
tree2755bff7094e05d3580d9c2818af5e9916e7ead5 /testsuite/pyunit
parentaec9873b7f2a175611f431b7f67840cdb74eee96 (diff)
downloadghdl-147375c38a56820595df54047ad904cdc21d94ec.tar.gz
ghdl-147375c38a56820595df54047ad904cdc21d94ec.tar.bz2
ghdl-147375c38a56820595df54047ad904cdc21d94ec.zip
merge testsuite/python into testsuite/pyunit
Diffstat (limited to 'testsuite/pyunit')
-rwxr-xr-xtestsuite/pyunit/testsuite.sh57
-rw-r--r--testsuite/pyunit/units01/demo.vhdl12
-rw-r--r--testsuite/pyunit/units01/show_ports.py111
-rwxr-xr-xtestsuite/pyunit/units01/show_units.py55
-rwxr-xr-xtestsuite/pyunit/units01/testsuite.sh11
5 files changed, 246 insertions, 0 deletions
diff --git a/testsuite/pyunit/testsuite.sh b/testsuite/pyunit/testsuite.sh
new file mode 100755
index 000000000..7eac028ca
--- /dev/null
+++ b/testsuite/pyunit/testsuite.sh
@@ -0,0 +1,57 @@
+#! /bin/sh
+
+# Driver for a testsuite.
+
+set -e
+
+# This is the only place where test dirs are specified. Do not duplicate this
+# line
+#dirs="*[0-9]"
+#
+#failures=""
+#full=n
+#
+#for opt; do
+# case "$opt" in
+# -k | --keep-going) full=y ;;
+# --dir=*) dirs=`echo $opt | sed -e 's/--dir=//'` ;;
+# --skip=*) d=`echo $opt | sed -e 's/--skip=//'`
+# dirs=`echo "" $dirs | sed -e "s/ $d//"` ;;
+# --start-at=*) d=`echo $opt | sed -e 's/--start-at=//'`
+# dirs=`echo "" $dirs | sed -e "s/^.* $d//"`
+# dirs="$d $dirs" ;;
+# --list-tests) echo $dirs; exit 0;;
+# *) echo "Unknown option $opt"
+# exit 2
+# ;;
+# esac
+#done
+#
+#singlerun() {
+# echo ""
+# echo "dir $1:"
+# cd $1
+# if ! ./testsuite.sh; then
+# echo "#################################################################"
+# echo "######### FAILURE: $1"
+# echo "#################################################################"
+# if [ $2 = "y" ]; then
+# failures="$failures $1"
+# else
+# exit 1;
+# fi
+# fi
+# cd ..
+#}
+#
+#for i in $dirs; do singlerun $i $full; done
+#
+#if [ x"$failures" = x"" ]; then
+# echo "tests are successful" && exit 0
+#else
+# echo "test failed ($failures)" && exit 1
+#fi
+
+cd $(dirname "$0")/..
+
+python3 -m unittest pyunit
diff --git a/testsuite/pyunit/units01/demo.vhdl b/testsuite/pyunit/units01/demo.vhdl
new file mode 100644
index 000000000..ed98c936a
--- /dev/null
+++ b/testsuite/pyunit/units01/demo.vhdl
@@ -0,0 +1,12 @@
+entity e1 is
+port (
+ CLK: in std_logic;
+ RST: in std_logic;
+ Q: out std_logic_vector(7 downto 0)
+);
+end e1;
+
+architecture behav of e1 is
+begin
+ assert false report "arch" severity note;
+end behav;
diff --git a/testsuite/pyunit/units01/show_ports.py b/testsuite/pyunit/units01/show_ports.py
new file mode 100644
index 000000000..a11f2acbd
--- /dev/null
+++ b/testsuite/pyunit/units01/show_ports.py
@@ -0,0 +1,111 @@
+#!/usr/bin/env python
+from sys import argv
+from pathlib import Path
+
+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.vhdl import pyutils
+from libghdl.thin import errorout_console
+
+
+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 get_port_mode(port) -> str:
+ """Return the Mode of a port, as a string"""
+ mode = nodes.Get_Mode(port)
+ return (
+ "in"
+ if mode == nodes.Iir_Mode.In_Mode
+ else "out"
+ if mode == nodes.Iir_Mode.Out_Mode
+ else "inout"
+ if mode == nodes.Iir_Mode.Inout_Mode
+ else "buffer"
+ if mode == nodes.Iir_Mode.Buffer_Mode
+ else "linkage"
+ if mode == nodes.Iir_Mode.Linkage_Mode
+ else "unknown"
+ )
+
+
+def get_port_type(port) -> str:
+ "Return the Type of a port, as a string"
+ subtype = nodes.Get_Subtype_Indication(port)
+ skind = nodes.Get_Kind(subtype)
+
+ if skind == nodes.Iir_Kind.Simple_Name:
+ return get_identifier_ptr(subtype)
+
+ if skind == nodes.Iir_Kind.Array_Subtype_Definition:
+ mark = get_identifier_ptr(nodes.Get_Subtype_Type_Mark(subtype))
+
+ for rng in pyutils.flist_iter(nodes.Get_Index_Constraint_List(subtype)):
+ if nodes.Get_Kind(rng) == nodes.Iir_Kind.Range_Expression:
+ return "%s(%d %s %d)" % (
+ mark,
+ nodes.Get_Value(nodes.Get_Left_Limit_Expr(rng)),
+ "downto" if nodes.Get_Direction(rng) else "to",
+ nodes.Get_Value(nodes.Get_Right_Limit_Expr(rng)),
+ )
+ return "UNSUPPORTED array_subtype_definition"
+
+ return "UNSUPPORTED"
+
+
+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))
+ for port in pyutils.chain_iter(nodes.Get_Port_Chain(lib_unit)):
+ print(
+ " * %s %s %s"
+ % (
+ get_identifier_ptr(port),
+ get_port_mode(port),
+ get_port_type(port),
+ )
+ )
+ elif nodes.Get_Kind(lib_unit) == nodes.Iir_Kind.Architecture_Body:
+ print(
+ " - architecture %s of %s"
+ % (
+ get_identifier_ptr(lib_unit),
+ get_identifier_ptr(nodes.Get_Entity_Name(lib_unit)),
+ )
+ )
+ else:
+ print("unknown unit!")
+ unit = nodes.Get_Chain(unit)
+
+
+if __name__ == "__main__":
+ # Initialization: set options and then load libaries
+ errorout_console.Install_Handler()
+ libghdl.set_option(b"--std=08")
+ if libghdl.analyze_init_status() != 0:
+ raise Exception("libghdl initialization error")
+
+ # Recursively find and parse all the files with extension *.vhdl
+ if len(argv) > 1:
+ for file in Path(argv[1]).glob("**/*.vhdl"):
+ print("ยท %s" % file)
+ list_units(str(file))
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()
diff --git a/testsuite/pyunit/units01/testsuite.sh b/testsuite/pyunit/units01/testsuite.sh
new file mode 100755
index 000000000..f45d12ac3
--- /dev/null
+++ b/testsuite/pyunit/units01/testsuite.sh
@@ -0,0 +1,11 @@
+#! /bin/sh
+
+. ../../testenv.sh
+
+$PYTHON show_units.py
+
+echo ""
+
+$PYTHON show_ports.py ./
+
+echo "Test successful"