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

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