python,sim,util: Move EmbeddedPython into it's own file.

By separating out this utility class, we make it possible to build
embedded python modules into other binarys without dragging along lots
of other, unrelated gem5 dependencies.

Also, move the class from sim/init.hh (which is a largely unrelated
name) to python/embedded.hh which much more directly describes what that
file contains.

Change-Id: Ia83439144893ad8401a5d51003e2686d9c9b2d7b
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/49418
Reviewed-by: Gabe Black <gabe.black@gmail.com>
Maintainer: Gabe Black <gabe.black@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
diff --git a/build_tools/marshal.py b/build_tools/marshal.py
index a2af1b0..9c2964b 100644
--- a/build_tools/marshal.py
+++ b/build_tools/marshal.py
@@ -77,7 +77,7 @@
 
 code = code_formatter()
 code('''\
-#include "sim/init.hh"
+#include "python/embedded.hh"
 
 namespace gem5
 {
diff --git a/src/python/SConscript b/src/python/SConscript
index 1fab974..c9cd2d4 100644
--- a/src/python/SConscript
+++ b/src/python/SConscript
@@ -253,6 +253,7 @@
 PySource('m5.ext.pystats', 'm5/ext/pystats/jsonloader.py')
 PySource('m5.stats', 'm5/stats/gem5stats.py')
 
+Source('embedded.cc', add_tags='python')
 Source('importer.cc', add_tags='python')
 cc, hh = env.Blob('m5ImporterCode', 'importer.py')
 Source(cc, add_tags='python')
diff --git a/src/python/embedded.cc b/src/python/embedded.cc
new file mode 100644
index 0000000..27bf770
--- /dev/null
+++ b/src/python/embedded.cc
@@ -0,0 +1,111 @@
+/*
+ * Copyright (c) 2012, 2017 ARM Limited
+ * All rights reserved
+ *
+ * The license below extends only to copyright in the software and shall
+ * not be construed as granting a license to any other intellectual
+ * property including but not limited to intellectual property relating
+ * to a hardware implementation of the functionality of the software
+ * licensed hereunder.  You may use the software subject to the license
+ * terms below provided that you ensure that this notice is replicated
+ * unmodified and in its entirety in all distributions of the software,
+ * modified or unmodified, in source code or in binary form.
+ *
+ * Copyright (c) 2000-2005 The Regents of The University of Michigan
+ * Copyright (c) 2008 The Hewlett-Packard Development Company
+ * All rights reserved.
+ *
+ * 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.
+ */
+
+#include "pybind11/embed.h"
+
+#include "python/embedded.hh"
+
+#include <zlib.h>
+
+#include <list>
+
+#include "base/logging.hh"
+
+namespace py = pybind11;
+
+namespace gem5
+{
+
+EmbeddedPython::EmbeddedPython(const char *abspath, const char *modpath,
+        const unsigned char *code, int zlen, int len)
+    : abspath(abspath), modpath(modpath), code(code), zlen(zlen), len(len)
+{
+    getList().push_back(this);
+}
+
+std::list<EmbeddedPython *> &
+EmbeddedPython::getList()
+{
+    static std::list<EmbeddedPython *> the_list;
+    return the_list;
+}
+
+/*
+ * Uncompress and unmarshal the code object stored in the
+ * EmbeddedPython
+ */
+py::object
+EmbeddedPython::getCode() const
+{
+    Bytef marshalled[len];
+    uLongf unzlen = len;
+    int ret = uncompress(marshalled, &unzlen, (const Bytef *)code, zlen);
+    if (ret != Z_OK)
+        panic("Could not uncompress code: %s\n", zError(ret));
+    assert(unzlen == (uLongf)len);
+
+    auto marshal = py::module_::import("marshal");
+    return marshal.attr("loads")(py::bytes((char *)marshalled, len));
+}
+
+bool
+EmbeddedPython::addModule() const
+{
+    auto importer = py::module_::import("importer");
+    importer.attr("add_module")(abspath, modpath, getCode());
+    return true;
+}
+
+/*
+ * Load and initialize all of the python parts of M5.
+ */
+int
+EmbeddedPython::initAll()
+{
+    // Load the embedded python files into the embedded python importer.
+    for (auto *embedded: getList()) {
+        if (!embedded->addModule())
+            return 1;
+    }
+    return 0;
+}
+
+} // namespace gem5
diff --git a/src/python/embedded.hh b/src/python/embedded.hh
new file mode 100644
index 0000000..574856b
--- /dev/null
+++ b/src/python/embedded.hh
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2017 ARM Limited
+ * All rights reserved
+ *
+ * The license below extends only to copyright in the software and shall
+ * not be construed as granting a license to any other intellectual
+ * property including but not limited to intellectual property relating
+ * to a hardware implementation of the functionality of the software
+ * licensed hereunder.  You may use the software subject to the license
+ * terms below provided that you ensure that this notice is replicated
+ * unmodified and in its entirety in all distributions of the software,
+ * modified or unmodified, in source code or in binary form.
+ *
+ * Copyright (c) 2008 The Hewlett-Packard Development Company
+ * All rights reserved.
+ *
+ * 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.
+ */
+
+#ifndef __PYTHON_EMBEDDED_HH__
+#define __PYTHON_EMBEDDED_HH__
+
+#include "pybind11/pybind11.h"
+
+#include <inttypes.h>
+
+#include <list>
+
+namespace gem5
+{
+
+/*
+ * Data structure describing an embedded python file.
+ */
+struct EmbeddedPython
+{
+    const char *abspath;
+    const char *modpath;
+    const uint8_t *code;
+    int zlen;
+    int len;
+
+    EmbeddedPython(const char *abspath, const char *modpath,
+            const uint8_t *code, int zlen, int len);
+
+    pybind11::object getCode() const;
+    bool addModule() const;
+
+    static std::list<EmbeddedPython *> &getList();
+    static int initAll();
+};
+
+} // namespace gem5
+
+#endif // __PYTHON_EMBEDDED_HH__
diff --git a/src/python/importer.cc b/src/python/importer.cc
index 7d1f7dc..2d10e96 100644
--- a/src/python/importer.cc
+++ b/src/python/importer.cc
@@ -29,7 +29,7 @@
 #include "pybind11/pybind11.h"
 #include "python/m5ImporterCode.hh"
 
-#include "sim/init.hh"
+#include "python/embedded.hh"
 
 namespace py = pybind11;
 
diff --git a/src/sim/init.cc b/src/sim/init.cc
index c8a545f..8c0d1aa 100644
--- a/src/sim/init.cc
+++ b/src/sim/init.cc
@@ -43,16 +43,10 @@
 
 #include "sim/init.hh"
 
-#include <zlib.h>
-
-#include <list>
+#include <map>
 #include <string>
-#include <vector>
 
-#include "base/compiler.hh"
 #include "base/cprintf.hh"
-#include "base/logging.hh"
-#include "base/types.hh"
 #include "python/pybind11/pybind.hh"
 
 namespace py = pybind11;
@@ -60,60 +54,6 @@
 namespace gem5
 {
 
-EmbeddedPython::EmbeddedPython(const char *abspath, const char *modpath,
-        const unsigned char *code, int zlen, int len)
-    : abspath(abspath), modpath(modpath), code(code), zlen(zlen), len(len)
-{
-    getList().push_back(this);
-}
-
-std::list<EmbeddedPython *> &
-EmbeddedPython::getList()
-{
-    static std::list<EmbeddedPython *> the_list;
-    return the_list;
-}
-
-/*
- * Uncompress and unmarshal the code object stored in the
- * EmbeddedPython
- */
-py::object
-EmbeddedPython::getCode() const
-{
-    Bytef marshalled[len];
-    uLongf unzlen = len;
-    int ret = uncompress(marshalled, &unzlen, (const Bytef *)code, zlen);
-    if (ret != Z_OK)
-        panic("Could not uncompress code: %s\n", zError(ret));
-    assert(unzlen == (uLongf)len);
-
-    auto marshal = py::module_::import("marshal");
-    return marshal.attr("loads")(py::bytes((char *)marshalled, len));
-}
-
-bool
-EmbeddedPython::addModule() const
-{
-    auto importer = py::module_::import("importer");
-    importer.attr("add_module")(abspath, modpath, getCode());
-    return true;
-}
-
-/*
- * Load and initialize all of the python parts of M5.
- */
-int
-EmbeddedPython::initAll()
-{
-    // Load the embedded python files into the embedded python importer.
-    for (auto *embedded: getList()) {
-        if (!embedded->addModule())
-            return 1;
-    }
-    return 0;
-}
-
 EmbeddedPyBind::EmbeddedPyBind(const char *_name,
                                void (*init_func)(py::module_ &),
                                const char *_base)
diff --git a/src/sim/init.hh b/src/sim/init.hh
index 95c597b..9e7158a 100644
--- a/src/sim/init.hh
+++ b/src/sim/init.hh
@@ -43,36 +43,12 @@
 
 #include "pybind11/pybind11.h"
 
-#include <list>
 #include <map>
 #include <string>
 
-#include <inttypes.h>
-
 namespace gem5
 {
 
-/*
- * Data structure describing an embedded python file.
- */
-struct EmbeddedPython
-{
-    const char *abspath;
-    const char *modpath;
-    const uint8_t *code;
-    int zlen;
-    int len;
-
-    EmbeddedPython(const char *abspath, const char *modpath,
-            const uint8_t *code, int zlen, int len);
-
-    pybind11::object getCode() const;
-    bool addModule() const;
-
-    static std::list<EmbeddedPython *> &getList();
-    static int initAll();
-};
-
 class EmbeddedPyBind
 {
   public:
diff --git a/src/sim/main.cc b/src/sim/main.cc
index 0d9bcb7..c44531e 100644
--- a/src/sim/main.cc
+++ b/src/sim/main.cc
@@ -33,7 +33,7 @@
 #include "pybind11/embed.h"
 #include "pybind11/pybind11.h"
 
-#include "sim/init.hh"
+#include "python/embedded.hh"
 #include "sim/init_signals.hh"
 
 using namespace gem5;