summaryrefslogtreecommitdiff
path: root/ext/systemc/src/sysc/kernel/sc_object_manager.cpp
blob: e46413928d089fdbc0997c6b201bce2656860ff7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*****************************************************************************

  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
  more contributor license agreements.  See the NOTICE file distributed
  with this work for additional information regarding copyright ownership.
  Accellera licenses this file to you under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with the
  License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  implied.  See the License for the specific language governing
  permissions and limitations under the License.

 *****************************************************************************/

/*****************************************************************************

  sc_object_manager.cpp -- Manager of objects (naming, &c.)

  Original Author: Stan Y. Liao, Synopsys, Inc.

  CHANGE LOG AT THE END OF THE FILE
 *****************************************************************************/


#include <stdio.h>
#include <cstdlib>
#include <cassert>
#include <ctype.h>
#include <stddef.h>
#include <algorithm> // pick up std::sort.

#include "sysc/utils/sc_iostream.h"
#include "sysc/kernel/sc_object.h"
#include "sysc/utils/sc_hash.h"
#include "sysc/utils/sc_list.h"
#include "sysc/utils/sc_mempool.h"
#include "sysc/kernel/sc_simcontext.h"
#include "sysc/kernel/sc_object_manager.h"
#include "sysc/kernel/sc_kernel_ids.h"
#include "sysc/kernel/sc_process.h"
#include "sysc/kernel/sc_module_name.h"

