module.cc revision 13091:81fceed26e1e
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_port.hh"
36#include "systemc/ext/utils/sc_report_handler.hh"
37
38namespace sc_gem5
39{
40
41namespace
42{
43
44std::list<Module *> _modules;
45Module *_new_module;
46
47Module *_callbackModule = nullptr;
48
49} // anonymous namespace
50
51Module::Module(const char *name) : _name(name), _sc_mod(nullptr), _obj(nullptr)
52{
53    panic_if(_new_module, "Previous module not finished.\n");
54    _new_module = this;
55}
56
57Module::~Module()
58{
59    if (_new_module == this) {
60        // Aborted module construction?
61        _new_module = nullptr;
62    }
63    allModules.remove(this);
64}
65
66void
67Module::finish(Object *this_obj)
68{
69    assert(!_obj);
70    _obj = this_obj;
71    _modules.push_back(this);
72    _new_module = nullptr;
73    // This is called from the constructor of this_obj, so it can't use
74    // dynamic cast.
75    sc_mod(static_cast<::sc_core::sc_module *>(this_obj->sc_obj()));
76    allModules.emplace_back(this);
77}
78
79void
80Module::pop()
81{
82    panic_if(!_modules.size(), "Popping from empty module list.\n");
83    panic_if(_modules.back() != this,
84            "Popping module which isn't at the end of the module list.\n");
85    panic_if(_new_module, "Pop with unfinished module.\n");
86    _modules.pop_back();
87}
88
89void
90Module::bindPorts(std::vector<const ::sc_core::sc_bind_proxy *> &proxies)
91{
92    panic_if(proxies.size() > ports.size(),
93            "Trying to bind %d interfaces/ports to %d ports.\n",
94            proxies.size(), ports.size());
95
96    auto proxyIt = proxies.begin();
97    auto portIt = ports.begin();
98    for (; proxyIt != proxies.end(); proxyIt++, portIt++) {
99        auto proxy = *proxyIt;
100        auto port = *portIt;
101        if (proxy->interface())
102            port->vbind(*proxy->interface());
103        else
104            port->vbind(*proxy->port());
105    }
106}
107
108Module *
109currentModule()
110{
111    if (_modules.empty())
112        return nullptr;
113    return _modules.back();
114}
115
116Module *
117newModuleChecked()
118{
119    if (!_new_module) {
120        SC_REPORT_ERROR("(E533) module name stack is empty: "
121                "did you forget to add a sc_module_name parameter to "
122                "your module constructor?", nullptr);
123    }
124    return _new_module;
125}
126
127Module *
128newModule()
129{
130    return _new_module;
131}
132
133void callbackModule(Module *m) { _callbackModule = m; }
134Module *callbackModule() { return _callbackModule; }
135
136std::list<Module *> allModules;
137
138} // namespace sc_gem5
139