object.cc revision 13179:7445c43d036b
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
34#include "base/logging.hh"
35#include "systemc/core/event.hh"
36#include "systemc/core/module.hh"
37#include "systemc/core/scheduler.hh"
38#include "systemc/ext/core/sc_module.hh"
39
40namespace sc_gem5
41{
42
43namespace
44{
45
46ObjectsIt
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 == "")
92        _basename = ::sc_core::sc_gen_unique_name("object");
93
94    Module *p = currentModule();
95    if (!p)
96        p = callbackModule();
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    if (p) {
105        // We're "within" a parent module, ie we're being created while its
106        // constructor or end_of_elaboration callback is running.
107        parent = p->obj()->_sc_obj;
108    } else if (scheduler.current()) {
109        // Our parent is the currently running process.
110        parent = scheduler.current();
111    }
112
113    std::string original_name = _basename;
114    _basename = sc_gem5::pickUniqueName(parent, original_name);
115
116    if (parent)
117        addObject(&parent->_gem5_object->children, _sc_obj);
118    else
119        addObject(&topLevelObjects, _sc_obj);
120
121    addObject(&allObjects, _sc_obj);
122
123    sc_core::sc_object *sc_p = parent;
124    std::string path = "";
125    while (sc_p) {
126        path = std::string(sc_p->basename()) + std::string(".") + path;
127        sc_p = sc_p->get_parent_object();
128    }
129
130    if (_basename != original_name) {
131        std::string message = path + original_name +
132            ". Latter declaration will be renamed to " +
133            path + _basename;
134        SC_REPORT_WARNING("(W505) object already exists", message.c_str());
135    }
136    _name = path + _basename;
137}
138
139Object::Object(sc_core::sc_object *_sc_obj, const Object &arg) :
140    Object(_sc_obj, arg._basename.c_str())
141{}
142
143Object &
144Object::operator = (const Object &)
145{
146    return *this;
147}
148
149Object::~Object()
150{
151    // Promote all children to be top level objects.
152    for (auto child: children) {
153        addObject(&topLevelObjects, child);
154        child->_gem5_object->parent = nullptr;
155    }
156    children.clear();
157
158    if (parent)
159        popObject(&parent->_gem5_object->children, _name);
160    else
161        popObject(&topLevelObjects, _name);
162    popObject(&allObjects, _name);
163}
164
165const char *
166Object::name() const
167{
168    return _name.c_str();
169}
170
171const char *
172Object::basename() const
173{
174    return _basename.c_str();
175}
176
177void
178Object::print(std::ostream &out) const
179{
180    out << name();
181}
182
183void
184Object::dump(std::ostream &out) const
185{
186    out << "name = " << name() << "\n";
187    out << "kind = " << _sc_obj->kind() << "\n";
188}
189
190const std::vector<sc_core::sc_object *> &
191Object::get_child_objects() const
192{
193    return children;
194}
195
196const std::vector<sc_core::sc_event *> &
197Object::get_child_events() const
198{
199    return events;
200}
201
202sc_core::sc_object *Object::get_parent_object() const
203{
204    return parent;
205}
206
207bool
208Object::add_attribute(sc_core::sc_attr_base &attr)
209{
210    return cltn.push_back(&attr);
211}
212
213sc_core::sc_attr_base *
214Object::get_attribute(const std::string &attr)
215{
216    return cltn[attr];
217}
218
219sc_core::sc_attr_base *
220Object::remove_attribute(const std::string &attr)
221{
222    return cltn.remove(attr);
223}
224
225void
226Object::remove_all_attributes()
227{
228    cltn.remove_all();
229}
230
231int
232Object::num_attributes() const
233{
234    return cltn.size();
235}
236
237sc_core::sc_attr_cltn &
238Object::attr_cltn()
239{
240    return cltn;
241}
242
243const sc_core::sc_attr_cltn &
244Object::attr_cltn() const
245{
246    return cltn;
247}
248
249sc_core::sc_simcontext *
250Object::simcontext() const
251{
252    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
253    return nullptr;
254}
255
256EventsIt
257Object::addChildEvent(sc_core::sc_event *e)
258{
259    return events.emplace(events.end(), e);
260}
261
262void
263Object::delChildEvent(sc_core::sc_event *e)
264{
265    EventsIt it = std::find(events.begin(), events.end(), e);
266    assert(it != events.end());
267    std::swap(*it, events.back());
268    events.pop_back();
269}
270
271std::string
272Object::pickUniqueName(std::string base)
273{
274    std::string seed = base;
275    while (!nameIsUnique(&children, &events, base))
276        base = ::sc_core::sc_gen_unique_name(seed.c_str());
277
278    return base;
279}
280
281std::string
282pickUniqueName(::sc_core::sc_object *parent, std::string base)
283{
284    if (parent)
285        return Object::getFromScObject(parent)->pickUniqueName(base);
286
287    std::string seed = base;
288    while (!nameIsUnique(&topLevelObjects, &topLevelEvents, base))
289        base = ::sc_core::sc_gen_unique_name(seed.c_str());
290
291    return base;
292}
293
294
295Objects topLevelObjects;
296Objects allObjects;
297
298const std::vector<sc_core::sc_object *> &
299getTopLevelScObjects()
300{
301    return topLevelObjects;
302}
303
304sc_core::sc_object *
305findObject(const char *name, const Objects &objects)
306{
307    ObjectsIt it = findObjectIn(allObjects, name);
308    return it == allObjects.end() ? nullptr : *it;
309}
310
311} // namespace sc_gem5
312