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