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

Generated on Fri Feb 28 2020 16:27:03 for gem5 by doxygen 1.8.13