object.cc revision 13334
112950Sgabeblack@google.com/*
212950Sgabeblack@google.com * Copyright 2018 Google, Inc.
312950Sgabeblack@google.com *
412950Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
512950Sgabeblack@google.com * modification, are permitted provided that the following conditions are
612950Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
712950Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
812950Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
912950Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1012950Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1112950Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1212950Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1312950Sgabeblack@google.com * this software without specific prior written permission.
1412950Sgabeblack@google.com *
1512950Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612950Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712950Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812950Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912950Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012950Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112950Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212950Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312950Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412950Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512950Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612950Sgabeblack@google.com *
2712950Sgabeblack@google.com * Authors: Gabe Black
2812950Sgabeblack@google.com */
2912950Sgabeblack@google.com
3012950Sgabeblack@google.com#include "systemc/core/object.hh"
3112950Sgabeblack@google.com
3212988Sgabeblack@google.com#include <algorithm>
3313268Sgabeblack@google.com#include <stack>
3412988Sgabeblack@google.com
3512950Sgabeblack@google.com#include "base/logging.hh"
3613127Sgabeblack@google.com#include "systemc/core/event.hh"
3712950Sgabeblack@google.com#include "systemc/core/module.hh"
3812953Sgabeblack@google.com#include "systemc/core/scheduler.hh"
3913317Sgabeblack@google.com#include "systemc/ext/core/messages.hh"
4013161Sgabeblack@google.com#include "systemc/ext/core/sc_module.hh"
4113334Sgabeblack@google.com#include "systemc/ext/core/sc_simcontext.hh"
4212950Sgabeblack@google.com
4312950Sgabeblack@google.comnamespace sc_gem5
4412950Sgabeblack@google.com{
4512950Sgabeblack@google.com
4612950Sgabeblack@google.comnamespace
4712950Sgabeblack@google.com{
4812950Sgabeblack@google.com
4912950Sgabeblack@google.comObjectsIt
5012950Sgabeblack@google.comfindObjectIn(Objects &objects, const std::string &name)
5112950Sgabeblack@google.com{
5212950Sgabeblack@google.com    ObjectsIt it;
5312950Sgabeblack@google.com    for (it = objects.begin(); it != objects.end(); it++)
5412950Sgabeblack@google.com        if (!strcmp((*it)->name(), name.c_str()))
5512950Sgabeblack@google.com            break;
5612950Sgabeblack@google.com
5712950Sgabeblack@google.com    return it;
5812950Sgabeblack@google.com}
5912950Sgabeblack@google.com
6012950Sgabeblack@google.comvoid
6112950Sgabeblack@google.comaddObject(Objects *objects, sc_core::sc_object *object)
6212950Sgabeblack@google.com{
6312950Sgabeblack@google.com    objects->emplace(objects->end(), object);
6412950Sgabeblack@google.com}
6512950Sgabeblack@google.com
6612950Sgabeblack@google.comvoid
6712950Sgabeblack@google.compopObject(Objects *objects, const std::string &name)
6812950Sgabeblack@google.com{
6912950Sgabeblack@google.com    ObjectsIt it = findObjectIn(*objects, name);
7012950Sgabeblack@google.com    assert(it != objects->end());
7112950Sgabeblack@google.com    std::swap(objects->back(), *it);
7212950Sgabeblack@google.com    objects->pop_back();
7312950Sgabeblack@google.com}
7412950Sgabeblack@google.com
7513127Sgabeblack@google.combool
7613127Sgabeblack@google.comnameIsUnique(Objects *objects, Events *events, const std::string &name)
7713127Sgabeblack@google.com{
7813127Sgabeblack@google.com    for (auto obj: *objects)
7913127Sgabeblack@google.com        if (!strcmp(obj->basename(), name.c_str()))
8013127Sgabeblack@google.com            return false;
8113127Sgabeblack@google.com    for (auto event: *events)
8213127Sgabeblack@google.com        if (!strcmp(event->basename(), name.c_str()))
8313127Sgabeblack@google.com            return false;
8413127Sgabeblack@google.com    return true;
8513127Sgabeblack@google.com}
8613127Sgabeblack@google.com
8712950Sgabeblack@google.com} // anonymous namespace
8812950Sgabeblack@google.com
8913161Sgabeblack@google.comObject::Object(sc_core::sc_object *_sc_obj) : Object(_sc_obj, nullptr) {}
9012950Sgabeblack@google.com
9112955Sgabeblack@google.comObject::Object(sc_core::sc_object *_sc_obj, const char *obj_name) :
9213085Sgabeblack@google.com    _sc_obj(_sc_obj), _basename(obj_name ? obj_name : ""), parent(nullptr)
9312950Sgabeblack@google.com{
9412950Sgabeblack@google.com    if (_basename == "")
9513161Sgabeblack@google.com        _basename = ::sc_core::sc_gen_unique_name("object");
9612950Sgabeblack@google.com
9713268Sgabeblack@google.com    parent = pickParentObj();
9812950Sgabeblack@google.com
9912950Sgabeblack@google.com    Module *n = newModule();
10012950Sgabeblack@google.com    if (n) {
10112950Sgabeblack@google.com        // We are a module in the process of being constructed.
10212950Sgabeblack@google.com        n->finish(this);
10312950Sgabeblack@google.com    }
10412950Sgabeblack@google.com
10513179Sgabeblack@google.com    std::string original_name = _basename;
10613179Sgabeblack@google.com    _basename = sc_gem5::pickUniqueName(parent, original_name);
10713127Sgabeblack@google.com
10813127Sgabeblack@google.com    if (parent)
10913126Sgabeblack@google.com        addObject(&parent->_gem5_object->children, _sc_obj);
11013127Sgabeblack@google.com    else
11112955Sgabeblack@google.com        addObject(&topLevelObjects, _sc_obj);
11212950Sgabeblack@google.com
11312955Sgabeblack@google.com    addObject(&allObjects, _sc_obj);
11412950Sgabeblack@google.com
11512950Sgabeblack@google.com    sc_core::sc_object *sc_p = parent;
11613179Sgabeblack@google.com    std::string path = "";
11712950Sgabeblack@google.com    while (sc_p) {
11813179Sgabeblack@google.com        path = std::string(sc_p->basename()) + std::string(".") + path;
11912952Sgabeblack@google.com        sc_p = sc_p->get_parent_object();
12012950Sgabeblack@google.com    }
12113179Sgabeblack@google.com
12213179Sgabeblack@google.com    if (_basename != original_name) {
12313179Sgabeblack@google.com        std::string message = path + original_name +
12413179Sgabeblack@google.com            ". Latter declaration will be renamed to " +
12513179Sgabeblack@google.com            path + _basename;
12613317Sgabeblack@google.com        SC_REPORT_WARNING(sc_core::SC_ID_INSTANCE_EXISTS_, message.c_str());
12713179Sgabeblack@google.com    }
12813179Sgabeblack@google.com    _name = path + _basename;
12912950Sgabeblack@google.com}
13012950Sgabeblack@google.com
13112955Sgabeblack@google.comObject::Object(sc_core::sc_object *_sc_obj, const Object &arg) :
13212955Sgabeblack@google.com    Object(_sc_obj, arg._basename.c_str())
13312950Sgabeblack@google.com{}
13412950Sgabeblack@google.com
13512950Sgabeblack@google.comObject &
13612950Sgabeblack@google.comObject::operator = (const Object &)
13712950Sgabeblack@google.com{
13812950Sgabeblack@google.com    return *this;
13912950Sgabeblack@google.com}
14012950Sgabeblack@google.com
14112950Sgabeblack@google.comObject::~Object()
14212950Sgabeblack@google.com{
14312984Sgabeblack@google.com    // Promote all children to be top level objects.
14412984Sgabeblack@google.com    for (auto child: children) {
14512984Sgabeblack@google.com        addObject(&topLevelObjects, child);
14612984Sgabeblack@google.com        child->_gem5_object->parent = nullptr;
14712984Sgabeblack@google.com    }
14812984Sgabeblack@google.com    children.clear();
14912950Sgabeblack@google.com
15013295Sgabeblack@google.com    for (auto event: events)
15113295Sgabeblack@google.com        Event::getFromScEvent(event)->clearParent();
15213295Sgabeblack@google.com
15312950Sgabeblack@google.com    if (parent)
15412950Sgabeblack@google.com        popObject(&parent->_gem5_object->children, _name);
15512950Sgabeblack@google.com    else
15612950Sgabeblack@google.com        popObject(&topLevelObjects, _name);
15712950Sgabeblack@google.com    popObject(&allObjects, _name);
15812950Sgabeblack@google.com}
15912950Sgabeblack@google.com
16012950Sgabeblack@google.comconst char *
16112950Sgabeblack@google.comObject::name() const
16212950Sgabeblack@google.com{
16312950Sgabeblack@google.com    return _name.c_str();
16412950Sgabeblack@google.com}
16512950Sgabeblack@google.com
16612950Sgabeblack@google.comconst char *
16712950Sgabeblack@google.comObject::basename() const
16812950Sgabeblack@google.com{
16912950Sgabeblack@google.com    return _basename.c_str();
17012950Sgabeblack@google.com}
17112950Sgabeblack@google.com
17212950Sgabeblack@google.comvoid
17312950Sgabeblack@google.comObject::print(std::ostream &out) const
17412950Sgabeblack@google.com{
17512950Sgabeblack@google.com    out << name();
17612950Sgabeblack@google.com}
17712950Sgabeblack@google.com
17812950Sgabeblack@google.comvoid
17912950Sgabeblack@google.comObject::dump(std::ostream &out) const
18012950Sgabeblack@google.com{
18112950Sgabeblack@google.com    out << "name = " << name() << "\n";
18212955Sgabeblack@google.com    out << "kind = " << _sc_obj->kind() << "\n";
18312950Sgabeblack@google.com}
18412950Sgabeblack@google.com
18512950Sgabeblack@google.comconst std::vector<sc_core::sc_object *> &
18612950Sgabeblack@google.comObject::get_child_objects() const
18712950Sgabeblack@google.com{
18812950Sgabeblack@google.com    return children;
18912950Sgabeblack@google.com}
19012950Sgabeblack@google.com
19112950Sgabeblack@google.comconst std::vector<sc_core::sc_event *> &
19212950Sgabeblack@google.comObject::get_child_events() const
19312950Sgabeblack@google.com{
19412950Sgabeblack@google.com    return events;
19512950Sgabeblack@google.com}
19612950Sgabeblack@google.com
19712950Sgabeblack@google.comsc_core::sc_object *Object::get_parent_object() const
19812950Sgabeblack@google.com{
19912950Sgabeblack@google.com    return parent;
20012950Sgabeblack@google.com}
20112950Sgabeblack@google.com
20212950Sgabeblack@google.combool
20312950Sgabeblack@google.comObject::add_attribute(sc_core::sc_attr_base &attr)
20412950Sgabeblack@google.com{
20512950Sgabeblack@google.com    return cltn.push_back(&attr);
20612950Sgabeblack@google.com}
20712950Sgabeblack@google.com
20812950Sgabeblack@google.comsc_core::sc_attr_base *
20912950Sgabeblack@google.comObject::get_attribute(const std::string &attr)
21012950Sgabeblack@google.com{
21112950Sgabeblack@google.com    return cltn[attr];
21212950Sgabeblack@google.com}
21312950Sgabeblack@google.com
21412950Sgabeblack@google.comsc_core::sc_attr_base *
21512950Sgabeblack@google.comObject::remove_attribute(const std::string &attr)
21612950Sgabeblack@google.com{
21712950Sgabeblack@google.com    return cltn.remove(attr);
21812950Sgabeblack@google.com}
21912950Sgabeblack@google.com
22012950Sgabeblack@google.comvoid
22112950Sgabeblack@google.comObject::remove_all_attributes()
22212950Sgabeblack@google.com{
22312950Sgabeblack@google.com    cltn.remove_all();
22412950Sgabeblack@google.com}
22512950Sgabeblack@google.com
22612950Sgabeblack@google.comint
22712950Sgabeblack@google.comObject::num_attributes() const
22812950Sgabeblack@google.com{
22912950Sgabeblack@google.com    return cltn.size();
23012950Sgabeblack@google.com}
23112950Sgabeblack@google.com
23212950Sgabeblack@google.comsc_core::sc_attr_cltn &
23312950Sgabeblack@google.comObject::attr_cltn()
23412950Sgabeblack@google.com{
23512950Sgabeblack@google.com    return cltn;
23612950Sgabeblack@google.com}
23712950Sgabeblack@google.com
23812950Sgabeblack@google.comconst sc_core::sc_attr_cltn &
23912950Sgabeblack@google.comObject::attr_cltn() const
24012950Sgabeblack@google.com{
24112950Sgabeblack@google.com    return cltn;
24212950Sgabeblack@google.com}
24312950Sgabeblack@google.com
24412950Sgabeblack@google.comsc_core::sc_simcontext *
24512950Sgabeblack@google.comObject::simcontext() const
24612950Sgabeblack@google.com{
24713334Sgabeblack@google.com    return sc_core::sc_get_curr_simcontext();
24812950Sgabeblack@google.com}
24912950Sgabeblack@google.com
25012955Sgabeblack@google.comEventsIt
25112955Sgabeblack@google.comObject::addChildEvent(sc_core::sc_event *e)
25212955Sgabeblack@google.com{
25312955Sgabeblack@google.com    return events.emplace(events.end(), e);
25412955Sgabeblack@google.com}
25512955Sgabeblack@google.com
25612955Sgabeblack@google.comvoid
25712988Sgabeblack@google.comObject::delChildEvent(sc_core::sc_event *e)
25812955Sgabeblack@google.com{
25912988Sgabeblack@google.com    EventsIt it = std::find(events.begin(), events.end(), e);
26012988Sgabeblack@google.com    assert(it != events.end());
26112955Sgabeblack@google.com    std::swap(*it, events.back());
26212955Sgabeblack@google.com    events.pop_back();
26312955Sgabeblack@google.com}
26412955Sgabeblack@google.com
26513179Sgabeblack@google.comstd::string
26613179Sgabeblack@google.comObject::pickUniqueName(std::string base)
26713127Sgabeblack@google.com{
26813127Sgabeblack@google.com    std::string seed = base;
26913127Sgabeblack@google.com    while (!nameIsUnique(&children, &events, base))
27013127Sgabeblack@google.com        base = ::sc_core::sc_gen_unique_name(seed.c_str());
27113179Sgabeblack@google.com
27213179Sgabeblack@google.com    return base;
27313127Sgabeblack@google.com}
27413127Sgabeblack@google.com
27513179Sgabeblack@google.comstd::string
27613179Sgabeblack@google.compickUniqueName(::sc_core::sc_object *parent, std::string base)
27713127Sgabeblack@google.com{
27813179Sgabeblack@google.com    if (parent)
27913179Sgabeblack@google.com        return Object::getFromScObject(parent)->pickUniqueName(base);
28013127Sgabeblack@google.com
28113127Sgabeblack@google.com    std::string seed = base;
28213127Sgabeblack@google.com    while (!nameIsUnique(&topLevelObjects, &topLevelEvents, base))
28313127Sgabeblack@google.com        base = ::sc_core::sc_gen_unique_name(seed.c_str());
28413179Sgabeblack@google.com
28513179Sgabeblack@google.com    return base;
28613127Sgabeblack@google.com}
28713127Sgabeblack@google.com
28812950Sgabeblack@google.com
28912950Sgabeblack@google.comObjects topLevelObjects;
29012950Sgabeblack@google.comObjects allObjects;
29112950Sgabeblack@google.com
29212950Sgabeblack@google.comconst std::vector<sc_core::sc_object *> &
29312950Sgabeblack@google.comgetTopLevelScObjects()
29412950Sgabeblack@google.com{
29512950Sgabeblack@google.com    return topLevelObjects;
29612950Sgabeblack@google.com}
29712950Sgabeblack@google.com
29812950Sgabeblack@google.comsc_core::sc_object *
29912950Sgabeblack@google.comfindObject(const char *name, const Objects &objects)
30012950Sgabeblack@google.com{
30112950Sgabeblack@google.com    ObjectsIt it = findObjectIn(allObjects, name);
30212950Sgabeblack@google.com    return it == allObjects.end() ? nullptr : *it;
30312950Sgabeblack@google.com}
30412950Sgabeblack@google.com
30513268Sgabeblack@google.comnamespace
30613268Sgabeblack@google.com{
30713268Sgabeblack@google.com
30813268Sgabeblack@google.comstd::stack<sc_core::sc_object *> objParentStack;
30913268Sgabeblack@google.com
31013268Sgabeblack@google.com} // anonymous namespace
31113268Sgabeblack@google.com
31213268Sgabeblack@google.comsc_core::sc_object *
31313268Sgabeblack@google.compickParentObj()
31413268Sgabeblack@google.com{
31513268Sgabeblack@google.com    if (!objParentStack.empty())
31613268Sgabeblack@google.com        return objParentStack.top();
31713268Sgabeblack@google.com
31813268Sgabeblack@google.com    Process *p = scheduler.current();
31913268Sgabeblack@google.com    if (p)
32013268Sgabeblack@google.com        return p;
32113268Sgabeblack@google.com
32213268Sgabeblack@google.com    return nullptr;
32313268Sgabeblack@google.com}
32413268Sgabeblack@google.com
32513268Sgabeblack@google.comvoid pushParentObj(sc_core::sc_object *obj) { objParentStack.push(obj); }
32613268Sgabeblack@google.comvoid popParentObj() { objParentStack.pop(); }
32713268Sgabeblack@google.com
32812950Sgabeblack@google.com} // namespace sc_gem5
329