summaryrefslogtreecommitdiff
path: root/src/python/m5/params.py
diff options
context:
space:
mode:
authorAndreas Sandberg <andreas.sandberg@arm.com>2017-02-27 13:17:51 +0000
committerAndreas Sandberg <andreas.sandberg@arm.com>2017-05-02 12:37:32 +0000
commit60e6e785f970578eba6dbee9330eaecf514b23c8 (patch)
tree9c47c06d22a39b9467d3d4e35c11849a17df7815 /src/python/m5/params.py
parentba42457254cc362eddc099f22b60d469cc6369e0 (diff)
downloadgem5-60e6e785f970578eba6dbee9330eaecf514b23c8.tar.xz
python: Use PyBind11 instead of SWIG for Python wrappers
Use the PyBind11 wrapping infrastructure instead of SWIG to generate wrappers for functionality that needs to be exported to Python. This has several benefits: * PyBind11 can be redistributed with gem5, which means that we have full control of the version used. This avoid a large number of hard-to-debug SWIG issues we have seen in the past. * PyBind11 doesn't rely on a custom C++ parser, instead it relies on wrappers being explicitly declared in C++. The leads to slightly more boiler-plate code in manually created wrappers, but doesn't doesn't increase the overall code size. A big benefit is that this avoids strange compilation errors when SWIG doesn't understand modern language features. * Unlike SWIG, there is no risk that the wrapper code incorporates incorrect type casts (this has happened on numerous occasions in the past) since these will result in compile-time errors. As a part of this change, the mechanism to define exported methods has been redesigned slightly. New methods can be exported either by declaring them in the SimObject declaration and decorating them with the cxxMethod decorator or by adding an instance of PyBindMethod/PyBindProperty to the cxx_exports class variable. The decorator has the added benefit of making it possible to add a docstring and naming the method's parameters. The new wrappers have the following known issues: * Global events can't be memory managed correctly. This was the case in SWIG as well. Change-Id: I88c5a95b6cf6c32fa9e1ad31dfc08b2e8199a763 Signed-off-by: Andreas Sandberg <andreas.sandberg@arm.com> Reviewed-by: Andreas Hansson <andreas.hansson@arm.com> Reviewed-by: Andrew Bardsley <andrew.bardsley@arm.com> Reviewed-on: https://gem5-review.googlesource.com/2231 Reviewed-by: Tony Gutierrez <anthony.gutierrez@amd.com> Reviewed-by: Pierre-Yves PĂ©neau <pierre-yves.peneau@lirmm.fr> Reviewed-by: Jason Lowe-Power <jason@lowepower.com>
Diffstat (limited to 'src/python/m5/params.py')
-rw-r--r--src/python/m5/params.py93
1 files changed, 64 insertions, 29 deletions
diff --git a/src/python/m5/params.py b/src/python/m5/params.py
index ae2b74a23..506fb8c8d 100644
--- a/src/python/m5/params.py
+++ b/src/python/m5/params.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2012-2014 ARM Limited
+# Copyright (c) 2012-2014, 2017 ARM Limited
# All rights reserved.
#
# The license below extends only to copyright in the software and shall
@@ -102,6 +102,10 @@ class ParamValue(object):
def cxx_predecls(cls, code):
pass
+ @classmethod
+ def pybind_predecls(cls, code):
+ cls.cxx_predecls(code)
+
# Generate the code needed as a prerequisite for including a
# reference to a C++ object of this type in a SWIG .i file.
# Typically generates one or more %import or %include statements.
@@ -222,6 +226,9 @@ class ParamDesc(object):
code('#include <cstddef>')
self.ptype.cxx_predecls(code)
+ def pybind_predecls(self, code):
+ self.ptype.pybind_predecls(code)
+
def swig_predecls(self, code):
self.ptype.swig_predecls(code)
@@ -408,6 +415,10 @@ class VectorParamDesc(ParamDesc):
code('#include <vector>')
self.ptype.cxx_predecls(code)
+ def pybind_predecls(self, code):
+ code('#include <vector>')
+ self.ptype.pybind_predecls(code)
+
def cxx_decl(self, code):
code('std::vector< ${{self.ptype.cxx_type}} > ${{self.name}};')
@@ -792,6 +803,11 @@ class AddrRange(ParamValue):
code('#include "base/addr_range.hh"')
@classmethod
+ def pybind_predecls(cls, code):
+ Addr.pybind_predecls(code)
+ code('#include "base/addr_range.hh"')
+
+ @classmethod
def swig_predecls(cls, code):
Addr.swig_predecls(code)
@@ -941,7 +957,7 @@ class EthernetAddr(ParamValue):
return self
def getValue(self):
- from m5.internal.params import EthAddr
+ from _m5.net import EthAddr
return EthAddr(self.value)
def __str__(self):
@@ -1007,7 +1023,7 @@ class IpAddress(ParamValue):
raise TypeError, "invalid ip address %#08x" % self.ip
def getValue(self):
- from m5.internal.params import IpAddress
+ from _m5.net import IpAddress
return IpAddress(self.ip)
# When initializing an IpNetmask, pass in an existing IpNetmask, a string of
@@ -1086,7 +1102,7 @@ class IpNetmask(IpAddress):
raise TypeError, "invalid netmask %d" % netmask
def getValue(self):
- from m5.internal.params import IpNetmask
+ from _m5.net import IpNetmask
return IpNetmask(self.ip, self.netmask)
# When initializing an IpWithPort, pass in an existing IpWithPort, a string of
@@ -1164,7 +1180,7 @@ class IpWithPort(IpAddress):
raise TypeError, "invalid port %d" % self.port
def getValue(self):
- from m5.internal.params import IpWithPort
+ from _m5.net import IpWithPort
return IpWithPort(self.ip, self.port)
time_formats = [ "%a %b %d %H:%M:%S %Z %Y",
@@ -1224,30 +1240,10 @@ class Time(ParamValue):
return value
def getValue(self):
- from m5.internal.params import tm
-
- c_time = tm()
- py_time = self.value
-
- # UNIX is years since 1900
- c_time.tm_year = py_time.tm_year - 1900;
-
- # Python starts at 1, UNIX starts at 0
- c_time.tm_mon = py_time.tm_mon - 1;
- c_time.tm_mday = py_time.tm_mday;
- c_time.tm_hour = py_time.tm_hour;
- c_time.tm_min = py_time.tm_min;
- c_time.tm_sec = py_time.tm_sec;
+ from _m5.core import tm
+ import calendar
- # Python has 0 as Monday, UNIX is 0 as sunday
- c_time.tm_wday = py_time.tm_wday + 1
- if c_time.tm_wday > 6:
- c_time.tm_wday -= 7;
-
- # Python starts at 1, Unix starts at 0
- c_time.tm_yday = py_time.tm_yday - 1;
-
- return c_time
+ return tm.gmtime(calendar.timegm(self.value))
def __str__(self):
return time.asctime(self.value)
@@ -1375,6 +1371,40 @@ $wrapper $wrapper_name {
code('} // namespace $wrapper_name')
code.dedent(1)
+ def pybind_def(cls, code):
+ name = cls.__name__
+ wrapper_name = cls.wrapper_name
+ enum_name = cls.__name__ if cls.enum_name is None else cls.enum_name
+
+ code('''#include "pybind11/pybind11.h"
+#include "pybind11/stl.h"
+
+#include <sim/init.hh>
+
+namespace py = pybind11;
+
+static void
+module_init(py::module &m_internal)
+{
+ py::module m = m_internal.def_submodule("enum_${name}");
+
+ py::enum_<${wrapper_name}::${enum_name}>(m, "enum_${name}")
+''')
+
+ code.indent()
+ code.indent()
+ for val in cls.vals:
+ code('.value("${val}", ${wrapper_name}::${val})')
+ code('.value("Num_${name}", ${wrapper_name}::Num_${enum_name})')
+ code('.export_values()')
+ code(';')
+ code.dedent()
+
+ code('}')
+ code.dedent()
+ code()
+ code('static EmbeddedPyBind embed_enum("enum_${name}", module_init);')
+
def swig_decl(cls, code):
name = cls.__name__
code('''\
@@ -1435,7 +1465,9 @@ class Enum(ParamValue):
code('}')
def getValue(self):
- return int(self.map[self.value])
+ import m5.internal.params
+ e = getattr(m5.internal.params, "enum_%s" % self.__class__.__name__)
+ return e(self.map[self.value])
def __str__(self):
return self.value
@@ -2040,6 +2072,9 @@ class Port(object):
def cxx_predecls(self, code):
pass
+ def pybind_predecls(self, code):
+ cls.cxx_predecls(self, code)
+
# Declare an unsigned int with the same name as the port, that
# will eventually hold the number of connected ports (and thus the
# number of elements for a VectorPort).