module.cc revision 13079
113325Sgabeblack@google.com/*
213325Sgabeblack@google.com * Copyright 2018 Google, Inc.
313325Sgabeblack@google.com *
413325Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
513325Sgabeblack@google.com * modification, are permitted provided that the following conditions are
613325Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
713325Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
813325Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
913325Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1013325Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1113325Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1213325Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1313325Sgabeblack@google.com * this software without specific prior written permission.
1413325Sgabeblack@google.com *
1513325Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1613325Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1713325Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1813325Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1913325Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2013325Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2113325Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2213325Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2313325Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2413325Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2513325Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2613325Sgabeblack@google.com *
2713325Sgabeblack@google.com * Authors: Gabe Black
2813325Sgabeblack@google.com */
2913325Sgabeblack@google.com
3013325Sgabeblack@google.com#include "systemc/core/module.hh"
3113325Sgabeblack@google.com
3213325Sgabeblack@google.com#include <cassert>
3313325Sgabeblack@google.com
3413325Sgabeblack@google.com#include "base/logging.hh"
3513325Sgabeblack@google.com#include "systemc/ext/utils/sc_report_handler.hh"
3613325Sgabeblack@google.com
3713325Sgabeblack@google.comnamespace sc_gem5
3813325Sgabeblack@google.com{
3913325Sgabeblack@google.com
4013325Sgabeblack@google.comnamespace
4113325Sgabeblack@google.com{
4213325Sgabeblack@google.com
4313325Sgabeblack@google.comstd::list<Module *> _modules;
4413325Sgabeblack@google.comModule *_new_module;
4513325Sgabeblack@google.com
4613325Sgabeblack@google.comModule *_callbackModule = nullptr;
4713325Sgabeblack@google.com
4813325Sgabeblack@google.com} // anonymous namespace
4913325Sgabeblack@google.com
5013325Sgabeblack@google.comModule::Module(const char *name) : _name(name), _sc_mod(nullptr), _obj(nullptr)
5113325Sgabeblack@google.com{
5213325Sgabeblack@google.com    panic_if(_new_module, "Previous module not finished.\n");
5313325Sgabeblack@google.com    _new_module = this;
5413325Sgabeblack@google.com}
5513325Sgabeblack@google.com
5613325Sgabeblack@google.comModule::~Module()
5713325Sgabeblack@google.com{
5813325Sgabeblack@google.com    if (_new_module == this) {
5913325Sgabeblack@google.com        // Aborted module construction?
6013325Sgabeblack@google.com        _new_module = nullptr;
6113325Sgabeblack@google.com    }
6213325Sgabeblack@google.com    allModules.remove(this);
6313325Sgabeblack@google.com}
6413325Sgabeblack@google.com
6513325Sgabeblack@google.comvoid
6613325Sgabeblack@google.comModule::finish(Object *this_obj)
6713325Sgabeblack@google.com{
6813325Sgabeblack@google.com    assert(!_obj);
6913325Sgabeblack@google.com    _obj = this_obj;
70    _modules.push_back(this);
71    _new_module = nullptr;
72    // This is called from the constructor of this_obj, so it can't use
73    // dynamic cast.
74    sc_mod(static_cast<::sc_core::sc_module *>(this_obj->sc_obj()));
75    allModules.emplace_back(this);
76}
77
78void
79Module::pop()
80{
81    panic_if(!_modules.size(), "Popping from empty module list.\n");
82    panic_if(_modules.back() != this,
83            "Popping module which isn't at the end of the module list.\n");
84    panic_if(_new_module, "Pop with unfinished module.\n");
85    _modules.pop_back();
86}
87
88Module *
89currentModule()
90{
91    if (_modules.empty())
92        return nullptr;
93    return _modules.back();
94}
95
96Module *
97newModuleChecked()
98{
99    if (!_new_module) {
100        SC_REPORT_ERROR("(E533) module name stack is empty: "
101                "did you forget to add a sc_module_name parameter to "
102                "your module constructor?", nullptr);
103    }
104    return _new_module;
105}
106
107Module *
108newModule()
109{
110    return _new_module;
111}
112
113void callbackModule(Module *m) { _callbackModule = m; }
114Module *callbackModule() { return _callbackModule; }
115
116std::list<Module *> allModules;
117
118} // namespace sc_gem5
119