gem5 [DEVELOP-FOR-25.1]
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 <mutex>
32#include <shared_mutex>
33#include <stack>
34
35#include "systemc/core/event.hh"
41
42namespace sc_gem5
43{
44
45namespace
46{
47
49findObjectIn(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
59void
60addObject(Objects *objects, sc_core::sc_object *object)
61{
62 objects->emplace(objects->end(), object);
63}
64
65void
66popObject(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
74bool
75nameIsUnique(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
86std::shared_mutex globalObjectLock;
87std::stack<sc_core::sc_object *> objParentStack;
88
89} // anonymous namespace
90
93
95
96Object::Object(sc_core::sc_object *_sc_obj, const char *obj_name) :
97 _sc_obj(_sc_obj), _basename(obj_name ? obj_name : ""), parent(nullptr)
98{
99 [[maybe_unused]] std::unique_lock lock(globalObjectLock);
100
101 if (_basename == "")
103
105
106 Module *n = newModule();
107 if (n) {
108 // We are a module in the process of being constructed.
109 n->finish(this);
110 }
111
112 std::string original_name = _basename;
113 _basename = sc_gem5::pickUniqueName(parent, original_name);
114
115 if (parent)
116 addObject(&parent->_gem5_object->children, _sc_obj);
117 else
118 addObject(&topLevelObjects, _sc_obj);
119
120 addObject(&allObjects, _sc_obj);
121
123 std::string path = "";
124 while (sc_p) {
125 path = std::string(sc_p->basename()) + std::string(".") + path;
126 sc_p = sc_p->get_parent_object();
127 }
128
129 if (_basename != original_name) {
130 std::string message = path + original_name +
131 ". Latter declaration will be renamed to " +
132 path + _basename;
134 }
135 _name = path + _basename;
136}
137
141
142Object &
144{
145 return *this;
146}
147
149{
150 [[maybe_unused]] std::unique_lock lock(globalObjectLock);
151
152 // Promote all children to be top level objects.
153 for (auto child: children) {
154 addObject(&topLevelObjects, child);
155 child->_gem5_object->parent = nullptr;
156 }
157 children.clear();
158
159 for (auto event: events)
161
162 if (parent)
163 popObject(&parent->_gem5_object->children, _name);
164 else
165 popObject(&topLevelObjects, _name);
166 popObject(&allObjects, _name);
167}
168
169const char *
171{
172 return _name.c_str();
173}
174
175const char *
177{
178 return _basename.c_str();
179}
180
181void
182Object::print(std::ostream &out) const
183{
184 out << name();
185}
186
187void
188Object::dump(std::ostream &out) const
189{
190 out << "name = " << name() << "\n";
191 out << "kind = " << _sc_obj->kind() << "\n";
192}
193
196{
197 return children;
198}
199
202{
203 return events;
204}
205
210
211bool
213{
214 return cltn.push_back(&attr);
215}
216
218Object::get_attribute(const std::string &attr)
219{
220 return cltn[attr];
221}
222
224Object::remove_attribute(const std::string &attr)
225{
226 return cltn.remove(attr);
227}
228
229void
231{
232 cltn.remove_all();
233}
234
235int
237{
238 return cltn.size();
239}
240
243{
244 return cltn;
245}
246
249{
250 return cltn;
251}
252
258
261{
262 [[maybe_unused]] std::unique_lock lock(_lock);
263
264 return events.emplace(events.end(), e);
265}
266
267void
269{
270 [[maybe_unused]] std::unique_lock lock(_lock);
271
272 EventsIt it = std::find(events.begin(), events.end(), e);
273 assert(it != events.end());
274 std::swap(*it, events.back());
275 events.pop_back();
276}
277
278std::string
279Object::pickUniqueName(std::string base)
280{
281 [[maybe_unused]] std::shared_lock lock(_lock);
282 std::string seed = base;
283 while (!nameIsUnique(&children, &events, base))
284 base = ::sc_core::sc_gen_unique_name(seed.c_str());
285
286 return base;
287}
288
289std::string
290pickUniqueName(::sc_core::sc_object *parent, std::string base)
291{
292 if (parent)
293 return Object::getFromScObject(parent)->pickUniqueName(base);
294
295 std::string seed = base;
296 while (!nameIsUnique(&topLevelObjects, &topLevelEvents, base))
297 base = ::sc_core::sc_gen_unique_name(seed.c_str());
298
299 return base;
300}
301
304{
305 return topLevelObjects;
306}
307
309findObject(const char *name)
310{
311 [[maybe_unused]] std::shared_lock lock(globalObjectLock);
312
313 ObjectsIt it = findObjectIn(allObjects, name);
314 return it == allObjects.end() ? nullptr : *it;
315}
316
319{
320 if (!objParentStack.empty())
321 return objParentStack.top();
322
323 Process *p = scheduler.current();
324 if (p)
325 return p;
326
327 return nullptr;
328}
329
330void pushParentObj(sc_core::sc_object *obj) { objParentStack.push(obj); }
331void popParentObj() { objParentStack.pop(); }
332
333} // namespace sc_gem5
sc_object * get_parent_object() const
Definition sc_object.cc:73
const char * basename() const
Definition sc_object.cc:43
static Event * getFromScEvent(sc_core::sc_event *e)
Definition event.hh:91
void clearParent()
Definition event.cc:244
sc_core::sc_attr_base * remove_attribute(const std::string &)
Definition object.cc:224
const char * name() const
Definition object.cc:170
EventsIt addChildEvent(sc_core::sc_event *e)
Definition object.cc:260
const std::vector< sc_core::sc_event * > & get_child_events() const
Definition object.cc:201
sc_core::sc_simcontext * simcontext() const
Definition object.cc:254
std::shared_mutex _lock
Definition object.hh:102
int num_attributes() const
Definition object.cc:236
Object & operator=(const Object &)
Definition object.cc:143
Objects children
Definition object.hh:100
std::string _name
Definition object.hh:98
void delChildEvent(sc_core::sc_event *e)
Definition object.cc:268
const char * basename() const
Definition object.cc:176
sc_core::sc_object * get_parent_object() const
Definition object.cc:206
sc_core::sc_attr_cltn cltn
Definition object.hh:105
bool add_attribute(sc_core::sc_attr_base &)
Definition object.cc:212
sc_core::sc_object * _sc_obj
Definition object.hh:95
Events events
Definition object.hh:101
std::string pickUniqueName(std::string name)
Definition object.cc:279
void remove_all_attributes()
Definition object.cc:230
void dump(std::ostream &=std::cout) const
Definition object.cc:188
virtual ~Object()
Definition object.cc:148
void print(std::ostream &=std::cout) const
Definition object.cc:182
sc_core::sc_object * parent
Definition object.hh:103
Object(sc_core::sc_object *_sc_obj)
Definition object.cc:94
std::string _basename
Definition object.hh:97
const std::vector< sc_core::sc_object * > & get_child_objects() const
Definition object.cc:195
sc_core::sc_attr_cltn & attr_cltn()
Definition object.cc:242
static Object * getFromScObject(sc_core::sc_object *sc_obj)
Definition object.hh:82
sc_core::sc_attr_base * get_attribute(const std::string &)
Definition object.cc:218
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:331
const std::vector< sc_core::sc_object * > & getTopLevelScObjects()
Definition object.cc:303
std::vector< sc_core::sc_event * > Events
Definition event.hh:54
Objects allObjects
Definition object.cc:92
std::string pickUniqueName(::sc_core::sc_object *parent, std::string base)
Definition object.cc:290
Events::iterator EventsIt
Definition object.hh:46
Events topLevelEvents
Definition event.cc:78
sc_core::sc_object * pickParentObj()
Definition object.cc:318
sc_core::sc_object * findObject(const char *name)
Definition object.cc:309
Scheduler scheduler
Definition scheduler.cc:494
Module * newModule()
Definition module.cc:209
std::vector< sc_core::sc_object * > Objects
Definition object.hh:43
Objects::iterator ObjectsIt
Definition object.hh:45
void pushParentObj(sc_core::sc_object *obj)
Definition object.cc:330
Objects topLevelObjects
Definition object.cc:91
#define SC_REPORT_WARNING(msg_type, msg)
const std::string & name()
Definition trace.cc:48

Generated on Mon Oct 27 2025 04:13:04 for gem5 by doxygen 1.14.0