init.cc (11808:f254d8a17da9) init.cc (11988:665cd5f8b52b)
1/*
1/*
2 * Copyright (c) 2012 ARM Limited
2 * Copyright (c) 2012, 2017 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated

--- 40 unchanged lines hidden (view full) ---

51#include <iostream>
52#include <list>
53#include <string>
54
55#include "base/cprintf.hh"
56#include "base/misc.hh"
57#include "base/types.hh"
58#include "config/have_protobuf.hh"
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated

--- 40 unchanged lines hidden (view full) ---

51#include <iostream>
52#include <list>
53#include <string>
54
55#include "base/cprintf.hh"
56#include "base/misc.hh"
57#include "base/types.hh"
58#include "config/have_protobuf.hh"
59#include "python/pybind11/pybind.hh"
59#include "sim/async.hh"
60#include "sim/core.hh"
61
62#if HAVE_PROTOBUF
63#include <google/protobuf/stubs/common.h>
64
65#endif
66
67using namespace std;
60#include "sim/async.hh"
61#include "sim/core.hh"
62
63#if HAVE_PROTOBUF
64#include <google/protobuf/stubs/common.h>
65
66#endif
67
68using namespace std;
69namespace py = pybind11;
68
69// The python library is totally messed up with respect to constness,
70// so make a simple macro to make life a little easier
71#define PyCC(x) (const_cast<char *>(x))
72
73EmbeddedPython *EmbeddedPython::importer = NULL;
74PyObject *EmbeddedPython::importerModule = NULL;
75EmbeddedPython::EmbeddedPython(const char *filename, const char *abspath,

--- 103 unchanged lines hidden (view full) ---

179 strcpy(cstr, i->context.c_str());
180 _Py_PackageContext = cstr;
181 i->initFunc();
182 delete[] cstr;
183 }
184 _Py_PackageContext = old_context;
185}
186
70
71// The python library is totally messed up with respect to constness,
72// so make a simple macro to make life a little easier
73#define PyCC(x) (const_cast<char *>(x))
74
75EmbeddedPython *EmbeddedPython::importer = NULL;
76PyObject *EmbeddedPython::importerModule = NULL;
77EmbeddedPython::EmbeddedPython(const char *filename, const char *abspath,

--- 103 unchanged lines hidden (view full) ---

181 strcpy(cstr, i->context.c_str());
182 _Py_PackageContext = cstr;
183 i->initFunc();
184 delete[] cstr;
185 }
186 _Py_PackageContext = old_context;
187}
188
189EmbeddedPyBind::EmbeddedPyBind(const char *_name,
190 void (*init_func)(py::module &),
191 const char *_base)
192 : initFunc(init_func), registered(false), name(_name), base(_base)
193{
194 getMap()[_name] = this;
195}
196
197EmbeddedPyBind::EmbeddedPyBind(const char *_name,
198 void (*init_func)(py::module &))
199 : initFunc(init_func), registered(false), name(_name), base("")
200{
201 getMap()[_name] = this;
202}
203
204void
205EmbeddedPyBind::init(py::module &m)
206{
207 if (!registered) {
208 initFunc(m);
209 registered = true;
210 } else {
211 cprintf("Warning: %s already registered.\n", name);
212 }
213}
214
215bool
216EmbeddedPyBind::depsReady() const
217{
218 return base.empty() || getMap()[base]->registered;
219}
220
221std::map<std::string, EmbeddedPyBind *> &
222EmbeddedPyBind::getMap()
223{
224 static std::map<std::string, EmbeddedPyBind *> objs;
225 return objs;
226}
227
228void
229EmbeddedPyBind::initAll()
230{
231 std::list<EmbeddedPyBind *> pending;
232
233 py::module m_m5 = py::module("_m5");
234 m_m5.attr("__package__") = py::cast("_m5");
235
236 pybind_init_core(m_m5);
237 pybind_init_debug(m_m5);
238
239 pybind_init_event(m_m5);
240 pybind_init_pyobject(m_m5);
241 pybind_init_stats(m_m5);
242
243 for (auto &kv : getMap()) {
244 auto &obj = kv.second;
245 if (obj->base.empty()) {
246 obj->init(m_m5);
247 } else {
248 pending.push_back(obj);
249 }
250 }
251
252 while (!pending.empty()) {
253 for (auto it = pending.begin(); it != pending.end(); ) {
254 EmbeddedPyBind &obj = **it;
255 if (obj.depsReady()) {
256 obj.init(m_m5);
257 it = pending.erase(it);
258 } else {
259 ++it;
260 }
261 }
262 }
263}
264
187int
188initM5Python()
189{
190 EmbeddedSwig::initAll();
265int
266initM5Python()
267{
268 EmbeddedSwig::initAll();
269 EmbeddedPyBind::initAll();
191 return EmbeddedPython::initAll();
192}
193
194/*
195 * Make the commands array weak so that they can be overridden (used
196 * by unit tests to specify a different python main function.
197 */
198const char * __attribute__((weak)) m5MainCommands[] = {

--- 57 unchanged lines hidden ---
270 return EmbeddedPython::initAll();
271}
272
273/*
274 * Make the commands array weak so that they can be overridden (used
275 * by unit tests to specify a different python main function.
276 */
277const char * __attribute__((weak)) m5MainCommands[] = {

--- 57 unchanged lines hidden ---