object.cc revision 13127
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 <algorithm> 33 34#include "base/logging.hh" 35#include "systemc/core/event.hh" 36#include "systemc/core/module.hh" 37#include "systemc/core/scheduler.hh" 38 39namespace sc_gem5 40{ 41 42namespace 43{ 44 45ObjectsIt 46findObjectIn(Objects &objects, const std::string &name) 47{ 48 ObjectsIt it; 49 for (it = objects.begin(); it != objects.end(); it++) 50 if (!strcmp((*it)->name(), name.c_str())) 51 break; 52 53 return it; 54} 55 56void 57addObject(Objects *objects, sc_core::sc_object *object) 58{ 59 objects->emplace(objects->end(), object); 60} 61 62void 63popObject(Objects *objects, const std::string &name) 64{ 65 ObjectsIt it = findObjectIn(*objects, name); 66 assert(it != objects->end()); 67 std::swap(objects->back(), *it); 68 objects->pop_back(); 69} 70 71bool 72nameIsUnique(Objects *objects, Events *events, const std::string &name) 73{ 74 for (auto obj: *objects) 75 if (!strcmp(obj->basename(), name.c_str())) 76 return false; 77 for (auto event: *events) 78 if (!strcmp(event->basename(), name.c_str())) 79 return false; 80 return true; 81} 82 83} // anonymous namespace 84 85Object::Object(sc_core::sc_object *_sc_obj) : Object(_sc_obj, "object") {} 86 87Object::Object(sc_core::sc_object *_sc_obj, const char *obj_name) : 88 _sc_obj(_sc_obj), _basename(obj_name ? obj_name : ""), parent(nullptr) 89{ 90 if (_basename == "") 91 _basename = "object"; 92 93 Module *p = currentModule(); 94 if (!p) 95 p = callbackModule(); 96 97 Module *n = newModule(); 98 if (n) { 99 // We are a module in the process of being constructed. 100 n->finish(this); 101 } 102 103 if (p) { 104 // We're "within" a parent module, ie we're being created while its 105 // constructor or end_of_elaboration callback is running. 106 parent = p->obj()->_sc_obj; 107 } else if (scheduler.current()) { 108 // Our parent is the currently running process. 109 parent = scheduler.current(); 110 } 111 112 sc_gem5::pickUniqueName(parent, _basename); 113 114 if (parent) 115 addObject(&parent->_gem5_object->children, _sc_obj); 116 else 117 addObject(&topLevelObjects, _sc_obj); 118 119 addObject(&allObjects, _sc_obj); 120 121 _name = _basename; 122 sc_core::sc_object *sc_p = parent; 123 while (sc_p) { 124 _name = std::string(sc_p->basename()) + std::string(".") + _name; 125 sc_p = sc_p->get_parent_object(); 126 } 127} 128 129Object::Object(sc_core::sc_object *_sc_obj, const Object &arg) : 130 Object(_sc_obj, arg._basename.c_str()) 131{} 132 133Object & 134Object::operator = (const Object &) 135{ 136 return *this; 137} 138 139Object::~Object() 140{ 141 // Promote all children to be top level objects. 142 for (auto child: children) { 143 addObject(&topLevelObjects, child); 144 child->_gem5_object->parent = nullptr; 145 } 146 children.clear(); 147 148 if (parent) 149 popObject(&parent->_gem5_object->children, _name); 150 else 151 popObject(&topLevelObjects, _name); 152 popObject(&allObjects, _name); 153} 154 155const char * 156Object::name() const 157{ 158 return _name.c_str(); 159} 160 161const char * 162Object::basename() const 163{ 164 return _basename.c_str(); 165} 166 167void 168Object::print(std::ostream &out) const 169{ 170 out << name(); 171} 172 173void 174Object::dump(std::ostream &out) const 175{ 176 out << "name = " << name() << "\n"; 177 out << "kind = " << _sc_obj->kind() << "\n"; 178} 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