SimObject.py (5543:3af77710f397) SimObject.py (5610:0e1e9c186769)
1# Copyright (c) 2004-2006 The Regents of The University of Michigan
2# All rights reserved.
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

--- 109 unchanged lines hidden (view full) ---

118
119# The metaclass for SimObject. This class controls how new classes
120# that derive from SimObject are instantiated, and provides inherited
121# class behavior (just like a class controls how instances of that
122# class are instantiated, and provides inherited instance behavior).
123class MetaSimObject(type):
124 # Attributes that can be set only at initialization time
125 init_keywords = { 'abstract' : types.BooleanType,
1# Copyright (c) 2004-2006 The Regents of The University of Michigan
2# All rights reserved.
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

--- 109 unchanged lines hidden (view full) ---

118
119# The metaclass for SimObject. This class controls how new classes
120# that derive from SimObject are instantiated, and provides inherited
121# class behavior (just like a class controls how instances of that
122# class are instantiated, and provides inherited instance behavior).
123class MetaSimObject(type):
124 # Attributes that can be set only at initialization time
125 init_keywords = { 'abstract' : types.BooleanType,
126 'cxx_namespace' : types.StringType,
127 'cxx_class' : types.StringType,
128 'cxx_type' : types.StringType,
129 'cxx_predecls' : types.ListType,
130 'swig_objdecls' : types.ListType,
131 'swig_predecls' : types.ListType,
132 'type' : types.StringType }
133 # Attributes that can be set any time
134 keywords = { 'check' : types.FunctionType }

--- 50 unchanged lines hidden (view full) ---

185
186 base = bases[0]
187
188 # Set up general inheritance via multidicts. A subclass will
189 # inherit all its settings from the base class. The only time
190 # the following is not true is when we define the SimObject
191 # class itself (in which case the multidicts have no parent).
192 if isinstance(base, MetaSimObject):
126 'cxx_class' : types.StringType,
127 'cxx_type' : types.StringType,
128 'cxx_predecls' : types.ListType,
129 'swig_objdecls' : types.ListType,
130 'swig_predecls' : types.ListType,
131 'type' : types.StringType }
132 # Attributes that can be set any time
133 keywords = { 'check' : types.FunctionType }

--- 50 unchanged lines hidden (view full) ---

184
185 base = bases[0]
186
187 # Set up general inheritance via multidicts. A subclass will
188 # inherit all its settings from the base class. The only time
189 # the following is not true is when we define the SimObject
190 # class itself (in which case the multidicts have no parent).
191 if isinstance(base, MetaSimObject):
192 cls._base = base
193 cls._params.parent = base._params
194 cls._ports.parent = base._ports
195 cls._values.parent = base._values
196 cls._port_refs.parent = base._port_refs
197 # mark base as having been subclassed
198 base._instantiated = True
193 cls._params.parent = base._params
194 cls._ports.parent = base._ports
195 cls._values.parent = base._values
196 cls._port_refs.parent = base._port_refs
197 # mark base as having been subclassed
198 base._instantiated = True
199 else:
200 cls._base = None
199
200 # default keyword values
201 if 'type' in cls._value_dict:
201
202 # default keyword values
203 if 'type' in cls._value_dict:
202 _type = cls._value_dict['type']
203 if 'cxx_class' not in cls._value_dict:
204 if 'cxx_class' not in cls._value_dict:
204 cls._value_dict['cxx_class'] = _type
205 cls._value_dict['cxx_class'] = cls._value_dict['type']
205
206
206 namespace = cls._value_dict.get('cxx_namespace', None)
207
208 _cxx_class = cls._value_dict['cxx_class']
209 if 'cxx_type' not in cls._value_dict:
210 t = _cxx_class + '*'
211 if namespace:
212 t = '%s::%s' % (namespace, t)
213 cls._value_dict['cxx_type'] = t
207 cls._value_dict['cxx_type'] = '%s *' % cls._value_dict['cxx_class']
208
214 if 'cxx_predecls' not in cls._value_dict:
215 # A forward class declaration is sufficient since we are
216 # just declaring a pointer.
209 if 'cxx_predecls' not in cls._value_dict:
210 # A forward class declaration is sufficient since we are
211 # just declaring a pointer.
217 decl = 'class %s;' % _cxx_class
218 if namespace:
219 namespaces = namespace.split('::')
220 namespaces.reverse()
221 for namespace in namespaces:
222 decl = 'namespace %s { %s }' % (namespace, decl)
212 class_path = cls._value_dict['cxx_class'].split('::')
213 class_path.reverse()
214 decl = 'class %s;' % class_path[0]
215 for ns in class_path[1:]:
216 decl = 'namespace %s { %s }' % (ns, decl)
223 cls._value_dict['cxx_predecls'] = [decl]
224
225 if 'swig_predecls' not in cls._value_dict:
226 # A forward class declaration is sufficient since we are
227 # just declaring a pointer.
228 cls._value_dict['swig_predecls'] = \
229 cls._value_dict['cxx_predecls']
230

