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