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