--- 115 unchanged lines hidden (view full) ---

346 return cls._values[attr]
347
348 raise AttributeError, \
349 "object '%s' has no attribute '%s'" % (cls.__name__, attr)
350
351 def __str__(cls):
352 return cls.__name__
353
217 cls._value_dict['cxx_predecls'] = [decl]
218
219 if 'swig_predecls' not in cls._value_dict:
220 # A forward class declaration is sufficient since we are
221 # just declaring a pointer.
222 cls._value_dict['swig_predecls'] = \
223 cls._value_dict['cxx_predecls']
224

--- 115 unchanged lines hidden (view full) ---

340 return cls._values[attr]
341
342 raise AttributeError, \
343 "object '%s' has no attribute '%s'" % (cls.__name__, attr)
344
345 def __str__(cls):
346 return cls.__name__
347
354 def get_base(cls):
355 if str(cls) == 'SimObject':
356 return None
357
358 return cls.__bases__[0].type
359
360 def cxx_decl(cls):
361 code = "#ifndef __PARAMS__%s\n" % cls
362 code += "#define __PARAMS__%s\n\n" % cls
363
364 # The 'dict' attribute restricts us to the params declared in
365 # the object itself, not including inherited params (which
366 # will also be inherited from the base class's param struct
367 # here).

--- 14 unchanged lines hidden (view full) ---

382 predecls2 = []
383 for pd in predecls:
384 if pd not in predecls2:
385 predecls2.append(pd)
386 predecls2.sort()
387 code += "\n".join(predecls2)
388 code += "\n\n";
389
348 def cxx_decl(cls):
349 code = "#ifndef __PARAMS__%s\n" % cls
350 code += "#define __PARAMS__%s\n\n" % cls
351
352 # The 'dict' attribute restricts us to the params declared in
353 # the object itself, not including inherited params (which
354 # will also be inherited from the base class's param struct
355 # here).

--- 14 unchanged lines hidden (view full) ---

