From 33e3365bf9876b09362897a3ad9941090e3aca9c Mon Sep 17 00:00:00 2001 From: Gabe Black Date: Tue, 7 Aug 2018 03:51:18 -0700 Subject: [PATCH] systemc: Generalize working with the systemc python module from C++. This change pulls the systemc python module creation code out of sc_main and puts it into a more general purpose python.hh and python.cc which can be used by other code to add other entries into that module without having to track that in a central place. This change also adds a mechanism for notifying C++ code that the embedded python interpretter is up and ready to interact with in case it needs to call some python only functionality. An example of that is the code which tracks and then fixes the timescale for the simulator. Change-Id: I9afcd5a089b21d23ebc1b5fdb6f643ae2f7e5f11 Reviewed-on: https://gem5-review.googlesource.com/12069 Reviewed-by: Gabe Black Maintainer: Gabe Black --- src/systemc/core/SConscript | 1 + src/systemc/core/SystemC.py | 8 ++++ src/systemc/core/python.cc | 75 +++++++++++++++++++++++++++++++++++++ src/systemc/core/python.hh | 54 ++++++++++++++++++++++++++ src/systemc/core/sc_main.cc | 16 ++++---- 5 files changed, 147 insertions(+), 7 deletions(-) create mode 100644 src/systemc/core/python.cc create mode 100644 src/systemc/core/python.hh diff --git a/src/systemc/core/SConscript b/src/systemc/core/SConscript index 8996a7de5..76327c860 100644 --- a/src/systemc/core/SConscript +++ b/src/systemc/core/SConscript @@ -37,6 +37,7 @@ if env['USE_SYSTEMC']: Source('object.cc') Source('process.cc') Source('process_types.cc') + Source('python.cc') Source('scheduler.cc') Source('sc_attr.cc') diff --git a/src/systemc/core/SystemC.py b/src/systemc/core/SystemC.py index dff72e2ce..13ef4eb98 100644 --- a/src/systemc/core/SystemC.py +++ b/src/systemc/core/SystemC.py @@ -61,3 +61,11 @@ class SystemC_ScObject(SimObject): locals().update({ method.name: (lambda *a, **k: None) for method in SimObject.cxx_exports }) + +try: + import _m5 +except: + pass +else: + import _m5.systemc + _m5.systemc.python_ready() diff --git a/src/systemc/core/python.cc b/src/systemc/core/python.cc new file mode 100644 index 000000000..99d6cc9a0 --- /dev/null +++ b/src/systemc/core/python.cc @@ -0,0 +1,75 @@ +/* + * Copyright 2018 Google, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer; + * redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution; + * neither the name of the copyright holders nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Authors: Gabe Black + */ + +#include "systemc/core/python.hh" + +#include + +#include "python/pybind11/pybind.hh" +#include "sim/init.hh" + +namespace sc_gem5 +{ + +namespace +{ + +std::vector pythonReadyFuncs; +std::vector pythonInitFuncs; + +void +python_ready(pybind11::args args) +{ + for (auto &func: pythonReadyFuncs) + func->run(); +} + +void +systemc_pybind(pybind11::module &m_internal) +{ + pybind11::module m = m_internal.def_submodule("systemc"); + m.def("python_ready", &python_ready); + for (auto &func: pythonInitFuncs) + func->run(m); +} +EmbeddedPyBind embed_("systemc", &systemc_pybind); + +} // anonymous namespace + +PythonReadyFunc::PythonReadyFunc() +{ + pythonReadyFuncs.push_back(this); +} + +PythonInitFunc::PythonInitFunc() +{ + pythonInitFuncs.push_back(this); +} + +} // namespace sc_gem5 diff --git a/src/systemc/core/python.hh b/src/systemc/core/python.hh new file mode 100644 index 000000000..0d68c5916 --- /dev/null +++ b/src/systemc/core/python.hh @@ -0,0 +1,54 @@ +/* + * Copyright 2018 Google, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer; + * redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution; + * neither the name of the copyright holders nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Authors: Gabe Black + */ + +#ifndef __SYSTEMC_CORE_PYTHON_HH__ +#define __SYSTEMC_CORE_PYTHON_HH__ + +#include "python/pybind11/pybind.hh" + +namespace sc_gem5 +{ + +struct PythonReadyFunc +{ + PythonReadyFunc(); + ~PythonReadyFunc() {} + virtual void run() = 0; +}; + +struct PythonInitFunc +{ + PythonInitFunc(); + ~PythonInitFunc() {} + virtual void run(pybind11::module &systemc) = 0; +}; + +} // namespace sc_gem5 + +#endif //__SYSTEMC_CORE_PYTHON_HH__ diff --git a/src/systemc/core/sc_main.cc b/src/systemc/core/sc_main.cc index 45ca728c3..446b7377d 100644 --- a/src/systemc/core/sc_main.cc +++ b/src/systemc/core/sc_main.cc @@ -32,11 +32,11 @@ #include "base/fiber.hh" #include "base/logging.hh" #include "base/types.hh" -#include "python/pybind11/pybind.hh" #include "sim/core.hh" #include "sim/eventq.hh" #include "sim/init.hh" #include "systemc/core/kernel.hh" +#include "systemc/core/python.hh" #include "systemc/core/scheduler.hh" #include "systemc/ext/core/sc_main.hh" #include "systemc/ext/utils/sc_report_handler.hh" @@ -113,13 +113,15 @@ sc_main(pybind11::args args) // Make our sc_main wrapper available in the internal _m5 python module under // the systemc submodule. -void -systemc_pybind(pybind11::module &m_internal) + +struct InstallScMain : public ::sc_gem5::PythonInitFunc { - pybind11::module m = m_internal.def_submodule("systemc"); - m.def("sc_main", &sc_main); -} -EmbeddedPyBind embed_("systemc", &systemc_pybind); + void + run(pybind11::module &systemc) override + { + systemc.def("sc_main", &sc_main); + } +} installScMain; sc_stop_mode _stop_mode = SC_STOP_FINISH_DELTA; -- 2.30.2