Skip to content

Commit

Permalink
Merge branch 'master' of github.com:anikau31/systemc-clang
Browse files Browse the repository at this point in the history
  • Loading branch information
rmrf committed Dec 13, 2019
2 parents 8e73fe9 + f126047 commit 875a966
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 19 deletions.
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ else()

endif()

# Keep python tests off by default
if("${ENABLE_PYTHON_TESTS}" STREQUAL "")
set( PY_TESTS_ON OFF )
else()
set( PY_TESTS_ON ${ENABLE_PYTHON_TESTS} )
if(PY_TESTS_ON)
find_package(PythonInterp 3 REQUIRED)
endif()
endif()

# Test package

Expand Down Expand Up @@ -331,6 +340,7 @@ message(STATUS "Plugin Xlat : ${PLUGIN_XLAT}")
message(STATUS "")
message(STATUS "[ Tests ]")
message(STATUS "Build tests : ${TESTS_ON}")
message(STATUS "Build python tests : ${PY_TESTS_ON}")
message(STATUS "")
message(STATUS "[ Environment ]")
message(STATUS "CMAKE_GENERATOR : ${CMAKE_GENERATOR}")
Expand All @@ -348,6 +358,9 @@ message(STATUS "Install path : ${CMAKE_INSTALL_PREFIX}")
message(STATUS "CMAKE_SOURCE_DIR : ${CMAKE_SOURCE_DIR}")
message(STATUS "CMAKE_BINARY_DIR : ${CMAKE_BINARY_DIR}")
message(STATUS "")
message(STATUS "Python found : ${PYTHONINTERP_FOUND}")
message(STATUS "Python interpreter : ${PYTHON_EXECUTABLE}")
message(STATUS "")
message(STATUS "[ Git info. ]")
message(STATUS "Git repo url : ${GIT_REPO_URL}")
message(STATUS "Git commit hash : ${GIT_COMMIT_HASH}")
Expand Down
1 change: 0 additions & 1 deletion plugins/xlat/Xlat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ void Xlat::xlattype(FindTemplateTypes *tt, hNodep &h_typeinfo) {
for (auto const &targ : ttargs) {
h_typeinfo->child_list.push_back(
new hNode(targ.getTypeName(), hNode::hdlopsEnum::hType));
const Type * tmpptr = targ.getTypePtr();
(targ.getTypePtr())->dump(os_);
}
}
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lark-parser==0.7.8
pytest==5.0.1
6 changes: 6 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@ foreach(NAME IN LISTS UNIT_TEST_LIST)
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/tests/data/ )
endforeach()

# for pytest tests
if(PY_TESTS_ON)
add_test(NAME python-tests
COMMAND ${PYTHON_EXECUTABLE} -m pytest --color=yes ${CMAKE_CURRENT_SOURCE_DIR}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} )
endif()

