sc_main.cc (13317:36c574a4036e) sc_main.cc (13403:cebee63981d3)
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

--- 24 unchanged lines hidden (view full) ---

33#include "base/fiber.hh"
34#include "base/logging.hh"
35#include "base/types.hh"
36#include "sim/core.hh"
37#include "sim/eventq.hh"
38#include "sim/init.hh"
39#include "systemc/core/kernel.hh"
40#include "systemc/core/python.hh"
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

--- 24 unchanged lines hidden (view full) ---

33#include "base/fiber.hh"
34#include "base/logging.hh"
35#include "base/types.hh"
36#include "sim/core.hh"
37#include "sim/eventq.hh"
38#include "sim/init.hh"
39#include "systemc/core/kernel.hh"
40#include "systemc/core/python.hh"
41#include "systemc/core/sc_main_fiber.hh"
41#include "systemc/core/scheduler.hh"
42#include "systemc/ext/core/messages.hh"
43#include "systemc/ext/core/sc_main.hh"
44#include "systemc/ext/utils/sc_report_handler.hh"
45#include "systemc/utils/report.hh"
46
42#include "systemc/core/scheduler.hh"
43#include "systemc/ext/core/messages.hh"
44#include "systemc/ext/core/sc_main.hh"
45#include "systemc/ext/utils/sc_report_handler.hh"
46#include "systemc/utils/report.hh"
47
47// A weak symbol to detect if sc_main has been defined, and if so where it is.
48[[gnu::weak]] int sc_main(int argc, char *argv[]);
49
50namespace sc_core
51{
52
53namespace
54{
55
48namespace sc_core
49{
50
51namespace
52{
53
56bool scMainCalled = false;
57
58int _argc = 0;
59char **_argv = NULL;
60
61class ScMainFiber : public Fiber
62{
63 public:
64 std::string resultStr;
65 int resultInt;
66
67 ScMainFiber() : resultInt(1) {}
68
69 void
70 main()
71 {
72 if (::sc_main) {
73 try {
74 resultInt = ::sc_main(_argc, _argv);
75 if (resultInt)
76 resultStr = "sc_main returned non-zero";
77 else
78 resultStr = "sc_main finished";
79 // Make sure no systemc events/notifications are scheduled
80 // after sc_main returns.
81 } catch (const sc_report &r) {
82 // There was an exception nobody caught.
83 resultStr = "uncaught sc_report";
84 sc_gem5::reportHandlerProc(
85 r, sc_report_handler::get_catch_actions());
86 } catch (...) {
87 // There was some other type of exception we need to wrap.
88 resultStr = "uncaught exception";
89 sc_gem5::reportHandlerProc(
90 ::sc_gem5::reportifyException(),
91 sc_report_handler::get_catch_actions());
92 }
93 ::sc_gem5::Kernel::scMainFinished(true);
94 ::sc_gem5::scheduler.clear();
95 } else {
96 // If python tries to call sc_main but no sc_main was defined...
97 fatal("sc_main called but not defined.\n");
98 }
99 }
100};
101
102ScMainFiber scMainFiber;
103
104// This wrapper adapts the python version of sc_main to the c++ version.
105void
106sc_main(pybind11::args args)
107{
54// This wrapper adapts the python version of sc_main to the c++ version.
55void
56sc_main(pybind11::args args)
57{
108 panic_if(scMainCalled, "sc_main called more than once.");
58 panic_if(::sc_gem5::scMainFiber.called(),
59 "sc_main called more than once.");
109
60
110 _argc = args.size();
111 _argv = new char *[_argc];
61 int argc = args.size();
62 char **argv = new char *[argc];
112
63
113 // Initialize all the _argvs to NULL so we can delete [] them
64 // Initialize all the argvs to NULL so we can delete [] them
114 // unconditionally.
65 // unconditionally.
115 for (int idx = 0; idx < _argc; idx++)
116 _argv[idx] = NULL;
66 for (int idx = 0; idx < argc; idx++)
67 argv[idx] = NULL;
117
118 // Attempt to convert all the arguments to strings. If that fails, clean
119 // up after ourselves. Also don't count this as a call to sc_main since
120 // we never got to the c++ version of that function.
121 try {
68
69 // Attempt to convert all the arguments to strings. If that fails, clean
70 // up after ourselves. Also don't count this as a call to sc_main since
71 // we never got to the c++ version of that function.
72 try {
122 for (int idx = 0; idx < _argc; idx++) {
73 for (int idx = 0; idx < argc; idx++) {
123 std::string arg = args[idx].cast<std::string>();
74 std::string arg = args[idx].cast<std::string>();
124 _argv[idx] = new char[arg.length() + 1];
125 strcpy(_argv[idx], arg.c_str());
75 argv[idx] = new char[arg.length() + 1];
76 strcpy(argv[idx], arg.c_str());
126 }
127 } catch (...) {
128 // If that didn't work for some reason (probably a conversion error)
77 }
78 } catch (...) {
79 // If that didn't work for some reason (probably a conversion error)
129 // blow away _argv and _argc and pass on the exception.
130 for (int idx = 0; idx < _argc; idx++)
131 delete [] _argv[idx];
132 delete [] _argv;
133 _argc = 0;
80 // blow away argv and argc and pass on the exception.
81 for (int idx = 0; idx < argc; idx++)
82 delete [] argv[idx];
83 delete [] argv;
84 argc = 0;
134 throw;
135 }
136
85 throw;
86 }
87
137 // At this point we're going to call the c++ sc_main, so we can't try
138 // again later.
139 scMainCalled = true;
140
141 scMainFiber.run();
88 ::sc_gem5::scMainFiber.setArgs(argc, argv);
89 ::sc_gem5::scMainFiber.run();
142}
143
144int
145sc_main_result_code()
146{
90}
91
92int
93sc_main_result_code()
94{
147 return scMainFiber.resultInt;
95 return ::sc_gem5::scMainFiber.resultInt();
148}
149
150std::string
151sc_main_result_str()
152{
96}
97
98std::string
99sc_main_result_str()
100{
153 return scMainFiber.resultStr;
101 return ::sc_gem5::scMainFiber.resultStr();
154}
155
156// Make our sc_main wrapper available in the internal _m5 python module under
157// the systemc submodule.
158
159struct InstallScMain : public ::sc_gem5::PythonInitFunc
160{
161 void

--- 7 unchanged lines hidden (view full) ---

169
170sc_stop_mode _stop_mode = SC_STOP_FINISH_DELTA;
171
172} // anonymous namespace
173
174int
175sc_argc()
176{
102}
103
104// Make our sc_main wrapper available in the internal _m5 python module under
105// the systemc submodule.
106
107struct InstallScMain : public ::sc_gem5::PythonInitFunc
108{
109 void

--- 7 unchanged lines hidden (view full) ---

117
118sc_stop_mode _stop_mode = SC_STOP_FINISH_DELTA;
119
120} // anonymous namespace
121
122int
123sc_argc()
124{
177 return _argc;
125 return ::sc_gem5::scMainFiber.argc();
178}
179
180const char *const *
181sc_argv()
182{
126}
127
128const char *const *
129sc_argv()
130{
183 return _argv;
131 return ::sc_gem5::scMainFiber.argv();
184}
185
186void
187sc_start()
188{
189 Tick now = ::sc_gem5::scheduler.getCurTick();
190 sc_start(sc_time::from_value(MaxTick - now), SC_EXIT_ON_STARVATION);
191}

--- 173 unchanged lines hidden ---
132}
133
134void
135sc_start()
136{
137 Tick now = ::sc_gem5::scheduler.getCurTick();
138 sc_start(sc_time::from_value(MaxTick - now), SC_EXIT_ON_STARVATION);
139}

--- 173 unchanged lines hidden ---