1 # Copyright (c) 2012 ARM Limited
4 # The license below extends only to copyright in the software and shall
5 # not be construed as granting a license to any other intellectual
6 # property including but not limited to intellectual property relating
7 # to a hardware implementation of the functionality of the software
8 # licensed hereunder. You may use the software subject to the license
9 # terms below provided that you ensure that this notice is replicated
10 # unmodified and in its entirety in all distributions of the software,
11 # modified or unmodified, in source code or in binary form.
13 # Copyright (c) 2004-2006 The Regents of The University of Michigan
14 # Copyright (c) 2010 Advanced Micro Devices, Inc.
15 # All rights reserved.
17 # Redistribution and use in source and binary forms, with or without
18 # modification, are permitted provided that the following conditions are
19 # met: redistributions of source code must retain the above copyright
20 # notice, this list of conditions and the following disclaimer;
21 # redistributions in binary form must reproduce the above copyright
22 # notice, this list of conditions and the following disclaimer in the
23 # documentation and/or other materials provided with the distribution;
24 # neither the name of the copyright holders nor the names of its
25 # contributors may be used to endorse or promote products derived from
26 # this software without specific prior written permission.
28 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 # Authors: Steve Reinhardt
45 from types
import FunctionType
, MethodType
, ModuleType
50 # Have to import params up top since Param is referenced on initial
51 # load (when SimObject class references Param to create a class
52 # variable, the 'name' param)...
53 from m5
.params
import *
54 # There are a few things we need that aren't in params.__all__ since
55 # normal users don't need them
56 from m5
.params
import ParamDesc
, VectorParamDesc
, \
57 isNullPointer
, SimObjectVector
, Port
59 from m5
.proxy
import *
60 from m5
.proxy
import isproxy
62 #####################################################################
64 # M5 Python Configuration Utility
66 # The basic idea is to write simple Python programs that build Python
67 # objects corresponding to M5 SimObjects for the desired simulation
68 # configuration. For now, the Python emits a .ini file that can be
69 # parsed by M5. In the future, some tighter integration between M5
70 # and the Python interpreter may allow bypassing the .ini file.
72 # Each SimObject class in M5 is represented by a Python class with the
73 # same name. The Python inheritance tree mirrors the M5 C++ tree
74 # (e.g., SimpleCPU derives from BaseCPU in both cases, and all
75 # SimObjects inherit from a single SimObject base class). To specify
76 # an instance of an M5 SimObject in a configuration, the user simply
77 # instantiates the corresponding Python object. The parameters for
78 # that SimObject are given by assigning to attributes of the Python
79 # object, either using keyword assignment in the constructor or in
80 # separate assignment statements. For example:
82 # cache = BaseCache(size='64KB')
83 # cache.hit_latency = 3
86 # The magic lies in the mapping of the Python attributes for SimObject
87 # classes to the actual SimObject parameter specifications. This
88 # allows parameter validity checking in the Python code. Continuing
89 # the example above, the statements "cache.blurfl=3" or
90 # "cache.assoc='hello'" would both result in runtime errors in Python,
91 # since the BaseCache object has no 'blurfl' parameter and the 'assoc'
92 # parameter requires an integer, respectively. This magic is done
93 # primarily by overriding the special __setattr__ method that controls
94 # assignment to object attributes.
96 # Once a set of Python objects have been instantiated in a hierarchy,
97 # calling 'instantiate(obj)' (where obj is the root of the hierarchy)
98 # will generate a .ini file.
100 #####################################################################
102 # list of all SimObject classes
105 # dict to look up SimObjects based on path
108 # Did any of the SimObjects lack a header file?
111 def public_value(key
, value
):
112 return key
.startswith('_') or \
113 isinstance(value
, (FunctionType
, MethodType
, ModuleType
,
116 # The metaclass for SimObject. This class controls how new classes
117 # that derive from SimObject are instantiated, and provides inherited
118 # class behavior (just like a class controls how instances of that
119 # class are instantiated, and provides inherited instance behavior).
120 class MetaSimObject(type):
121 # Attributes that can be set only at initialization time
122 init_keywords
= { 'abstract' : bool,
128 # Attributes that can be set any time
129 keywords
= { 'check' : FunctionType
}
131 # __new__ is called before __init__, and is where the statements
132 # in the body of the class definition get loaded into the class's
133 # __dict__. We intercept this to filter out parameter & port assignments
134 # and only allow "private" attributes to be passed to the base
135 # __new__ (starting with underscore).
136 def __new__(mcls
, name
, bases
, dict):
137 assert name
not in allClasses
, "SimObject %s already present" % name
139 # Copy "private" attributes, functions, and classes to the
140 # official dict. Everything else goes in _init_dict to be
141 # filtered in __init__.
144 for key
,val
in dict.items():
145 if public_value(key
, val
):
148 # must be a param/port setting
149 value_dict
[key
] = val
150 if 'abstract' not in value_dict
:
151 value_dict
['abstract'] = False
152 if 'cxx_bases' not in value_dict
:
153 value_dict
['cxx_bases'] = []
154 cls_dict
['_value_dict'] = value_dict
155 cls
= super(MetaSimObject
, mcls
).__new
__(mcls
, name
, bases
, cls_dict
)
156 if 'type' in value_dict
:
157 allClasses
[name
] = cls
160 # subclass initialization
161 def __init__(cls
, name
, bases
, dict):
162 # calls type.__init__()... I think that's a no-op, but leave
163 # it here just in case it's not.
164 super(MetaSimObject
, cls
).__init
__(name
, bases
, dict)
166 # initialize required attributes
168 # class-only attributes
169 cls
._params
= multidict() # param descriptions
170 cls
._ports
= multidict() # port descriptions
172 # class or instance attributes
173 cls
._values
= multidict() # param values
174 cls
._children
= multidict() # SimObject children
175 cls
._port
_refs
= multidict() # port ref objects
176 cls
._instantiated
= False # really instantiated, cloned, or subclassed
178 # We don't support multiple inheritance of sim objects. If you want
179 # to, you must fix multidict to deal with it properly. Non sim-objects
183 if isinstance(c
, MetaSimObject
):
186 raise TypeError, "SimObjects do not support multiple inheritance"
190 # Set up general inheritance via multidicts. A subclass will
191 # inherit all its settings from the base class. The only time
192 # the following is not true is when we define the SimObject
193 # class itself (in which case the multidicts have no parent).
194 if isinstance(base
, MetaSimObject
):
196 cls
._params
.parent
= base
._params
197 cls
._ports
.parent
= base
._ports
198 cls
._values
.parent
= base
._values
199 cls
._children
.parent
= base
._children
200 cls
._port
_refs
.parent
= base
._port
_refs
201 # mark base as having been subclassed
202 base
._instantiated
= True
206 # default keyword values
207 if 'type' in cls
._value
_dict
:
208 if 'cxx_class' not in cls
._value
_dict
:
209 cls
._value
_dict
['cxx_class'] = cls
._value
_dict
['type']
211 cls
._value
_dict
['cxx_type'] = '%s *' % cls
._value
_dict
['cxx_class']
213 if 'cxx_header' not in cls
._value
_dict
:
216 print >> sys
.stderr
, \
217 "warning: No header file specified for SimObject: %s" % name
219 # Export methods are automatically inherited via C++, so we
220 # don't want the method declarations to get inherited on the
221 # python side (and thus end up getting repeated in the wrapped
222 # versions of derived classes). The code below basicallly
223 # suppresses inheritance by substituting in the base (null)
224 # versions of these methods unless a different version is
225 # explicitly supplied.
226 for method_name
in ('export_methods', 'export_method_cxx_predecls',
227 'export_method_swig_predecls'):
228 if method_name
not in cls
.__dict
__:
229 base_method
= getattr(MetaSimObject
, method_name
)
230 m
= MethodType(base_method
, cls
, MetaSimObject
)
231 setattr(cls
, method_name
, m
)
233 # Now process the _value_dict items. They could be defining
234 # new (or overriding existing) parameters or ports, setting
235 # class keywords (e.g., 'abstract'), or setting parameter
236 # values or port bindings. The first 3 can only be set when
237 # the class is defined, so we handle them here. The others
238 # can be set later too, so just emulate that by calling
240 for key
,val
in cls
._value
_dict
.items():
242 if isinstance(val
, ParamDesc
):
243 cls
._new
_param
(key
, val
)
246 elif isinstance(val
, Port
):
247 cls
._new
_port
(key
, val
)
249 # init-time-only keywords
250 elif cls
.init_keywords
.has_key(key
):
251 cls
._set
_keyword
(key
, val
, cls
.init_keywords
[key
])
253 # default: use normal path (ends up in __setattr__)
255 setattr(cls
, key
, val
)
257 def _set_keyword(cls
, keyword
, val
, kwtype
):
258 if not isinstance(val
, kwtype
):
259 raise TypeError, 'keyword %s has bad type %s (expecting %s)' % \
260 (keyword
, type(val
), kwtype
)
261 if isinstance(val
, FunctionType
):
262 val
= classmethod(val
)
263 type.__setattr
__(cls
, keyword
, val
)
265 def _new_param(cls
, name
, pdesc
):
266 # each param desc should be uniquely assigned to one variable
267 assert(not hasattr(pdesc
, 'name'))
269 cls
._params
[name
] = pdesc
270 if hasattr(pdesc
, 'default'):
271 cls
._set
_param
(name
, pdesc
.default
, pdesc
)
273 def _set_param(cls
, name
, value
, param
):
274 assert(param
.name
== name
)
276 value
= param
.convert(value
)
278 msg
= "%s\nError setting param %s.%s to %s\n" % \
279 (e
, cls
.__name
__, name
, value
)
282 cls
._values
[name
] = value
283 # if param value is a SimObject, make it a child too, so that
284 # it gets cloned properly when the class is instantiated
285 if isSimObjectOrVector(value
) and not value
.has_parent():
286 cls
._add
_cls
_child
(name
, value
)
288 def _add_cls_child(cls
, name
, child
):
289 # It's a little funky to have a class as a parent, but these
290 # objects should never be instantiated (only cloned, which
291 # clears the parent pointer), and this makes it clear that the
292 # object is not an orphan and can provide better error
294 child
.set_parent(cls
, name
)
295 cls
._children
[name
] = child
297 def _new_port(cls
, name
, port
):
298 # each port should be uniquely assigned to one variable
299 assert(not hasattr(port
, 'name'))
301 cls
._ports
[name
] = port
303 # same as _get_port_ref, effectively, but for classes
304 def _cls_get_port_ref(cls
, attr
):
305 # Return reference that can be assigned to another port
306 # via __setattr__. There is only ever one reference
307 # object per port, but we create them lazily here.
308 ref
= cls
._port
_refs
.get(attr
)
310 ref
= cls
._ports
[attr
].makeRef(cls
)
311 cls
._port
_refs
[attr
] = ref
314 # Set attribute (called on foo.attr = value when foo is an
315 # instance of class cls).
316 def __setattr__(cls
, attr
, value
):
317 # normal processing for private attributes
318 if public_value(attr
, value
):
319 type.__setattr
__(cls
, attr
, value
)
322 if cls
.keywords
.has_key(attr
):
323 cls
._set
_keyword
(attr
, value
, cls
.keywords
[attr
])
326 if cls
._ports
.has_key(attr
):
327 cls
._cls
_get
_port
_ref
(attr
).connect(value
)
330 if isSimObjectOrSequence(value
) and cls
._instantiated
:
331 raise RuntimeError, \
332 "cannot set SimObject parameter '%s' after\n" \
333 " class %s has been instantiated or subclassed" \
334 % (attr
, cls
.__name
__)
337 param
= cls
._params
.get(attr
)
339 cls
._set
_param
(attr
, value
, param
)
342 if isSimObjectOrSequence(value
):
343 # If RHS is a SimObject, it's an implicit child assignment.
344 cls
._add
_cls
_child
(attr
, coerceSimObjectOrVector(value
))
347 # no valid assignment... raise exception
348 raise AttributeError, \
349 "Class %s has no parameter \'%s\'" % (cls
.__name
__, attr
)
351 def __getattr__(cls
, attr
):
352 if attr
== 'cxx_class_path':
353 return cls
.cxx_class
.split('::')
355 if attr
== 'cxx_class_name':
356 return cls
.cxx_class_path
[-1]
358 if attr
== 'cxx_namespaces':
359 return cls
.cxx_class_path
[:-1]
361 if cls
._values
.has_key(attr
):
362 return cls
._values
[attr
]
364 if cls
._children
.has_key(attr
):
365 return cls
._children
[attr
]
367 raise AttributeError, \
368 "object '%s' has no attribute '%s'" % (cls
.__name
__, attr
)
373 # See ParamValue.cxx_predecls for description.
374 def cxx_predecls(cls
, code
):
375 code('#include "params/$cls.hh"')
377 # See ParamValue.swig_predecls for description.
378 def swig_predecls(cls
, code
):
379 code('%import "python/m5/internal/param_$cls.i"')
381 # Hook for exporting additional C++ methods to Python via SWIG.
382 # Default is none, override using @classmethod in class definition.
383 def export_methods(cls
, code
):
386 # Generate the code needed as a prerequisite for the C++ methods
387 # exported via export_methods() to be compiled in the _wrap.cc
388 # file. Typically generates one or more #include statements. If
389 # any methods are exported, typically at least the C++ header
390 # declaring the relevant SimObject class must be included.
391 def export_method_cxx_predecls(cls
, code
):
394 # Generate the code needed as a prerequisite for the C++ methods
395 # exported via export_methods() to be processed by SWIG.
396 # Typically generates one or more %include or %import statements.
397 # If any methods are exported, typically at least the C++ header
398 # declaring the relevant SimObject class must be included.
399 def export_method_swig_predecls(cls
, code
):
402 # Generate the declaration for this object for wrapping with SWIG.
403 # Generates code that goes into a SWIG .i file. Called from
405 def swig_decl(cls
, code
):
406 class_path
= cls
.cxx_class
.split('::')
407 classname
= class_path
[-1]
408 namespaces
= class_path
[:-1]
410 # The 'local' attribute restricts us to the params declared in
411 # the object itself, not including inherited params (which
412 # will also be inherited from the base class's param struct
414 params
= cls
._params
.local
.values()
415 ports
= cls
._ports
.local
417 code('%module(package="m5.internal") param_$cls')
420 code('#include "sim/sim_object.hh"')
421 code('#include "params/$cls.hh"')
423 param
.cxx_predecls(code
)
424 code('#include "${{cls.cxx_header}}"')
425 cls
.export_method_cxx_predecls(code
)
428 * This is a workaround for bug in swig. Prior to gcc 4.6.1 the STL
429 * headers like vector, string, etc. used to automatically pull in
430 * the cstddef header but starting with gcc 4.6.1 they no longer do.
431 * This leads to swig generated a file that does not compile so we
432 * explicitly include cstddef. Additionally, including version 2.0.4,
433 * swig uses ptrdiff_t without the std:: namespace prefix which is
434 * required with gcc 4.6.1. We explicitly provide access to it.
437 using std::ptrdiff_t;
443 param
.swig_predecls(code
)
444 cls
.export_method_swig_predecls(code
)
448 code('%import "python/m5/internal/param_${{cls._base}}.i"')
451 for ns
in namespaces
:
452 code('namespace $ns {')
455 code('// avoid name conflicts')
456 sep_string
= '_COLONS_'
457 flat_name
= sep_string
.join(class_path
)
458 code('%rename($flat_name) $classname;')
461 code('// stop swig from creating/wrapping default ctor/dtor')
462 code('%nodefault $classname;')
463 code('class $classname')
465 bases
= [ cls
._base
.cxx_class
] + cls
.cxx_bases
467 bases
= cls
.cxx_bases
471 code(' : public ${{base}}')
474 code(' , public ${{base}}')
478 cls
.export_methods(code
)
481 for ns
in reversed(namespaces
):
482 code('} // namespace $ns')
485 code('%include "params/$cls.hh"')
488 # Generate the C++ declaration (.hh file) for this SimObject's
489 # param struct. Called from src/SConscript.
490 def cxx_param_decl(cls
, code
):
491 # The 'local' attribute restricts us to the params declared in
492 # the object itself, not including inherited params (which
493 # will also be inherited from the base class's param struct
495 params
= cls
._params
.local
.values()
496 ports
= cls
._ports
.local
498 ptypes
= [p
.ptype
for p
in params
]
500 print cls
, p
, p
.ptype_str
504 class_path
= cls
._value
_dict
['cxx_class'].split('::')
507 #ifndef __PARAMS__${cls}__
508 #define __PARAMS__${cls}__
512 # A forward class declaration is sufficient since we are just
513 # declaring a pointer.
514 for ns
in class_path
[:-1]:
515 code('namespace $ns {')
516 code('class $0;', class_path
[-1])
517 for ns
in reversed(class_path
[:-1]):
518 code('} // namespace $ns')
521 # The base SimObject has a couple of params that get
522 # automatically set from Python without being declared through
523 # the normal Param mechanism; we slip them in here (needed
524 # predecls now, actual declarations below)
536 param
.cxx_predecls(code
)
537 for port
in ports
.itervalues():
538 port
.cxx_predecls(code
)
542 code('#include "params/${{cls._base.type}}.hh"')
546 if issubclass(ptype
, Enum
):
547 code('#include "enums/${{ptype.__name__}}.hh"')
550 # now generate the actual param struct
551 code("struct ${cls}Params")
553 code(" : public ${{cls._base.type}}Params")
555 if not hasattr(cls
, 'abstract') or not cls
.abstract
:
556 if 'type' in cls
.__dict
__:
557 code(" ${{cls.cxx_type}} create();")
564 extern EventQueue mainEventQueue;
565 eventq = &mainEventQueue;
567 virtual ~SimObjectParams() {}
575 for port
in ports
.itervalues():
582 code('#endif // __PARAMS__${cls}__')
587 # The SimObject class is the root of the special hierarchy. Most of
588 # the code in this class deals with the configuration hierarchy itself
589 # (parent/child node relationships).
590 class SimObject(object):
591 # Specify metaclass. Any class inheriting from SimObject will
592 # get this metaclass.
593 __metaclass__
= MetaSimObject
596 cxx_header
= "sim/sim_object.hh"
598 cxx_bases
= [ "Drainable", "Serializable" ]
601 def export_method_swig_predecls(cls
, code
):
603 %include <std_string.i>
605 %import "python/swig/drain.i"
606 %import "python/swig/serialize.i"
610 def export_methods(cls
, code
):
613 void loadState(Checkpoint *cp);
620 # Initialize new instance. For objects with SimObject-valued
621 # children, we need to recursively clone the classes represented
622 # by those param values as well in a consistent "deep copy"-style
623 # fashion. That is, we want to make sure that each instance is
624 # cloned only once, and that if there are multiple references to
625 # the same original object, we end up with the corresponding
626 # cloned references all pointing to the same cloned instance.
627 def __init__(self
, **kwargs
):
628 ancestor
= kwargs
.get('_ancestor')
629 memo_dict
= kwargs
.get('_memo')
630 if memo_dict
is None:
631 # prepare to memoize any recursively instantiated objects
634 # memoize me now to avoid problems with recursive calls
635 memo_dict
[ancestor
] = self
638 ancestor
= self
.__class
__
639 ancestor
._instantiated
= True
641 # initialize required attributes
644 self
._ccObject
= None # pointer to C++ object
645 self
._ccParams
= None
646 self
._instantiated
= False # really "cloned"
648 # Clone children specified at class level. No need for a
649 # multidict here since we will be cloning everything.
650 # Do children before parameter values so that children that
651 # are also param values get cloned properly.
653 for key
,val
in ancestor
._children
.iteritems():
654 self
.add_child(key
, val(_memo
=memo_dict
))
656 # Inherit parameter values from class using multidict so
657 # individual value settings can be overridden but we still
658 # inherit late changes to non-overridden class values.
659 self
._values
= multidict(ancestor
._values
)
660 # clone SimObject-valued parameters
661 for key
,val
in ancestor
._values
.iteritems():
662 val
= tryAsSimObjectOrVector(val
)
664 self
._values
[key
] = val(_memo
=memo_dict
)
666 # clone port references. no need to use a multidict here
667 # since we will be creating new references for all ports.
669 for key
,val
in ancestor
._port
_refs
.iteritems():
670 self
._port
_refs
[key
] = val
.clone(self
, memo_dict
)
671 # apply attribute assignments from keyword args, if any
672 for key
,val
in kwargs
.iteritems():
673 setattr(self
, key
, val
)
675 # "Clone" the current instance by creating another instance of
676 # this instance's class, but that inherits its parameter values
677 # and port mappings from the current instance. If we're in a
678 # "deep copy" recursive clone, check the _memo dict to see if
679 # we've already cloned this instance.
680 def __call__(self
, **kwargs
):
681 memo_dict
= kwargs
.get('_memo')
682 if memo_dict
is None:
683 # no memo_dict: must be top-level clone operation.
684 # this is only allowed at the root of a hierarchy
686 raise RuntimeError, "attempt to clone object %s " \
687 "not at the root of a tree (parent = %s)" \
688 % (self
, self
._parent
)
689 # create a new dict and use that.
691 kwargs
['_memo'] = memo_dict
692 elif memo_dict
.has_key(self
):
693 # clone already done & memoized
694 return memo_dict
[self
]
695 return self
.__class
__(_ancestor
= self
, **kwargs
)
697 def _get_port_ref(self
, attr
):
698 # Return reference that can be assigned to another port
699 # via __setattr__. There is only ever one reference
700 # object per port, but we create them lazily here.
701 ref
= self
._port
_refs
.get(attr
)
703 ref
= self
._ports
[attr
].makeRef(self
)
704 self
._port
_refs
[attr
] = ref
707 def __getattr__(self
, attr
):
708 if self
._ports
.has_key(attr
):
709 return self
._get
_port
_ref
(attr
)
711 if self
._values
.has_key(attr
):
712 return self
._values
[attr
]
714 if self
._children
.has_key(attr
):
715 return self
._children
[attr
]
717 # If the attribute exists on the C++ object, transparently
718 # forward the reference there. This is typically used for
719 # SWIG-wrapped methods such as init(), regStats(),
720 # resetStats(), startup(), drain(), and
722 if self
._ccObject
and hasattr(self
._ccObject
, attr
):
723 return getattr(self
._ccObject
, attr
)
725 raise AttributeError, "object '%s' has no attribute '%s'" \
726 % (self
.__class
__.__name
__, attr
)
728 # Set attribute (called on foo.attr = value when foo is an
729 # instance of class cls).
730 def __setattr__(self
, attr
, value
):
731 # normal processing for private attributes
732 if attr
.startswith('_'):
733 object.__setattr
__(self
, attr
, value
)
736 if self
._ports
.has_key(attr
):
737 # set up port connection
738 self
._get
_port
_ref
(attr
).connect(value
)
741 if isSimObjectOrSequence(value
) and self
._instantiated
:
742 raise RuntimeError, \
743 "cannot set SimObject parameter '%s' after\n" \
744 " instance been cloned %s" % (attr
, `self`
)
746 param
= self
._params
.get(attr
)
749 value
= param
.convert(value
)
751 msg
= "%s\nError setting param %s.%s to %s\n" % \
752 (e
, self
.__class
__.__name
__, attr
, value
)
755 self
._values
[attr
] = value
756 # implicitly parent unparented objects assigned as params
757 if isSimObjectOrVector(value
) and not value
.has_parent():
758 self
.add_child(attr
, value
)
761 # if RHS is a SimObject, it's an implicit child assignment
762 if isSimObjectOrSequence(value
):
763 self
.add_child(attr
, value
)
766 # no valid assignment... raise exception
767 raise AttributeError, "Class %s has no parameter %s" \
768 % (self
.__class
__.__name
__, attr
)
771 # this hack allows tacking a '[0]' onto parameters that may or may
772 # not be vectors, and always getting the first element (e.g. cpus)
773 def __getitem__(self
, key
):
776 raise TypeError, "Non-zero index '%s' to SimObject" % key
778 # Also implemented by SimObjectVector
779 def clear_parent(self
, old_parent
):
780 assert self
._parent
is old_parent
783 # Also implemented by SimObjectVector
784 def set_parent(self
, parent
, name
):
785 self
._parent
= parent
788 # Also implemented by SimObjectVector
792 # Also implemented by SimObjectVector
793 def has_parent(self
):
794 return self
._parent
is not None
796 # clear out child with given name. This code is not likely to be exercised.
797 # See comment in add_child.
798 def clear_child(self
, name
):
799 child
= self
._children
[name
]
800 child
.clear_parent(self
)
801 del self
._children
[name
]
803 # Add a new child to this object.
804 def add_child(self
, name
, child
):
805 child
= coerceSimObjectOrVector(child
)
806 if child
.has_parent():
807 print "warning: add_child('%s'): child '%s' already has parent" % \
808 (name
, child
.get_name())
809 if self
._children
.has_key(name
):
810 # This code path had an undiscovered bug that would make it fail
811 # at runtime. It had been here for a long time and was only
812 # exposed by a buggy script. Changes here will probably not be
813 # exercised without specialized testing.
814 self
.clear_child(name
)
815 child
.set_parent(self
, name
)
816 self
._children
[name
] = child
818 # Take SimObject-valued parameters that haven't been explicitly
819 # assigned as children and make them children of the object that
820 # they were assigned to as a parameter value. This guarantees
821 # that when we instantiate all the parameter objects we're still
822 # inside the configuration hierarchy.
823 def adoptOrphanParams(self
):
824 for key
,val
in self
._values
.iteritems():
825 if not isSimObjectVector(val
) and isSimObjectSequence(val
):
826 # need to convert raw SimObject sequences to
827 # SimObjectVector class so we can call has_parent()
828 val
= SimObjectVector(val
)
829 self
._values
[key
] = val
830 if isSimObjectOrVector(val
) and not val
.has_parent():
831 print "warning: %s adopting orphan SimObject param '%s'" \
833 self
.add_child(key
, val
)
837 return '<orphan %s>' % self
.__class
__
838 ppath
= self
._parent
.path()
841 return ppath
+ "." + self
._name
849 def find_any(self
, ptype
):
850 if isinstance(self
, ptype
):
854 for child
in self
._children
.itervalues():
855 if isinstance(child
, ptype
):
856 if found_obj
!= None and child
!= found_obj
:
857 raise AttributeError, \
858 'parent.any matched more than one: %s %s' % \
859 (found_obj
.path
, child
.path
)
862 for pname
,pdesc
in self
._params
.iteritems():
863 if issubclass(pdesc
.ptype
, ptype
):
864 match_obj
= self
._values
[pname
]
865 if found_obj
!= None and found_obj
!= match_obj
:
866 raise AttributeError, \
867 'parent.any matched more than one: %s and %s' % (found_obj
.path
, match_obj
.path
)
868 found_obj
= match_obj
869 return found_obj
, found_obj
!= None
871 def find_all(self
, ptype
):
874 for child
in self
._children
.itervalues():
875 # a child could be a list, so ensure we visit each item
876 if isinstance(child
, list):
881 for child
in children
:
882 if isinstance(child
, ptype
) and not isproxy(child
) and \
883 not isNullPointer(child
):
885 if isSimObject(child
):
886 # also add results from the child itself
887 child_all
, done
= child
.find_all(ptype
)
888 all
.update(dict(zip(child_all
, [done
] * len(child_all
))))
890 for pname
,pdesc
in self
._params
.iteritems():
891 if issubclass(pdesc
.ptype
, ptype
):
892 match_obj
= self
._values
[pname
]
893 if not isproxy(match_obj
) and not isNullPointer(match_obj
):
894 all
[match_obj
] = True
895 return all
.keys(), True
897 def unproxy(self
, base
):
900 def unproxyParams(self
):
901 for param
in self
._params
.iterkeys():
902 value
= self
._values
.get(param
)
903 if value
!= None and isproxy(value
):
905 value
= value
.unproxy(self
)
907 print "Error in unproxying param '%s' of %s" % \
910 setattr(self
, param
, value
)
912 # Unproxy ports in sorted order so that 'append' operations on
913 # vector ports are done in a deterministic fashion.
914 port_names
= self
._ports
.keys()
916 for port_name
in port_names
:
917 port
= self
._port
_refs
.get(port_name
)
921 def print_ini(self
, ini_file
):
922 print >>ini_file
, '[' + self
.path() + ']' # .ini section header
924 instanceDict
[self
.path()] = self
926 if hasattr(self
, 'type'):
927 print >>ini_file
, 'type=%s' % self
.type
929 if len(self
._children
.keys()):
930 print >>ini_file
, 'children=%s' % \
931 ' '.join(self
._children
[n
].get_name() \
932 for n
in sorted(self
._children
.keys()))
934 for param
in sorted(self
._params
.keys()):
935 value
= self
._values
.get(param
)
937 print >>ini_file
, '%s=%s' % (param
,
938 self
._values
[param
].ini_str())
940 for port_name
in sorted(self
._ports
.keys()):
941 port
= self
._port
_refs
.get(port_name
, None)
943 print >>ini_file
, '%s=%s' % (port_name
, port
.ini_str())
945 print >>ini_file
# blank line between objects
947 # generate a tree of dictionaries expressing all the parameters in the
948 # instantiated system for use by scripts that want to do power, thermal
949 # visualization, and other similar tasks
950 def get_config_as_dict(self
):
952 if hasattr(self
, 'type'):
954 if hasattr(self
, 'cxx_class'):
955 d
.cxx_class
= self
.cxx_class
956 # Add the name and path of this object to be able to link to
958 d
.name
= self
.get_name()
961 for param
in sorted(self
._params
.keys()):
962 value
= self
._values
.get(param
)
965 # Use native type for those supported by JSON and
966 # strings for everything else. skipkeys=True seems
967 # to not work as well as one would hope
968 if type(self
._values
[param
].value
) in \
969 [str, unicode, int, long, float, bool, None]:
970 d
[param
] = self
._values
[param
].value
972 d
[param
] = str(self
._values
[param
])
974 except AttributeError:
977 for n
in sorted(self
._children
.keys()):
978 child
= self
._children
[n
]
979 # Use the name of the attribute (and not get_name()) as
980 # the key in the JSON dictionary to capture the hierarchy
981 # in the Python code that assembled this system
982 d
[n
] = child
.get_config_as_dict()
984 for port_name
in sorted(self
._ports
.keys()):
985 port
= self
._port
_refs
.get(port_name
, None)
987 # Represent each port with a dictionary containing the
988 # prominent attributes
989 d
[port_name
] = port
.get_config_as_dict()
993 def getCCParams(self
):
995 return self
._ccParams
997 cc_params_struct
= getattr(m5
.internal
.params
, '%sParams' % self
.type)
998 cc_params
= cc_params_struct()
999 cc_params
.pyobj
= self
1000 cc_params
.name
= str(self
)
1002 param_names
= self
._params
.keys()
1004 for param
in param_names
:
1005 value
= self
._values
.get(param
)
1007 fatal("%s.%s without default or user set value",
1010 value
= value
.getValue()
1011 if isinstance(self
._params
[param
], VectorParamDesc
):
1012 assert isinstance(value
, list)
1013 vec
= getattr(cc_params
, param
)
1018 setattr(cc_params
, param
, value
)
1020 port_names
= self
._ports
.keys()
1022 for port_name
in port_names
:
1023 port
= self
._port
_refs
.get(port_name
, None)
1025 port_count
= len(port
)
1028 setattr(cc_params
, 'port_' + port_name
+ '_connection_count',
1030 self
._ccParams
= cc_params
1031 return self
._ccParams
1033 # Get C++ object corresponding to this object, calling C++ if
1034 # necessary to construct it. Does *not* recursively create
1036 def getCCObject(self
):
1037 if not self
._ccObject
:
1038 # Make sure this object is in the configuration hierarchy
1039 if not self
._parent
and not isRoot(self
):
1040 raise RuntimeError, "Attempt to instantiate orphan node"
1041 # Cycles in the configuration hierarchy are not supported. This
1042 # will catch the resulting recursion and stop.
1044 params
= self
.getCCParams()
1045 self
._ccObject
= params
.create()
1046 elif self
._ccObject
== -1:
1047 raise RuntimeError, "%s: Cycle found in configuration hierarchy." \
1049 return self
._ccObject
1051 def descendants(self
):
1053 for child
in self
._children
.itervalues():
1054 for obj
in child
.descendants():
1057 # Call C++ to create C++ object corresponding to this object
1058 def createCCObject(self
):
1060 self
.getCCObject() # force creation
1063 return self
.getCCObject()
1065 # Create C++ port connections corresponding to the connections in
1067 def connectPorts(self
):
1068 for portRef
in self
._port
_refs
.itervalues():
1071 # Function to provide to C++ so it can look up instances based on paths
1072 def resolveSimObject(name
):
1073 obj
= instanceDict
[name
]
1074 return obj
.getCCObject()
1076 def isSimObject(value
):
1077 return isinstance(value
, SimObject
)
1079 def isSimObjectClass(value
):
1080 return issubclass(value
, SimObject
)
1082 def isSimObjectVector(value
):
1083 return isinstance(value
, SimObjectVector
)
1085 def isSimObjectSequence(value
):
1086 if not isinstance(value
, (list, tuple)) or len(value
) == 0:
1090 if not isNullPointer(val
) and not isSimObject(val
):
1095 def isSimObjectOrSequence(value
):
1096 return isSimObject(value
) or isSimObjectSequence(value
)
1099 from m5
.objects
import Root
1100 return obj
and obj
is Root
.getInstance()
1102 def isSimObjectOrVector(value
):
1103 return isSimObject(value
) or isSimObjectVector(value
)
1105 def tryAsSimObjectOrVector(value
):
1106 if isSimObjectOrVector(value
):
1108 if isSimObjectSequence(value
):
1109 return SimObjectVector(value
)
1112 def coerceSimObjectOrVector(value
):
1113 value
= tryAsSimObjectOrVector(value
)
1115 raise TypeError, "SimObject or SimObjectVector expected"
1118 baseClasses
= allClasses
.copy()
1119 baseInstances
= instanceDict
.copy()
1122 global allClasses
, instanceDict
, noCxxHeader
1124 allClasses
= baseClasses
.copy()
1125 instanceDict
= baseInstances
.copy()
1128 # __all__ defines the list of symbols that get exported when
1129 # 'from config import *' is invoked. Try to keep this reasonably
1130 # short to avoid polluting other namespaces.
1131 __all__
= [ 'SimObject' ]