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