namespace sc_core {

// ----------------------------------------------------------------------------
//  CLASS : sc_object_manager
//
//  Manager of objects.
// ----------------------------------------------------------------------------

sc_object_manager::sc_object_manager() :
    m_event_it(),
    m_event_walk_ok(0),
    m_instance_table(),
    m_module_name_stack(0),
    m_object_it(),
    m_object_stack(),
    m_object_walk_ok()
{
}

// +----------------------------------------------------------------------------
// |"sc_object_manager::~sc_object_manager"
// | 
// | This is the object instance destructor for this class. It goes through
// | each sc_object instance in the instance table and sets its m_simc field
// | to NULL.
// +----------------------------------------------------------------------------
sc_object_manager::~sc_object_manager()
{
    instance_table_t::iterator it;     // instance table iterator.

    for ( it = m_instance_table.begin(); it != m_instance_table.end(); it++) 
    {
        sc_object* obj_p = it->second.m_object_p;
        if ( obj_p ) obj_p->m_simc = 0;
    }
}

// +----------------------------------------------------------------------------
// |"sc_object_manager::create_name"
// | 
// | This method creates a hierarchical name based on the name of the active 
// | object and the supplied leaf name. If the resultant name is not unique it 
// | will be made unique and a warning message issued.
// |
// | Arguments:
// |     leaf_name = name to use for the leaf of the hierarchy.
// | Result is an std::string containing the name.
// +----------------------------------------------------------------------------
std::string sc_object_manager::create_name(const char* leaf_name) 
{ 
    bool        clash;                  // true if path name exists in obj table
    std::string leafname_string;        // string containing the leaf name.
    std::string parentname_string;      // parent path name 
    sc_object*  parent_p;               // parent for this instance or NULL.
    std::string result_orig_string;     // save for warning message.
    std::string result_string;          // name to return.
 
    // CONSTRUCT PATHNAME TO THE NAME TO BE RETURNED:
    // 
    // If there is not a leaf name generate one. 

    parent_p = sc_get_curr_simcontext()->active_object();
    parentname_string = parent_p ? parent_p->name() : ""; 
    leafname_string = leaf_name; 
    if (parent_p) {
        result_string = parentname_string;
	result_string += SC_HIERARCHY_CHAR;
	result_string += leafname_string;
    } else { 
        result_string = leafname_string;
    } 

    // SAVE the original path name 

    result_orig_string = result_string;

    // MAKE SURE THE ENTITY NAME IS UNIQUE:
    // 
    // If not use unique name generator to make it unique. 

    clash = false; 
    for (;;)
    {
	instance_table_t::iterator it = m_instance_table.find(result_string);
	if ( it == m_instance_table.end() ||
	     (it->second.m_event_p == NULL && it->second.m_object_p == NULL ) )
	{
	    break;
	}
        clash = true; 
        leafname_string = sc_gen_unique_name(leafname_string.c_str(), false); 
	if (parent_p) {
	    result_string = parentname_string;
	    result_string += SC_HIERARCHY_CHAR;
	    result_string += leafname_string;
	} else { 
	    result_string = leafname_string;
	} 
    } 
    if (clash) { 
	std::string message = result_orig_string;
	message += ". Latter declaration will be renamed to ";
	message += result_string;
        SC_REPORT_WARNING( SC_ID_INSTANCE_EXISTS_, message.c_str());
    } 

    return result_string;
}

// +----------------------------------------------------------------------------
// |"sc_object_manager::find_event"
// | 
// | This method returns the sc_event with the supplied name, or a NULL if
// | the event does not exist.
// |
// | Arguments:
// |     name = name of the event
// | Result is a pointer to the event or NULL if it does not exist.
// +----------------------------------------------------------------------------
sc_event*
sc_object_manager::find_event(const char* name)
{
    instance_table_t::iterator it;
    it = m_instance_table.find(name);
    return it == m_instance_table.end() ? NULL : it->second.m_event_p;
}

// +----------------------------------------------------------------------------
// |"sc_object_manager::find_object"
// | 
// | This method returns the sc_object with the supplied name, or a NULL if
// | the object does not exist.
// |
// | Arguments:
// |     name = name of the object
// | Result is a pointer to the object or NULL if it does not exist.
// +----------------------------------------------------------------------------
sc_object*
sc_object_manager::find_object(const char* name)
{
    instance_table_t::iterator it;
    it = m_instance_table.find(name);
    return it == m_instance_table.end() ? NULL : it->second.m_object_p;
}


// +----------------------------------------------------------------------------
// |"sc_object_manager::first_object"
// | 
// | This method initializes the object iterator to point to the first object
// | in the instance table, and returns its address. If there are no objects
// | in the table a NULL value is returned.
// +----------------------------------------------------------------------------
sc_object*
sc_object_manager::first_object()
{
    sc_object* result_p; // result to return.

    m_object_walk_ok = true;
    result_p = NULL;
    for ( m_object_it = m_instance_table.begin(); 
          m_object_it != m_instance_table.end(); 
	  m_object_it++ )
    {
        result_p = m_object_it->second.m_object_p;
	if ( result_p ) break;
    }
    return result_p;
}

// +----------------------------------------------------------------------------
// |"sc_object_manager::hierarchy_curr"
// | 
// | This method returns the current object in the object hierarchy or NULL
// | if it does not exist.
// +----------------------------------------------------------------------------
sc_object*
sc_object_manager::hierarchy_curr()
{
    size_t     hierarchy_n; // current size of the hierarchy.

    hierarchy_n = m_object_stack.size();
    return hierarchy_n ? m_object_stack[hierarchy_n-1] : 0;
}

// +----------------------------------------------------------------------------
// |"sc_object_manager::hierarchy_pop"
// | 
// | This method pops the current object off the object hierarchy and returns
// | it.
// +----------------------------------------------------------------------------
sc_object*
sc_object_manager::hierarchy_pop()
{
    size_t     hierarchy_n; // current size of the hierarchy.
    sc_object* result_p;    // object to return.

    hierarchy_n = m_object_stack.size();
    if ( hierarchy_n == 0 ) return NULL;
    hierarchy_n--;
    result_p = m_object_stack[hierarchy_n];
    m_object_stack.pop_back();
    return result_p;
}

// +----------------------------------------------------------------------------
// |"sc_object_manager::hierarchy_push"
// | 
// | This method pushes down the sc_object hierarchy to make the supplied 
// | object the current object in the hierarchy.
// |
// | Arguments:
// |     object_p -> object to become the new current object in the hierarchy.
// +----------------------------------------------------------------------------
void
sc_object_manager::hierarchy_push(sc_object* object_p)
{
    m_object_stack.push_back(object_p);
}


// +----------------------------------------------------------------------------
// |"sc_object_manager::hierarchy_size"
// | 
// | This method returns the current size of the object hierarchy stack.
// +----------------------------------------------------------------------------
int
sc_object_manager::hierarchy_size()
{
    return m_object_stack.size();
}

// +----------------------------------------------------------------------------
// |"sc_object_manager::insert_event"
// | 
// | This method inserts the supplied sc_event instance into the instance
// | table using the supplied name.
// |
// | Arguments:
// |     name    =  name of the event to be inserted.
// |     event_p -> event to be inserted.
// +----------------------------------------------------------------------------
void
sc_object_manager::insert_event(const std::string& name, sc_event* event_p)
{
    m_instance_table[name].m_event_p = event_p;
}

// +----------------------------------------------------------------------------
// |"sc_object_manager::insert_object"
// | 
// | This method inserts the supplied sc_object instance into the instance
// | table using the supplied name.
// |
// | Arguments:
// |     name     =  name of the event to be inserted.
// |     object_p -> object to be inserted.
// +----------------------------------------------------------------------------
void
sc_object_manager::insert_object(const std::string& name, sc_object* object_p)
{
    m_instance_table[name].m_object_p = object_p;
}

// +----------------------------------------------------------------------------
// |"sc_object_manager::next_object"
// | 
// | This method returns the next object pointed to by the instance iterator.
// +----------------------------------------------------------------------------
sc_object*
sc_object_manager::next_object()
{
    sc_object* result_p; // result to return.

    assert( m_object_walk_ok );

    if ( m_object_it == m_instance_table.end() ) return NULL;
    m_object_it++;

    for ( result_p = NULL; m_object_it != m_instance_table.end(); 
	  m_object_it++ )
    {
        result_p = m_object_it->second.m_object_p;
	if ( result_p ) break;
    }
    return result_p;
}

// +----------------------------------------------------------------------------
// |"sc_object_manager::pop_module_name"
// | 
// | This method pops an entry off the module name stack and returns it.
// +----------------------------------------------------------------------------
sc_module_name*
sc_object_manager::pop_module_name()
{
    sc_module_name* mod_name = m_module_name_stack;
    m_module_name_stack = m_module_name_stack->m_next;
    mod_name->m_next = 0;
    return mod_name;
}

// +----------------------------------------------------------------------------
// |"sc_object_manager::push_module_name"
// | 
// | This method pushes the supplied entry onto the module name stack.
// |
// | Arguments:
// |     mod_name_p -> entry to push onto the module name stack.
// +----------------------------------------------------------------------------
void
sc_object_manager::push_module_name(sc_module_name* mod_name_p)
{
    mod_name_p->m_next = m_module_name_stack;
    m_module_name_stack = mod_name_p;
}

// +----------------------------------------------------------------------------
// |"sc_object_manager::top_of_module_name_stack"
// | 
// | This method returns the module name that is on the top of the module
// | name stack.
// +----------------------------------------------------------------------------
sc_module_name*
sc_object_manager::top_of_module_name_stack()
{
    if( m_module_name_stack == 0 ) {
	SC_REPORT_ERROR( SC_ID_MODULE_NAME_STACK_EMPTY_, 0 );
    }
    return m_module_name_stack;
}

// +----------------------------------------------------------------------------
// |"sc_object_manager::remove_event"
// | 
// | This method removes the sc_event instance with the supplied name from
// | the table of instances. Note we just clear the pointer since if the name
// | was for an sc_object the m_event_p pointer will be null anyway.
// |
// | Arguments:
// |     name = name of the event to be removed.
// +----------------------------------------------------------------------------
void
sc_object_manager::remove_event(const std::string& name)
{
    instance_table_t::iterator it;     // instance table iterator.
    it = m_instance_table.find(name);
    if ( it != m_instance_table.end() ) it->second.m_event_p = NULL;
}

// +----------------------------------------------------------------------------
// |"sc_object_manager::remove_object"
// | 
// | This method removes the sc_object instance with the supplied name from
// | the table of instances. Note we just clear the pointer since if the name
// | was for an sc_event the m_object_p pointer will be null anyway.
// |
// | Arguments:
// |     name = name of the object to be removed.
// +----------------------------------------------------------------------------
void
sc_object_manager::remove_object(const std::string& name)
{
    instance_table_t::iterator it;     // instance table iterator.
    it = m_instance_table.find(name);
    if ( it != m_instance_table.end() ) it->second.m_object_p = NULL;
}

} // namespace sc_core

