gem5  v20.1.0.0
module.cc
Go to the documentation of this file.
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 
28 #include "systemc/core/module.hh"
29 
30 #include <cassert>
31 
32 #include "base/logging.hh"
37 
38 namespace sc_gem5
39 {
40 
41 namespace
42 {
43 
44 std::list<Module *> _modules;
45 Module *_new_module;
46 
47 } // anonymous namespace
48 
50 
51 Module::Module(const char *name) :
52  _name(name), _sc_mod(nullptr), _obj(nullptr), _ended(false),
53  _deprecatedConstructor(false), bindingIndex(0)
54 {
55  panic_if(_new_module, "Previous module not finished.\n");
56  _new_module = this;
57 }
58 
60 {
61  // Aborted module construction?
62  if (_new_module == this)
63  _new_module = nullptr;
64 
65  // Attempt to pop now in case we're at the top of the stack, so that
66  // a stale pointer to us isn't left floating around for somebody to trip
67  // on.
68  pop();
69 
70  allModules.remove(this);
71 }
72 
73 void
75 {
76  assert(!_obj);
77  _obj = this_obj;
78  _modules.push_back(this);
79  pushParentModule(this);
80  try {
81  _new_module = nullptr;
82  // This is called from the constructor of this_obj, so it can't use
83  // dynamic cast.
84  sc_mod(static_cast<::sc_core::sc_module *>(this_obj->sc_obj()));
85  allModules.emplace_back(this);
86  } catch (...) {
88  throw;
89  }
90 }
91 
92 void
94 {
95  if (_modules.empty() || _modules.back() != this)
96  return;
97 
98  panic_if(_new_module, "Pop with unfinished module.\n");
99 
100  _modules.pop_back();
101  popParentModule();
102 }
103 
104 void
106 {
107  panic_if(proxies.size() > ports.size(),
108  "Trying to bind %d interfaces/ports to %d ports.\n",
109  proxies.size(), ports.size());
110 
111  auto proxyIt = proxies.begin();
112  auto portIt = ports.begin();
113  portIt += bindingIndex;
114  for (; proxyIt != proxies.end(); proxyIt++, portIt++) {
115  auto proxy = *proxyIt;
116  auto port = *portIt;
117  if (proxy->interface())
118  port->vbind(*proxy->interface());
119  else
120  port->vbind(*proxy->port());
121  }
122  bindingIndex += proxies.size();
123 }
124 
125 void
127 {
128  pushParentModule(this);
129  try {
131  for (auto e: exports)
132  e->before_end_of_elaboration();
133  } catch (...) {
134  popParentModule();
135  throw;
136  }
137  popParentModule();
138 }
139 
140 void
142 {
143  if (_deprecatedConstructor && !_ended) {
144  std::string msg = csprintf("module '%s'", name());
146  }
147  pushParentModule(this);
148  try {
150  for (auto e: exports)
151  e->end_of_elaboration();
152  } catch (...) {
153  popParentModule();
154  throw;
155  }
156  popParentModule();
157 }
158 
159 void
161 {
162  pushParentModule(this);
163  try {
165  for (auto e: exports)
166  e->start_of_simulation();
167  } catch (...) {
168  popParentModule();
169  throw;
170  }
171  popParentModule();
172 }
173 
174 void
176 {
177  pushParentModule(this);
178  try {
180  for (auto e: exports)
181  e->end_of_simulation();
182  } catch(...) {
183  popParentModule();
184  throw;
185  }
186  popParentModule();
187 }
188 
189 Module *
191 {
192  if (_modules.empty())
193  return nullptr;
194  return _modules.back();
195 }
196 
197 Module *
199 {
200  if (!_new_module)
202  return _new_module;
203 }
204 
205 Module *
207 {
208  return _new_module;
209 }
210 
212 
213 } // namespace sc_gem5
sc_gem5::globalNameGen
UniqueNameGen globalNameGen
Definition: module.cc:49
sc_gem5::Module::exports
std::vector<::sc_core::sc_export_base * > exports
Definition: module.hh:126
sc_gem5::Module::startOfSimulation
void startOfSimulation()
Definition: module.cc:160
sc_core::sc_module
Definition: sc_module.hh:97
sc_gem5::Object::sc_obj
sc_core::sc_object * sc_obj()
Definition: object.hh:86
module.hh
sc_gem5::Module::ports
std::vector<::sc_core::sc_port_base * > ports
Definition: module.hh:125
sc_gem5::Module::pop
void pop()
Definition: module.cc:93
sc_gem5::Module::_obj
Object * _obj
Definition: module.hh:76
sc_gem5::Module::name
const char * name() const
Definition: module.hh:94
sc_core::SC_ID_END_MODULE_NOT_CALLED_
const char SC_ID_END_MODULE_NOT_CALLED_[]
Definition: messages.cc:45
sc_port.hh
sc_gem5::UniqueNameGen
Definition: module.hh:52
std::vector
STL vector class.
Definition: stl.hh:37
sc_gem5::Module::_ended
bool _ended
Definition: module.hh:77
sc_gem5::Module::sc_mod
sc_core::sc_module * sc_mod() const
Definition: module.hh:99
sc_gem5::Module::Module
Module(const char *name)
Definition: module.cc:51
sc_gem5::Module::finish
void finish(Object *this_obj)
Definition: module.cc:74
sc_gem5::Module::~Module
~Module()
Definition: module.cc:59
sc_gem5::popParentModule
static void popParentModule()
Definition: module.hh:155
sc_core::sc_module::end_of_simulation
virtual void end_of_simulation()
Definition: sc_module.hh:251
sc_gem5::currentModule
Module * currentModule()
Definition: module.cc:190
SC_REPORT_ERROR
#define SC_REPORT_ERROR(msg_type, msg)
Definition: sc_report_handler.hh:127
sc_gem5::Module::bindPorts
void bindPorts(std::vector< const ::sc_core::sc_bind_proxy * > &proxies)
Definition: module.cc:105
sc_gem5::Object
Definition: object.hh:47
sc_gem5::pushParentModule
static void pushParentModule(Module *m)
Definition: module.hh:150
sc_gem5::newModuleChecked
Module * newModuleChecked()
Definition: module.cc:198
messages.hh
name
const std::string & name()
Definition: trace.cc:50
sc_gem5::Module::endOfElaboration
void endOfElaboration()
Definition: module.cc:141
sc_core::sc_module::end_of_elaboration
virtual void end_of_elaboration()
Definition: sc_module.hh:249
ArmISA::e
Bitfield< 9 > e
Definition: miscregs_types.hh:61
sc_gem5::newModule
Module * newModule()
Definition: module.cc:206
SC_REPORT_WARNING
#define SC_REPORT_WARNING(msg_type, msg)
Definition: sc_report_handler.hh:123
sc_report_handler.hh
panic_if
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:197
sc_gem5::Module::endOfSimulation
void endOfSimulation()
Definition: module.cc:175
sc_core::SC_ID_MODULE_NAME_STACK_EMPTY_
const char SC_ID_MODULE_NAME_STACK_EMPTY_[]
Definition: messages.cc:84
sc_core::sc_module::before_end_of_elaboration
virtual void before_end_of_elaboration()
Definition: sc_module.hh:248
sc_gem5::Module::_deprecatedConstructor
bool _deprecatedConstructor
Definition: module.hh:78
sc_gem5::allModules
std::list< Module * > allModules
Definition: module.cc:211
logging.hh
sc_gem5
Definition: sc_clock.cc:42
sc_export.hh
sc_core::sc_module::start_of_simulation
virtual void start_of_simulation()
Definition: sc_module.hh:250
std::list
STL list class.
Definition: stl.hh:51
sc_gem5::Module::bindingIndex
int bindingIndex
Definition: module.hh:128
sc_gem5::Module::beforeEndOfElaboration
void beforeEndOfElaboration()
Definition: module.cc:126
sc_gem5::Module::_sc_mod
sc_core::sc_module * _sc_mod
Definition: module.hh:75
sc_gem5::Module
Definition: module.hh:71
csprintf
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:158

Generated on Wed Sep 30 2020 14:02:14 for gem5 by doxygen 1.8.17