gem5  v20.1.0.0
object.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/object.hh"
29 
30 #include <algorithm>
31 #include <stack>
32 
33 #include "systemc/core/event.hh"
34 #include "systemc/core/module.hh"
39 
40 namespace sc_gem5
41 {
42 
43 namespace
44 {
45 
47 findObjectIn(Objects &objects, const std::string &name)
48 {
49  ObjectsIt it;
50  for (it = objects.begin(); it != objects.end(); it++)
51  if (!strcmp((*it)->name(), name.c_str()))
52  break;
53 
54  return it;
55 }
56 
57 void
58 addObject(Objects *objects, sc_core::sc_object *object)
59 {
60  objects->emplace(objects->end(), object);
61 }
62 
63 void
64 popObject(Objects *objects, const std::string &name)
65 {
66  ObjectsIt it = findObjectIn(*objects, name);
67  assert(it != objects->end());
68  std::swap(objects->back(), *it);
69  objects->pop_back();
70 }
71 
72 bool
73 nameIsUnique(Objects *objects, Events *events, const std::string &name)
74 {
75  for (auto obj: *objects)
76  if (!strcmp(obj->basename(), name.c_str()))
77  return false;
78  for (auto event: *events)
79  if (!strcmp(event->basename(), name.c_str()))
80  return false;
81  return true;
82 }
83 
84 } // anonymous namespace
85 
86 Object::Object(sc_core::sc_object *_sc_obj) : Object(_sc_obj, nullptr) {}
87 
88 Object::Object(sc_core::sc_object *_sc_obj, const char *obj_name) :
89  _sc_obj(_sc_obj), _basename(obj_name ? obj_name : ""), parent(nullptr)
90 {
91  if (_basename == "")
93 
95 
96  Module *n = newModule();
97  if (n) {
98  // We are a module in the process of being constructed.
99  n->finish(this);
100  }
101 
102  std::string original_name = _basename;
103  _basename = sc_gem5::pickUniqueName(parent, original_name);
104 
105  if (parent)
106  addObject(&parent->_gem5_object->children, _sc_obj);
107  else
108  addObject(&topLevelObjects, _sc_obj);
109 
110  addObject(&allObjects, _sc_obj);
111 
112  sc_core::sc_object *sc_p = parent;
113  std::string path = "";
114  while (sc_p) {
115  path = std::string(sc_p->basename()) + std::string(".") + path;
116  sc_p = sc_p->get_parent_object();
117  }
118 
119  if (_basename != original_name) {
120  std::string message = path + original_name +
121  ". Latter declaration will be renamed to " +
122  path + _basename;
124  }
125  _name = path + _basename;
126 }
127 
128 Object::Object(sc_core::sc_object *_sc_obj, const Object &arg) :
129  Object(_sc_obj, arg._basename.c_str())
130 {}
131 
132 Object &
134 {
135  return *this;
136 }
137 
139 {
140  // Promote all children to be top level objects.
141  for (auto child: children) {
142  addObject(&topLevelObjects, child);
143  child->_gem5_object->parent = nullptr;
144  }
145  children.clear();
146 
147  for (auto event: events)
149 
150  if (parent)
151  popObject(&parent->_gem5_object->children, _name);
152  else
153  popObject(&topLevelObjects, _name);
154  popObject(&allObjects, _name);
155 }
156 
157 const char *
159 {
160  return _name.c_str();
161 }
162 
163 const char *
165 {
166  return _basename.c_str();
167 }
168 
169 void
170 Object::print(std::ostream &out) const
171 {
172  out << name();
173 }
174 
175 void
176 Object::dump(std::ostream &out) const
177 {
178  out << "name = " << name() << "\n";
179  out << "kind = " << _sc_obj->kind() << "\n";
180 }
181 
184 {
185  return children;
186 }
187 
190 {
191  return events;
192 }
193 
195 {
196  return parent;
197 }
198 
199 bool
201 {
202  return cltn.push_back(&attr);
203 }
204 
206 Object::get_attribute(const std::string &attr)
207 {
208  return cltn[attr];
209 }
210 
212 Object::remove_attribute(const std::string &attr)
213 {
214  return cltn.remove(attr);
215 }
216 
217 void
219 {
220  cltn.remove_all();
221 }
222 
223 int
225 {
226  return cltn.size();
227 }
228 
231 {
232  return cltn;
233 }
234 
235 const sc_core::sc_attr_cltn &
236 Object::attr_cltn() const
237 {
238  return cltn;
239 }
240 
243 {
245 }
246 
247 EventsIt
249 {
250  return events.emplace(events.end(), e);
251 }
252 
253 void
255 {
256  EventsIt it = std::find(events.begin(), events.end(), e);
257  assert(it != events.end());
258  std::swap(*it, events.back());
259  events.pop_back();
260 }
261 
262 std::string
264 {
265  std::string seed = base;
266  while (!nameIsUnique(&children, &events, base))
267  base = ::sc_core::sc_gen_unique_name(seed.c_str());
268 
269  return base;
270 }
271 
272 std::string
273 pickUniqueName(::sc_core::sc_object *parent, std::string base)
274 {
275  if (parent)
277 
278  std::string seed = base;
279  while (!nameIsUnique(&topLevelObjects, &topLevelEvents, base))
280  base = ::sc_core::sc_gen_unique_name(seed.c_str());
281 
282  return base;
283 }
284 
285 
288 
291 {
292  return topLevelObjects;
293 }
294 
296 findObject(const char *name, const Objects &objects)
297 {
298  ObjectsIt it = findObjectIn(allObjects, name);
299  return it == allObjects.end() ? nullptr : *it;
300 }
301 
302 namespace
303 {
304 
305 std::stack<sc_core::sc_object *> objParentStack;
306 
307 } // anonymous namespace
308 
311 {
312  if (!objParentStack.empty())
313  return objParentStack.top();
314 
315  Process *p = scheduler.current();
316  if (p)
317  return p;
318 
319  return nullptr;
320 }
321 
322 void pushParentObj(sc_core::sc_object *obj) { objParentStack.push(obj); }
323 void popParentObj() { objParentStack.pop(); }
324 
325 } // namespace sc_gem5
sc_gem5::pickParentObj
sc_core::sc_object * pickParentObj()
Definition: object.cc:310
sc_gem5::Object::getFromScObject
static Object * getFromScObject(sc_core::sc_object *sc_obj)
Definition: object.hh:81
sc_gem5::Object::print
void print(std::ostream &=std::cout) const
Definition: object.cc:170
sc_gem5::Object::get_attribute
sc_core::sc_attr_base * get_attribute(const std::string &)
Definition: object.cc:206
sc_gem5::Object::Object
Object(sc_core::sc_object *_sc_obj)
Definition: object.cc:86
sc_gem5::getTopLevelScObjects
const std::vector< sc_core::sc_object * > & getTopLevelScObjects()
Definition: object.cc:290
module.hh
sc_gem5::topLevelObjects
Objects topLevelObjects
Definition: object.cc:286
sc_core::sc_attr_cltn
Definition: sc_attr.hh:74
sc_core::sc_object::_gem5_object
sc_gem5::Object * _gem5_object
Definition: sc_object.hh:85
sc_gem5::Event::getFromScEvent
static Event * getFromScEvent(sc_core::sc_event *e)
Definition: event.hh:91
sc_gem5::EventsIt
Events::iterator EventsIt
Definition: object.hh:45
sc_gem5::popParentObj
void popParentObj()
Definition: object.cc:323
sc_gem5::Object::basename
const char * basename() const
Definition: object.cc:164
X86ISA::base
Bitfield< 51, 12 > base
Definition: pagetable.hh:141
std::vector< sc_core::sc_object * >
sc_gem5::pickUniqueName
std::string pickUniqueName(::sc_core::sc_object *parent, std::string base)
Definition: object.cc:273
sc_gem5::Object::_name
std::string _name
Definition: object.hh:97
sc_gem5::Object::get_child_objects
const std::vector< sc_core::sc_object * > & get_child_objects() const
Definition: object.cc:183
sc_gem5::Object::attr_cltn
sc_core::sc_attr_cltn & attr_cltn()
Definition: object.cc:230
sc_gem5::Object::addChildEvent
EventsIt addChildEvent(sc_core::sc_event *e)
Definition: object.cc:248
sc_core::sc_attr_base
Definition: sc_attr.hh:37
sc_gem5::Object::remove_attribute
sc_core::sc_attr_base * remove_attribute(const std::string &)
Definition: object.cc:212
sc_core::sc_get_curr_simcontext
sc_simcontext * sc_get_curr_simcontext()
Definition: sc_simcontext.cc:85
ArmISA::n
Bitfield< 31 > n
Definition: miscregs_types.hh:450
Events
Events
Definition: ide_disk.hh:138
sc_gem5::Object::name
const char * name() const
Definition: object.cc:158
sc_gem5::Object::_sc_obj
sc_core::sc_object * _sc_obj
Definition: object.hh:94
sc_gem5::Object::~Object
virtual ~Object()
Definition: object.cc:138
sc_core::sc_event
Definition: sc_event.hh:169
sc_gem5::Object::cltn
sc_core::sc_attr_cltn cltn
Definition: object.hh:103
MipsISA::event
Bitfield< 10, 5 > event
Definition: pra_constants.hh:297
sc_gem5::Object
Definition: object.hh:47
ArmISA::attr
attr
Definition: miscregs_types.hh:649
sc_core::sc_simcontext
Definition: sc_simcontext.hh:37
messages.hh
sc_core::sc_gen_unique_name
const char * sc_gen_unique_name(const char *seed)
Definition: sc_module.cc:820
sc_gem5::Object::simcontext
sc_core::sc_simcontext * simcontext() const
Definition: object.cc:242
sc_simcontext.hh
sc_core::sc_attr_cltn::remove
sc_attr_base * remove(const std::string &name)
Definition: sc_attr.cc:98
sc_gem5::Process
Definition: process.hh:62
sc_core::sc_object
Definition: sc_object.hh:50
name
const std::string & name()
Definition: trace.cc:50
sc_gem5::Object::num_attributes
int num_attributes() const
Definition: object.cc:224
sc_module.hh
sc_core::SC_ID_INSTANCE_EXISTS_
const char SC_ID_INSTANCE_EXISTS_[]
Definition: messages.cc:39
sc_gem5::Object::dump
void dump(std::ostream &=std::cout) const
Definition: object.cc:176
sc_gem5::Object::delChildEvent
void delChildEvent(sc_core::sc_event *e)
Definition: object.cc:254
sc_gem5::Object::parent
sc_core::sc_object * parent
Definition: object.hh:101
ArmISA::e
Bitfield< 9 > e
Definition: miscregs_types.hh:61
sc_gem5::pushParentObj
void pushParentObj(sc_core::sc_object *obj)
Definition: object.cc:322
sc_gem5::newModule
Module * newModule()
Definition: module.cc:206
sc_gem5::findObject
sc_core::sc_object * findObject(const char *name, const Objects &objects)
Definition: object.cc:296
SC_REPORT_WARNING
#define SC_REPORT_WARNING(msg_type, msg)
Definition: sc_report_handler.hh:123
sc_gem5::Object::children
Objects children
Definition: object.hh:99
sc_gem5::Object::_basename
std::string _basename
Definition: object.hh:96
sc_gem5::Scheduler::current
Process * current()
Definition: scheduler.hh:170
sc_gem5::Object::pickUniqueName
std::string pickUniqueName(std::string name)
Definition: object.cc:263
sc_gem5::Event::clearParent
void clearParent()
Definition: event.cc:209
sc_gem5::Object::get_child_events
const std::vector< sc_core::sc_event * > & get_child_events() const
Definition: object.cc:189
sc_gem5::ObjectsIt
Objects::iterator ObjectsIt
Definition: object.hh:44
sc_core::sc_attr_cltn::size
int size() const
Definition: sc_attr.cc:113
object.hh
sc_gem5::topLevelEvents
Events topLevelEvents
Definition: event.cc:218
sc_gem5
Definition: sc_clock.cc:42
event.hh
sc_gem5::Objects
std::vector< sc_core::sc_object * > Objects
Definition: object.hh:40
MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:323
sc_gem5::Object::remove_all_attributes
void remove_all_attributes()
Definition: object.cc:218
sc_core::sc_object::kind
virtual const char * kind() const
Definition: sc_object.hh:56
sc_gem5::scheduler
Scheduler scheduler
Definition: scheduler.cc:489
sc_gem5::Object::operator=
Object & operator=(const Object &)
Definition: object.cc:133
sc_core::sc_attr_cltn::remove_all
void remove_all()
Definition: sc_attr.cc:111
sc_gem5::allObjects
Objects allObjects
Definition: object.cc:287
sc_gem5::Object::add_attribute
bool add_attribute(sc_core::sc_attr_base &)
Definition: object.cc:200
sc_gem5::Module
Definition: module.hh:71
scheduler.hh
sc_core::sc_attr_cltn::push_back
bool push_back(sc_attr_base *)
Definition: sc_attr.cc:70
sc_gem5::Object::events
Events events
Definition: object.hh:100
sc_gem5::Object::get_parent_object
sc_core::sc_object * get_parent_object() const
Definition: object.cc:194
sc_core::sc_object::get_parent_object
sc_object * get_parent_object() const
Definition: sc_object.cc:80
sc_core::sc_object::basename
const char * basename() const
Definition: sc_object.cc:50

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