370 predecls2 = []
371 for pd in predecls:
372 if pd not in predecls2:
373 predecls2.append(pd)
374 predecls2.sort()
375 code += "\n".join(predecls2)
376 code += "\n\n";
377
390 base = cls.get_base()
391 if base:
392 code += '#include "params/%s.hh"\n\n' % base
378 if cls._base:
379 code += '#include "params/%s.hh"\n\n' % cls._base.type
393
394 for ptype in ptypes:
395 if issubclass(ptype, Enum):
396 code += '#include "enums/%s.hh"\n' % ptype.__name__
397 code += "\n\n"
398
380
381 for ptype in ptypes:
382 if issubclass(ptype, Enum):
383 code += '#include "enums/%s.hh"\n' % ptype.__name__
384 code += "\n\n"
385
399 code += cls.cxx_struct(base, params)
386 code += cls.cxx_struct(cls._base, params)
400
401 # close #ifndef __PARAMS__* guard
402 code += "\n#endif\n"
403 return code
404
405 def cxx_struct(cls, base, params):
406 if cls == SimObject:
407 return '#include "sim/sim_object_params.hh"\n'
408
409 # now generate the actual param struct
410 code = "struct %sParams" % cls
411 if base:
387
388 # close #ifndef __PARAMS__* guard
389 code += "\n#endif\n"
390 return code
391
392 def cxx_struct(cls, base, params):
393 if cls == SimObject:
394 return '#include "sim/sim_object_params.hh"\n'
395
396 # now generate the actual param struct
397 code = "struct %sParams" % cls
398 if base:
412 code += " : public %sParams" % base
399 code += " : public %sParams" % base.type
413 code += "\n{\n"
414 if not hasattr(cls, 'abstract') or not cls.abstract:
415 if 'type' in cls.__dict__:
416 code += " %s create();\n" % cls.cxx_type
417 decls = [p.cxx_decl() for p in params]
418 decls.sort()
419 code += "".join([" %s\n" % d for d in decls])
420 code += "};\n"
421
422 return code
423
400 code += "\n{\n"
401 if not hasattr(cls, 'abstract') or not cls.abstract:
402 if 'type' in cls.__dict__:
403 code += " %s create();\n" % cls.cxx_type
404 decls = [p.cxx_decl() for p in params]
405 decls.sort()
406 code += "".join([" %s\n" % d for d in decls])
407 code += "};\n"
408
409 return code
410
424 def cxx_type_decl(cls):
425 base = cls.get_base()
426 code = ''
427
428 if base:
429 code += '#include "%s_type.h"\n' % base
430
431 # now generate dummy code for inheritance
432 code += "struct %s" % cls.cxx_class
433 if base:
434 code += " : public %s" % base.cxx_class
435 code += "\n{};\n"
436
437 return code
438
439 def swig_decl(cls):
411 def swig_decl(cls):
440 base = cls.get_base()
441
442 code = '%%module %s\n' % cls
443
444 code += '%{\n'
445 code += '#include "params/%s.hh"\n' % cls
446 code += '%}\n\n'
447
448 # The 'dict' attribute restricts us to the params declared in
449 # the object itself, not including inherited params (which

--- 11 unchanged lines hidden (view full) ---

461 predecls2 = []
462 for pd in predecls:
463 if pd not in predecls2:
464 predecls2.append(pd)
465 predecls2.sort()
466 code += "\n".join(predecls2)
467 code += "\n\n";
468
412 code = '%%module %s\n' % cls
413
414 code += '%{\n'
415 code += '#include "params/%s.hh"\n' % cls
416 code += '%}\n\n'
417
418 # The 'dict' attribute restricts us to the params declared in
419 # the object itself, not including inherited params (which

--- 11 unchanged lines hidden (view full) ---

431 predecls2 = []
432 for pd in predecls:
433 if pd not in predecls2:
434 predecls2.append(pd)
435 predecls2.sort()
436 code += "\n".join(predecls2)
437 code += "\n\n";
438
469 if base:
470 code += '%%import "params/%s.i"\n\n' % base
439 if cls._base:
440 code += '%%import "params/%s.i"\n\n' % cls._base.type
471
472 for ptype in ptypes:
473 if issubclass(ptype, Enum):
474 code += '%%import "enums/%s.hh"\n' % ptype.__name__
475 code += "\n\n"
476
477 code += '%%import "params/%s_type.hh"\n\n' % cls
478 code += '%%include "params/%s.hh"\n\n' % cls

--- 439 unchanged lines hidden ---
441
442 for ptype in ptypes:
443 if issubclass(ptype, Enum):
444 code += '%%import "enums/%s.hh"\n' % ptype.__name__
445 code += "\n\n"
446
447 code += '%%import "params/%s_type.hh"\n\n' % cls
448 code += '%%include "params/%s.hh"\n\n' % cls

--- 439 unchanged lines hidden ---