gem5  v20.1.0.0
sc_main_python.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Google, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met: redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer;
8  * redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution;
11  * neither the name of the copyright holders nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 // This should be on top since it is including python headers
29 #include "systemc/core/python.hh"
30 
31 #include <cstring>
32 #include <string>
33 
34 #include "base/fiber.hh"
35 #include "base/logging.hh"
37 
38 namespace
39 {
40 
41 // This wrapper adapts the python version of sc_main to the c++ version.
42 void
43 sc_main(pybind11::args args)
44 {
45  panic_if(::sc_gem5::scMainFiber.called(),
46  "sc_main called more than once.");
47 
48  int argc = args.size();
49  char **argv = new char *[argc];
50 
51  // Initialize all the argvs to NULL so we can delete [] them
52  // unconditionally.
53  for (int idx = 0; idx < argc; idx++)
54  argv[idx] = NULL;
55 
56  // Attempt to convert all the arguments to strings. If that fails, clean
57  // up after ourselves. Also don't count this as a call to sc_main since
58  // we never got to the c++ version of that function.
59  try {
60  for (int idx = 0; idx < argc; idx++) {
61  std::string arg = args[idx].cast<std::string>();
62  argv[idx] = new char[arg.length() + 1];
63  strcpy(argv[idx], arg.c_str());
64  }
65  } catch (...) {
66  // If that didn't work for some reason (probably a conversion error)
67  // blow away argv and argc and pass on the exception.
68  for (int idx = 0; idx < argc; idx++)
69  delete [] argv[idx];
70  delete [] argv;
71  argc = 0;
72  throw;
73  }
74 
75  ::sc_gem5::scMainFiber.setArgs(argc, argv);
77 }
78 
79 int
80 sc_main_result_code()
81 {
83 }
84 
85 std::string
86 sc_main_result_str()
87 {
89 }
90 
91 // Make our sc_main wrapper available in the internal _m5 python module under
92 // the systemc submodule.
93 
94 struct InstallScMain : public ::sc_gem5::PythonInitFunc
95 {
96  void
97  run(pybind11::module &systemc) override
98  {
99  systemc.def("sc_main", &sc_main);
100  systemc.def("sc_main_result_code", &sc_main_result_code);
101  systemc.def("sc_main_result_str", &sc_main_result_str);
102  }
103 } installScMain;
104 
105 } // anonymous namespace
sc_gem5::ScMainFiber::resultInt
int resultInt()
Definition: sc_main_fiber.hh:53
sc_main_fiber.hh
sc_gem5::PythonInitFunc::run
virtual void run(pybind11::module &systemc)=0
sc_gem5::ScMainFiber::resultStr
std::string resultStr()
Definition: sc_main_fiber.hh:52
python.hh
sc_gem5::ScMainFiber::setArgs
void setArgs(int new_argc, char **new_argv)
Definition: sc_main_fiber.hh:57
Fiber::run
void run()
Start executing the fiber represented by this object.
Definition: fiber.cc:163
sc_gem5::scMainFiber
ScMainFiber scMainFiber
Definition: sc_main_fiber.cc:78
panic_if
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:197
sc_main
int sc_main(int argc, char *argv[])
logging.hh
fiber.hh
sc_gem5::PythonInitFunc
Definition: python.hh:45

Generated on Wed Sep 30 2020 14:02:14 for gem5 by doxygen 1.8.17