object.cc revision 12952
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
3212950Sgabeblack@google.com#include "base/logging.hh"
3312950Sgabeblack@google.com#include "systemc/core/module.hh"
3412950Sgabeblack@google.com
3512950Sgabeblack@google.comnamespace sc_gem5
3612950Sgabeblack@google.com{
3712950Sgabeblack@google.com
3812950Sgabeblack@google.comnamespace
3912950Sgabeblack@google.com{
4012950Sgabeblack@google.com
4112950Sgabeblack@google.comObjectsIt
4212950Sgabeblack@google.comfindObjectIn(Objects &objects, const std::string &name)
4312950Sgabeblack@google.com{
4412950Sgabeblack@google.com    ObjectsIt it;
4512950Sgabeblack@google.com    for (it = objects.begin(); it != objects.end(); it++)
4612950Sgabeblack@google.com        if (!strcmp((*it)->name(), name.c_str()))
4712950Sgabeblack@google.com            break;
4812950Sgabeblack@google.com
4912950Sgabeblack@google.com    return it;
5012950Sgabeblack@google.com}
5112950Sgabeblack@google.com
5212950Sgabeblack@google.comvoid
5312950Sgabeblack@google.comaddObject(Objects *objects, sc_core::sc_object *object)
5412950Sgabeblack@google.com{
5512950Sgabeblack@google.com    objects->emplace(objects->end(), object);
5612950Sgabeblack@google.com}
5712950Sgabeblack@google.com
5812950Sgabeblack@google.comvoid
5912950Sgabeblack@google.compopObject(Objects *objects, const std::string &name)
6012950Sgabeblack@google.com{
6112950Sgabeblack@google.com    ObjectsIt it = findObjectIn(*objects, name);
6212950Sgabeblack@google.com    assert(it != objects->end());
6312950Sgabeblack@google.com    std::swap(objects->back(), *it);
6412950Sgabeblack@google.com    objects->pop_back();
6512950Sgabeblack@google.com}
6612950Sgabeblack@google.com
6712950Sgabeblack@google.com} // anonymous namespace
6812950Sgabeblack@google.com
6912950Sgabeblack@google.comObject::Object(sc_core::sc_object *sc_obj) : Object(sc_obj, "object") {}
7012950Sgabeblack@google.com
7112950Sgabeblack@google.comObject::Object(sc_core::sc_object *sc_obj, const char *obj_name) :
7212950Sgabeblack@google.com    sc_obj(sc_obj), _basename(obj_name), parent(nullptr)
7312950Sgabeblack@google.com{
7412950Sgabeblack@google.com    if (_basename == "")
7512950Sgabeblack@google.com        _basename = "object";
7612950Sgabeblack@google.com
7712950Sgabeblack@google.com    Module *p = currentModule();
7812950Sgabeblack@google.com
7912950Sgabeblack@google.com    Module *n = newModule();
8012950Sgabeblack@google.com    if (n) {
8112950Sgabeblack@google.com        // We are a module in the process of being constructed.
8212950Sgabeblack@google.com        n->finish(this);
8312950Sgabeblack@google.com    }
8412950Sgabeblack@google.com
8512950Sgabeblack@google.com    if (p) {
8612950Sgabeblack@google.com        // We're "within" a parent module, ie we're being created while its
8712950Sgabeblack@google.com        // constructor is running.
8812950Sgabeblack@google.com        parent = p->obj()->sc_obj;
8912950Sgabeblack@google.com        addObject(&parent->_gem5_object->children, sc_obj);
9012950Sgabeblack@google.com    } else if (false /* TODO Check if a process is running */) {
9112950Sgabeblack@google.com        // The process is our parent.
9212950Sgabeblack@google.com    } else {
9312950Sgabeblack@google.com        // We're a top level object.
9412950Sgabeblack@google.com        addObject(&topLevelObjects, sc_obj);
9512950Sgabeblack@google.com    }
9612950Sgabeblack@google.com
9712950Sgabeblack@google.com    addObject(&allObjects, sc_obj);
9812950Sgabeblack@google.com
9912950Sgabeblack@google.com    _name = _basename;
10012950Sgabeblack@google.com    sc_core::sc_object *sc_p = parent;
10112950Sgabeblack@google.com    while (sc_p) {
10212950Sgabeblack@google.com        _name = std::string(sc_p->basename()) + std::string(".") + _name;
10312952Sgabeblack@google.com        sc_p = sc_p->get_parent_object();
10412950Sgabeblack@google.com    }
10512950Sgabeblack@google.com}
10612950Sgabeblack@google.com
10712950Sgabeblack@google.comObject::Object(sc_core::sc_object *sc_obj, const Object &arg) :
10812950Sgabeblack@google.com    Object(sc_obj, arg._basename.c_str())
10912950Sgabeblack@google.com{}
11012950Sgabeblack@google.com
11112950Sgabeblack@google.comObject &
11212950Sgabeblack@google.comObject::operator = (const Object &)
11312950Sgabeblack@google.com{
11412950Sgabeblack@google.com    return *this;
11512950Sgabeblack@google.com}
11612950Sgabeblack@google.com
11712950Sgabeblack@google.comObject::~Object()
11812950Sgabeblack@google.com{
11912950Sgabeblack@google.com    panic_if(!children.empty(), "Parent object still has children.\n");
12012950Sgabeblack@google.com
12112950Sgabeblack@google.com    if (parent)
12212950Sgabeblack@google.com        popObject(&parent->_gem5_object->children, _name);
12312950Sgabeblack@google.com    else
12412950Sgabeblack@google.com        popObject(&topLevelObjects, _name);
12512950Sgabeblack@google.com    popObject(&allObjects, _name);
12612950Sgabeblack@google.com}
12712950Sgabeblack@google.com
12812950Sgabeblack@google.comconst char *
12912950Sgabeblack@google.comObject::name() const
13012950Sgabeblack@google.com{
13112950Sgabeblack@google.com    return _name.c_str();
13212950Sgabeblack@google.com}
13312950Sgabeblack@google.com
13412950Sgabeblack@google.comconst char *
13512950Sgabeblack@google.comObject::basename() const
13612950Sgabeblack@google.com{
13712950Sgabeblack@google.com    return _basename.c_str();
13812950Sgabeblack@google.com}
13912950Sgabeblack@google.com
14012950Sgabeblack@google.comvoid
14112950Sgabeblack@google.comObject::print(std::ostream &out) const
14212950Sgabeblack@google.com{
14312950Sgabeblack@google.com    out << name();
14412950Sgabeblack@google.com}
14512950Sgabeblack@google.com
14612950Sgabeblack@google.comvoid
14712950Sgabeblack@google.comObject::dump(std::ostream &out) const
14812950Sgabeblack@google.com{
14912950Sgabeblack@google.com    out << "name = " << name() << "\n";
15012950Sgabeblack@google.com    out << "kind = " << sc_obj->kind() << "\n";
15112950Sgabeblack@google.com}
15212950Sgabeblack@google.com
15312950Sgabeblack@google.comconst std::vector<sc_core::sc_object *> &
15412950Sgabeblack@google.comObject::get_child_objects() const
15512950Sgabeblack@google.com{
15612950Sgabeblack@google.com    return children;
15712950Sgabeblack@google.com}
15812950Sgabeblack@google.com
15912950Sgabeblack@google.comconst std::vector<sc_core::sc_event *> &
16012950Sgabeblack@google.comObject::get_child_events() const
16112950Sgabeblack@google.com{
16212950Sgabeblack@google.com    return events;
16312950Sgabeblack@google.com}
16412950Sgabeblack@google.com
16512950Sgabeblack@google.comsc_core::sc_object *Object::get_parent_object() const
16612950Sgabeblack@google.com{
16712950Sgabeblack@google.com    return parent;
16812950Sgabeblack@google.com}
16912950Sgabeblack@google.com
17012950Sgabeblack@google.combool
17112950Sgabeblack@google.comObject::add_attribute(sc_core::sc_attr_base &attr)
17212950Sgabeblack@google.com{
17312950Sgabeblack@google.com    return cltn.push_back(&attr);
17412950Sgabeblack@google.com}
17512950Sgabeblack@google.com
17612950Sgabeblack@google.comsc_core::sc_attr_base *
17712950Sgabeblack@google.comObject::get_attribute(const std::string &attr)
17812950Sgabeblack@google.com{
17912950Sgabeblack@google.com    return cltn[attr];
18012950Sgabeblack@google.com}
18112950Sgabeblack@google.com
18212950Sgabeblack@google.comsc_core::sc_attr_base *
18312950Sgabeblack@google.comObject::remove_attribute(const std::string &attr)
18412950Sgabeblack@google.com{
18512950Sgabeblack@google.com    return cltn.remove(attr);
18612950Sgabeblack@google.com}
18712950Sgabeblack@google.com
18812950Sgabeblack@google.comvoid
18912950Sgabeblack@google.comObject::remove_all_attributes()
19012950Sgabeblack@google.com{
19112950Sgabeblack@google.com    cltn.remove_all();
19212950Sgabeblack@google.com}
19312950Sgabeblack@google.com
19412950Sgabeblack@google.comint
19512950Sgabeblack@google.comObject::num_attributes() const
19612950Sgabeblack@google.com{
19712950Sgabeblack@google.com    return cltn.size();
19812950Sgabeblack@google.com}
19912950Sgabeblack@google.com
20012950Sgabeblack@google.comsc_core::sc_attr_cltn &
20112950Sgabeblack@google.comObject::attr_cltn()
20212950Sgabeblack@google.com{
20312950Sgabeblack@google.com    return cltn;
20412950Sgabeblack@google.com}
20512950Sgabeblack@google.com
20612950Sgabeblack@google.comconst sc_core::sc_attr_cltn &
20712950Sgabeblack@google.comObject::attr_cltn() const
20812950Sgabeblack@google.com{
20912950Sgabeblack@google.com    return cltn;
21012950Sgabeblack@google.com}
21112950Sgabeblack@google.com
21212950Sgabeblack@google.comsc_core::sc_simcontext *
21312950Sgabeblack@google.comObject::simcontext() const
21412950Sgabeblack@google.com{
21512950Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
21612950Sgabeblack@google.com    return nullptr;
21712950Sgabeblack@google.com}
21812950Sgabeblack@google.com
21912950Sgabeblack@google.com
22012950Sgabeblack@google.comObjects topLevelObjects;
22112950Sgabeblack@google.comObjects allObjects;
22212950Sgabeblack@google.com
22312950Sgabeblack@google.comconst std::vector<sc_core::sc_object *> &
22412950Sgabeblack@google.comgetTopLevelScObjects()
22512950Sgabeblack@google.com{
22612950Sgabeblack@google.com    return topLevelObjects;
22712950Sgabeblack@google.com}
22812950Sgabeblack@google.com
22912950Sgabeblack@google.comsc_core::sc_object *
23012950Sgabeblack@google.comfindObject(const char *name, const Objects &objects)
23112950Sgabeblack@google.com{
23212950Sgabeblack@google.com    ObjectsIt it = findObjectIn(allObjects, name);
23312950Sgabeblack@google.com    return it == allObjects.end() ? nullptr : *it;
23412950Sgabeblack@google.com}
23512950Sgabeblack@google.com
23612950Sgabeblack@google.com} // namespace sc_gem5
237