sc_main.cc revision 12837
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 124} // namespace sc_core 125