gem5 v24.0.0.0
Loading...
Searching...
No Matches
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
29
30#include <algorithm>
31#include <stack>
32
33#include "systemc/core/event.hh"
39
40namespace sc_gem5
41{
42
43namespace
44{
45
47findObjectIn(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
57void
58addObject(Objects *objects, sc_core::sc_object *object)
59{
60 objects->emplace(objects->end(), object);
61}
62
63void
64popObject(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
72bool
73nameIsUnique(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
86Object::Object(sc_core::sc_object *_sc_obj) : Object(_sc_obj, nullptr) {}
87
88Object::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
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
129 Object(_sc_obj, arg._basename.c_str())
130{}
131
132Object &
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
157const char *
159{
160 return _name.c_str();
161}
162
163const char *
165{
166 return _basename.c_str();
167}
168
169void
170Object::print(std::ostream &out) const
171{
172 out << name();
173}
174
175void
176Object::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
198
199bool
201{
202 return cltn.push_back(&attr);
203}
204
206Object::get_attribute(const std::string &attr)
207{
208 return cltn[attr];
209}
210
212Object::remove_attribute(const std::string &attr)
213{
214 return cltn.remove(attr);
215}
216
217void
222
223int
225{
226 return cltn.size();
227}
228
231{
232 return cltn;
233}
234
237{
238 return cltn;
239}
240
246
249{
250 return events.emplace(events.end(), e);
251}
252
253void
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
262std::string
263Object::pickUniqueName(std::string base)
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
272std::string
273pickUniqueName(::sc_core::sc_object *parent, std::string base)
274{
275 if (parent)
276 return Object::getFromScObject(parent)->pickUniqueName(base);
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
296findObject(const char *name, const Objects &objects)
297{
298 ObjectsIt it = findObjectIn(allObjects, name);
299 return it == allObjects.end() ? nullptr : *it;
300}
301
302namespace
303{
304
305std::stack<sc_core::sc_object *> objParentStack;
306
307} // anonymous namespace
308
311{
312 if (!objParentStack.empty())
313 return objParentStack.top();
314
316 if (p)
317 return p;
318
319 return nullptr;
320}
321
322void pushParentObj(sc_core::sc_object *obj) { objParentStack.push(obj); }
323void popParentObj() { objParentStack.pop(); }
324
325} // namespace sc_gem5
bool push_back(sc_attr_base *)
Definition sc_attr.cc:70
sc_attr_base * remove(const std::string &name)
Definition sc_attr.cc:98
sc_object * get_parent_object() const
Definition sc_object.cc:80
const char * basename() const
Definition sc_object.cc:50
sc_gem5::Object * _gem5_object
Definition sc_object.hh:85
virtual const char * kind() const
Definition sc_object.hh:56
static Event * getFromScEvent(sc_core::sc_event *e)
Definition event.hh:91
void clearParent()
Definition event.cc:208
sc_core::sc_attr_base * remove_attribute(const std::string &)
Definition object.cc:212
const char * name() const
Definition object.cc:158
EventsIt addChildEvent(sc_core::sc_event *e)
Definition object.cc:248
const std::vector< sc_core::sc_event * > & get_child_events() const
Definition object.cc:189
sc_core::sc_simcontext * simcontext() const
Definition object.cc:242
int num_attributes() const
Definition object.cc:224
Object & operator=(const Object &)
Definition object.cc:133
Objects children
Definition object.hh:99
std::string _name
Definition object.hh:97
void delChildEvent(sc_core::sc_event *e)
Definition object.cc:254
const char * basename() const
Definition object.cc:164
sc_core::sc_object * get_parent_object() const
Definition object.cc:194
sc_core::sc_attr_cltn cltn
Definition object.hh:103
bool add_attribute(sc_core::sc_attr_base &)
Definition object.cc:200
sc_core::sc_object * _sc_obj
Definition object.hh:94
Events events
Definition object.hh:100
std::string pickUniqueName(std::string name)
Definition object.cc:263
void remove_all_attributes()
Definition object.cc:218
void dump(std::ostream &=std::cout) const
Definition object.cc:176
virtual ~Object()
Definition object.cc:138
void print(std::ostream &=std::cout) const
Definition object.cc:170
sc_core::sc_object * parent
Definition object.hh:101
Object(sc_core::sc_object *_sc_obj)
Definition object.cc:86
std::string _basename
Definition object.hh:96
const std::vector< sc_core::sc_object * > & get_child_objects() const
Definition object.cc:183
sc_core::sc_attr_cltn & attr_cltn()
Definition object.cc:230
static Object * getFromScObject(sc_core::sc_object *sc_obj)
Definition object.hh:81
sc_core::sc_attr_base * get_attribute(const std::string &)
Definition object.cc:206
Process * current()
Definition scheduler.hh:185
STL vector class.
Definition stl.hh:37
Bitfield< 10, 5 > event
const char SC_ID_INSTANCE_EXISTS_[]
Definition messages.cc:39
sc_simcontext * sc_get_curr_simcontext()
const char * sc_gen_unique_name(const char *seed)
Definition sc_module.cc:820
void popParentObj()
Definition object.cc:323
const std::vector< sc_core::sc_object * > & getTopLevelScObjects()
Definition object.cc:290
std::vector< sc_core::sc_event * > Events
Definition event.hh:54
Objects allObjects
Definition object.cc:287
std::string pickUniqueName(::sc_core::sc_object *parent, std::string base)
Definition object.cc:273
Events::iterator EventsIt
Definition object.hh:45
Events topLevelEvents
Definition event.cc:217
sc_core::sc_object * findObject(const char *name, const Objects &objects)
Definition object.cc:296
sc_core::sc_object * pickParentObj()
Definition object.cc:310
Scheduler scheduler
Definition scheduler.cc:494
Module * newModule()
Definition module.cc:209
std::vector< sc_core::sc_object * > Objects
Definition object.hh:42
Objects::iterator ObjectsIt
Definition object.hh:44
void pushParentObj(sc_core::sc_object *obj)
Definition object.cc:322
Objects topLevelObjects
Definition object.cc:286
#define SC_REPORT_WARNING(msg_type, msg)
const std::string & name()
Definition trace.cc:48

Generated on Tue Jun 18 2024 16:24:06 for gem5 by doxygen 1.11.0