module.cc revision 13303
112863Sgabeblack@google.com/*
212863Sgabeblack@google.com * Copyright 2018 Google, Inc.
312863Sgabeblack@google.com *
412863Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
512863Sgabeblack@google.com * modification, are permitted provided that the following conditions are
612863Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
712863Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
812863Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
912863Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1012863Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1112863Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1212863Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1312863Sgabeblack@google.com * this software without specific prior written permission.
1412863Sgabeblack@google.com *
1512863Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612863Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712863Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812863Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912863Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012863Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112863Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212863Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312863Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412863Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512863Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612863Sgabeblack@google.com *
2712863Sgabeblack@google.com * Authors: Gabe Black
2812863Sgabeblack@google.com */
2912863Sgabeblack@google.com
3012863Sgabeblack@google.com#include "systemc/core/module.hh"
3112863Sgabeblack@google.com
3212950Sgabeblack@google.com#include <cassert>
3312863Sgabeblack@google.com
3412863Sgabeblack@google.com#include "base/logging.hh"
3513191Sgabeblack@google.com#include "systemc/ext/core/sc_export.hh"
3613091Sgabeblack@google.com#include "systemc/ext/core/sc_port.hh"
3713079Sgabeblack@google.com#include "systemc/ext/utils/sc_report_handler.hh"
3812863Sgabeblack@google.com
3912950Sgabeblack@google.comnamespace sc_gem5
4012863Sgabeblack@google.com{
4112863Sgabeblack@google.com
4212863Sgabeblack@google.comnamespace
4312863Sgabeblack@google.com{
4412863Sgabeblack@google.com
4512863Sgabeblack@google.comstd::list<Module *> _modules;
4612950Sgabeblack@google.comModule *_new_module;
4712863Sgabeblack@google.com
4812863Sgabeblack@google.com} // anonymous namespace
4912863Sgabeblack@google.com
5013303Sgabeblack@google.comUniqueNameGen globalNameGen;
5113303Sgabeblack@google.com
5213191Sgabeblack@google.comModule::Module(const char *name) :
5313191Sgabeblack@google.com    _name(name), _sc_mod(nullptr), _obj(nullptr), _ended(false),
5413291Sgabeblack@google.com    _deprecatedConstructor(false), bindingIndex(0)
5512950Sgabeblack@google.com{
5612950Sgabeblack@google.com    panic_if(_new_module, "Previous module not finished.\n");
5712950Sgabeblack@google.com    _new_module = this;
5812950Sgabeblack@google.com}
5912950Sgabeblack@google.com
6013079Sgabeblack@google.comModule::~Module()
6113079Sgabeblack@google.com{
6213268Sgabeblack@google.com    // Aborted module construction?
6313268Sgabeblack@google.com    if (_new_module == this)
6413079Sgabeblack@google.com        _new_module = nullptr;
6513268Sgabeblack@google.com
6613268Sgabeblack@google.com    // Attempt to pop now in case we're at the top of the stack, so that
6713268Sgabeblack@google.com    // a stale pointer to us isn't left floating around for somebody to trip
6813268Sgabeblack@google.com    // on.
6913268Sgabeblack@google.com    pop();
7013268Sgabeblack@google.com
7113079Sgabeblack@google.com    allModules.remove(this);
7213079Sgabeblack@google.com}
7312982Sgabeblack@google.com
7412863Sgabeblack@google.comvoid
7512950Sgabeblack@google.comModule::finish(Object *this_obj)
7612863Sgabeblack@google.com{
7712950Sgabeblack@google.com    assert(!_obj);
7812950Sgabeblack@google.com    _obj = this_obj;
7912863Sgabeblack@google.com    _modules.push_back(this);
8013268Sgabeblack@google.com    pushParentModule(this);
8113268Sgabeblack@google.com    try {
8213268Sgabeblack@google.com        _new_module = nullptr;
8313268Sgabeblack@google.com        // This is called from the constructor of this_obj, so it can't use
8413268Sgabeblack@google.com        // dynamic cast.
8513268Sgabeblack@google.com        sc_mod(static_cast<::sc_core::sc_module *>(this_obj->sc_obj()));
8613268Sgabeblack@google.com        allModules.emplace_back(this);
8713268Sgabeblack@google.com    } catch (...) {
8813268Sgabeblack@google.com        popParentModule();
8913268Sgabeblack@google.com        throw;
9013268Sgabeblack@google.com    }
9112863Sgabeblack@google.com}
9212863Sgabeblack@google.com
9312863Sgabeblack@google.comvoid
9412863Sgabeblack@google.comModule::pop()
9512863Sgabeblack@google.com{
9613268Sgabeblack@google.com    if (_modules.empty() || _modules.back() != this)
9713268Sgabeblack@google.com        return;
9813268Sgabeblack@google.com
9912950Sgabeblack@google.com    panic_if(_new_module, "Pop with unfinished module.\n");
10013268Sgabeblack@google.com
10112950Sgabeblack@google.com    _modules.pop_back();
10213268Sgabeblack@google.com    popParentModule();
10312863Sgabeblack@google.com}
10412863Sgabeblack@google.com
10513091Sgabeblack@google.comvoid
10613091Sgabeblack@google.comModule::bindPorts(std::vector<const ::sc_core::sc_bind_proxy *> &proxies)
10713091Sgabeblack@google.com{
10813091Sgabeblack@google.com    panic_if(proxies.size() > ports.size(),
10913091Sgabeblack@google.com            "Trying to bind %d interfaces/ports to %d ports.\n",
11013091Sgabeblack@google.com            proxies.size(), ports.size());
11113091Sgabeblack@google.com
11213091Sgabeblack@google.com    auto proxyIt = proxies.begin();
11313091Sgabeblack@google.com    auto portIt = ports.begin();
11413291Sgabeblack@google.com    portIt += bindingIndex;
11513091Sgabeblack@google.com    for (; proxyIt != proxies.end(); proxyIt++, portIt++) {
11613091Sgabeblack@google.com        auto proxy = *proxyIt;
11713091Sgabeblack@google.com        auto port = *portIt;
11813091Sgabeblack@google.com        if (proxy->interface())
11913091Sgabeblack@google.com            port->vbind(*proxy->interface());
12013091Sgabeblack@google.com        else
12113091Sgabeblack@google.com            port->vbind(*proxy->port());
12213091Sgabeblack@google.com    }
12313291Sgabeblack@google.com    bindingIndex += proxies.size();
12413091Sgabeblack@google.com}
12513091Sgabeblack@google.com
12613191Sgabeblack@google.comvoid
12713191Sgabeblack@google.comModule::beforeEndOfElaboration()
12813191Sgabeblack@google.com{
12913268Sgabeblack@google.com    pushParentModule(this);
13013268Sgabeblack@google.com    try {
13113268Sgabeblack@google.com        _sc_mod->before_end_of_elaboration();
13213268Sgabeblack@google.com        for (auto e: exports)
13313268Sgabeblack@google.com            e->before_end_of_elaboration();
13413268Sgabeblack@google.com    } catch (...) {
13513268Sgabeblack@google.com        popParentModule();
13613268Sgabeblack@google.com        throw;
13713268Sgabeblack@google.com    }
13813268Sgabeblack@google.com    popParentModule();
13913191Sgabeblack@google.com}
14013191Sgabeblack@google.com
14113191Sgabeblack@google.comvoid
14213191Sgabeblack@google.comModule::endOfElaboration()
14313191Sgabeblack@google.com{
14413191Sgabeblack@google.com    if (_deprecatedConstructor && !_ended) {
14513191Sgabeblack@google.com        std::string msg = csprintf("module '%s'", name());
14613191Sgabeblack@google.com        SC_REPORT_WARNING("(W509) module construction not properly completed: "
14713191Sgabeblack@google.com                "did you forget to add a sc_module_name parameter to "
14813191Sgabeblack@google.com                "your module constructor?", msg.c_str());
14913191Sgabeblack@google.com    }
15013268Sgabeblack@google.com    pushParentModule(this);
15113268Sgabeblack@google.com    try {
15213268Sgabeblack@google.com        _sc_mod->end_of_elaboration();
15313268Sgabeblack@google.com        for (auto e: exports)
15413268Sgabeblack@google.com            e->end_of_elaboration();
15513268Sgabeblack@google.com    } catch (...) {
15613268Sgabeblack@google.com        popParentModule();
15713268Sgabeblack@google.com        throw;
15813268Sgabeblack@google.com    }
15913268Sgabeblack@google.com    popParentModule();
16013191Sgabeblack@google.com}
16113191Sgabeblack@google.com
16213191Sgabeblack@google.comvoid
16313191Sgabeblack@google.comModule::startOfSimulation()
16413191Sgabeblack@google.com{
16513268Sgabeblack@google.com    pushParentModule(this);
16613268Sgabeblack@google.com    try {
16713268Sgabeblack@google.com        _sc_mod->start_of_simulation();
16813268Sgabeblack@google.com        for (auto e: exports)
16913268Sgabeblack@google.com            e->start_of_simulation();
17013268Sgabeblack@google.com    } catch (...) {
17113268Sgabeblack@google.com        popParentModule();
17213268Sgabeblack@google.com        throw;
17313268Sgabeblack@google.com    }
17413268Sgabeblack@google.com    popParentModule();
17513191Sgabeblack@google.com}
17613191Sgabeblack@google.com
17713191Sgabeblack@google.comvoid
17813191Sgabeblack@google.comModule::endOfSimulation()
17913191Sgabeblack@google.com{
18013268Sgabeblack@google.com    pushParentModule(this);
18113268Sgabeblack@google.com    try {
18213268Sgabeblack@google.com        _sc_mod->end_of_simulation();
18313268Sgabeblack@google.com        for (auto e: exports)
18413268Sgabeblack@google.com            e->end_of_simulation();
18513268Sgabeblack@google.com    } catch(...) {
18613268Sgabeblack@google.com        popParentModule();
18713268Sgabeblack@google.com        throw;
18813268Sgabeblack@google.com    }
18913268Sgabeblack@google.com    popParentModule();
19013191Sgabeblack@google.com}
19113191Sgabeblack@google.com
19212863Sgabeblack@google.comModule *
19312950Sgabeblack@google.comcurrentModule()
19412863Sgabeblack@google.com{
19512950Sgabeblack@google.com    if (_modules.empty())
19612950Sgabeblack@google.com        return nullptr;
19712950Sgabeblack@google.com    return _modules.back();
19812863Sgabeblack@google.com}
19912863Sgabeblack@google.com
20012950Sgabeblack@google.comModule *
20113079Sgabeblack@google.comnewModuleChecked()
20213079Sgabeblack@google.com{
20313079Sgabeblack@google.com    if (!_new_module) {
20413079Sgabeblack@google.com        SC_REPORT_ERROR("(E533) module name stack is empty: "
20513079Sgabeblack@google.com                "did you forget to add a sc_module_name parameter to "
20613079Sgabeblack@google.com                "your module constructor?", nullptr);
20713079Sgabeblack@google.com    }
20813079Sgabeblack@google.com    return _new_module;
20913079Sgabeblack@google.com}
21013079Sgabeblack@google.com
21113079Sgabeblack@google.comModule *
21212950Sgabeblack@google.comnewModule()
21312950Sgabeblack@google.com{
21412950Sgabeblack@google.com    return _new_module;
21512950Sgabeblack@google.com}
21612950Sgabeblack@google.com
21713046Sgabeblack@google.comstd::list<Module *> allModules;
21812982Sgabeblack@google.com
21912950Sgabeblack@google.com} // namespace sc_gem5
220