// $Log: sc_object_manager.cpp,v $
// Revision 1.13  2011/08/26 20:46:10  acg
//  Andy Goodrich: moved the modification log to the end of the file to
//  eliminate source line number skew when check-ins are done.
//
// Revision 1.12  2011/08/24 22:05:51  acg
//  Torsten Maehne: initialization changes to remove warnings.
//
// Revision 1.11  2011/06/25 17:08:39  acg
//  Andy Goodrich: Jerome Cornet's changes to use libtool to build the
//  library.
//
// Revision 1.10  2011/04/01 21:27:54  acg
//  Andy Goodrich: documentation of event and object insertion methods.
//
// Revision 1.9  2011/03/06 15:55:11  acg
//  Andy Goodrich: Changes for named events.
//
// Revision 1.8  2011/03/05 19:44:20  acg
//  Andy Goodrich: changes for object and event naming and structures.
//
// Revision 1.7  2011/03/05 04:45:16  acg
//  Andy Goodrich: moved active process calculation to the sc_simcontext class.
//
// Revision 1.6  2011/03/05 01:39:21  acg
//  Andy Goodrich: changes for named events.
//
// Revision 1.5  2011/02/18 20:27:14  acg
//  Andy Goodrich: Updated Copyrights.
//
// Revision 1.4  2011/02/13 21:47:37  acg
//  Andy Goodrich: update copyright notice.
//
// Revision 1.3  2010/07/22 20:02:33  acg
//  Andy Goodrich: bug fixes.
//
// Revision 1.2  2008/05/22 17:06:26  acg
//  Andy Goodrich: updated copyright notice to include 2008.
//
// Revision 1.1.1.1  2006/12/15 20:20:05  acg
// SystemC 2.3
//
// Revision 1.3  2006/01/13 18:44:30  acg
// Added $Log to record CVS changes into the source.
//