104 changes: 104 additions & 0 deletions tests/data/llnl-examples/handcrafted/sreg_hdl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
[
(hModule sreg_0)
[
(hPortin clk)
[
(hType sc_in)
(hType _Bool)
]
(hPortin reset)
[
(hType sc_in)
(hType _Bool)
]
]
[
[
(hProcess mc_io)
[
]
[
(hCStmt )
(hLiteral m_port)
(hLiteral s_port)
(hLiteral s_port)
]
]
]
]
[
(hModule sreg_1)
[
(hPortin clk)
[
(hType sc_in)
(hType _Bool)
]
(hPortin reset)
[
(hType sc_in)
(hType _Bool)
]
]
[
[
(hProcess mc_io)
[
]
[
(hCStmt )
(hLiteral m_port)
(hLiteral s_port)
(hLiteral s_port)
]
]
]
]
[
(hModule sreg_2)
[
(hPortin clk)
[
(hType sc_in)
(hType _Bool)
]
(hPortin reset)
[
(hType sc_in)
(hType _Bool)
]
]
[
[
(hProcess mc_io)
[
]
[
(hCStmt )
(hLiteral m_port)
(hLiteral s_port)
(hLiteral s_port)
]
]
]
]
[
(hModule test_0)
[
(hPortin clk)
[
(hType sc_in)
(hType _Bool)
]
]
[
[
(hProcess mc_test)
[
]
[
(hCStmt )
]
]
]
]
66 changes: 52 additions & 14 deletions tests/verilog-conversion/llnl.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Testing utilities"""
import os
import re
import copy
Expand All @@ -7,27 +8,32 @@

# TODO: fix these paths... and command line options
_systemc_clang_bin_path = os.environ['LLVM_INSTALL_DIR'] + "bin/systemc-clang"
# TODO: we may not get the directory of the source folder, need to add that in the script
_test_file = os.environ['SYSTEMC_CLANG_BUILD_DIR'] + "../systemc-clang/tests/data/llnl-examples/{}.cpp"
# TODO: we may not get the directory of the source folder
# need to add that in the script
_python_convert_template = 'python {} {{}}_hdl.txt'.format(os.environ['SYSTEMC_CLANG_BUILD_DIR'] + "../systemc-clang/plugins/xlat/convert.py")
_test_folder = os.environ['SYSTEMC_CLANG_BUILD_DIR'] + "../systemc-clang/tests/data/llnl-examples/"
_test_cpp_file = _test_folder + "{}.cpp"
_golden_verilog_file = _test_folder + "handcrafted/{}.v"
_golden_sexp_file = _test_folder + "handcrafted/{}_hdl.txt"
_example_header_folder = os.environ['SYSTEMC_CLANG_BUILD_DIR'] + "../systemc-clang/examples/llnl-examples/"
_systemc_clang_argument = ["-I", "{}/systemc/include/".format(os.environ['SYSTEMC']),
_systemc_clang_argument = ["-I", "{}/include/".format(os.environ['SYSTEMC']),
"-std=c++14",
"-I", "{}".format(_example_header_folder),
"-I", "/usr/include/",
"-D__STDC_CONSTANT_MACROS",
"-D__STDC_LIMIT_MACROS",
"-x c++ -w -c"]
_cmdline = [_systemc_clang_bin_path, _test_file, "--"] + _systemc_clang_argument
_cmdline = [_systemc_clang_bin_path, _test_cpp_file, "--"] + _systemc_clang_argument
_parser = None

def get_test_command(module_name):
f = _test_file.format(module_name)
f = _test_cpp_file.format(module_name)
cmd = copy.copy(_cmdline)
cmd[1] = f
return cmd

def get_sexp_filename(module_name):
f = _test_file.format(module_name)
f = _test_cpp_file.format(module_name)
f = re.sub(".cpp$", "_hdl.txt", f)
return f

Expand All @@ -41,28 +47,60 @@ def get_parser():

def do_diff(golden, generated):
diff = difflib.ndiff(golden.splitlines(keepends=True), generated.splitlines(keepends=True))
# diff = filter(lambda x: x[0] == '+' or x[0] == '-', diff)
diff = filter(lambda x: x.startswith('+ ') or x.startswith('- ') or x.startswith('? '), diff)
diff = list(diff)
print('golden:\n', golden)
print('generated:\n', generated)
print("diff output: ")
print(''.join(diff))
return len(diff) == True
return len(diff) == 0

def run_verilog_matched_test(module_name):
def run_sexp_matched_test(module_name, args=None):
parser = get_parser()
args = parser.parse_args()
keep_sexp = args.keep_sexp
keep_v = args.keep_v
golden_standard_file = '{}.v'.format(module_name)
if args is None:
keep_sexp = parser.get_default('keep_sexp')
else:
args_ = parser.parse_args()
keep_sexp = args_.keep_sexp
golden_standard_file = _golden_sexp_file.format(module_name)
with open(golden_standard_file, 'r') as f:
golden = f.read()
sexp = get_sexp_filename(module_name)
try:
cmdline = get_test_command(module_name)
with open(os.devnull, 'wb') as null:
subprocess.run(' '.join(cmdline), shell=True, stdout=null, stderr=null)
with open(sexp, 'r') as f:
res = f.read()
subprocess.run('mv {} .'.format(sexp), shell=True)
return do_diff(golden, res)
finally:
subprocess.run('rm -rf {}'.format(sexp), shell=True)
if not keep_sexp:
subprocess.run('rm -rf ./{}_hdl.txt'.format(module_name), shell=True)

def run_verilog_matched_test(module_name, args=None):
parser = get_parser()
if args is None:
keep_sexp = parser.get_default('keep_sexp')
keep_v = parser.get_default('keep_v')
else:
args_ = parser.parse_args()
keep_sexp = args_.keep_sexp
keep_v = args_.keep_v
golden_standard_file = _golden_verilog_file.format(module_name)
with open(golden_standard_file, 'r') as f:
golden = f.read()
sexp = get_sexp_filename(module_name)
try:
cmdline = get_test_command(module_name)
print(' '.join(cmdline))
with open(os.devnull, 'wb') as null:
# suppressing output, assuming we are sucessfull
subprocess.run(' '.join(cmdline), shell=True, stdout=null, stderr=null)

subprocess.run('mv {} .'.format(sexp), shell=True)
res = subprocess.check_output('python convert.small.py {}_hdl.txt'.format(module_name), shell=True, stderr=null)
res = subprocess.check_output(_python_convert_template.format(module_name), shell=True, stderr=null)
res = res.decode('utf-8')
return do_diff(golden, res)
finally:
Expand Down
4 changes: 0 additions & 4 deletions tests/verilog-conversion/sreg.py

This file was deleted.

16 changes: 16 additions & 0 deletions tests/verilog-conversion/test_sreg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os
import sys
import pytest
from llnl import run_verilog_matched_test, run_sexp_matched_test

@pytest.mark.xfail(reason="the golden standard now is incorrect")
def test_sreg_sexp(args=None):
assert run_sexp_matched_test('sreg', args), "sreg sexp should match golden standard"

@pytest.mark.skip(reason="no golden standard available for now")
def test_sreg_verilog(args=None):
assert run_verilog_matched_test('sreg', args), "sreg verilog should match golden standard"

if __name__ == '__main__':
test_sreg_verilog(args)
test_sreg_sexp(args)

0 comments on commit 875a966

Please sign in to comment.