Added pythonization of some SystemC classes and cleanup

This commit is contained in:
Eyck Jentzsch 2019-01-01 14:48:37 +01:00
parent 796d8bf69f
commit 3f7fbba0aa
3 changed files with 35 additions and 14 deletions

View File

@ -1,11 +1,9 @@
import json import json
import cppyy import cppyy
import os.path import os.path
import os
import sys import sys
import tempfile import tempfile
import conans.client.conan_api as conan import conans.client.conan_api as conan
from conans.client.command import main
from contextlib import (redirect_stdout, redirect_stderr) from contextlib import (redirect_stdout, redirect_stderr)
import io import io
@ -54,7 +52,7 @@ def read_config_from_conan(conanfile):
# set include pathes and load libraries # set include pathes and load libraries
for d in data['dependencies']: for d in data['dependencies']:
for p in d['include_paths']: for p in d['include_paths']:
add_sys_include_path(p) add_sys_include_path(p)
if d['name'] == 'SystemC': if d['name'] == 'SystemC':
for l in d['lib_paths']: for l in d['lib_paths']:
if os.path.exists(l+'/'+'libsystemc.so'): if os.path.exists(l+'/'+'libsystemc.so'):
@ -94,8 +92,29 @@ def add_sys_include_path(incl):
sysIncludeDirs.add(incl) sysIncludeDirs.add(incl)
cppyy.add_include_path(incl) cppyy.add_include_path(incl)
# prepare a pythonizor
def _pythonizor(klass, name):
# A pythonizor receives the freshly prepared bound C++ class, and a name stripped down to
# the namespace the pythonizor is applied. Also accessible are klass.__name__ (for the
# Python name) and klass.__cppname__ (for the C++ name)
if name == 'sc_time':
klass.__repr__ = lambda self: repr(self.to_string())
klass.__str__ = lambda self: self.to_string()
elif name in ['sc_object', 'sc_module']:
klass.__repr__ = lambda self: repr(self.name())
elif len(name) > 8 and name[:7] == 'sc_port<':
klass.__repr__ = lambda self: repr(self.name())
elif len(name) > 10 and name[:9] == 'sc_export<':
klass.__repr__ = lambda self: repr(self.name())
# else:
# print('not pythonizing', name)
# install the pythonizor as a callback on namespace 'Math' (default is the global namespace)
cppyy.py.add_pythonization(_pythonizor, 'sc_core')
# reflection methods # reflection methods
def get_members(object): def get_members(sc_object):
def is_cpp_data_type(name, module): def is_cpp_data_type(name, module):
matches = [x for x in ['int', 'char', 'float', 'double'] if name == x] matches = [x for x in ['int', 'char', 'float', 'double'] if name == x]
if len(matches) > 0 or module[:10] == "cppyy.gbl.": if len(matches) > 0 or module[:10] == "cppyy.gbl.":
@ -103,11 +122,11 @@ def get_members(object):
else: else:
return False return False
members = [(e, getattr(object, e)) for e in dir(object)] members = [(e, getattr(sc_object, e)) for e in dir(sc_object)]
return [(k,v) for k,v in members if is_cpp_data_type(type(v).__name__, type(v).__module__)] return [(k,v) for k,v in members if is_cpp_data_type(type(v).__name__, type(v).__module__)]
def get_methods(object): def get_methods(sc_object):
members = [(e, getattr(object, e)) for e in dir(object)] members = [(e, getattr(sc_object, e)) for e in dir(sc_object)]
return [(k,v) for k,v in members if type(v).__name__=='CPPOverload'] return [(k,v) for k,v in members if type(v).__name__=='CPPOverload']
def get_ports(module): def get_ports(module):

View File

@ -6,7 +6,7 @@ def readme():
return f.read() return f.read()
setup(name='PySysC-UI', setup(name='PySysC',
version='0.1', version='0.1',
description='Python SystemC binding', description='Python SystemC binding',
long_description=readme(), long_description=readme(),

View File

@ -3,11 +3,12 @@ import cppyy
import os.path import os.path
import pysysc as scpy import pysysc as scpy
from cppyy import gbl as cpp from cppyy import gbl as cpp
conan_err = scpy.read_config_from_conan('../conanfile.txt') proj_home='../../PySysC-SC'
conan_res = scpy.read_config_from_conan(os.path.join(proj_home, 'conanfile.txt'))
scpy.load_systemc() scpy.load_systemc()
scpy.add_include_path('..') scpy.add_include_path(os.path.join(proj_home, 'components'))
scpy.add_library('components.h', '../build/Debug/lib/libcomponents.so') scpy.add_library('components.h', os.path.join(proj_home, 'build/Debug/lib/libcomponents.so'))
initiator = cpp.Initiator(cpp.sc_core.sc_module_name("initiator")) initiator = cpp.Initiator(cpp.sc_core.sc_module_name("initiator"))
memories = [cpp.Memory(cpp.sc_core.sc_module_name(name)) for name in ["mem0", "mem1", "mem2", "mem3"]] memories = [cpp.Memory(cpp.sc_core.sc_module_name(name)) for name in ["mem0", "mem1", "mem2", "mem3"]]
@ -24,7 +25,8 @@ childs = scpy.get_submodules(initiator)
initiator.socket.bind(router.target_socket) initiator.socket.bind(router.target_socket)
for idx,m in enumerate(memories): for idx,m in enumerate(memories):
router.initiator_socket.at(idx).bind(m.socket) router.initiator_socket.at(idx).bind(m.socket)
time = cpp.sc_core.sc_time_stamp()
print(intors[0].name(), "connects to sc_object:", isinstance(intors[0][0], cpp.sc_core.sc_object))
cpp.sc_core.sc_in_action=True cpp.sc_core.sc_in_action=True
cpp.sc_core.sc_start() cpp.sc_core.sc_start()
print("Done") print("Done at", cpp.sc_core.sc_time_stamp())