forked from anikau31/systemc-clang
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: new tool driver with sexp conversion
- Loading branch information
1 parent
ec9446d
commit 80f615b
Showing
2 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
"""Driver for using systemc-clang tool in python""" | ||
import os | ||
import re | ||
import subprocess | ||
from pathlib import Path | ||
|
||
|
||
class SystemCClangDriver(object): | ||
"""A clang driver class for running systemc-clang binaries | ||
""" | ||
SYSTEMC_CLANG_BIN_PATH = os.environ['LLVM_INSTALL_DIR'] + "/bin/systemc-clang" | ||
PYTHON_CONVERT_TEMPLATE = 'python {}'.format(os.environ['SYSTEMC_CLANG_BUILD_DIR'] + "../systemc-clang/plugins/xlat/convert.py") | ||
SYSTEMC_CLANG_ARGUMENTS = [ | ||
"-I", "{}/include/".format(os.environ['SYSTEMC']), | ||
"-std=c++14", | ||
"-I", "/usr/include/", | ||
"-D__STDC_CONSTANT_MACROS", | ||
"-D__STDC_LIMIT_MACROS", | ||
"-x c++ -w -c" | ||
] | ||
|
||
|
||
|
||
""" | ||
Extra args are positional and non positional arguments, for example, | ||
extra header etc | ||
""" | ||
def systemc_clang_commandline(self, filename): | ||
args = SystemCClangDriver.SYSTEMC_CLANG_ARGUMENTS + self.conf.positional_arguments | ||
return [ | ||
SystemCClangDriver.SYSTEMC_CLANG_BIN_PATH, | ||
filename, | ||
'--' | ||
] + args + [ | ||
# for the containing folder of the cpp/hpp | ||
"-I", str(Path(filename).parent) | ||
] | ||
|
||
def __init__(self, conf, *args, **kwargs): | ||
self.conf=conf | ||
pass | ||
|
||
""" | ||
Takes _hdl.txt as input, generate .v | ||
""" | ||
def generate_verilog_from_sexp(self, path): | ||
pass | ||
|
||
""" | ||
Takes .cpp as input, generate _hdl.txt | ||
""" | ||
def generate_sexp(self, path, output_folder, keep_sexp=False, verbose=False): | ||
cmdline = self.systemc_clang_commandline(filename=path) | ||
if path.endswith('.cpp'): | ||
sexp_loc = re.sub(".cpp$", "_hdl.txt", path) | ||
elif path.endswith('.hpp'): | ||
sexp_loc = re.sub(".hpp$", "_hdl.txt", path) | ||
else: | ||
raise RuntimeError('Filename should end with .cpp or .hpp') | ||
|
||
if os.path.isfile(sexp_loc): | ||
raise RuntimeError('File to generate: {} exists'.format(sexp_loc)) | ||
|
||
sexp_filename = os.path.basename(sexp_loc) | ||
output_filename = '{}/{}'.format(output_folder, sexp_filename) | ||
if os.path.isfile(output_filename): | ||
raise RuntimeError('File to generate: {} exists'.format(output_filename)) | ||
|
||
if verbose: | ||
print('path: ', path) | ||
print('sexp_loc: ', sexp_loc) | ||
print('sexp_filename: ', sexp_filename) | ||
print('output_filename: ', output_filename) | ||
|
||
try: | ||
if verbose: | ||
subprocess.run(' '.join(cmdline), shell=True) | ||
else: | ||
with open(os.devnull, 'wb') as null: | ||
subprocess.run(' '.join(cmdline), shell=True, | ||
stdout=null, | ||
stderr=null) | ||
subprocess.run('mv {} {}'.format(sexp_loc, output_folder), shell=True) | ||
finally: | ||
# we remove the generated file anyway | ||
if not keep_sexp: | ||
subprocess.run('rm {}'.format(output_filename), shell=True) | ||
# Currently XLAT will generate the file in a fixed location | ||
if keep_sexp and os.path.normpath(sexp_loc) != os.path.normpath(output_filename): | ||
subprocess.run('rm -f {}'.format(sexp_loc), shell=True) | ||
|
||
""" | ||
Takes .cpp as input, generate .v | ||
""" | ||
def generate_verilog(self, path, output_folder): | ||
generate_sexp(path) | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from driver import * | ||
import tempfile | ||
import contextlib | ||
|
||
class TestingConfigurations(object): | ||
"""parameters in this configuration might change""" | ||
|
||
@property | ||
def extra_header_folders(self): | ||
# SystemC Clang build llnl-examples | ||
return self.header_folders | ||
|
||
@property | ||
def positional_arguments(self): | ||
headers = [] | ||
extra_header_folders = self.extra_header_folders | ||
for h in extra_header_folders: | ||
headers.extend(["-I", h]) | ||
return headers | ||
|
||
def __init__(self, header_folders): | ||
self.header_folders = header_folders | ||
|
||
class LLNLExampleTestingConfigurations(TestingConfigurations): | ||
|
||
def __init__(self, header_folders=None): | ||
if header_folders is None: | ||
header_folders = [] | ||
this_folders = header_folders + [ | ||
'/home/allen/working/systemc-clang/examples/llnl-examples/' | ||
] | ||
super(LLNLExampleTestingConfigurations, self).__init__(this_folders) | ||
|
||
@contextlib.contextmanager | ||
def make_temp_directory(): | ||
temp_dir = tempfile.mkdtemp() | ||
try: | ||
yield temp_dir | ||
finally: | ||
shutil.rmtree(temp_dir) | ||
|
||
def main(): | ||
conf = LLNLExampleTestingConfigurations() | ||
driver = SystemCClangDriver(conf=conf) | ||
filename='~/working/systemc-clang/tests/data/verilog-conversion/llnl-examples/sreg.cpp' | ||
# print(' '.join(driver.systemc_clang_commandline(filename=filename))) | ||
tempfolder = tempfile.mkdtemp() | ||
driver.generate_sexp(path=filename, output_folder='.', verbose=True, keep_sexp=True) | ||
|
||
if __name__ == '__main__': | ||
main() | ||
|