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/messages.hh" |
40#include "systemc/ext/core/sc_module.hh" 41 42namespace sc_gem5 43{ 44 45namespace 46{ 47 48ObjectsIt 49findObjectIn(Objects &objects, const std::string &name) 50{ 51 ObjectsIt it; 52 for (it = objects.begin(); it != objects.end(); it++) 53 if (!strcmp((*it)->name(), name.c_str())) 54 break; 55 56 return it; 57} 58 59void 60addObject(Objects *objects, sc_core::sc_object *object) 61{ 62 objects->emplace(objects->end(), object); 63} 64 65void 66popObject(Objects *objects, const std::string &name) 67{ 68 ObjectsIt it = findObjectIn(*objects, name); 69 assert(it != objects->end()); 70 std::swap(objects->back(), *it); 71 objects->pop_back(); 72} 73 74bool 75nameIsUnique(Objects *objects, Events *events, const std::string &name) 76{ 77 for (auto obj: *objects) 78 if (!strcmp(obj->basename(), name.c_str())) 79 return false; 80 for (auto event: *events) 81 if (!strcmp(event->basename(), name.c_str())) 82 return false; 83 return true; 84} 85 86} // anonymous namespace 87 88Object::Object(sc_core::sc_object *_sc_obj) : Object(_sc_obj, nullptr) {} 89 90Object::Object(sc_core::sc_object *_sc_obj, const char *obj_name) : 91 _sc_obj(_sc_obj), _basename(obj_name ? obj_name : ""), parent(nullptr) 92{ 93 if (_basename == "") 94 _basename = ::sc_core::sc_gen_unique_name("object"); 95 96 parent = pickParentObj(); 97 98 Module *n = newModule(); 99 if (n) { 100 // We are a module in the process of being constructed. 101 n->finish(this); 102 } 103 104 std::string original_name = _basename; 105 _basename = sc_gem5::pickUniqueName(parent, original_name); 106 107 if (parent) 108 addObject(&parent->_gem5_object->children, _sc_obj); 109 else 110 addObject(&topLevelObjects, _sc_obj); 111 112 addObject(&allObjects, _sc_obj); 113 114 sc_core::sc_object *sc_p = parent; 115 std::string path = ""; 116 while (sc_p) { 117 path = std::string(sc_p->basename()) + std::string(".") + path; 118 sc_p = sc_p->get_parent_object(); 119 } 120 121 if (_basename != original_name) { 122 std::string message = path + original_name + 123 ". Latter declaration will be renamed to " + 124 path + _basename;
|
124 SC_REPORT_WARNING("(W505) object already exists", message.c_str());
|
125 SC_REPORT_WARNING(sc_core::SC_ID_INSTANCE_EXISTS_, message.c_str()); |
126 } 127 _name = path + _basename; 128} 129 130Object::Object(sc_core::sc_object *_sc_obj, const Object &arg) : 131 Object(_sc_obj, arg._basename.c_str()) 132{} 133 134Object & 135Object::operator = (const Object &) 136{ 137 return *this; 138} 139 140Object::~Object() 141{ 142 // Promote all children to be top level objects. 143 for (auto child: children) { 144 addObject(&topLevelObjects, child); 145 child->_gem5_object->parent = nullptr; 146 } 147 children.clear(); 148 149 for (auto event: events) 150 Event::getFromScEvent(event)->clearParent(); 151 152 if (parent) 153 popObject(&parent->_gem5_object->children, _name); 154 else 155 popObject(&topLevelObjects, _name); 156 popObject(&allObjects, _name); 157} 158 159const char * 160Object::name() const 161{ 162 return _name.c_str(); 163} 164 165const char * 166Object::basename() const 167{ 168 return _basename.c_str(); 169} 170 171void 172Object::print(std::ostream &out) const 173{ 174 out << name(); 175} 176 177void 178Object::dump(std::ostream &out) const 179{ 180 out << "name = " << name() << "\n"; 181 out << "kind = " << _sc_obj->kind() << "\n"; 182} 183 184const std::vector<sc_core::sc_object *> & 185Object::get_child_objects() const 186{ 187 return children; 188} 189 190const std::vector<sc_core::sc_event *> & 191Object::get_child_events() const 192{ 193 return events; 194} 195 196sc_core::sc_object *Object::get_parent_object() const 197{ 198 return parent; 199} 200 201bool 202Object::add_attribute(sc_core::sc_attr_base &attr) 203{ 204 return cltn.push_back(&attr); 205} 206 207sc_core::sc_attr_base * 208Object::get_attribute(const std::string &attr) 209{ 210 return cltn[attr]; 211} 212 213sc_core::sc_attr_base * 214Object::remove_attribute(const std::string &attr) 215{ 216 return cltn.remove(attr); 217} 218 219void 220Object::remove_all_attributes() 221{ 222 cltn.remove_all(); 223} 224 225int 226Object::num_attributes() const 227{ 228 return cltn.size(); 229} 230 231sc_core::sc_attr_cltn & 232Object::attr_cltn() 233{ 234 return cltn; 235} 236 237const sc_core::sc_attr_cltn & 238Object::attr_cltn() const 239{ 240 return cltn; 241} 242 243sc_core::sc_simcontext * 244Object::simcontext() const 245{ 246 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 247 return nullptr; 248} 249 250EventsIt 251Object::addChildEvent(sc_core::sc_event *e) 252{ 253 return events.emplace(events.end(), e); 254} 255 256void 257Object::delChildEvent(sc_core::sc_event *e) 258{ 259 EventsIt it = std::find(events.begin(), events.end(), e); 260 assert(it != events.end()); 261 std::swap(*it, events.back()); 262 events.pop_back(); 263} 264 265std::string 266Object::pickUniqueName(std::string base) 267{ 268 std::string seed = base; 269 while (!nameIsUnique(&children, &events, base)) 270 base = ::sc_core::sc_gen_unique_name(seed.c_str()); 271 272 return base; 273} 274 275std::string 276pickUniqueName(::sc_core::sc_object *parent, std::string base) 277{ 278 if (parent) 279 return Object::getFromScObject(parent)->pickUniqueName(base); 280 281 std::string seed = base; 282 while (!nameIsUnique(&topLevelObjects, &topLevelEvents, base)) 283 base = ::sc_core::sc_gen_unique_name(seed.c_str()); 284 285 return base; 286} 287 288 289Objects topLevelObjects; 290Objects allObjects; 291 292const std::vector<sc_core::sc_object *> & 293getTopLevelScObjects() 294{ 295 return topLevelObjects; 296} 297 298sc_core::sc_object * 299findObject(const char *name, const Objects &objects) 300{ 301 ObjectsIt it = findObjectIn(allObjects, name); 302 return it == allObjects.end() ? nullptr : *it; 303} 304 305namespace 306{ 307 308std::stack<sc_core::sc_object *> objParentStack; 309 310} // anonymous namespace 311 312sc_core::sc_object * 313pickParentObj() 314{ 315 if (!objParentStack.empty()) 316 return objParentStack.top(); 317 318 Process *p = scheduler.current(); 319 if (p) 320 return p; 321 322 return nullptr; 323} 324 325void pushParentObj(sc_core::sc_object *obj) { objParentStack.push(obj); } 326void popParentObj() { objParentStack.pop(); } 327 328} // namespace sc_gem5
|