module.cc revision 13091
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"
3513091Sgabeblack@google.com#include "systemc/ext/core/sc_port.hh"
3613079Sgabeblack@google.com#include "systemc/ext/utils/sc_report_handler.hh"
3712863Sgabeblack@google.com
3812950Sgabeblack@google.comnamespace sc_gem5
3912863Sgabeblack@google.com{
4012863Sgabeblack@google.com
4112863Sgabeblack@google.comnamespace
4212863Sgabeblack@google.com{
4312863Sgabeblack@google.com
4412863Sgabeblack@google.comstd::list<Module *> _modules;
4512950Sgabeblack@google.comModule *_new_module;
4612863Sgabeblack@google.com
4713045Sgabeblack@google.comModule *_callbackModule = nullptr;
4813045Sgabeblack@google.com
4912863Sgabeblack@google.com} // anonymous namespace
5012863Sgabeblack@google.com
5112950Sgabeblack@google.comModule::Module(const char *name) : _name(name), _sc_mod(nullptr), _obj(nullptr)
5212950Sgabeblack@google.com{
5312950Sgabeblack@google.com    panic_if(_new_module, "Previous module not finished.\n");
5412950Sgabeblack@google.com    _new_module = this;
5512950Sgabeblack@google.com}
5612950Sgabeblack@google.com
5713079Sgabeblack@google.comModule::~Module()
5813079Sgabeblack@google.com{
5913079Sgabeblack@google.com    if (_new_module == this) {
6013079Sgabeblack@google.com        // Aborted module construction?
6113079Sgabeblack@google.com        _new_module = nullptr;
6213079Sgabeblack@google.com    }
6313079Sgabeblack@google.com    allModules.remove(this);
6413079Sgabeblack@google.com}
6512982Sgabeblack@google.com
6612863Sgabeblack@google.comvoid
6712950Sgabeblack@google.comModule::finish(Object *this_obj)
6812863Sgabeblack@google.com{
6912950Sgabeblack@google.com    assert(!_obj);
7012950Sgabeblack@google.com    _obj = this_obj;
7112863Sgabeblack@google.com    _modules.push_back(this);
7212950Sgabeblack@google.com    _new_module = nullptr;
7312982Sgabeblack@google.com    // This is called from the constructor of this_obj, so it can't use
7412982Sgabeblack@google.com    // dynamic cast.
7512982Sgabeblack@google.com    sc_mod(static_cast<::sc_core::sc_module *>(this_obj->sc_obj()));
7613046Sgabeblack@google.com    allModules.emplace_back(this);
7712863Sgabeblack@google.com}
7812863Sgabeblack@google.com
7912863Sgabeblack@google.comvoid
8012863Sgabeblack@google.comModule::pop()
8112863Sgabeblack@google.com{
8212950Sgabeblack@google.com    panic_if(!_modules.size(), "Popping from empty module list.\n");
8312863Sgabeblack@google.com    panic_if(_modules.back() != this,
8412863Sgabeblack@google.com            "Popping module which isn't at the end of the module list.\n");
8512950Sgabeblack@google.com    panic_if(_new_module, "Pop with unfinished module.\n");
8612950Sgabeblack@google.com    _modules.pop_back();
8712863Sgabeblack@google.com}
8812863Sgabeblack@google.com
8913091Sgabeblack@google.comvoid
9013091Sgabeblack@google.comModule::bindPorts(std::vector<const ::sc_core::sc_bind_proxy *> &proxies)
9113091Sgabeblack@google.com{
9213091Sgabeblack@google.com    panic_if(proxies.size() > ports.size(),
9313091Sgabeblack@google.com            "Trying to bind %d interfaces/ports to %d ports.\n",
9413091Sgabeblack@google.com            proxies.size(), ports.size());
9513091Sgabeblack@google.com
9613091Sgabeblack@google.com    auto proxyIt = proxies.begin();
9713091Sgabeblack@google.com    auto portIt = ports.begin();
9813091Sgabeblack@google.com    for (; proxyIt != proxies.end(); proxyIt++, portIt++) {
9913091Sgabeblack@google.com        auto proxy = *proxyIt;
10013091Sgabeblack@google.com        auto port = *portIt;
10113091Sgabeblack@google.com        if (proxy->interface())
10213091Sgabeblack@google.com            port->vbind(*proxy->interface());
10313091Sgabeblack@google.com        else
10413091Sgabeblack@google.com            port->vbind(*proxy->port());
10513091Sgabeblack@google.com    }
10613091Sgabeblack@google.com}
10713091Sgabeblack@google.com
10812863Sgabeblack@google.comModule *
10912950Sgabeblack@google.comcurrentModule()
11012863Sgabeblack@google.com{
11112950Sgabeblack@google.com    if (_modules.empty())
11212950Sgabeblack@google.com        return nullptr;
11312950Sgabeblack@google.com    return _modules.back();
11412863Sgabeblack@google.com}
11512863Sgabeblack@google.com
11612950Sgabeblack@google.comModule *
11713079Sgabeblack@google.comnewModuleChecked()
11813079Sgabeblack@google.com{
11913079Sgabeblack@google.com    if (!_new_module) {
12013079Sgabeblack@google.com        SC_REPORT_ERROR("(E533) module name stack is empty: "
12113079Sgabeblack@google.com                "did you forget to add a sc_module_name parameter to "
12213079Sgabeblack@google.com                "your module constructor?", nullptr);
12313079Sgabeblack@google.com    }
12413079Sgabeblack@google.com    return _new_module;
12513079Sgabeblack@google.com}
12613079Sgabeblack@google.com
12713079Sgabeblack@google.comModule *
12812950Sgabeblack@google.comnewModule()
12912950Sgabeblack@google.com{
13012950Sgabeblack@google.com    return _new_module;
13112950Sgabeblack@google.com}
13212950Sgabeblack@google.com
13313045Sgabeblack@google.comvoid callbackModule(Module *m) { _callbackModule = m; }
13413045Sgabeblack@google.comModule *callbackModule() { return _callbackModule; }
13513045Sgabeblack@google.com
13613046Sgabeblack@google.comstd::list<Module *> allModules;
13712982Sgabeblack@google.com
13812950Sgabeblack@google.com} // namespace sc_gem5
139