Deleted Added
sdiff udiff text old ( 7534:c76a14014c27 ) new ( 7673:b28bd1fa9a35 )
full compact
1# Copyright (c) 2004-2006 The Regents of The University of Michigan
2# Copyright (c) 2010 Advanced Micro Devices, Inc.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met: redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer;

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

24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27#
28# Authors: Steve Reinhardt
29# Nathan Binkert
30
31import sys
32from types import FunctionType
33
34try:
35 import pydot
36except:
37 pydot = False
38
39import m5
40from m5.util import *

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

92#####################################################################
93
94# list of all SimObject classes
95allClasses = {}
96
97# dict to look up SimObjects based on path
98instanceDict = {}
99
100# The metaclass for SimObject. This class controls how new classes
101# that derive from SimObject are instantiated, and provides inherited
102# class behavior (just like a class controls how instances of that
103# class are instantiated, and provides inherited instance behavior).
104class MetaSimObject(type):
105 # Attributes that can be set only at initialization time
106 init_keywords = { 'abstract' : bool,
107 'cxx_class' : str,
108 'cxx_type' : str,
109 'cxx_predecls' : list,
110 'swig_objdecls' : list,
111 'swig_predecls' : list,
112 'type' : str }
113 # Attributes that can be set any time
114 keywords = { 'check' : FunctionType }
115
116 # __new__ is called before __init__, and is where the statements
117 # in the body of the class definition get loaded into the class's
118 # __dict__. We intercept this to filter out parameter & port assignments
119 # and only allow "private" attributes to be passed to the base
120 # __new__ (starting with underscore).
121 def __new__(mcls, name, bases, dict):
122 assert name not in allClasses, "SimObject %s already present" % name
123
124 # Copy "private" attributes, functions, and classes to the
125 # official dict. Everything else goes in _init_dict to be
126 # filtered in __init__.
127 cls_dict = {}
128 value_dict = {}
129 for key,val in dict.items():
130 if key.startswith('_') or isinstance(val, (FunctionType,
131 classmethod,
132 type)):
133 cls_dict[key] = val
134 else:
135 # must be a param/port setting
136 value_dict[key] = val
137 if 'abstract' not in value_dict:
138 value_dict['abstract'] = False
139 cls_dict['_value_dict'] = value_dict
140 cls = super(MetaSimObject, mcls).__new__(mcls, name, bases, cls_dict)

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

185
186 # default keyword values
187 if 'type' in cls._value_dict:
188 if 'cxx_class' not in cls._value_dict:
189 cls._value_dict['cxx_class'] = cls._value_dict['type']
190
191 cls._value_dict['cxx_type'] = '%s *' % cls._value_dict['cxx_class']
192
193 if 'cxx_predecls' not in cls._value_dict:
194 # A forward class declaration is sufficient since we are
195 # just declaring a pointer.
196 class_path = cls._value_dict['cxx_class'].split('::')
197 class_path.reverse()
198 decl = 'class %s;' % class_path[0]
199 for ns in class_path[1:]:
200 decl = 'namespace %s { %s }' % (ns, decl)
201 cls._value_dict['cxx_predecls'] = [decl]
202
203 if 'swig_predecls' not in cls._value_dict:
204 # A forward class declaration is sufficient since we are
205 # just declaring a pointer.
206 cls._value_dict['swig_predecls'] = \
207 cls._value_dict['cxx_predecls']
208
209 if 'swig_objdecls' not in cls._value_dict:
210 cls._value_dict['swig_objdecls'] = []
211
212 # Now process the _value_dict items. They could be defining
213 # new (or overriding existing) parameters or ports, setting
214 # class keywords (e.g., 'abstract'), or setting parameter
215 # values or port bindings. The first 3 can only be set when
216 # the class is defined, so we handle them here. The others
217 # can be set later too, so just emulate that by calling
218 # setattr().

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

277 ref = cls._ports[attr].makeRef(cls)
278 cls._port_refs[attr] = ref
279 return ref
280
281 # Set attribute (called on foo.attr = value when foo is an
282 # instance of class cls).
283 def __setattr__(cls, attr, value):
284 # normal processing for private attributes
285 if attr.startswith('_'):
286 type.__setattr__(cls, attr, value)
287 return
288
289 if cls.keywords.has_key(attr):
290 cls._set_keyword(attr, value, cls.keywords[attr])
291 return
292
293 if cls._ports.has_key(attr):

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

