module.cc revision 13317:36c574a4036e
112027Sjungma@eit.uni-kl.de/*
212027Sjungma@eit.uni-kl.de * Copyright 2018 Google, Inc.
312027Sjungma@eit.uni-kl.de *
412027Sjungma@eit.uni-kl.de * Redistribution and use in source and binary forms, with or without
512027Sjungma@eit.uni-kl.de * modification, are permitted provided that the following conditions are
612027Sjungma@eit.uni-kl.de * met: redistributions of source code must retain the above copyright
712027Sjungma@eit.uni-kl.de * notice, this list of conditions and the following disclaimer;
812027Sjungma@eit.uni-kl.de * redistributions in binary form must reproduce the above copyright
912027Sjungma@eit.uni-kl.de * notice, this list of conditions and the following disclaimer in the
1012027Sjungma@eit.uni-kl.de * documentation and/or other materials provided with the distribution;
1112027Sjungma@eit.uni-kl.de * neither the name of the copyright holders nor the names of its
1212027Sjungma@eit.uni-kl.de * contributors may be used to endorse or promote products derived from
1312027Sjungma@eit.uni-kl.de * this software without specific prior written permission.
1412027Sjungma@eit.uni-kl.de *
1512027Sjungma@eit.uni-kl.de * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612027Sjungma@eit.uni-kl.de * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712027Sjungma@eit.uni-kl.de * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812027Sjungma@eit.uni-kl.de * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912027Sjungma@eit.uni-kl.de * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012027Sjungma@eit.uni-kl.de * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112027Sjungma@eit.uni-kl.de * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212027Sjungma@eit.uni-kl.de * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312027Sjungma@eit.uni-kl.de * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412027Sjungma@eit.uni-kl.de * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512027Sjungma@eit.uni-kl.de * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612027Sjungma@eit.uni-kl.de *
2712027Sjungma@eit.uni-kl.de * Authors: Gabe Black
2812027Sjungma@eit.uni-kl.de */
2912027Sjungma@eit.uni-kl.de
3012027Sjungma@eit.uni-kl.de#include "systemc/core/module.hh"
3112027Sjungma@eit.uni-kl.de
3212027Sjungma@eit.uni-kl.de#include <cassert>
3312027Sjungma@eit.uni-kl.de
3412027Sjungma@eit.uni-kl.de#include "base/logging.hh"
3512027Sjungma@eit.uni-kl.de#include "systemc/ext/core/messages.hh"
3612027Sjungma@eit.uni-kl.de#include "systemc/ext/core/sc_export.hh"
3712027Sjungma@eit.uni-kl.de#include "systemc/ext/core/sc_port.hh"
3812027Sjungma@eit.uni-kl.de#include "systemc/ext/utils/sc_report_handler.hh"
3912027Sjungma@eit.uni-kl.de
4012027Sjungma@eit.uni-kl.denamespace sc_gem5
4112027Sjungma@eit.uni-kl.de{
4212027Sjungma@eit.uni-kl.de
4312027Sjungma@eit.uni-kl.denamespace
4412027Sjungma@eit.uni-kl.de{
4512027Sjungma@eit.uni-kl.de
4612027Sjungma@eit.uni-kl.destd::list<Module *> _modules;
4712027Sjungma@eit.uni-kl.deModule *_new_module;
4812027Sjungma@eit.uni-kl.de
4912027Sjungma@eit.uni-kl.de} // anonymous namespace
5012027Sjungma@eit.uni-kl.de
5112027Sjungma@eit.uni-kl.deUniqueNameGen globalNameGen;
5212027Sjungma@eit.uni-kl.de
5312027Sjungma@eit.uni-kl.deModule::Module(const char *name) :
5412027Sjungma@eit.uni-kl.de    _name(name), _sc_mod(nullptr), _obj(nullptr), _ended(false),
5512027Sjungma@eit.uni-kl.de    _deprecatedConstructor(false), bindingIndex(0)
5612027Sjungma@eit.uni-kl.de{
5712027Sjungma@eit.uni-kl.de    panic_if(_new_module, "Previous module not finished.\n");
5812027Sjungma@eit.uni-kl.de    _new_module = this;
5912027Sjungma@eit.uni-kl.de}
6012027Sjungma@eit.uni-kl.de
6112027Sjungma@eit.uni-kl.deModule::~Module()
6212027Sjungma@eit.uni-kl.de{
6312027Sjungma@eit.uni-kl.de    // Aborted module construction?
6412027Sjungma@eit.uni-kl.de    if (_new_module == this)
6512027Sjungma@eit.uni-kl.de        _new_module = nullptr;
6612027Sjungma@eit.uni-kl.de
6712027Sjungma@eit.uni-kl.de    // Attempt to pop now in case we're at the top of the stack, so that
6812027Sjungma@eit.uni-kl.de    // a stale pointer to us isn't left floating around for somebody to trip
6912027Sjungma@eit.uni-kl.de    // on.
7012027Sjungma@eit.uni-kl.de    pop();
7112027Sjungma@eit.uni-kl.de
7212027Sjungma@eit.uni-kl.de    allModules.remove(this);
7312027Sjungma@eit.uni-kl.de}
7412027Sjungma@eit.uni-kl.de
7512027Sjungma@eit.uni-kl.devoid
7612027Sjungma@eit.uni-kl.deModule::finish(Object *this_obj)
7712027Sjungma@eit.uni-kl.de{
7812027Sjungma@eit.uni-kl.de    assert(!_obj);
7912027Sjungma@eit.uni-kl.de    _obj = this_obj;
8012027Sjungma@eit.uni-kl.de    _modules.push_back(this);
8112027Sjungma@eit.uni-kl.de    pushParentModule(this);
8212027Sjungma@eit.uni-kl.de    try {
8312027Sjungma@eit.uni-kl.de        _new_module = nullptr;
8412027Sjungma@eit.uni-kl.de        // This is called from the constructor of this_obj, so it can't use
8512027Sjungma@eit.uni-kl.de        // dynamic cast.
8612027Sjungma@eit.uni-kl.de        sc_mod(static_cast<::sc_core::sc_module *>(this_obj->sc_obj()));
8712027Sjungma@eit.uni-kl.de        allModules.emplace_back(this);
8812027Sjungma@eit.uni-kl.de    } catch (...) {
8912027Sjungma@eit.uni-kl.de        popParentModule();
9012027Sjungma@eit.uni-kl.de        throw;
9112027Sjungma@eit.uni-kl.de    }
9212027Sjungma@eit.uni-kl.de}
9312027Sjungma@eit.uni-kl.de
9412027Sjungma@eit.uni-kl.devoid
9512027Sjungma@eit.uni-kl.deModule::pop()
9612027Sjungma@eit.uni-kl.de{
9712027Sjungma@eit.uni-kl.de    if (_modules.empty() || _modules.back() != this)
9812027Sjungma@eit.uni-kl.de        return;
9912027Sjungma@eit.uni-kl.de
10012027Sjungma@eit.uni-kl.de    panic_if(_new_module, "Pop with unfinished module.\n");
10112027Sjungma@eit.uni-kl.de
10212027Sjungma@eit.uni-kl.de    _modules.pop_back();
10312027Sjungma@eit.uni-kl.de    popParentModule();
10412027Sjungma@eit.uni-kl.de}
10512027Sjungma@eit.uni-kl.de
10612027Sjungma@eit.uni-kl.devoid
10712027Sjungma@eit.uni-kl.deModule::bindPorts(std::vector<const ::sc_core::sc_bind_proxy *> &proxies)
10812027Sjungma@eit.uni-kl.de{
10912027Sjungma@eit.uni-kl.de    panic_if(proxies.size() > ports.size(),
11012027Sjungma@eit.uni-kl.de            "Trying to bind %d interfaces/ports to %d ports.\n",
11112027Sjungma@eit.uni-kl.de            proxies.size(), ports.size());
11212027Sjungma@eit.uni-kl.de
11312027Sjungma@eit.uni-kl.de    auto proxyIt = proxies.begin();
11412027Sjungma@eit.uni-kl.de    auto portIt = ports.begin();
11512027Sjungma@eit.uni-kl.de    portIt += bindingIndex;
11612027Sjungma@eit.uni-kl.de    for (; proxyIt != proxies.end(); proxyIt++, portIt++) {
11712027Sjungma@eit.uni-kl.de        auto proxy = *proxyIt;
11812027Sjungma@eit.uni-kl.de        auto port = *portIt;
11912027Sjungma@eit.uni-kl.de        if (proxy->interface())
12012027Sjungma@eit.uni-kl.de            port->vbind(*proxy->interface());
12112027Sjungma@eit.uni-kl.de        else
12212027Sjungma@eit.uni-kl.de            port->vbind(*proxy->port());
12312027Sjungma@eit.uni-kl.de    }
12412027Sjungma@eit.uni-kl.de    bindingIndex += proxies.size();
12512027Sjungma@eit.uni-kl.de}
12612027Sjungma@eit.uni-kl.de
12712027Sjungma@eit.uni-kl.devoid
12812027Sjungma@eit.uni-kl.deModule::beforeEndOfElaboration()
12912027Sjungma@eit.uni-kl.de{
13012027Sjungma@eit.uni-kl.de    pushParentModule(this);
13112027Sjungma@eit.uni-kl.de    try {
13212027Sjungma@eit.uni-kl.de        _sc_mod->before_end_of_elaboration();
13312027Sjungma@eit.uni-kl.de        for (auto e: exports)
13412027Sjungma@eit.uni-kl.de            e->before_end_of_elaboration();
13512027Sjungma@eit.uni-kl.de    } catch (...) {
13612027Sjungma@eit.uni-kl.de        popParentModule();
13712027Sjungma@eit.uni-kl.de        throw;
13812027Sjungma@eit.uni-kl.de    }
13912027Sjungma@eit.uni-kl.de    popParentModule();
14012027Sjungma@eit.uni-kl.de}
14112027Sjungma@eit.uni-kl.de
14212027Sjungma@eit.uni-kl.devoid
14312027Sjungma@eit.uni-kl.deModule::endOfElaboration()
14412027Sjungma@eit.uni-kl.de{
14512027Sjungma@eit.uni-kl.de    if (_deprecatedConstructor && !_ended) {
14612027Sjungma@eit.uni-kl.de        std::string msg = csprintf("module '%s'", name());
14712027Sjungma@eit.uni-kl.de        SC_REPORT_WARNING(sc_core::SC_ID_END_MODULE_NOT_CALLED_, msg.c_str());
14812027Sjungma@eit.uni-kl.de    }
14912027Sjungma@eit.uni-kl.de    pushParentModule(this);
15012027Sjungma@eit.uni-kl.de    try {
15112027Sjungma@eit.uni-kl.de        _sc_mod->end_of_elaboration();
15212027Sjungma@eit.uni-kl.de        for (auto e: exports)
15312027Sjungma@eit.uni-kl.de            e->end_of_elaboration();
15412027Sjungma@eit.uni-kl.de    } catch (...) {
15512027Sjungma@eit.uni-kl.de        popParentModule();
15612027Sjungma@eit.uni-kl.de        throw;
15712027Sjungma@eit.uni-kl.de    }
15812027Sjungma@eit.uni-kl.de    popParentModule();
15912027Sjungma@eit.uni-kl.de}
16012027Sjungma@eit.uni-kl.de
16112027Sjungma@eit.uni-kl.devoid
16212027Sjungma@eit.uni-kl.deModule::startOfSimulation()
16312027Sjungma@eit.uni-kl.de{
16412027Sjungma@eit.uni-kl.de    pushParentModule(this);
16512027Sjungma@eit.uni-kl.de    try {
16612027Sjungma@eit.uni-kl.de        _sc_mod->start_of_simulation();
16712027Sjungma@eit.uni-kl.de        for (auto e: exports)
16812027Sjungma@eit.uni-kl.de            e->start_of_simulation();
16912027Sjungma@eit.uni-kl.de    } catch (...) {
17012027Sjungma@eit.uni-kl.de        popParentModule();
17112027Sjungma@eit.uni-kl.de        throw;
17212027Sjungma@eit.uni-kl.de    }
17312027Sjungma@eit.uni-kl.de    popParentModule();
17412027Sjungma@eit.uni-kl.de}
17512027Sjungma@eit.uni-kl.de
17612027Sjungma@eit.uni-kl.devoid
17712027Sjungma@eit.uni-kl.deModule::endOfSimulation()
17812027Sjungma@eit.uni-kl.de{
17912027Sjungma@eit.uni-kl.de    pushParentModule(this);
18012027Sjungma@eit.uni-kl.de    try {
18112027Sjungma@eit.uni-kl.de        _sc_mod->end_of_simulation();
18212027Sjungma@eit.uni-kl.de        for (auto e: exports)
18312027Sjungma@eit.uni-kl.de            e->end_of_simulation();
18412027Sjungma@eit.uni-kl.de    } catch(...) {
18512027Sjungma@eit.uni-kl.de        popParentModule();
18612027Sjungma@eit.uni-kl.de        throw;
18712027Sjungma@eit.uni-kl.de    }
18812027Sjungma@eit.uni-kl.de    popParentModule();
18912027Sjungma@eit.uni-kl.de}
19012027Sjungma@eit.uni-kl.de
19112027Sjungma@eit.uni-kl.deModule *
19212027Sjungma@eit.uni-kl.decurrentModule()
19312027Sjungma@eit.uni-kl.de{
19412027Sjungma@eit.uni-kl.de    if (_modules.empty())
19512027Sjungma@eit.uni-kl.de        return nullptr;
19612027Sjungma@eit.uni-kl.de    return _modules.back();
19712027Sjungma@eit.uni-kl.de}
19812027Sjungma@eit.uni-kl.de
19912027Sjungma@eit.uni-kl.deModule *
20012027Sjungma@eit.uni-kl.denewModuleChecked()
20112027Sjungma@eit.uni-kl.de{
20212027Sjungma@eit.uni-kl.de    if (!_new_module)
20312027Sjungma@eit.uni-kl.de        SC_REPORT_ERROR(sc_core::SC_ID_MODULE_NAME_STACK_EMPTY_, "");
20412027Sjungma@eit.uni-kl.de    return _new_module;
20512027Sjungma@eit.uni-kl.de}
20612027Sjungma@eit.uni-kl.de
20712027Sjungma@eit.uni-kl.deModule *
20812027Sjungma@eit.uni-kl.denewModule()
20912027Sjungma@eit.uni-kl.de{
21012027Sjungma@eit.uni-kl.de    return _new_module;
21112027Sjungma@eit.uni-kl.de}
21212027Sjungma@eit.uni-kl.de
21312027Sjungma@eit.uni-kl.destd::list<Module *> allModules;
21412027Sjungma@eit.uni-kl.de
21512027Sjungma@eit.uni-kl.de} // namespace sc_gem5
21612027Sjungma@eit.uni-kl.de