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