1 | /*------------------------------------------------------------\ |
---|
2 | | | |
---|
3 | | Tool : systemcass | |
---|
4 | | | |
---|
5 | | File : sc_object.cc | |
---|
6 | | | |
---|
7 | | Author : Buchmann Richard | |
---|
8 | | | |
---|
9 | | Date : 09_07_2004 | |
---|
10 | | | |
---|
11 | \------------------------------------------------------------*/ |
---|
12 | |
---|
13 | /* |
---|
14 | * This file is part of the Disydent Project |
---|
15 | * Copyright (C) Laboratoire LIP6 - Département ASIM |
---|
16 | * Universite Pierre et Marie Curie |
---|
17 | * |
---|
18 | * Home page : http://www-asim.lip6.fr/disydent |
---|
19 | * E-mail : mailto:richard.buchmann@lip6.fr |
---|
20 | * |
---|
21 | * This library is free software; you can redistribute it and/or modify it |
---|
22 | * under the terms of the GNU Library General Public License as published |
---|
23 | * by the Free Software Foundation; either version 2 of the License, or (at |
---|
24 | * your option) any later version. |
---|
25 | * |
---|
26 | * Disydent is distributed in the hope that it will be |
---|
27 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
---|
29 | * Public License for more details. |
---|
30 | * |
---|
31 | * You should have received a copy of the GNU General Public License along |
---|
32 | * with the GNU C Library; see the file COPYING. If not, write to the Free |
---|
33 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
34 | */ |
---|
35 | |
---|
36 | |
---|
37 | #include <stdio.h> |
---|
38 | #include <string> |
---|
39 | #include <map> |
---|
40 | |
---|
41 | #include <cassert> |
---|
42 | #include "sc_object.h" |
---|
43 | //#include "sc_port.h" |
---|
44 | #include "internal.h" |
---|
45 | #include "sc_signal.h" |
---|
46 | #include "module_hierarchy.h" |
---|
47 | #ifdef HAVE_CONFIG_H |
---|
48 | #include "config.h" |
---|
49 | #endif |
---|
50 | |
---|
51 | using namespace std; |
---|
52 | |
---|
53 | namespace sc_core { |
---|
54 | |
---|
55 | static void |
---|
56 | gen_name (const char *prefix, string &s) |
---|
57 | { |
---|
58 | s = prefix; |
---|
59 | static int i = 0; |
---|
60 | char ic[10]; |
---|
61 | sprintf (ic,"%d",i++); |
---|
62 | s += ic; |
---|
63 | } |
---|
64 | // ---------------------------------------------------------------------------- |
---|
65 | static void |
---|
66 | build_complete_name (const char *name, string &out) |
---|
67 | { |
---|
68 | out = ""; |
---|
69 | module_name_stack_t::const_iterator i; |
---|
70 | for (i = module_name_stack.begin (); i != module_name_stack.end (); ++i) { |
---|
71 | const string &module_name = *i; |
---|
72 | out += (module_name + "."); |
---|
73 | // out += "."; |
---|
74 | } |
---|
75 | // assert(name != NULL); |
---|
76 | if (name) |
---|
77 | out += name; |
---|
78 | else |
---|
79 | out[out.length ()-1] = '\0'; |
---|
80 | #if 0 |
---|
81 | cerr << "complete_name = " << out << endl; |
---|
82 | #endif |
---|
83 | } |
---|
84 | |
---|
85 | #if 0 |
---|
86 | static |
---|
87 | void |
---|
88 | build_full_name_r (string &out, const sc_object *obj) |
---|
89 | { |
---|
90 | if (obj == NULL) |
---|
91 | { |
---|
92 | out = ""; |
---|
93 | } else { |
---|
94 | const sc_object *parent = obj->get_parent_object (); |
---|
95 | build_full_name_r (out, parent); |
---|
96 | out += obj->basename (); |
---|
97 | out += "."; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | static |
---|
102 | void |
---|
103 | build_full_name (string &out, const sc_object &obj) |
---|
104 | { |
---|
105 | const sc_object *parent = obj.get_parent_object (); |
---|
106 | build_full_name_r (out, parent); |
---|
107 | out += obj.basename (); |
---|
108 | cerr << "build_full_name = " << out << endl; |
---|
109 | } |
---|
110 | #endif |
---|
111 | |
---|
112 | typedef std::map<const sc_object* const,string> object2name_t; |
---|
113 | static object2name_t object2basename; |
---|
114 | static object2name_t object2fullname; |
---|
115 | |
---|
116 | struct object_infos_t { |
---|
117 | const char *kind_string; |
---|
118 | }; |
---|
119 | typedef std::map<const sc_object* const,object_infos_t> object2infos_t; |
---|
120 | static object2infos_t object2infos; |
---|
121 | |
---|
122 | // We initialize SC_BIND_PROXY_NIL there to make sure object2infos, |
---|
123 | // object2fullname, and object2basename are already initialized. |
---|
124 | // SC_BIND_PROXY_NIL should be declared into sc_module.cc but it prevents us |
---|
125 | // to force the initialization order. |
---|
126 | const char *SC_BIND_PROXY_NIL_string = "SC_BIND_PROXY_NIL"; |
---|
127 | sc_bind_proxy SC_BIND_PROXY_NIL (SC_BIND_PROXY_NIL_string,NULL); |
---|
128 | |
---|
129 | // ---------------------------------------------------------------------------- |
---|
130 | // FUNCTION : sc_gen_unique_name |
---|
131 | // |
---|
132 | // ---------------------------------------------------------------------------- |
---|
133 | |
---|
134 | const char * |
---|
135 | sc_gen_unique_name (const char *basename_) |
---|
136 | { |
---|
137 | string s; |
---|
138 | gen_name (basename_,s); |
---|
139 | return strdup (s.c_str ()); |
---|
140 | } |
---|
141 | |
---|
142 | // ---------------------------------------------------------------------------- |
---|
143 | // CLASS : sc_object |
---|
144 | // |
---|
145 | // ---------------------------------------------------------------------------- |
---|
146 | |
---|
147 | const char* const sc_object::kind_string = "sc_object"; |
---|
148 | |
---|
149 | void |
---|
150 | sc_object::set_kind (const char *k) |
---|
151 | { |
---|
152 | object2infos[this].kind_string = k; |
---|
153 | } |
---|
154 | |
---|
155 | void |
---|
156 | sc_object::init () |
---|
157 | { |
---|
158 | set_kind ("sc_object"); |
---|
159 | add_child (*this); |
---|
160 | } |
---|
161 | |
---|
162 | sc_object::sc_object() |
---|
163 | { |
---|
164 | string noname; |
---|
165 | gen_name ("noname_",noname); |
---|
166 | #if 0 |
---|
167 | cerr << "object2basename[this] = " << noname << "\n"; |
---|
168 | #endif |
---|
169 | object2basename[this] = noname; |
---|
170 | build_complete_name (noname.c_str(),object2fullname[this]); |
---|
171 | init (); |
---|
172 | } |
---|
173 | |
---|
174 | sc_object::sc_object(const char *name_) |
---|
175 | { |
---|
176 | const char *temp; |
---|
177 | if (name_ == NULL) { |
---|
178 | if (module_name_stack.empty()) |
---|
179 | { |
---|
180 | cerr << "Internal error : module_name_stack is empty."; |
---|
181 | exit (21092005); |
---|
182 | } |
---|
183 | string &module_name = module_name_stack.back (); |
---|
184 | temp = module_name.c_str (); |
---|
185 | } else { |
---|
186 | temp = name_; |
---|
187 | } |
---|
188 | #if 0 |
---|
189 | cerr << "object2basename[this] = " << temp << "\n"; |
---|
190 | cerr << "name temp = " << temp << endl; |
---|
191 | #endif |
---|
192 | object2basename[this] = temp; |
---|
193 | build_complete_name (name_,object2fullname[this]); |
---|
194 | init (); |
---|
195 | } |
---|
196 | |
---|
197 | const char * |
---|
198 | sc_object::basename () const |
---|
199 | { |
---|
200 | return object2basename[this].c_str (); |
---|
201 | } |
---|
202 | |
---|
203 | const char * |
---|
204 | sc_object::name () const |
---|
205 | { |
---|
206 | object2name_t::iterator i = object2fullname.find (this); |
---|
207 | #ifdef CONFIG_DEBUG |
---|
208 | if (i == object2fullname.end ()) { |
---|
209 | cerr << "Internal error : can't find name of " << this << "\n"; |
---|
210 | exit (90); |
---|
211 | } |
---|
212 | #endif |
---|
213 | return i->second.c_str (); |
---|
214 | } |
---|
215 | |
---|
216 | void |
---|
217 | sc_object::rename (const char* newname) const |
---|
218 | { |
---|
219 | /* |
---|
220 | object2name_t::iterator i = object2fullname.find (this); |
---|
221 | #ifdef CONFIG_DEBUG |
---|
222 | if (i == object2fullname.end ()) { |
---|
223 | cerr << "Internal error : can't find name of " << this << "\n"; |
---|
224 | exit (90); |
---|
225 | } |
---|
226 | #endif |
---|
227 | i->second = newname; |
---|
228 | object2basename[this] = newname; |
---|
229 | */ |
---|
230 | object2basename[this] = newname; |
---|
231 | build_complete_name (newname,object2fullname[this]); |
---|
232 | /* |
---|
233 | const std::vector<sc_object*>& childs = get_child_objects(); |
---|
234 | std::vector<sc_object*>::const_iterator it; |
---|
235 | for (it = childs.begin (); it != childs.end (); ++it) |
---|
236 | { |
---|
237 | string out; |
---|
238 | sc_object* obj = *it; |
---|
239 | assert(obj != NULL); |
---|
240 | build_full_name (out, *obj); |
---|
241 | } |
---|
242 | */ |
---|
243 | } |
---|
244 | const char *sc_object::kind () const |
---|
245 | { |
---|
246 | object2infos_t::iterator i = object2infos.find (this); |
---|
247 | #ifdef CONFIG_DEBUG |
---|
248 | if (i == object2infos.end ()) { |
---|
249 | cerr << "Internal error : can't find kind of " << this << "\n"; |
---|
250 | exit (90); |
---|
251 | } |
---|
252 | #endif |
---|
253 | return i->second.kind_string; |
---|
254 | } |
---|
255 | |
---|
256 | sc_object::~sc_object () |
---|
257 | { |
---|
258 | if (save_on_exit) |
---|
259 | { |
---|
260 | sc_save_simulation (save_on_exit); |
---|
261 | save_on_exit = NULL; |
---|
262 | } |
---|
263 | object2fullname.erase (this); |
---|
264 | object2basename.erase (this); |
---|
265 | object2infos.erase (this); |
---|
266 | } |
---|
267 | |
---|
268 | std::ostream& |
---|
269 | operator << (std::ostream& os, const sc_object& obj) |
---|
270 | { |
---|
271 | return os << obj.name (); |
---|
272 | } |
---|
273 | |
---|
274 | /* virtual */ |
---|
275 | const std::vector<sc_object*>& |
---|
276 | sc_object::get_child_objects() const |
---|
277 | { |
---|
278 | return sc_core::get_child_objects (*this); |
---|
279 | } |
---|
280 | |
---|
281 | sc_object* |
---|
282 | sc_object::get_parent_object () const |
---|
283 | { |
---|
284 | return sc_core::get_parent_object (*this); |
---|
285 | } |
---|
286 | |
---|
287 | } // end of sc_core namespace |
---|
288 | |
---|