object.cc revision 13268
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#include <stack> 34 35#include "base/logging.hh" 36#include "systemc/core/event.hh" 37#include "systemc/core/module.hh" 38#include "systemc/core/scheduler.hh" 39#include "systemc/ext/core/sc_module.hh" 40 41namespace sc_gem5 42{ 43 44namespace 45{ 46 47ObjectsIt 48findObjectIn(Objects &objects, const std::string &name) 49{ 50 ObjectsIt it; 51 for (it = objects.begin(); it != objects.end(); it++) 52 if (!strcmp((*it)->name(), name.c_str())) 53 break; 54 55 return it; 56} 57 58void 59addObject(Objects *objects, sc_core::sc_object *object) 60{ 61 objects->emplace(objects->end(), object); 62} 63 64void 65popObject(Objects *objects, const std::string &name) 66{ 67 ObjectsIt it = findObjectIn(*objects, name); 68 assert(it != objects->end()); 69 std::swap(objects->back(), *it); 70 objects->pop_back(); 71} 72 73bool 74nameIsUnique(Objects *objects, Events *events, const std::string &name) 75{ 76 for (auto obj: *objects) 77 if (!strcmp(obj->basename(), name.c_str())) 78 return false; 79 for (auto event: *events) 80 if (!strcmp(event->basename(), name.c_str())) 81 return false; 82 return true; 83} 84 85} // anonymous namespace 86 87Object::Object(sc_core::sc_object *_sc_obj) : Object(_sc_obj, nullptr) {} 88 89Object::Object(sc_core::sc_object *_sc_obj, const char *obj_name) : 90 _sc_obj(_sc_obj), _basename(obj_name ? obj_name : ""), parent(nullptr) 91{ 92 if (_basename == "") 93 _basename = ::sc_core::sc_gen_unique_name("object"); 94 95 parent = pickParentObj(); 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 std::string original_name = _basename; 104 _basename = sc_gem5::pickUniqueName(parent, original_name); 105 106 if (parent) 107 addObject(&parent->_gem5_object->children, _sc_obj); 108 else 109 addObject(&topLevelObjects, _sc_obj); 110 111 addObject(&allObjects, _sc_obj); 112 113 sc_core::sc_object *sc_p = parent; 114 std::string path = ""; 115 while (sc_p) { 116 path = std::string(sc_p->basename()) + std::string(".") + path; 117 sc_p = sc_p->get_parent_object(); 118 } 119 120 if (_basename != original_name) { 121 std::string message = path + original_name + 122 ". Latter declaration will be renamed to " + 123 path + _basename; 124 SC_REPORT_WARNING("(W505) object already exists", message.c_str()); 125 } 126 _name = path + _basename; 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 261std::string 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 return base; 269} 270 271std::string 272pickUniqueName(::sc_core::sc_object *parent, std::string base) 273{ 274 if (parent) 275 return Object::getFromScObject(parent)->pickUniqueName(base); 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 return base; 282} 283 284 285Objects topLevelObjects; 286Objects allObjects; 287 288const std::vector<sc_core::sc_object *> & 289getTopLevelScObjects() 290{ 291 return topLevelObjects; 292} 293 294sc_core::sc_object * 295findObject(const char *name, const Objects &objects) 296{ 297 ObjectsIt it = findObjectIn(allObjects, name); 298 return it == allObjects.end() ? nullptr : *it; 299} 300 301namespace 302{ 303 304std::stack<sc_core::sc_object *> objParentStack; 305 306} // anonymous namespace 307 308sc_core::sc_object * 309pickParentObj() 310{ 311 if (!objParentStack.empty()) 312 return objParentStack.top(); 313 314 Process *p = scheduler.current(); 315 if (p) 316 return p; 317 318 return nullptr; 319} 320 321void pushParentObj(sc_core::sc_object *obj) { objParentStack.push(obj); } 322void popParentObj() { objParentStack.pop(); } 323 324} // namespace sc_gem5 325