object.cc revision 13126:770f2c828e33
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/module.hh"
36#include "systemc/core/scheduler.hh"
37
38namespace sc_gem5
39{
40
41namespace
42{
43
44ObjectsIt
45findObjectIn(Objects &objects, const std::string &name)
46{
47    ObjectsIt it;
48    for (it = objects.begin(); it != objects.end(); it++)
49        if (!strcmp((*it)->name(), name.c_str()))
50            break;
51
52    return it;
53}
54
55void
56addObject(Objects *objects, sc_core::sc_object *object)
57{
58    objects->emplace(objects->end(), object);
59}
60
61void
62popObject(Objects *objects, const std::string &name)
63{
64    ObjectsIt it = findObjectIn(*objects, name);
65    assert(it != objects->end());
66    std::swap(objects->back(), *it);
67    objects->pop_back();
68}
69
70} // anonymous namespace
71
72Object::Object(sc_core::sc_object *_sc_obj) : Object(_sc_obj, "object") {}
73
74Object::Object(sc_core::sc_object *_sc_obj, const char *obj_name) :
75    _sc_obj(_sc_obj), _basename(obj_name ? obj_name : ""), parent(nullptr)
76{
77    if (_basename == "")
78        _basename = "object";
79
80    Module *p = currentModule();
81    if (!p)
82        p = callbackModule();
83
84    Module *n = newModule();
85    if (n) {
86        // We are a module in the process of being constructed.
87        n->finish(this);
88    }
89
90    if (p) {
91        // We're "within" a parent module, ie we're being created while its
92        // constructor or end_of_elaboration callback is running.
93        parent = p->obj()->_sc_obj;
94    } else if (scheduler.current()) {
95        // Our parent is the currently running process.
96        parent = scheduler.current();
97    }
98    if (parent) {
99        addObject(&parent->_gem5_object->children, _sc_obj);
100    } else {
101        // We're a top level object.
102        addObject(&topLevelObjects, _sc_obj);
103    }
104
105    addObject(&allObjects, _sc_obj);
106
107    _name = _basename;
108    sc_core::sc_object *sc_p = parent;
109    while (sc_p) {
110        _name = std::string(sc_p->basename()) + std::string(".") + _name;
111        sc_p = sc_p->get_parent_object();
112    }
113}
114
115Object::Object(sc_core::sc_object *_sc_obj, const Object &arg) :
116    Object(_sc_obj, arg._basename.c_str())
117{}
118
119Object &
120Object::operator = (const Object &)
121{
122    return *this;
123}
124
125Object::~Object()
126{
127    // Promote all children to be top level objects.
128    for (auto child: children) {
129        addObject(&topLevelObjects, child);
130        child->_gem5_object->parent = nullptr;
131    }
132    children.clear();
133
134    if (parent)
135        popObject(&parent->_gem5_object->children, _name);
136    else
137        popObject(&topLevelObjects, _name);
138    popObject(&allObjects, _name);
139}
140
141const char *
142Object::name() const
143{
144    return _name.c_str();
145}
146
147const char *
148Object::basename() const
149{
150    return _basename.c_str();
151}
152
153void
154Object::print(std::ostream &out) const
155{
156    out << name();
157}
158
159void
160Object::dump(std::ostream &out) const
161{
162    out << "name = " << name() << "\n";
163    out << "kind = " << _sc_obj->kind() << "\n";
164}
165
166const std::vector<sc_core::sc_object *> &
167Object::get_child_objects() const
168{
169    return children;
170}
171
172const std::vector<sc_core::sc_event *> &
173Object::get_child_events() const
174{
175    return events;
176}
177
178sc_core::sc_object *Object::get_parent_object() const
179{
180    return parent;
181}
182
183bool
184Object::add_attribute(sc_core::sc_attr_base &attr)
185{
186    return cltn.push_back(&attr);
187}
188
189sc_core::sc_attr_base *
190Object::get_attribute(const std::string &attr)
191{
192    return cltn[attr];
193}
194
195sc_core::sc_attr_base *
196Object::remove_attribute(const std::string &attr)
197{
198    return cltn.remove(attr);
199}
200
201void
202Object::remove_all_attributes()
203{
204    cltn.remove_all();
205}
206
207int
208Object::num_attributes() const
209{
210    return cltn.size();
211}
212
213sc_core::sc_attr_cltn &
214Object::attr_cltn()
215{
216    return cltn;
217}
218
219const sc_core::sc_attr_cltn &
220Object::attr_cltn() const
221{
222    return cltn;
223}
224
225sc_core::sc_simcontext *
226Object::simcontext() const
227{
228    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
229    return nullptr;
230}
231
232EventsIt
233Object::addChildEvent(sc_core::sc_event *e)
234{
235    return events.emplace(events.end(), e);
236}
237
238void
239Object::delChildEvent(sc_core::sc_event *e)
240{
241    EventsIt it = std::find(events.begin(), events.end(), e);
242    assert(it != events.end());
243    std::swap(*it, events.back());
244    events.pop_back();
245}
246
247
248Objects topLevelObjects;
249Objects allObjects;
250
251const std::vector<sc_core::sc_object *> &
252getTopLevelScObjects()
253{
254    return topLevelObjects;
255}
256
257sc_core::sc_object *
258findObject(const char *name, const Objects &objects)
259{
260    ObjectsIt it = findObjectIn(allObjects, name);
261    return it == allObjects.end() ? nullptr : *it;
262}
263
264} // namespace sc_gem5
265