sc_main_python.cc revision 13404:5da37c38d749
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#include <cstring>
31#include <string>
32
33#include "base/fiber.hh"
34#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
106