sc_main.cc revision 12860:6051cef4adec
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
32#include "base/logging.hh"
33#include "python/pybind11/pybind.hh"
34#include "sim/init.hh"
35#include "systemc/ext/core/sc_main.hh"
36
37// A default version of this function in case one isn't otherwise defined.
38// This ensures everything will link properly whether or not the user defined
39// a custom sc_main function. If they didn't but still try to call it, throw
40// an error and die.
41[[gnu::weak]] int
42sc_main(int argc, char *argv[])
43{
44    // If python attempts to call sc_main but no sc_main was defined...
45    fatal("sc_main called but not defined.\n");
46}
47
48namespace sc_core
49{
50
51namespace
52{
53
54bool scMainCalled = false;
55
56int _argc = 0;
57char **_argv = NULL;
58
59// This wrapper adapts the python version of sc_main to the c++ version.
60void
61sc_main(pybind11::args args)
62{
63    panic_if(scMainCalled, "sc_main called more than once.");
64
65    _argc = args.size();
66    _argv = new char *[_argc];
67
68    // Initialize all the _argvs to NULL so we can delete [] them
69    // unconditionally.
70    for (int idx = 0; idx < _argc; idx++)
71        _argv[idx] = NULL;
72
73    // Attempt to convert all the arguments to strings. If that fails, clean
74    // up after ourselves. Also don't count this as a call to sc_main since
75    // we never got to the c++ version of that function.
76    try {
77        for (int idx = 0; idx < _argc; idx++) {
78            std::string arg = args[idx].cast<std::string>();
79            _argv[idx] = new char[arg.length() + 1];
80            strcpy(_argv[idx], arg.c_str());
81        }
82    } catch (...) {
83        // If that didn't work for some reason (probably a conversion error)
84        // blow away _argv and _argc and pass on the exception.
85        for (int idx = 0; idx < _argc; idx++)
86            delete [] _argv[idx];
87        delete [] _argv;
88        _argc = 0;
89        throw;
90    }
91
92    // At this point we're going to call the c++ sc_main, so we can't try
93    // again later.
94    scMainCalled = true;
95
96    //TODO Start a new fiber to call sc_main from.
97    ::sc_main(_argc, _argv);
98}
99
100// Make our sc_main wrapper available in the internal _m5 python module under
101// the systemc submodule.
102void
103systemc_pybind(pybind11::module &m_internal)
104{
105    pybind11::module m = m_internal.def_submodule("systemc");
106    m.def("sc_main", &sc_main);
107}
108EmbeddedPyBind embed_("systemc", &systemc_pybind);
109
110} // anonymous namespace
111
112int
113sc_argc()
114{
115    return _argc;
116}
117
118const char *const *
119sc_argv()
120{
121    return _argv;
122}
123
124void
125sc_start()
126{
127    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
128}
129
130void
131sc_pause()
132{
133    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
134}
135
136void
137sc_start(const sc_time &time, sc_starvation_policy p)
138{
139    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
140}
141
142void
143sc_set_stop_mode(sc_stop_mode mode)
144{
145    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
146}
147
148sc_stop_mode
149sc_get_stop_mode()
150{
151    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
152    return SC_STOP_FINISH_DELTA;
153}
154
155void
156sc_stop()
157{
158    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
159}
160
161const sc_time &
162sc_time_stamp()
163{
164    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
165    return *(sc_time *)nullptr;
166}
167
168sc_dt::uint64
169sc_delta_count()
170{
171    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
172    return 0;
173}
174
175bool
176sc_is_running()
177{
178    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
179    return false;
180}
181
182bool
183sc_pending_activity_at_current_time()
184{
185    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
186    return false;
187}
188
189bool
190sc_pending_activity_at_future_time()
191{
192    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
193    return false;
194}
195
196bool
197sc_pending_activity()
198{
199    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
200    return false;
201}
202
203sc_time
204sc_time_to_pending_activity()
205{
206    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
207    return sc_time();
208}
209
210sc_status
211sc_get_status()
212{
213    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
214    return SC_ELABORATION;
215}
216
217} // namespace sc_core
218