113403Sgabeblack@google.com/*
213403Sgabeblack@google.com * Copyright 2018 Google, Inc.
313403Sgabeblack@google.com *
413403Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
513403Sgabeblack@google.com * modification, are permitted provided that the following conditions are
613403Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
713403Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
813403Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
913403Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1013403Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1113403Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1213403Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1313403Sgabeblack@google.com * this software without specific prior written permission.
1413403Sgabeblack@google.com *
1513403Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1613403Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1713403Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1813403Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1913403Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2013403Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2113403Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2213403Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2313403Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2413403Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2513403Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2613403Sgabeblack@google.com *
2713403Sgabeblack@google.com * Authors: Gabe Black
2813403Sgabeblack@google.com */
2913403Sgabeblack@google.com
3013403Sgabeblack@google.com#include "systemc/core/sc_main_fiber.hh"
3113403Sgabeblack@google.com
3213403Sgabeblack@google.com#include <cstring>
3313403Sgabeblack@google.com#include <string>
3413403Sgabeblack@google.com
3513403Sgabeblack@google.com#include "systemc/core/kernel.hh"
3613403Sgabeblack@google.com#include "systemc/core/scheduler.hh"
3713403Sgabeblack@google.com#include "systemc/ext/core/messages.hh"
3813403Sgabeblack@google.com#include "systemc/ext/core/sc_main.hh"
3913403Sgabeblack@google.com#include "systemc/ext/utils/sc_report_handler.hh"
4013403Sgabeblack@google.com#include "systemc/utils/report.hh"
4113403Sgabeblack@google.com
4213403Sgabeblack@google.com// A weak symbol to detect if sc_main has been defined, and if so where it is.
4313403Sgabeblack@google.com[[gnu::weak]] int sc_main(int argc, char *argv[]);
4413403Sgabeblack@google.com
4513403Sgabeblack@google.comnamespace sc_gem5
4613403Sgabeblack@google.com{
4713403Sgabeblack@google.com
4813403Sgabeblack@google.comvoid
4913403Sgabeblack@google.comScMainFiber::main()
5013403Sgabeblack@google.com{
5113403Sgabeblack@google.com    _called = true;
5213403Sgabeblack@google.com
5313403Sgabeblack@google.com    if (::sc_main) {
5413403Sgabeblack@google.com        try {
5513403Sgabeblack@google.com            _resultInt = ::sc_main(_argc, _argv);
5613403Sgabeblack@google.com            if (_resultInt)
5713403Sgabeblack@google.com                _resultStr = "sc_main returned non-zero";
5813403Sgabeblack@google.com            else
5913403Sgabeblack@google.com                _resultStr = "sc_main finished";
6013403Sgabeblack@google.com            // Make sure no systemc events/notifications are scheduled
6113403Sgabeblack@google.com            // after sc_main returns.
6213403Sgabeblack@google.com        } catch (const ::sc_core::sc_report &r) {
6313403Sgabeblack@google.com            // There was an exception nobody caught.
6413403Sgabeblack@google.com            _resultStr = "uncaught sc_report";
6513403Sgabeblack@google.com            reportHandlerProc(
6613403Sgabeblack@google.com                    r, ::sc_core::sc_report_handler::get_catch_actions());
6713403Sgabeblack@google.com        } catch (...) {
6813403Sgabeblack@google.com            // There was some other type of exception we need to wrap.
6913403Sgabeblack@google.com            _resultStr = "uncaught exception";
7013403Sgabeblack@google.com            reportHandlerProc(reportifyException(),
7113403Sgabeblack@google.com                    ::sc_core::sc_report_handler::get_catch_actions());
7213403Sgabeblack@google.com        }
7313403Sgabeblack@google.com        scheduler.clear();
7413403Sgabeblack@google.com    } else {
7513403Sgabeblack@google.com        // If python tries to call sc_main but no sc_main was defined...
7613403Sgabeblack@google.com        fatal("sc_main called but not defined.\n");
7713403Sgabeblack@google.com    }
7813403Sgabeblack@google.com}
7913403Sgabeblack@google.com
8013403Sgabeblack@google.comScMainFiber scMainFiber;
8113403Sgabeblack@google.com
8213403Sgabeblack@google.com} // namespace sc_gem5
83