object.cc revision 12950:af1f0b5e8dfb
1572SN/A/*
21762SN/A * Copyright 2018 Google, Inc.
3572SN/A *
4572SN/A * Redistribution and use in source and binary forms, with or without
5572SN/A * modification, are permitted provided that the following conditions are
6572SN/A * met: redistributions of source code must retain the above copyright
7572SN/A * notice, this list of conditions and the following disclaimer;
8572SN/A * redistributions in binary form must reproduce the above copyright
9572SN/A * notice, this list of conditions and the following disclaimer in the
10572SN/A * documentation and/or other materials provided with the distribution;
11572SN/A * neither the name of the copyright holders nor the names of its
12572SN/A * contributors may be used to endorse or promote products derived from
13572SN/A * this software without specific prior written permission.
14572SN/A *
15572SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16572SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17572SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18572SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19572SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20572SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21572SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22572SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23572SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24572SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25572SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26572SN/A *
272665SN/A * Authors: Gabe Black
282665SN/A */
29572SN/A
30572SN/A#include "systemc/core/object.hh"
3111263Sandreas.sandberg@arm.com
3211263Sandreas.sandberg@arm.com#include "base/logging.hh"
33572SN/A#include "systemc/core/module.hh"
34572SN/A
3511263Sandreas.sandberg@arm.comnamespace sc_gem5
361153SN/A{
37572SN/A
38572SN/Anamespace
39572SN/A{
40572SN/A
41572SN/AObjectsIt
4210905SN/AfindObjectIn(Objects &objects, const std::string &name)
43572SN/A{
4411701Smichael.lebeane@amd.com    ObjectsIt it;
4511719Smichael.lebeane@amd.com    for (it = objects.begin(); it != objects.end(); it++)
4610905SN/A        if (!strcmp((*it)->name(), name.c_str()))
4710905SN/A            break;
48572SN/A
49572SN/A    return it;
50572SN/A}
5110905SN/A
52572SN/Avoid
5310905SN/AaddObject(Objects *objects, sc_core::sc_object *object)
5411719Smichael.lebeane@amd.com{
5511719Smichael.lebeane@amd.com    objects->emplace(objects->end(), object);
5611719Smichael.lebeane@amd.com}
5711719Smichael.lebeane@amd.com
5811719Smichael.lebeane@amd.comvoid
5911719Smichael.lebeane@amd.compopObject(Objects *objects, const std::string &name)
6011719Smichael.lebeane@amd.com{
6111719Smichael.lebeane@amd.com    ObjectsIt it = findObjectIn(*objects, name);
6211719Smichael.lebeane@amd.com    assert(it != objects->end());
6311719Smichael.lebeane@amd.com    std::swap(objects->back(), *it);
6411719Smichael.lebeane@amd.com    objects->pop_back();
6511719Smichael.lebeane@amd.com}
6611701Smichael.lebeane@amd.com
6711719Smichael.lebeane@amd.com} // anonymous namespace
6811719Smichael.lebeane@amd.com
6911719Smichael.lebeane@amd.comObject::Object(sc_core::sc_object *sc_obj) : Object(sc_obj, "object") {}
7011719Smichael.lebeane@amd.com
7111701Smichael.lebeane@amd.comObject::Object(sc_core::sc_object *sc_obj, const char *obj_name) :
7211701Smichael.lebeane@amd.com    sc_obj(sc_obj), _basename(obj_name), parent(nullptr)
73572SN/A{
7410923SN/A    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 = 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