python,util: Pull param struct generation code out of SimObject.

Change-Id: I9f9c3b858a214650f6f07e6127bb316a227982a0
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/49450
Reviewed-by: Gabe Black <gabe.black@gmail.com>
Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com>
Maintainer: Gabe Black <gabe.black@gmail.com>
Maintainer: Andreas Sandberg <andreas.sandberg@arm.com>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Gabe Black
2021-08-19 21:59:22 -07:00
parent f42b198371
commit df540f0dbf
3 changed files with 387 additions and 356 deletions

View File

@@ -708,360 +708,6 @@ class MetaSimObject(type):
def pybind_predecls(cls, code):
code('#include "${{cls.cxx_header}}"')
def params_create_decl(cls, code, python_enabled):
py_class_name = cls.pybind_class
# The 'local' attribute restricts us to the params declared in
# the object itself, not including inherited params (which
# will also be inherited from the base class's param struct
# here). Sort the params based on their key
params = list(map(lambda k_v: k_v[1],
sorted(cls._params.local.items())))
ports = cls._ports.local
# only include pybind if python is enabled in the build
if python_enabled:
code('''#include "pybind11/pybind11.h"
#include "pybind11/stl.h"
#include <type_traits>
#include "base/compiler.hh"
#include "params/$cls.hh"
#include "sim/init.hh"
#include "sim/sim_object.hh"
#include "${{cls.cxx_header}}"
''')
else:
code('''
#include <type_traits>
#include "base/compiler.hh"
#include "params/$cls.hh"
#include "${{cls.cxx_header}}"
''')
# only include the python params code if python is enabled.
if python_enabled:
for param in params:
param.pybind_predecls(code)
code('''namespace py = pybind11;
namespace gem5
{
static void
module_init(py::module_ &m_internal)
{
py::module_ m = m_internal.def_submodule("param_${cls}");
''')
code.indent()
if cls._base:
code('py::class_<${cls}Params, ${{cls._base.type}}Params, ' \
'std::unique_ptr<${{cls}}Params, py::nodelete>>(' \
'm, "${cls}Params")')
else:
code('py::class_<${cls}Params, ' \
'std::unique_ptr<${cls}Params, py::nodelete>>(' \
'm, "${cls}Params")')
code.indent()
if not hasattr(cls, 'abstract') or not cls.abstract:
code('.def(py::init<>())')
code('.def("create", &${cls}Params::create)')
param_exports = cls.cxx_param_exports + [
PyBindProperty(k)
for k, v in sorted(cls._params.local.items())
] + [
PyBindProperty("port_%s_connection_count" % port.name)
for port in ports.values()
]
for exp in param_exports:
exp.export(code, "%sParams" % cls)
code(';')
code()
code.dedent()
bases = []
if 'cxx_base' in cls._value_dict:
# If the c++ base class implied by python inheritance was
# overridden, use that value.
if cls.cxx_base:
bases.append(cls.cxx_base)
elif cls._base:
# If not and if there was a SimObject base, use its c++ class
# as this class' base.
bases.append(cls._base.cxx_class)
# Add in any extra bases that were requested.
bases.extend(cls.cxx_extra_bases)
if bases:
base_str = ", ".join(bases)
code('py::class_<${{cls.cxx_class}}, ${base_str}, ' \
'std::unique_ptr<${{cls.cxx_class}}, py::nodelete>>(' \
'm, "${py_class_name}")')
else:
code('py::class_<${{cls.cxx_class}}, ' \
'std::unique_ptr<${{cls.cxx_class}}, py::nodelete>>(' \
'm, "${py_class_name}")')
code.indent()
for exp in cls.cxx_exports:
exp.export(code, cls.cxx_class)
code(';')
code.dedent()
code()
code.dedent()
code('}')
code()
code('static EmbeddedPyBind '
'embed_obj("${0}", module_init, "${1}");',
cls, cls._base.type if cls._base else "")
code()
code('} // namespace gem5')
# include the create() methods whether or not python is enabled.
if not hasattr(cls, 'abstract') or not cls.abstract:
if 'type' in cls.__dict__:
code()
code('namespace gem5')
code('{')
code()
code('namespace')
code('{')
code()
# If we can't define a default create() method for this params
# struct because the SimObject doesn't have the right
# constructor, use template magic to make it so we're actually
# defining a create method for this class instead.
code('class Dummy${cls}ParamsClass')
code('{')
code(' public:')
code(' ${{cls.cxx_class}} *create() const;')
code('};')
code()
code('template <class CxxClass, class Enable=void>')
code('class Dummy${cls}Shunt;')
code()
# This version directs to the real Params struct and the
# default behavior of create if there's an appropriate
# constructor.
code('template <class CxxClass>')
code('class Dummy${cls}Shunt<CxxClass, std::enable_if_t<')
code(' std::is_constructible_v<CxxClass,')
code(' const ${cls}Params &>>>')
code('{')
code(' public:')
code(' using Params = ${cls}Params;')
code(' static ${{cls.cxx_class}} *')
code(' create(const Params &p)')
code(' {')
code(' return new CxxClass(p);')
code(' }')
code('};')
code()
# This version diverts to the DummyParamsClass and a dummy
# implementation of create if the appropriate constructor does
# not exist.
code('template <class CxxClass>')
code('class Dummy${cls}Shunt<CxxClass, std::enable_if_t<')
code(' !std::is_constructible_v<CxxClass,')
code(' const ${cls}Params &>>>')
code('{')
code(' public:')
code(' using Params = Dummy${cls}ParamsClass;')
code(' static ${{cls.cxx_class}} *')
code(' create(const Params &p)')
code(' {')
code(' return nullptr;')
code(' }')
code('};')
code()
code('} // anonymous namespace')
code()
# An implementation of either the real Params struct's create
# method, or the Dummy one. Either an implementation is
# mandantory since this was shunted off to the dummy class, or
# one is optional which will override this weak version.
code('[[maybe_unused]] ${{cls.cxx_class}} *')
code('Dummy${cls}Shunt<${{cls.cxx_class}}>::Params::create() '
'const')
code('{')
code(' return Dummy${cls}Shunt<${{cls.cxx_class}}>::')
code(' create(*this);')
code('}')
code()
code('} // namespace gem5')
_warned_about_nested_templates = False
# Generate the C++ declaration (.hh file) for this SimObject's
# param struct. Called from src/SConscript.
def cxx_param_decl(cls, code):
# The 'local' attribute restricts us to the params declared in
# the object itself, not including inherited params (which
# will also be inherited from the base class's param struct
# here). Sort the params based on their key
params = list(map(lambda k_v: k_v[1], sorted(cls._params.local.items())))
ports = cls._ports.local
try:
ptypes = [p.ptype for p in params]
except:
print(cls, p, p.ptype_str)
print(params)
raise
class CxxClass(object):
def __init__(self, sig, template_params=[]):
# Split the signature into its constituent parts. This could
# potentially be done with regular expressions, but
# it's simple enough to pick appart a class signature
# manually.
parts = sig.split('<', 1)
base = parts[0]
t_args = []
if len(parts) > 1:
# The signature had template arguments.
text = parts[1].rstrip(' \t\n>')
arg = ''
# Keep track of nesting to avoid splitting on ","s embedded
# in the arguments themselves.
depth = 0
for c in text:
if c == '<':
depth = depth + 1
if depth > 0 and not \
self._warned_about_nested_templates:
self._warned_about_nested_templates = True
print('Nested template argument in cxx_class.'
' This feature is largely untested and '
' may not work.')
elif c == '>':
depth = depth - 1
elif c == ',' and depth == 0:
t_args.append(arg.strip())
arg = ''
else:
arg = arg + c
if arg:
t_args.append(arg.strip())
# Split the non-template part on :: boundaries.
class_path = base.split('::')
# The namespaces are everything except the last part of the
# class path.
self.namespaces = class_path[:-1]
# And the class name is the last part.
self.name = class_path[-1]
self.template_params = template_params
self.template_arguments = []
# Iterate through the template arguments and their values. This
# will likely break if parameter packs are used.
for arg, param in zip(t_args, template_params):
type_keys = ('class', 'typename')
# If a parameter is a type, parse it recursively. Otherwise
# assume it's a constant, and store it verbatim.
if any(param.strip().startswith(kw) for kw in type_keys):
self.template_arguments.append(CxxClass(arg))
else:
self.template_arguments.append(arg)
def declare(self, code):
# First declare any template argument types.
for arg in self.template_arguments:
if isinstance(arg, CxxClass):
arg.declare(code)
# Re-open the target namespace.
for ns in self.namespaces:
code('namespace $ns {')
# If this is a class template...
if self.template_params:
code('template <${{", ".join(self.template_params)}}>')
# The actual class declaration.
code('class ${{self.name}};')
# Close the target namespaces.
for ns in reversed(self.namespaces):
code('} // namespace $ns')
code('''\
#ifndef __PARAMS__${cls}__
#define __PARAMS__${cls}__
''')
# The base SimObject has a couple of params that get
# automatically set from Python without being declared through
# the normal Param mechanism; we slip them in here (needed
# predecls now, actual declarations below)
if cls == SimObject:
code('''#include <string>''')
cxx_class = CxxClass(cls._value_dict['cxx_class'],
cls._value_dict['cxx_template_params'])
# A forward class declaration is sufficient since we are just
# declaring a pointer.
cxx_class.declare(code)
for param in params:
param.cxx_predecls(code)
for port in ports.values():
port.cxx_predecls(code)
code()
if cls._base:
code('#include "params/${{cls._base.type}}.hh"')
code()
for ptype in ptypes:
if issubclass(ptype, Enum):
code('#include "enums/${{ptype.__name__}}.hh"')
code()
code('namespace gem5')
code('{')
code('')
# now generate the actual param struct
code("struct ${cls}Params")
if cls._base:
code(" : public ${{cls._base.type}}Params")
code("{")
if not hasattr(cls, 'abstract') or not cls.abstract:
if 'type' in cls.__dict__:
code(" ${{cls.cxx_type}} create() const;")
code.indent()
if cls == SimObject:
code('''
SimObjectParams() {}
virtual ~SimObjectParams() {}
std::string name;
''')
for param in params:
param.cxx_decl(code)
for port in ports.values():
port.cxx_decl(code)
code.dedent()
code('};')
code()
code('} // namespace gem5')
code()
code('#endif // __PARAMS__${cls}__')
return code
# Generate the C++ declaration/definition files for this SimObject's
# param struct to allow C++ initialisation
def cxx_config_param_file(cls, code, is_header):