module.cc revision 13207:034ca389a810
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 "systemc/core/module.hh"
31
32#include <cassert>
33
34#include "base/logging.hh"
35#include "systemc/ext/core/sc_export.hh"
36#include "systemc/ext/core/sc_port.hh"
37#include "systemc/ext/utils/sc_report_handler.hh"
38
39namespace sc_gem5
40{
41
42namespace
43{
44
45std::list<Module *> _modules;
46Module *_new_module;
47
48Module *_callbackModule = nullptr;
49
50} // anonymous namespace
51
52Module::Module(const char *name) :
53    _name(name), _sc_mod(nullptr), _obj(nullptr), _ended(false),
54    _deprecatedConstructor(false)
55{
56    panic_if(_new_module, "Previous module not finished.\n");
57    _new_module = this;
58}
59
60Module::~Module()
61{
62    if (_new_module == this) {
63        // Aborted module construction?
64        _new_module = nullptr;
65    }
66    allModules.remove(this);
67}
68
69void
70Module::finish(Object *this_obj)
71{
72    assert(!_obj);
73    _obj = this_obj;
74    _modules.push_back(this);
75    _new_module = nullptr;
76    // This is called from the constructor of this_obj, so it can't use
77    // dynamic cast.
78    sc_mod(static_cast<::sc_core::sc_module *>(this_obj->sc_obj()));
79    allModules.emplace_back(this);
80}
81
82void
83Module::pop()
84{
85    panic_if(!_modules.size(), "Popping from empty module list.\n");
86    panic_if(_modules.back() != this,
87            "Popping module which isn't at the end of the module list.\n");
88    panic_if(_new_module, "Pop with unfinished module.\n");
89    _modules.pop_back();
90}
91
92void
93Module::bindPorts(std::vector<const ::sc_core::sc_bind_proxy *> &proxies)
94{
95    panic_if(proxies.size() > ports.size(),
96            "Trying to bind %d interfaces/ports to %d ports.\n",
97            proxies.size(), ports.size());
98
99    auto proxyIt = proxies.begin();
100    auto portIt = ports.begin();
101    for (; proxyIt != proxies.end(); proxyIt++, portIt++) {
102        auto proxy = *proxyIt;
103        auto port = *portIt;
104        if (proxy->interface())
105            port->vbind(*proxy->interface());
106        else
107            port->vbind(*proxy->port());
108    }
109}
110
111void
112Module::beforeEndOfElaboration()
113{
114    callbackModule(this);
115    _sc_mod->before_end_of_elaboration();
116    for (auto e: exports)
117        e->before_end_of_elaboration();
118    callbackModule(nullptr);
119}
120
121void
122Module::endOfElaboration()
123{
124    if (_deprecatedConstructor && !_ended) {
125        std::string msg = csprintf("module '%s'", name());
126        SC_REPORT_WARNING("(W509) module construction not properly completed: "
127                "did you forget to add a sc_module_name parameter to "
128                "your module constructor?", msg.c_str());
129    }
130    callbackModule(this);
131    _sc_mod->end_of_elaboration();
132    for (auto e: exports)
133        e->end_of_elaboration();
134    callbackModule(nullptr);
135}
136
137void
138Module::startOfSimulation()
139{
140    callbackModule(this);
141    _sc_mod->start_of_simulation();
142    for (auto e: exports)
143        e->start_of_simulation();
144    callbackModule(nullptr);
145}
146
147void
148Module::endOfSimulation()
149{
150    callbackModule(this);
151    _sc_mod->end_of_simulation();
152    for (auto e: exports)
153        e->end_of_simulation();
154    callbackModule(nullptr);
155}
156
157Module *
158currentModule()
159{
160    if (_modules.empty())
161        return nullptr;
162    return _modules.back();
163}
164
165Module *
166newModuleChecked()
167{
168    if (!_new_module) {
169        SC_REPORT_ERROR("(E533) module name stack is empty: "
170                "did you forget to add a sc_module_name parameter to "
171                "your module constructor?", nullptr);
172    }
173    return _new_module;
174}
175
176Module *
177newModule()
178{
179    return _new_module;
180}
181
182void callbackModule(Module *m) { _callbackModule = m; }
183Module *callbackModule() { return _callbackModule; }
184
185std::list<Module *> allModules;
186
187} // namespace sc_gem5
188