object.cc revision 13161:8bef8ce2784f
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#include "systemc/ext/core/sc_module.hh" 39 40namespace sc_gem5 41{ 42 43namespace 44{ 45 46ObjectsIt 47findObjectIn(Objects &objects, const std::string &name) 48{ 49 ObjectsIt it; 50 for (it = objects.begin(); it != objects.end(); it++) 51 if (!strcmp((*it)->name(), name.c_str())) 52 break; 53 54 return it; 55} 56 57void 58addObject(Objects *objects, sc_core::sc_object *object) 59{ 60 objects->emplace(objects->end(), object); 61} 62 63void 64popObject(Objects *objects, const std::string &name) 65{ 66 ObjectsIt it = findObjectIn(*objects, name); 67 assert(it != objects->end()); 68 std::swap(objects->back(), *it); 69 objects->pop_back(); 70} 71 72bool 73nameIsUnique(Objects *objects, Events *events, const std::string &name) 74{ 75 for (auto obj: *objects) 76 if (!strcmp(obj->basename(), name.c_str())) 77 return false; 78 for (auto event: *events) 79 if (!strcmp(event->basename(), name.c_str())) 80 return false; 81 return true; 82} 83 84} // anonymous namespace 85 86Object::Object(sc_core::sc_object *_sc_obj) : Object(_sc_obj, nullptr) {} 87 88Object::Object(sc_core::sc_object *_sc_obj, const char *obj_name) : 89 _sc_obj(_sc_obj), _basename(obj_name ? obj_name : ""), parent(nullptr) 90{ 91 if (_basename == "") 92 _basename = ::sc_core::sc_gen_unique_name("object"); 93 94 Module *p = currentModule(); 95 if (!p) 96 p = callbackModule(); 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 if (p) { 105 // We're "within" a parent module, ie we're being created while its 106 // constructor or end_of_elaboration callback is running. 107 parent = p->obj()->_sc_obj; 108 } else if (scheduler.current()) { 109 // Our parent is the currently running process. 110 parent = scheduler.current(); 111 } 112 113 sc_gem5::pickUniqueName(parent, _basename); 114 115 if (parent) 116 addObject(&parent->_gem5_object->children, _sc_obj); 117 else 118 addObject(&topLevelObjects, _sc_obj); 119 120 addObject(&allObjects, _sc_obj); 121 122 _name = _basename; 123 sc_core::sc_object *sc_p = parent; 124 while (sc_p) { 125 _name = std::string(sc_p->basename()) + std::string(".") + _name; 126 sc_p = sc_p->get_parent_object(); 127 } 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 if (parent) 150 popObject(&parent->_gem5_object->children, _name); 151 else 152 popObject(&topLevelObjects, _name); 153 popObject(&allObjects, _name); 154} 155 156const char * 157Object::name() const 158{ 159 return _name.c_str(); 160} 161 162const char * 163Object::basename() const 164{ 165 return _basename.c_str(); 166} 167 168void 169Object::print(std::ostream &out) const 170{ 171 out << name(); 172} 173 174void 175Object::dump(std::ostream &out) const 176{ 177 out << "name = " << name() << "\n"; 178 out << "kind = " << _sc_obj->kind() << "\n"; 179} 180 181const std::vector<sc_core::sc_object *> & 182Object::get_child_objects() const 183{ 184 return children; 185} 186 187const std::vector<sc_core::sc_event *> & 188Object::get_child_events() const 189{ 190 return events; 191} 192 193sc_core::sc_object *Object::get_parent_object() const 194{ 195 return parent; 196} 197 198bool 199Object::add_attribute(sc_core::sc_attr_base &attr) 200{ 201 return cltn.push_back(&attr); 202} 203 204sc_core::sc_attr_base * 205Object::get_attribute(const std::string &attr) 206{ 207 return cltn[attr]; 208} 209 210sc_core::sc_attr_base * 211Object::remove_attribute(const std::string &attr) 212{ 213 return cltn.remove(attr); 214} 215 216void 217Object::remove_all_attributes() 218{ 219 cltn.remove_all(); 220} 221 222int 223Object::num_attributes() const 224{ 225 return cltn.size(); 226} 227 228sc_core::sc_attr_cltn & 229Object::attr_cltn() 230{ 231 return cltn; 232} 233 234const sc_core::sc_attr_cltn & 235Object::attr_cltn() const 236{ 237 return cltn; 238} 239 240sc_core::sc_simcontext * 241Object::simcontext() const 242{ 243 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 244 return nullptr; 245} 246 247EventsIt 248Object::addChildEvent(sc_core::sc_event *e) 249{ 250 return events.emplace(events.end(), e); 251} 252 253void 254Object::delChildEvent(sc_core::sc_event *e) 255{ 256 EventsIt it = std::find(events.begin(), events.end(), e); 257 assert(it != events.end()); 258 std::swap(*it, events.back()); 259 events.pop_back(); 260} 261 262void 263Object::pickUniqueName(std::string &base) 264{ 265 std::string seed = base; 266 while (!nameIsUnique(&children, &events, base)) 267 base = ::sc_core::sc_gen_unique_name(seed.c_str()); 268} 269 270void 271pickUniqueName(::sc_core::sc_object *parent, std::string &base) 272{ 273 if (parent) { 274 Object::getFromScObject(parent)->pickUniqueName(base); 275 return; 276 } 277 278 std::string seed = base; 279 while (!nameIsUnique(&topLevelObjects, &topLevelEvents, base)) 280 base = ::sc_core::sc_gen_unique_name(seed.c_str()); 281} 282 283 284Objects topLevelObjects; 285Objects allObjects; 286 287const std::vector<sc_core::sc_object *> & 288getTopLevelScObjects() 289{ 290 return topLevelObjects; 291} 292 293sc_core::sc_object * 294findObject(const char *name, const Objects &objects) 295{ 296 ObjectsIt it = findObjectIn(allObjects, name); 297 return it == allObjects.end() ? nullptr : *it; 298} 299 300} // namespace sc_gem5 301