Deleted Added
sdiff udiff text old ( 13268:9802f3e0a6ae ) new ( 13295:e71ae162b863 )
full compact
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 "base/logging.hh"
36#include "systemc/core/event.hh"
37#include "systemc/core/module.hh"
38#include "systemc/core/scheduler.hh"
39#include "systemc/ext/core/sc_module.hh"
40
41namespace sc_gem5
42{
43
44namespace
45{
46
47ObjectsIt
48findObjectIn(Objects &objects, const std::string &name)
49{
50 ObjectsIt it;
51 for (it = objects.begin(); it != objects.end(); it++)
52 if (!strcmp((*it)->name(), name.c_str()))
53 break;
54
55 return it;
56}
57
58void
59addObject(Objects *objects, sc_core::sc_object *object)
60{
61 objects->emplace(objects->end(), object);
62}
63
64void
65popObject(Objects *objects, const std::string &name)
66{
67 ObjectsIt it = findObjectIn(*objects, name);
68 assert(it != objects->end());
69 std::swap(objects->back(), *it);
70 objects->pop_back();
71}
72
73bool
74nameIsUnique(Objects *objects, Events *events, const std::string &name)
75{
76 for (auto obj: *objects)
77 if (!strcmp(obj->basename(), name.c_str()))
78 return false;
79 for (auto event: *events)
80 if (!strcmp(event->basename(), name.c_str()))
81 return false;
82 return true;
83}
84
85} // anonymous namespace
86
87Object::Object(sc_core::sc_object *_sc_obj) : Object(_sc_obj, nullptr) {}
88
89Object::Object(sc_core::sc_object *_sc_obj, const char *obj_name) :
90 _sc_obj(_sc_obj), _basename(obj_name ? obj_name : ""), parent(nullptr)
91{
92 if (_basename == "")
93 _basename = ::sc_core::sc_gen_unique_name("object");
94
95 parent = pickParentObj();
96
97 Module *n = newModule();
98 if (n) {
99 // We are a module in the process of being constructed.
100 n->finish(this);
101 }
102
103 std::string original_name = _basename;
104 _basename = sc_gem5::pickUniqueName(parent, original_name);
105
106 if (parent)
107 addObject(&parent->_gem5_object->children, _sc_obj);
108 else
109 addObject(&topLevelObjects, _sc_obj);
110
111 addObject(&allObjects, _sc_obj);
112
113 sc_core::sc_object *sc_p = parent;
114 std::string path = "";
115 while (sc_p) {
116 path = std::string(sc_p->basename()) + std::string(".") + path;
117 sc_p = sc_p->get_parent_object();
118 }
119
120 if (_basename != original_name) {
121 std::string message = path + original_name +
122 ". Latter declaration will be renamed to " +
123 path + _basename;
124 SC_REPORT_WARNING("(W505) object already exists", message.c_str());
125 }
126 _name = path + _basename;
127}
128
129Object::Object(sc_core::sc_object *_sc_obj, const Object &arg) :
130 Object(_sc_obj, arg._basename.c_str())
131{}
132
133Object &
134Object::operator = (const Object &)
135{
136 return *this;
137}
138
139Object::~Object()
140{
141 // Promote all children to be top level objects.
142 for (auto child: children) {
143 addObject(&topLevelObjects, child);
144 child->_gem5_object->parent = nullptr;
145 }
146 children.clear();
147
148 for (auto event: events)
149 Event::getFromScEvent(event)->clearParent();
150
151 if (parent)
152 popObject(&parent->_gem5_object->children, _name);
153 else
154 popObject(&topLevelObjects, _name);
155 popObject(&allObjects, _name);
156}
157
158const char *
159Object::name() const
160{
161 return _name.c_str();
162}
163
164const char *
165Object::basename() const
166{
167 return _basename.c_str();
168}
169
170void
171Object::print(std::ostream &out) const
172{
173 out << name();
174}
175
176void
177Object::dump(std::ostream &out) const
178{
179 out << "name = " << name() << "\n";
180 out << "kind = " << _sc_obj->kind() << "\n";
181}
182
183const std::vector<sc_core::sc_object *> &
184Object::get_child_objects() const
185{
186 return children;
187}
188
189const std::vector<sc_core::sc_event *> &
190Object::get_child_events() const
191{
192 return events;
193}
194
195sc_core::sc_object *Object::get_parent_object() const
196{
197 return parent;
198}
199
200bool
201Object::add_attribute(sc_core::sc_attr_base &attr)
202{
203 return cltn.push_back(&attr);
204}
205
206sc_core::sc_attr_base *
207Object::get_attribute(const std::string &attr)
208{
209 return cltn[attr];
210}
211
212sc_core::sc_attr_base *
213Object::remove_attribute(const std::string &attr)
214{
215 return cltn.remove(attr);
216}
217
218void
219Object::remove_all_attributes()
220{
221 cltn.remove_all();
222}
223
224int
225Object::num_attributes() const
226{
227 return cltn.size();
228}
229
230sc_core::sc_attr_cltn &
231Object::attr_cltn()
232{
233 return cltn;
234}
235
236const sc_core::sc_attr_cltn &
237Object::attr_cltn() const
238{
239 return cltn;
240}
241
242sc_core::sc_simcontext *
243Object::simcontext() const
244{
245 warn("%s not implemented.\n", __PRETTY_FUNCTION__);
246 return nullptr;
247}
248
249EventsIt
250Object::addChildEvent(sc_core::sc_event *e)
251{
252 return events.emplace(events.end(), e);
253}
254
255void
256Object::delChildEvent(sc_core::sc_event *e)
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
264std::string
265Object::pickUniqueName(std::string base)
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
274std::string
275pickUniqueName(::sc_core::sc_object *parent, std::string base)
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
288Objects topLevelObjects;
289Objects allObjects;
290
291const std::vector<sc_core::sc_object *> &
292getTopLevelScObjects()
293{
294 return topLevelObjects;
295}
296
297sc_core::sc_object *
298findObject(const char *name, const Objects &objects)
299{
300 ObjectsIt it = findObjectIn(allObjects, name);
301 return it == allObjects.end() ? nullptr : *it;
302}
303
304namespace
305{
306
307std::stack<sc_core::sc_object *> objParentStack;
308
309} // anonymous namespace
310
311sc_core::sc_object *
312pickParentObj()
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
324void pushParentObj(sc_core::sc_object *obj) { objParentStack.push(obj); }
325void popParentObj() { objParentStack.pop(); }
326
327} // namespace sc_gem5