sc_main_python.cc (13404:5da37c38d749) sc_main_python.cc (13411:bd873635bba6)
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 * Authors: Gabe Black
28 */
29
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 * Authors: Gabe Black
28 */
29
30// This should be on top since it is including python headers
31#include "systemc/core/python.hh"
32
30#include <cstring>
31#include <string>
32
33#include "base/fiber.hh"
34#include "base/logging.hh"
33#include <cstring>
34#include <string>
35
36#include "base/fiber.hh"
37#include "base/logging.hh"
35#include "systemc/core/python.hh"
36#include "systemc/core/sc_main_fiber.hh"
37
38namespace
39{
40
41// This wrapper adapts the python version of sc_main to the c++ version.
42void
43sc_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);
76 ::sc_gem5::scMainFiber.run();
77}
78
79int
80sc_main_result_code()
81{
82 return ::sc_gem5::scMainFiber.resultInt();
83}
84
85std::string
86sc_main_result_str()
87{
88 return ::sc_gem5::scMainFiber.resultStr();
89}
90
91// Make our sc_main wrapper available in the internal _m5 python module under
92// the systemc submodule.
93
94struct 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
38#include "systemc/core/sc_main_fiber.hh"
39
40namespace
41{
42
43// This wrapper adapts the python version of sc_main to the c++ version.
44void
45sc_main(pybind11::args args)
46{
47 panic_if(::sc_gem5::scMainFiber.called(),
48 "sc_main called more than once.");
49
50 int argc = args.size();
51 char **argv = new char *[argc];
52
53 // Initialize all the argvs to NULL so we can delete [] them
54 // unconditionally.
55 for (int idx = 0; idx < argc; idx++)
56 argv[idx] = NULL;
57
58 // Attempt to convert all the arguments to strings. If that fails, clean
59 // up after ourselves. Also don't count this as a call to sc_main since
60 // we never got to the c++ version of that function.
61 try {
62 for (int idx = 0; idx < argc; idx++) {
63 std::string arg = args[idx].cast<std::string>();
64 argv[idx] = new char[arg.length() + 1];
65 strcpy(argv[idx], arg.c_str());
66 }
67 } catch (...) {
68 // If that didn't work for some reason (probably a conversion error)
69 // blow away argv and argc and pass on the exception.
70 for (int idx = 0; idx < argc; idx++)
71 delete [] argv[idx];
72 delete [] argv;
73 argc = 0;
74 throw;
75 }
76
77 ::sc_gem5::scMainFiber.setArgs(argc, argv);
78 ::sc_gem5::scMainFiber.run();
79}
80
81int
82sc_main_result_code()
83{
84 return ::sc_gem5::scMainFiber.resultInt();
85}
86
87std::string
88sc_main_result_str()
89{
90 return ::sc_gem5::scMainFiber.resultStr();
91}
92
93// Make our sc_main wrapper available in the internal _m5 python module under
94// the systemc submodule.
95
96struct InstallScMain : public ::sc_gem5::PythonInitFunc
97{
98 void
99 run(pybind11::module &systemc) override
100 {
101 systemc.def("sc_main", &sc_main);
102 systemc.def("sc_main_result_code", &sc_main_result_code);
103 systemc.def("sc_main_result_str", &sc_main_result_str);
104 }
105} installScMain;
106
107} // anonymous namespace