object.cc revision 13127
112955Sgabeblack@google.com/*
212955Sgabeblack@google.com * Copyright 2018 Google, Inc.
312955Sgabeblack@google.com *
412955Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
512955Sgabeblack@google.com * modification, are permitted provided that the following conditions are
612955Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
712955Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
812955Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
912955Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1012955Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1112955Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1212955Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1312955Sgabeblack@google.com * this software without specific prior written permission.
1412955Sgabeblack@google.com *
1512955Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612955Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712955Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812955Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912955Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012955Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112955Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212955Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312955Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412955Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512955Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612955Sgabeblack@google.com *
2712955Sgabeblack@google.com * Authors: Gabe Black
2812955Sgabeblack@google.com */
2912955Sgabeblack@google.com
3012955Sgabeblack@google.com#include "systemc/core/object.hh"
3112955Sgabeblack@google.com
3212988Sgabeblack@google.com#include <algorithm>
3312955Sgabeblack@google.com
3412955Sgabeblack@google.com#include "base/logging.hh"
3512955Sgabeblack@google.com#include "systemc/core/event.hh"
3612957Sgabeblack@google.com#include "systemc/core/module.hh"
3712957Sgabeblack@google.com#include "systemc/core/scheduler.hh"
3812955Sgabeblack@google.com
3912955Sgabeblack@google.comnamespace sc_gem5
4013086Sgabeblack@google.com{
4113086Sgabeblack@google.com
4212955Sgabeblack@google.comnamespace
4312955Sgabeblack@google.com{
4412955Sgabeblack@google.com
4512955Sgabeblack@google.comObjectsIt
4613086Sgabeblack@google.comfindObjectIn(Objects &objects, const std::string &name)
4712955Sgabeblack@google.com{
4813086Sgabeblack@google.com    ObjectsIt it;
4913086Sgabeblack@google.com    for (it = objects.begin(); it != objects.end(); it++)
5013063Sgabeblack@google.com        if (!strcmp((*it)->name(), name.c_str()))
5112955Sgabeblack@google.com            break;
5212955Sgabeblack@google.com
5312955Sgabeblack@google.com    return it;
5413086Sgabeblack@google.com}
5513086Sgabeblack@google.com
5613086Sgabeblack@google.comvoid
5712955Sgabeblack@google.comaddObject(Objects *objects, sc_core::sc_object *object)
5812955Sgabeblack@google.com{
5912955Sgabeblack@google.com    objects->emplace(objects->end(), object);
6012955Sgabeblack@google.com}
6112988Sgabeblack@google.com
6212988Sgabeblack@google.comvoid
6312955Sgabeblack@google.compopObject(Objects *objects, const std::string &name)
6412955Sgabeblack@google.com{
6512955Sgabeblack@google.com    ObjectsIt it = findObjectIn(*objects, name);
6612988Sgabeblack@google.com    assert(it != objects->end());
6712955Sgabeblack@google.com    std::swap(objects->back(), *it);
6812988Sgabeblack@google.com    objects->pop_back();
6912955Sgabeblack@google.com}
7012955Sgabeblack@google.com
7112955Sgabeblack@google.combool
7212955Sgabeblack@google.comnameIsUnique(Objects *objects, Events *events, const std::string &name)
7312955Sgabeblack@google.com{
7412955Sgabeblack@google.com    for (auto obj: *objects)
7512955Sgabeblack@google.com        if (!strcmp(obj->basename(), name.c_str()))
7612955Sgabeblack@google.com            return false;
7712955Sgabeblack@google.com    for (auto event: *events)
7812955Sgabeblack@google.com        if (!strcmp(event->basename(), name.c_str()))
7912955Sgabeblack@google.com            return false;
8012955Sgabeblack@google.com    return true;
8112955Sgabeblack@google.com}
8212955Sgabeblack@google.com
8312955Sgabeblack@google.com} // anonymous namespace
8412955Sgabeblack@google.com
8512955Sgabeblack@google.comObject::Object(sc_core::sc_object *_sc_obj) : Object(_sc_obj, "object") {}
8612988Sgabeblack@google.com
8712955Sgabeblack@google.comObject::Object(sc_core::sc_object *_sc_obj, const char *obj_name) :
8812988Sgabeblack@google.com    _sc_obj(_sc_obj), _basename(obj_name ? obj_name : ""), parent(nullptr)
8912988Sgabeblack@google.com{
9012988Sgabeblack@google.com    if (_basename == "")
9112988Sgabeblack@google.com        _basename = "object";
9212955Sgabeblack@google.com
9312955Sgabeblack@google.com    Module *p = currentModule();
9412955Sgabeblack@google.com    if (!p)
9512955Sgabeblack@google.com        p = callbackModule();
9612955Sgabeblack@google.com
9712955Sgabeblack@google.com    Module *n = newModule();
9812957Sgabeblack@google.com    if (n) {
9913063Sgabeblack@google.com        // We are a module in the process of being constructed.
10013063Sgabeblack@google.com        n->finish(this);
10112955Sgabeblack@google.com    }
10212955Sgabeblack@google.com
10312955Sgabeblack@google.com    if (p) {
10412955Sgabeblack@google.com        // We're "within" a parent module, ie we're being created while its
10512955Sgabeblack@google.com        // constructor or end_of_elaboration callback is running.
10612955Sgabeblack@google.com        parent = p->obj()->_sc_obj;
10712955Sgabeblack@google.com    } else if (scheduler.current()) {
10812955Sgabeblack@google.com        // Our parent is the currently running process.
10912955Sgabeblack@google.com        parent = scheduler.current();
11012955Sgabeblack@google.com    }
11112955Sgabeblack@google.com
11212955Sgabeblack@google.com    sc_gem5::pickUniqueName(parent, _basename);
11312955Sgabeblack@google.com
11412955Sgabeblack@google.com    if (parent)
11512955Sgabeblack@google.com        addObject(&parent->_gem5_object->children, _sc_obj);
11612955Sgabeblack@google.com    else
11712955Sgabeblack@google.com        addObject(&topLevelObjects, _sc_obj);
11812955Sgabeblack@google.com
11912955Sgabeblack@google.com    addObject(&allObjects, _sc_obj);
12012955Sgabeblack@google.com
12112955Sgabeblack@google.com    _name = _basename;
12212955Sgabeblack@google.com    sc_core::sc_object *sc_p = parent;
12312955Sgabeblack@google.com    while (sc_p) {
12412955Sgabeblack@google.com        _name = std::string(sc_p->basename()) + std::string(".") + _name;
12512955Sgabeblack@google.com        sc_p = sc_p->get_parent_object();
12612955Sgabeblack@google.com    }
12712955Sgabeblack@google.com}
12812955Sgabeblack@google.com
12912955Sgabeblack@google.comObject::Object(sc_core::sc_object *_sc_obj, const Object &arg) :
13013073Sgabeblack@google.com    Object(_sc_obj, arg._basename.c_str())
13113073Sgabeblack@google.com{}
13213073Sgabeblack@google.com
13313073Sgabeblack@google.comObject &
13412957Sgabeblack@google.comObject::operator = (const Object &)
13512957Sgabeblack@google.com{
13612957Sgabeblack@google.com    return *this;
13712955Sgabeblack@google.com}
13812955Sgabeblack@google.com
13912955Sgabeblack@google.comObject::~Object()
14012955Sgabeblack@google.com{
14112955Sgabeblack@google.com    // Promote all children to be top level objects.
14213063Sgabeblack@google.com    for (auto child: children) {
14313063Sgabeblack@google.com        addObject(&topLevelObjects, child);
14412957Sgabeblack@google.com        child->_gem5_object->parent = nullptr;
14512957Sgabeblack@google.com    }
14613063Sgabeblack@google.com    children.clear();
14712957Sgabeblack@google.com
14813063Sgabeblack@google.com    if (parent)
14912955Sgabeblack@google.com        popObject(&parent->_gem5_object->children, _name);
15012955Sgabeblack@google.com    else
15112955Sgabeblack@google.com        popObject(&topLevelObjects, _name);
15212955Sgabeblack@google.com    popObject(&allObjects, _name);
15312955Sgabeblack@google.com}
15413063Sgabeblack@google.com
15513063Sgabeblack@google.comconst char *
15612955Sgabeblack@google.comObject::name() const
15712955Sgabeblack@google.com{
15812955Sgabeblack@google.com    return _name.c_str();
15912955Sgabeblack@google.com}
16012955Sgabeblack@google.com
16112955Sgabeblack@google.comconst char *
16212955Sgabeblack@google.comObject::basename() const
16312955Sgabeblack@google.com{
16412955Sgabeblack@google.com    return _basename.c_str();
16512955Sgabeblack@google.com}
16612955Sgabeblack@google.com
16712955Sgabeblack@google.comvoid
16812955Sgabeblack@google.comObject::print(std::ostream &out) const
16912955Sgabeblack@google.com{
17012955Sgabeblack@google.com    out << name();
17112955Sgabeblack@google.com}
17212955Sgabeblack@google.com
17312955Sgabeblack@google.comvoid
17412955Sgabeblack@google.comObject::dump(std::ostream &out) const
17512955Sgabeblack@google.com{
17612955Sgabeblack@google.com    out << "name = " << name() << "\n";
17712955Sgabeblack@google.com    out << "kind = " << _sc_obj->kind() << "\n";
17812955Sgabeblack@google.com}
179
180const std::vector<sc_core::sc_object *> &
181Object::get_child_objects() const
182{
183    return children;
184}
185
186const std::vector<sc_core::sc_event *> &
187Object::get_child_events() const
188{
189    return events;
190}
191
192sc_core::sc_object *Object::get_parent_object() const
193{
194    return parent;
195}
196
197bool
198Object::add_attribute(sc_core::sc_attr_base &attr)
199{
200    return cltn.push_back(&attr);
201}
202
203sc_core::sc_attr_base *
204Object::get_attribute(const std::string &attr)
205{
206    return cltn[attr];
207}
208
209sc_core::sc_attr_base *
210Object::remove_attribute(const std::string &attr)
211{
212    return cltn.remove(attr);
213}
214
215void
216Object::remove_all_attributes()
217{
218    cltn.remove_all();
219}
220
221int
222Object::num_attributes() const
223{
224    return cltn.size();
225}
226
227sc_core::sc_attr_cltn &
228Object::attr_cltn()
229{
230    return cltn;
231}
232
233const sc_core::sc_attr_cltn &
234Object::attr_cltn() const
235{
236    return cltn;
237}
238
239sc_core::sc_simcontext *
240Object::simcontext() const
241{
242    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
243    return nullptr;
244}
245
246EventsIt
247Object::addChildEvent(sc_core::sc_event *e)
248{
249    return events.emplace(events.end(), e);
250}
251
252void
253Object::delChildEvent(sc_core::sc_event *e)
254{
255    EventsIt it = std::find(events.begin(), events.end(), e);
256    assert(it != events.end());
257    std::swap(*it, events.back());
258    events.pop_back();
259}
260
261void
262Object::pickUniqueName(std::string &base)
263{
264    std::string seed = base;
265    while (!nameIsUnique(&children, &events, base))
266        base = ::sc_core::sc_gen_unique_name(seed.c_str());
267}
268
269void
270pickUniqueName(::sc_core::sc_object *parent, std::string &base)
271{
272    if (parent) {
273        Object::getFromScObject(parent)->pickUniqueName(base);
274        return;
275    }
276
277    std::string seed = base;
278    while (!nameIsUnique(&topLevelObjects, &topLevelEvents, base))
279        base = ::sc_core::sc_gen_unique_name(seed.c_str());
280}
281
282
283Objects topLevelObjects;
284Objects allObjects;
285
286const std::vector<sc_core::sc_object *> &
287getTopLevelScObjects()
288{
289    return topLevelObjects;
290}
291
292sc_core::sc_object *
293findObject(const char *name, const Objects &objects)
294{
295    ObjectsIt it = findObjectIn(allObjects, name);
296    return it == allObjects.end() ? nullptr : *it;
297}
298
299} // namespace sc_gem5
300