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"
37#include "systemc/core/scheduler.hh"
38#include "systemc/ext/core/messages.hh"
39#include "systemc/ext/core/sc_module.hh"
40#include "systemc/ext/core/sc_simcontext.hh"
41
42namespace sc_gem5
43{
44
45namespace
46{
47
48ObjectsIt
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
86} // anonymous namespace
87
88Object::Object(sc_core::sc_object *_sc_obj) : Object(_sc_obj, nullptr) {}
89
90Object::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 == "")
94        _basename = ::sc_core::sc_gen_unique_name("object");
95
96    parent = pickParentObj();
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;
125        SC_REPORT_WARNING(sc_core::SC_ID_INSTANCE_EXISTS_, message.c_str());
126    }
127    _name = path + _basename;
128}
129
130Object::Object(sc_core::sc_object *_sc_obj, const Object &arg) :
131    Object(_sc_obj, arg._basename.c_str())
132{}
133
134Object &
135Object::operator = (const Object &)
136{
137    return *this;
138}
139
140Object::~Object()
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)
150        Event::getFromScEvent(event)->clearParent();
151
152    if (parent)
153        popObject(&parent->_gem5_object->children, _name);
154    else
155        popObject(&topLevelObjects, _name);
156    popObject(&allObjects, _name);
157}
158
159const char *
160Object::name() const
161{
162    return _name.c_str();
163}
164
165const char *
166Object::basename() const
167{
168    return _basename.c_str();
169}
170
171void
172Object::print(std::ostream &out) const
173{
174    out << name();
175}
176
177void
178Object::dump(std::ostream &out) const
179{
180    out << "name = " << name() << "\n";
181    out << "kind = " << _sc_obj->kind() << "\n";
182}
183
184const std::vector<sc_core::sc_object *> &
185Object::get_child_objects() const
186{
187    return children;
188}
189
190const std::vector<sc_core::sc_event *> &
191Object::get_child_events() const
192{
193    return events;
194}
195
196sc_core::sc_object *Object::get_parent_object() const
197{
198    return parent;
199}
200
201bool
202Object::add_attribute(sc_core::sc_attr_base &attr)
203{
204    return cltn.push_back(&attr);
205}
206
207sc_core::sc_attr_base *
208Object::get_attribute(const std::string &attr)
209{
210    return cltn[attr];
211}
212
213sc_core::sc_attr_base *
214Object::remove_attribute(const std::string &attr)
215{
216    return cltn.remove(attr);
217}
218
219void
220Object::remove_all_attributes()
221{
222    cltn.remove_all();
223}
224
225int
226Object::num_attributes() const
227{
228    return cltn.size();
229}
230
231sc_core::sc_attr_cltn &
232Object::attr_cltn()
233{
234    return cltn;
235}
236
237const sc_core::sc_attr_cltn &
238Object::attr_cltn() const
239{
240    return cltn;
241}
242
243sc_core::sc_simcontext *
244Object::simcontext() const
245{
246    return sc_core::sc_get_curr_simcontext();
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
328