323 return cls._children[attr]
324
325 raise AttributeError, \
326 "object '%s' has no attribute '%s'" % (cls.__name__, attr)
327
328 def __str__(cls):
329 return cls.__name__
330
331 def cxx_decl(cls):
332 code = "#ifndef __PARAMS__%s\n" % cls
333 code += "#define __PARAMS__%s\n\n" % cls
334
335 # The 'dict' attribute restricts us to the params declared in
336 # the object itself, not including inherited params (which
337 # will also be inherited from the base class's param struct
338 # here).
339 params = cls._params.local.values()
340 try:
341 ptypes = [p.ptype for p in params]
342 except:
343 print cls, p, p.ptype_str
344 print params
345 raise
346
347 # get a list of lists of predeclaration lines
348 predecls = []
349 predecls.extend(cls.cxx_predecls)
350 for p in params:
351 predecls.extend(p.cxx_predecls())
352 # remove redundant lines
353 predecls2 = []
354 for pd in predecls:
355 if pd not in predecls2:
356 predecls2.append(pd)
357 predecls2.sort()
358 code += "\n".join(predecls2)
359 code += "\n\n";
360
361 if cls._base:
362 code += '#include "params/%s.hh"\n\n' % cls._base.type
363
364 for ptype in ptypes:
365 if issubclass(ptype, Enum):
366 code += '#include "enums/%s.hh"\n' % ptype.__name__
367 code += "\n\n"
368
369 code += cls.cxx_struct(cls._base, params)
370
371 # close #ifndef __PARAMS__* guard
372 code += "\n#endif\n"
373 return code
374
375 def cxx_struct(cls, base, params):
376 if cls == SimObject:
377 return '#include "sim/sim_object_params.hh"\n'
378
379 # now generate the actual param struct
380 code = "struct %sParams" % cls
381 if base:
382 code += " : public %sParams" % base.type
383 code += "\n{\n"
384 if not hasattr(cls, 'abstract') or not cls.abstract:
385 if 'type' in cls.__dict__:
386 code += " %s create();\n" % cls.cxx_type
387 decls = [p.cxx_decl() for p in params]
388 decls.sort()
389 code += "".join([" %s\n" % d for d in decls])
390 code += "};\n"
391
392 return code
393
394 def swig_decl(cls):
395 code = '%%module %s\n' % cls
396
397 code += '%{\n'
398 code += '#include "params/%s.hh"\n' % cls
399 code += '%}\n\n'
400
401 # The 'dict' attribute restricts us to the params declared in
402 # the object itself, not including inherited params (which
403 # will also be inherited from the base class's param struct
404 # here).
405 params = cls._params.local.values()
406 ptypes = [p.ptype for p in params]
407
408 # get a list of lists of predeclaration lines
409 predecls = []
410 predecls.extend([ p.swig_predecls() for p in params ])
411 # flatten
412 predecls = reduce(lambda x,y:x+y, predecls, [])
413 # remove redundant lines
414 predecls2 = []
415 for pd in predecls:
416 if pd not in predecls2:
417 predecls2.append(pd)
418 predecls2.sort()
419 code += "\n".join(predecls2)
420 code += "\n\n";
421
422 if cls._base:
423 code += '%%import "params/%s.i"\n\n' % cls._base.type
424
425 for ptype in ptypes:
426 if issubclass(ptype, Enum):
427 code += '%%import "enums/%s.hh"\n' % ptype.__name__
428 code += "\n\n"
429
430 code += '%%import "params/%s_type.hh"\n\n' % cls
431 code += '%%include "params/%s.hh"\n\n' % cls
432
433 return code
434
435# The SimObject class is the root of the special hierarchy. Most of
436# the code in this class deals with the configuration hierarchy itself
437# (parent/child node relationships).
438class SimObject(object):
439 # Specify metaclass. Any class inheriting from SimObject will
440 # get this metaclass.
441 __metaclass__ = MetaSimObject
442 type = 'SimObject'
443 abstract = True
444
445 swig_objdecls = [ '%include "python/swig/sim_object.i"' ]
446
447 # Initialize new instance. For objects with SimObject-valued
448 # children, we need to recursively clone the classes represented
449 # by those param values as well in a consistent "deep copy"-style
450 # fashion. That is, we want to make sure that each instance is
451 # cloned only once, and that if there are multiple references to
452 # the same original object, we end up with the corresponding
453 # cloned references all pointing to the same cloned instance.

--- 482 unchanged lines hidden ---