| 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 <cstdio> | 
|---|
| 38 | #include <cstring> //strdup | 
|---|
| 39 | #include <string> | 
|---|
| 40 | #include <map> | 
|---|
| 41 | #include <cassert> | 
|---|
| 42 |  | 
|---|
| 43 | #include "sc_object.h" | 
|---|
| 44 | #include "internal.h" | 
|---|
| 45 | #include "sc_signal.h" | 
|---|
| 46 | #include "module_hierarchy.h" | 
|---|
| 47 |  | 
|---|
| 48 | #ifdef HAVE_CONFIG_H | 
|---|
| 49 | #include "config.h" | 
|---|
| 50 | #endif | 
|---|
| 51 |  | 
|---|
| 52 |  | 
|---|
| 53 | using namespace std; | 
|---|
| 54 |  | 
|---|
| 55 | namespace sc_core { | 
|---|
| 56 |  | 
|---|
| 57 | static void gen_name (const char *prefix, string &s) { | 
|---|
| 58 |     s = prefix; | 
|---|
| 59 |     static int i = 0; | 
|---|
| 60 |     char ic[10]; | 
|---|
| 61 |     sprintf (ic,"%d",i++); | 
|---|
| 62 |     s += ic; | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 |  | 
|---|
| 66 | static void build_complete_name (const char *name, string &out) { | 
|---|
| 67 |     out = ""; | 
|---|
| 68 |     module_name_stack_t::const_iterator i; | 
|---|
| 69 |     for (i = module_name_stack.begin (); i != module_name_stack.end (); ++i) { | 
|---|
| 70 |         const string &module_name = *i; | 
|---|
| 71 |         out += (module_name + "."); | 
|---|
| 72 |     } | 
|---|
| 73 |     if (name) { | 
|---|
| 74 |         out += name; | 
|---|
| 75 |     } | 
|---|
| 76 |     else { | 
|---|
| 77 |         out[out.length ()-1] = '\0'; | 
|---|
| 78 |     } | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 |  | 
|---|
| 82 | typedef std::map<const sc_object * const, string> object2name_t; | 
|---|
| 83 | static object2name_t object2basename; | 
|---|
| 84 | static object2name_t object2fullname; | 
|---|
| 85 |  | 
|---|
| 86 | struct object_infos_t { | 
|---|
| 87 |     const char * kind_string; | 
|---|
| 88 | }; | 
|---|
| 89 |  | 
|---|
| 90 | typedef std::map<const sc_object * const, object_infos_t> object2infos_t; | 
|---|
| 91 | static object2infos_t object2infos; | 
|---|
| 92 |  | 
|---|
| 93 |  | 
|---|
| 94 | // We initialize SC_BIND_PROXY_NIL there to make sure object2infos, | 
|---|
| 95 | // object2fullname, and object2basename are already initialized. | 
|---|
| 96 | // SC_BIND_PROXY_NIL should be declared into sc_module.cc but it prevents us | 
|---|
| 97 | // to force the initialization order. | 
|---|
| 98 | const char * SC_BIND_PROXY_NIL_string = "SC_BIND_PROXY_NIL"; | 
|---|
| 99 | sc_bind_proxy SC_BIND_PROXY_NIL(SC_BIND_PROXY_NIL_string, NULL); | 
|---|
| 100 |  | 
|---|
| 101 |  | 
|---|
| 102 | // ---------------------------------------------------------------------------- | 
|---|
| 103 | //  FUNCTION : sc_gen_unique_name | 
|---|
| 104 | //                                                | 
|---|
| 105 | // ---------------------------------------------------------------------------- | 
|---|
| 106 | const char * sc_gen_unique_name(const char * basename_) { | 
|---|
| 107 |     string s; | 
|---|
| 108 |     gen_name(basename_, s); | 
|---|
| 109 |     const char * ret = strdup(s.c_str()); | 
|---|
| 110 |     sc_core::allocated_names.push_back(ret); | 
|---|
| 111 |     return ret; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 |  | 
|---|
| 115 | // ---------------------------------------------------------------------------- | 
|---|
| 116 | //  CLASS : sc_object | 
|---|
| 117 | //                                                | 
|---|
| 118 | // ---------------------------------------------------------------------------- | 
|---|
| 119 |  | 
|---|
| 120 | const char * const sc_object::kind_string = "sc_object"; | 
|---|
| 121 |  | 
|---|
| 122 | void sc_object::set_kind(const char * k) { | 
|---|
| 123 |     object2infos[this].kind_string = k; | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 |  | 
|---|
| 127 | void sc_object::init() { | 
|---|
| 128 |     set_kind("sc_object"); | 
|---|
| 129 |     add_child(*this); | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 |  | 
|---|
| 133 | sc_object::sc_object() { | 
|---|
| 134 |     string noname; | 
|---|
| 135 |     gen_name("noname_", noname); | 
|---|
| 136 |     object2basename[this] = noname; | 
|---|
| 137 |     build_complete_name(noname.c_str(), object2fullname[this]); | 
|---|
| 138 |     init(); | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 |  | 
|---|
| 142 | sc_object::sc_object(const char * name_) { | 
|---|
| 143 |     const char * temp; | 
|---|
| 144 |     if (name_ == NULL) { | 
|---|
| 145 | #ifdef CONFIG_DEBUG | 
|---|
| 146 |         if (module_name_stack.empty()) { | 
|---|
| 147 |             cerr << "Internal error : module_name_stack is empty."; | 
|---|
| 148 |             exit(21092005); | 
|---|
| 149 |         } | 
|---|
| 150 | #endif | 
|---|
| 151 |         string & module_name = module_name_stack.back(); | 
|---|
| 152 |         temp = module_name.c_str(); | 
|---|
| 153 |     } | 
|---|
| 154 |     else { | 
|---|
| 155 |         temp = name_; | 
|---|
| 156 |     } | 
|---|
| 157 |     object2basename[this] = temp; | 
|---|
| 158 |     build_complete_name(name_, object2fullname[this]); | 
|---|
| 159 |     init(); | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 |  | 
|---|
| 163 | const char * sc_object::basename() const { | 
|---|
| 164 |     return object2basename[this].c_str(); | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 |  | 
|---|
| 168 | const char * sc_object::name() const { | 
|---|
| 169 |     object2name_t::iterator i = object2fullname.find(this); | 
|---|
| 170 | #ifdef CONFIG_DEBUG | 
|---|
| 171 |     if (i == object2fullname.end()) { | 
|---|
| 172 |         cerr << "Internal error : can't find name of " << this << "\n"; | 
|---|
| 173 |         exit (90); | 
|---|
| 174 |     } | 
|---|
| 175 | #endif | 
|---|
| 176 |     return i->second.c_str(); | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 |  | 
|---|
| 180 | void sc_object::rename(const char * newname) const { | 
|---|
| 181 |     object2basename[this] = newname; | 
|---|
| 182 |     build_complete_name (newname,object2fullname[this]); | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 |  | 
|---|
| 186 | const char * sc_object::kind() const { | 
|---|
| 187 |     object2infos_t::iterator i = object2infos.find(this); | 
|---|
| 188 | #ifdef CONFIG_DEBUG | 
|---|
| 189 |     if (i == object2infos.end()) { | 
|---|
| 190 |         cerr << "Internal error : can't find kind of " << this << "\n"; | 
|---|
| 191 |         exit(90); | 
|---|
| 192 |     } | 
|---|
| 193 | #endif | 
|---|
| 194 |     return i->second.kind_string; | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 |  | 
|---|
| 198 | sc_object::~sc_object() { | 
|---|
| 199 |     if (save_on_exit) { | 
|---|
| 200 |         sc_save_simulation(save_on_exit); | 
|---|
| 201 |         save_on_exit = NULL; | 
|---|
| 202 |     } | 
|---|
| 203 |     object2fullname.erase(this); | 
|---|
| 204 |     object2basename.erase(this); | 
|---|
| 205 |     object2infos.erase(this); | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 |  | 
|---|
| 209 | std::ostream & operator << (std::ostream & os, const sc_object & obj) { | 
|---|
| 210 |     return os << obj.name (); | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 |  | 
|---|
| 214 | /* virtual */  | 
|---|
| 215 | const std::vector<sc_object *> & sc_object::get_child_objects() const { | 
|---|
| 216 |     return sc_core::get_child_objects(*this); | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 |  | 
|---|
| 220 | sc_object * sc_object::get_parent_object() const { | 
|---|
| 221 |     return sc_core::get_parent_object(*this); | 
|---|
| 222 | } | 
|---|
| 223 |  | 
|---|
| 224 |  | 
|---|
| 225 | } // end of sc_core namespace | 
|---|
| 226 |  | 
|---|
| 227 |  | 
|---|
| 228 | /* | 
|---|
| 229 | # Local Variables: | 
|---|
| 230 | # tab-width: 4; | 
|---|
| 231 | # c-basic-offset: 4; | 
|---|
| 232 | # c-file-offsets:((innamespace . 0)(inline-open . 0)); | 
|---|
| 233 | # indent-tabs-mode: nil; | 
|---|
| 234 | # End: | 
|---|
| 235 | # | 
|---|
| 236 | # vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 | 
|---|
| 237 | */ | 
|---|
| 238 |  | 
|---|