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
33#include <cstring>
34#include <string>
35
36#include "base/fiber.hh"
37#include "base/logging.hh"
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
108