SimObject.py (9940:acc015106ac8) SimObject.py (9953:9caba3b84a9b)
1# Copyright (c) 2012 ARM Limited
2# All rights reserved.
3#
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.
12#
13# Copyright (c) 2004-2006 The Regents of The University of Michigan
14# Copyright (c) 2010 Advanced Micro Devices, Inc.
15# All rights reserved.
16#
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.
27#
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.
39#
40# Authors: Steve Reinhardt
41# Nathan Binkert
42# Andreas Hansson
43
44import sys
45from types import FunctionType, MethodType, ModuleType
46
47import m5
48from m5.util import *
49
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)...
53from 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
56from m5.params import ParamDesc, VectorParamDesc, \
57 isNullPointer, SimObjectVector, Port
58
59from m5.proxy import *
60from m5.proxy import isproxy
61
62#####################################################################
63#
64# M5 Python Configuration Utility
65#
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.
71#
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:
81#
82# cache = BaseCache(size='64KB')
83# cache.hit_latency = 3
84# cache.assoc = 8
85#
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.
95#
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.
99#
100#####################################################################
101
102# list of all SimObject classes
103allClasses = {}
104
105# dict to look up SimObjects based on path
106instanceDict = {}
107
108# Did any of the SimObjects lack a header file?
109noCxxHeader = False
110
111def public_value(key, value):
112 return key.startswith('_') or \
113 isinstance(value, (FunctionType, MethodType, ModuleType,
114 classmethod, type))
115
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).
120class MetaSimObject(type):
121 # Attributes that can be set only at initialization time
122 init_keywords = { 'abstract' : bool,
123 'cxx_class' : str,
124 'cxx_type' : str,
125 'cxx_header' : str,
126 'type' : str,
127 'cxx_bases' : list }
128 # Attributes that can be set any time
129 keywords = { 'check' : FunctionType }
130
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
138
139 # Copy "private" attributes, functions, and classes to the
140 # official dict. Everything else goes in _init_dict to be
141 # filtered in __init__.
142 cls_dict = {}
143 value_dict = {}
144 for key,val in dict.items():
145 if public_value(key, val):
146 cls_dict[key] = val
147 else:
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
158 return cls
159
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)
165
166 # initialize required attributes
167
168 # class-only attributes
169 cls._params = multidict() # param descriptions
170 cls._ports = multidict() # port descriptions
171
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
177
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
180 # are ok, though
181 bTotal = 0
182 for c in bases:
183 if isinstance(c, MetaSimObject):
184 bTotal += 1
185 if bTotal > 1:
186 raise TypeError, "SimObjects do not support multiple inheritance"
187
188 base = bases[0]
189
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):
195 cls._base = base
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
203 else:
204 cls._base = None
205
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']
210
211 cls._value_dict['cxx_type'] = '%s *' % cls._value_dict['cxx_class']
212
213 if 'cxx_header' not in cls._value_dict:
214 global noCxxHeader
215 noCxxHeader = True
216 warn("No header file specified for SimObject: %s", name)
217
218 # Export methods are automatically inherited via C++, so we
219 # don't want the method declarations to get inherited on the
220 # python side (and thus end up getting repeated in the wrapped
221 # versions of derived classes). The code below basicallly
222 # suppresses inheritance by substituting in the base (null)
223 # versions of these methods unless a different version is
224 # explicitly supplied.
225 for method_name in ('export_methods', 'export_method_cxx_predecls',
226 'export_method_swig_predecls'):
227 if method_name not in cls.__dict__:
228 base_method = getattr(MetaSimObject, method_name)
229 m = MethodType(base_method, cls, MetaSimObject)
230 setattr(cls, method_name, m)
231
232 # Now process the _value_dict items. They could be defining
233 # new (or overriding existing) parameters or ports, setting
234 # class keywords (e.g., 'abstract'), or setting parameter
235 # values or port bindings. The first 3 can only be set when
236 # the class is defined, so we handle them here. The others
237 # can be set later too, so just emulate that by calling
238 # setattr().
239 for key,val in cls._value_dict.items():
240 # param descriptions
241 if isinstance(val, ParamDesc):
242 cls._new_param(key, val)
243
244 # port objects
245 elif isinstance(val, Port):
246 cls._new_port(key, val)
247
248 # init-time-only keywords
249 elif cls.init_keywords.has_key(key):
250 cls._set_keyword(key, val, cls.init_keywords[key])
251
252 # default: use normal path (ends up in __setattr__)
253 else:
254 setattr(cls, key, val)
255
256 def _set_keyword(cls, keyword, val, kwtype):
257 if not isinstance(val, kwtype):
258 raise TypeError, 'keyword %s has bad type %s (expecting %s)' % \
259 (keyword, type(val), kwtype)
260 if isinstance(val, FunctionType):
261 val = classmethod(val)
262 type.__setattr__(cls, keyword, val)
263
264 def _new_param(cls, name, pdesc):
265 # each param desc should be uniquely assigned to one variable
266 assert(not hasattr(pdesc, 'name'))
267 pdesc.name = name
268 cls._params[name] = pdesc
269 if hasattr(pdesc, 'default'):
270 cls._set_param(name, pdesc.default, pdesc)
271
272 def _set_param(cls, name, value, param):
273 assert(param.name == name)
274 try:
275 value = param.convert(value)
276 except Exception, e:
277 msg = "%s\nError setting param %s.%s to %s\n" % \
278 (e, cls.__name__, name, value)
279 e.args = (msg, )
280 raise
281 cls._values[name] = value
282 # if param value is a SimObject, make it a child too, so that
283 # it gets cloned properly when the class is instantiated
284 if isSimObjectOrVector(value) and not value.has_parent():
285 cls._add_cls_child(name, value)
286
287 def _add_cls_child(cls, name, child):
288 # It's a little funky to have a class as a parent, but these
289 # objects should never be instantiated (only cloned, which
290 # clears the parent pointer), and this makes it clear that the
291 # object is not an orphan and can provide better error
292 # messages.
293 child.set_parent(cls, name)
294 cls._children[name] = child
295
296 def _new_port(cls, name, port):
297 # each port should be uniquely assigned to one variable
298 assert(not hasattr(port, 'name'))
299 port.name = name
300 cls._ports[name] = port
301
302 # same as _get_port_ref, effectively, but for classes
303 def _cls_get_port_ref(cls, attr):
304 # Return reference that can be assigned to another port
305 # via __setattr__. There is only ever one reference
306 # object per port, but we create them lazily here.
307 ref = cls._port_refs.get(attr)
308 if not ref:
309 ref = cls._ports[attr].makeRef(cls)
310 cls._port_refs[attr] = ref
311 return ref
312
313 # Set attribute (called on foo.attr = value when foo is an
314 # instance of class cls).
315 def __setattr__(cls, attr, value):
316 # normal processing for private attributes
317 if public_value(attr, value):
318 type.__setattr__(cls, attr, value)
319 return
320
321 if cls.keywords.has_key(attr):
322 cls._set_keyword(attr, value, cls.keywords[attr])
323 return
324
325 if cls._ports.has_key(attr):
326 cls._cls_get_port_ref(attr).connect(value)
327 return
328
329 if isSimObjectOrSequence(value) and cls._instantiated:
330 raise RuntimeError, \
331 "cannot set SimObject parameter '%s' after\n" \
332 " class %s has been instantiated or subclassed" \
333 % (attr, cls.__name__)
334
335 # check for param
336 param = cls._params.get(attr)
337 if param:
338 cls._set_param(attr, value, param)
339 return
340
341 if isSimObjectOrSequence(value):
342 # If RHS is a SimObject, it's an implicit child assignment.
343 cls._add_cls_child(attr, coerceSimObjectOrVector(value))
344 return
345
346 # no valid assignment... raise exception
347 raise AttributeError, \
348 "Class %s has no parameter \'%s\'" % (cls.__name__, attr)
349
350 def __getattr__(cls, attr):
351 if attr == 'cxx_class_path':
352 return cls.cxx_class.split('::')
353
354 if attr == 'cxx_class_name':
355 return cls.cxx_class_path[-1]
356
357 if attr == 'cxx_namespaces':
358 return cls.cxx_class_path[:-1]
359
360 if cls._values.has_key(attr):
361 return cls._values[attr]
362
363 if cls._children.has_key(attr):
364 return cls._children[attr]
365
366 raise AttributeError, \
367 "object '%s' has no attribute '%s'" % (cls.__name__, attr)
368
369 def __str__(cls):
370 return cls.__name__
371
372 # See ParamValue.cxx_predecls for description.
373 def cxx_predecls(cls, code):
374 code('#include "params/$cls.hh"')
375
376 # See ParamValue.swig_predecls for description.
377 def swig_predecls(cls, code):
378 code('%import "python/m5/internal/param_$cls.i"')
379
380 # Hook for exporting additional C++ methods to Python via SWIG.
381 # Default is none, override using @classmethod in class definition.
382 def export_methods(cls, code):
383 pass
384
385 # Generate the code needed as a prerequisite for the C++ methods
386 # exported via export_methods() to be compiled in the _wrap.cc
387 # file. Typically generates one or more #include statements. If
388 # any methods are exported, typically at least the C++ header
389 # declaring the relevant SimObject class must be included.
390 def export_method_cxx_predecls(cls, code):
391 pass
392
393 # Generate the code needed as a prerequisite for the C++ methods
394 # exported via export_methods() to be processed by SWIG.
395 # Typically generates one or more %include or %import statements.
396 # If any methods are exported, typically at least the C++ header
397 # declaring the relevant SimObject class must be included.
398 def export_method_swig_predecls(cls, code):
399 pass
400
401 # Generate the declaration for this object for wrapping with SWIG.
402 # Generates code that goes into a SWIG .i file. Called from
403 # src/SConscript.
404 def swig_decl(cls, code):
405 class_path = cls.cxx_class.split('::')
406 classname = class_path[-1]
407 namespaces = class_path[:-1]
408
409 # The 'local' attribute restricts us to the params declared in
410 # the object itself, not including inherited params (which
411 # will also be inherited from the base class's param struct
412 # here).
413 params = cls._params.local.values()
414 ports = cls._ports.local
415
416 code('%module(package="m5.internal") param_$cls')
417 code()
418 code('%{')
419 code('#include "sim/sim_object.hh"')
420 code('#include "params/$cls.hh"')
421 for param in params:
422 param.cxx_predecls(code)
423 code('#include "${{cls.cxx_header}}"')
424 cls.export_method_cxx_predecls(code)
425 code('''\
426/**
427 * This is a workaround for bug in swig. Prior to gcc 4.6.1 the STL
428 * headers like vector, string, etc. used to automatically pull in
429 * the cstddef header but starting with gcc 4.6.1 they no longer do.
430 * This leads to swig generated a file that does not compile so we
431 * explicitly include cstddef. Additionally, including version 2.0.4,
432 * swig uses ptrdiff_t without the std:: namespace prefix which is
433 * required with gcc 4.6.1. We explicitly provide access to it.
434 */
435#include <cstddef>
436using std::ptrdiff_t;
437''')
438 code('%}')
439 code()
440
441 for param in params:
442 param.swig_predecls(code)
443 cls.export_method_swig_predecls(code)
444
445 code()
446 if cls._base:
447 code('%import "python/m5/internal/param_${{cls._base}}.i"')
448 code()
449
450 for ns in namespaces:
451 code('namespace $ns {')
452
453 if namespaces:
454 code('// avoid name conflicts')
455 sep_string = '_COLONS_'
456 flat_name = sep_string.join(class_path)
457 code('%rename($flat_name) $classname;')
458
459 code()
460 code('// stop swig from creating/wrapping default ctor/dtor')
461 code('%nodefault $classname;')
462 code('class $classname')
463 if cls._base:
464 bases = [ cls._base.cxx_class ] + cls.cxx_bases
465 else:
466 bases = cls.cxx_bases
467 base_first = True
468 for base in bases:
469 if base_first:
470 code(' : public ${{base}}')
471 base_first = False
472 else:
473 code(' , public ${{base}}')
474
475 code('{')
476 code(' public:')
477 cls.export_methods(code)
478 code('};')
479
480 for ns in reversed(namespaces):
481 code('} // namespace $ns')
482
483 code()
484 code('%include "params/$cls.hh"')
485
486
487 # Generate the C++ declaration (.hh file) for this SimObject's
488 # param struct. Called from src/SConscript.
489 def cxx_param_decl(cls, code):
490 # The 'local' attribute restricts us to the params declared in
491 # the object itself, not including inherited params (which
492 # will also be inherited from the base class's param struct
493 # here).
494 params = cls._params.local.values()
495 ports = cls._ports.local
496 try:
497 ptypes = [p.ptype for p in params]
498 except:
499 print cls, p, p.ptype_str
500 print params
501 raise
502
503 class_path = cls._value_dict['cxx_class'].split('::')
504
505 code('''\
506#ifndef __PARAMS__${cls}__
507#define __PARAMS__${cls}__
508
509''')
510
511 # A forward class declaration is sufficient since we are just
512 # declaring a pointer.
513 for ns in class_path[:-1]:
514 code('namespace $ns {')
515 code('class $0;', class_path[-1])
516 for ns in reversed(class_path[:-1]):
517 code('} // namespace $ns')
518 code()
519
520 # The base SimObject has a couple of params that get
521 # automatically set from Python without being declared through
522 # the normal Param mechanism; we slip them in here (needed
523 # predecls now, actual declarations below)
524 if cls == SimObject:
525 code('''
526#ifndef PY_VERSION
527struct PyObject;
528#endif
529
530#include <string>
531
532class EventQueue;
533''')
534 for param in params:
535 param.cxx_predecls(code)
536 for port in ports.itervalues():
537 port.cxx_predecls(code)
538 code()
539
540 if cls._base:
541 code('#include "params/${{cls._base.type}}.hh"')
542 code()
543
544 for ptype in ptypes:
545 if issubclass(ptype, Enum):
546 code('#include "enums/${{ptype.__name__}}.hh"')
547 code()
548
549 # now generate the actual param struct
550 code("struct ${cls}Params")
551 if cls._base:
552 code(" : public ${{cls._base.type}}Params")
553 code("{")
554 if not hasattr(cls, 'abstract') or not cls.abstract:
555 if 'type' in cls.__dict__:
556 code(" ${{cls.cxx_type}} create();")
557
558 code.indent()
559 if cls == SimObject:
560 code('''
561 SimObjectParams()
562 {
563 extern EventQueue mainEventQueue;
564 eventq = &mainEventQueue;
565 }
566 virtual ~SimObjectParams() {}
567
568 std::string name;
569 PyObject *pyobj;
570 EventQueue *eventq;
571 ''')
572 for param in params:
573 param.cxx_decl(code)
574 for port in ports.itervalues():
575 port.cxx_decl(code)
576
577 code.dedent()
578 code('};')
579
580 code()
581 code('#endif // __PARAMS__${cls}__')
582 return code
583
584
585
586# The SimObject class is the root of the special hierarchy. Most of
587# the code in this class deals with the configuration hierarchy itself
588# (parent/child node relationships).
589class SimObject(object):
590 # Specify metaclass. Any class inheriting from SimObject will
591 # get this metaclass.
592 __metaclass__ = MetaSimObject
593 type = 'SimObject'
594 abstract = True
595 cxx_header = "sim/sim_object.hh"
596
597 cxx_bases = [ "Drainable", "Serializable" ]
598
599 @classmethod
600 def export_method_swig_predecls(cls, code):
601 code('''
602%include <std_string.i>
603
604%import "python/swig/drain.i"
605%import "python/swig/serialize.i"
606''')
607
608 @classmethod
609 def export_methods(cls, code):
610 code('''
611 void init();
612 void loadState(Checkpoint *cp);
613 void initState();
614 void regStats();
615 void resetStats();
616 void startup();
617''')
618
619 # Initialize new instance. For objects with SimObject-valued
620 # children, we need to recursively clone the classes represented
621 # by those param values as well in a consistent "deep copy"-style
622 # fashion. That is, we want to make sure that each instance is
623 # cloned only once, and that if there are multiple references to
624 # the same original object, we end up with the corresponding
625 # cloned references all pointing to the same cloned instance.
626 def __init__(self, **kwargs):
627 ancestor = kwargs.get('_ancestor')
628 memo_dict = kwargs.get('_memo')
629 if memo_dict is None:
630 # prepare to memoize any recursively instantiated objects
631 memo_dict = {}
632 elif ancestor:
633 # memoize me now to avoid problems with recursive calls
634 memo_dict[ancestor] = self
635
636 if not ancestor:
637 ancestor = self.__class__
638 ancestor._instantiated = True
639
640 # initialize required attributes
641 self._parent = None
642 self._name = None
643 self._ccObject = None # pointer to C++ object
644 self._ccParams = None
645 self._instantiated = False # really "cloned"
646
647 # Clone children specified at class level. No need for a
648 # multidict here since we will be cloning everything.
649 # Do children before parameter values so that children that
650 # are also param values get cloned properly.
651 self._children = {}
652 for key,val in ancestor._children.iteritems():
653 self.add_child(key, val(_memo=memo_dict))
654
655 # Inherit parameter values from class using multidict so
656 # individual value settings can be overridden but we still
657 # inherit late changes to non-overridden class values.
658 self._values = multidict(ancestor._values)
659 # clone SimObject-valued parameters
660 for key,val in ancestor._values.iteritems():
661 val = tryAsSimObjectOrVector(val)
662 if val is not None:
663 self._values[key] = val(_memo=memo_dict)
664
665 # clone port references. no need to use a multidict here
666 # since we will be creating new references for all ports.
667 self._port_refs = {}
668 for key,val in ancestor._port_refs.iteritems():
669 self._port_refs[key] = val.clone(self, memo_dict)
670 # apply attribute assignments from keyword args, if any
671 for key,val in kwargs.iteritems():
672 setattr(self, key, val)
673
674 # "Clone" the current instance by creating another instance of
675 # this instance's class, but that inherits its parameter values
676 # and port mappings from the current instance. If we're in a
677 # "deep copy" recursive clone, check the _memo dict to see if
678 # we've already cloned this instance.
679 def __call__(self, **kwargs):
680 memo_dict = kwargs.get('_memo')
681 if memo_dict is None:
682 # no memo_dict: must be top-level clone operation.
683 # this is only allowed at the root of a hierarchy
684 if self._parent:
685 raise RuntimeError, "attempt to clone object %s " \
686 "not at the root of a tree (parent = %s)" \
687 % (self, self._parent)
688 # create a new dict and use that.
689 memo_dict = {}
690 kwargs['_memo'] = memo_dict
691 elif memo_dict.has_key(self):
692 # clone already done & memoized
693 return memo_dict[self]
694 return self.__class__(_ancestor = self, **kwargs)
695
696 def _get_port_ref(self, attr):
697 # Return reference that can be assigned to another port
698 # via __setattr__. There is only ever one reference
699 # object per port, but we create them lazily here.
700 ref = self._port_refs.get(attr)
701 if ref == None:
702 ref = self._ports[attr].makeRef(self)
703 self._port_refs[attr] = ref
704 return ref
705
706 def __getattr__(self, attr):
707 if self._ports.has_key(attr):
708 return self._get_port_ref(attr)
709
710 if self._values.has_key(attr):
711 return self._values[attr]
712
713 if self._children.has_key(attr):
714 return self._children[attr]
715
716 # If the attribute exists on the C++ object, transparently
717 # forward the reference there. This is typically used for
718 # SWIG-wrapped methods such as init(), regStats(),
719 # resetStats(), startup(), drain(), and
720 # resume().
721 if self._ccObject and hasattr(self._ccObject, attr):
722 return getattr(self._ccObject, attr)
723
724 raise AttributeError, "object '%s' has no attribute '%s'" \
725 % (self.__class__.__name__, attr)
726
727 # Set attribute (called on foo.attr = value when foo is an
728 # instance of class cls).
729 def __setattr__(self, attr, value):
730 # normal processing for private attributes
731 if attr.startswith('_'):
732 object.__setattr__(self, attr, value)
733 return
734
735 if self._ports.has_key(attr):
736 # set up port connection
737 self._get_port_ref(attr).connect(value)
738 return
739
740 if isSimObjectOrSequence(value) and self._instantiated:
741 raise RuntimeError, \
742 "cannot set SimObject parameter '%s' after\n" \
743 " instance been cloned %s" % (attr, `self`)
744
745 param = self._params.get(attr)
746 if param:
747 try:
748 value = param.convert(value)
749 except Exception, e:
750 msg = "%s\nError setting param %s.%s to %s\n" % \
751 (e, self.__class__.__name__, attr, value)
752 e.args = (msg, )
753 raise
754 self._values[attr] = value
755 # implicitly parent unparented objects assigned as params
756 if isSimObjectOrVector(value) and not value.has_parent():
757 self.add_child(attr, value)
758 return
759
760 # if RHS is a SimObject, it's an implicit child assignment
761 if isSimObjectOrSequence(value):
762 self.add_child(attr, value)
763 return
764
765 # no valid assignment... raise exception
766 raise AttributeError, "Class %s has no parameter %s" \
767 % (self.__class__.__name__, attr)
768
769
770 # this hack allows tacking a '[0]' onto parameters that may or may
771 # not be vectors, and always getting the first element (e.g. cpus)
772 def __getitem__(self, key):
773 if key == 0:
774 return self
775 raise TypeError, "Non-zero index '%s' to SimObject" % key
776
777 # Also implemented by SimObjectVector
778 def clear_parent(self, old_parent):
779 assert self._parent is old_parent
780 self._parent = None
781
782 # Also implemented by SimObjectVector
783 def set_parent(self, parent, name):
784 self._parent = parent
785 self._name = name
786
1# Copyright (c) 2012 ARM Limited
2# All rights reserved.
3#
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.
12#
13# Copyright (c) 2004-2006 The Regents of The University of Michigan
14# Copyright (c) 2010 Advanced Micro Devices, Inc.
15# All rights reserved.
16#
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.
27#
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.
39#
40# Authors: Steve Reinhardt
41# Nathan Binkert
42# Andreas Hansson
43
44import sys
45from types import FunctionType, MethodType, ModuleType
46
47import m5
48from m5.util import *
49
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)...
53from 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
56from m5.params import ParamDesc, VectorParamDesc, \
57 isNullPointer, SimObjectVector, Port
58
59from m5.proxy import *
60from m5.proxy import isproxy
61
62#####################################################################
63#
64# M5 Python Configuration Utility
65#
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.
71#
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:
81#
82# cache = BaseCache(size='64KB')
83# cache.hit_latency = 3
84# cache.assoc = 8
85#
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.
95#
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.
99#
100#####################################################################
101
102# list of all SimObject classes
103allClasses = {}
104
105# dict to look up SimObjects based on path
106instanceDict = {}
107
108# Did any of the SimObjects lack a header file?
109noCxxHeader = False
110
111def public_value(key, value):
112 return key.startswith('_') or \
113 isinstance(value, (FunctionType, MethodType, ModuleType,
114 classmethod, type))
115
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).
120class MetaSimObject(type):
121 # Attributes that can be set only at initialization time
122 init_keywords = { 'abstract' : bool,
123 'cxx_class' : str,
124 'cxx_type' : str,
125 'cxx_header' : str,
126 'type' : str,
127 'cxx_bases' : list }
128 # Attributes that can be set any time
129 keywords = { 'check' : FunctionType }
130
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
138
139 # Copy "private" attributes, functions, and classes to the
140 # official dict. Everything else goes in _init_dict to be
141 # filtered in __init__.
142 cls_dict = {}
143 value_dict = {}
144 for key,val in dict.items():
145 if public_value(key, val):
146 cls_dict[key] = val
147 else:
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
158 return cls
159
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)
165
166 # initialize required attributes
167
168 # class-only attributes
169 cls._params = multidict() # param descriptions
170 cls._ports = multidict() # port descriptions
171
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
177
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
180 # are ok, though
181 bTotal = 0
182 for c in bases:
183 if isinstance(c, MetaSimObject):
184 bTotal += 1
185 if bTotal > 1:
186 raise TypeError, "SimObjects do not support multiple inheritance"
187
188 base = bases[0]
189
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):
195 cls._base = base
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
203 else:
204 cls._base = None
205
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']
210
211 cls._value_dict['cxx_type'] = '%s *' % cls._value_dict['cxx_class']
212
213 if 'cxx_header' not in cls._value_dict:
214 global noCxxHeader
215 noCxxHeader = True
216 warn("No header file specified for SimObject: %s", name)
217
218 # Export methods are automatically inherited via C++, so we
219 # don't want the method declarations to get inherited on the
220 # python side (and thus end up getting repeated in the wrapped
221 # versions of derived classes). The code below basicallly
222 # suppresses inheritance by substituting in the base (null)
223 # versions of these methods unless a different version is
224 # explicitly supplied.
225 for method_name in ('export_methods', 'export_method_cxx_predecls',
226 'export_method_swig_predecls'):
227 if method_name not in cls.__dict__:
228 base_method = getattr(MetaSimObject, method_name)
229 m = MethodType(base_method, cls, MetaSimObject)
230 setattr(cls, method_name, m)
231
232 # Now process the _value_dict items. They could be defining
233 # new (or overriding existing) parameters or ports, setting
234 # class keywords (e.g., 'abstract'), or setting parameter
235 # values or port bindings. The first 3 can only be set when
236 # the class is defined, so we handle them here. The others
237 # can be set later too, so just emulate that by calling
238 # setattr().
239 for key,val in cls._value_dict.items():
240 # param descriptions
241 if isinstance(val, ParamDesc):
242 cls._new_param(key, val)
243
244 # port objects
245 elif isinstance(val, Port):
246 cls._new_port(key, val)
247
248 # init-time-only keywords
249 elif cls.init_keywords.has_key(key):
250 cls._set_keyword(key, val, cls.init_keywords[key])
251
252 # default: use normal path (ends up in __setattr__)
253 else:
254 setattr(cls, key, val)
255
256 def _set_keyword(cls, keyword, val, kwtype):
257 if not isinstance(val, kwtype):
258 raise TypeError, 'keyword %s has bad type %s (expecting %s)' % \
259 (keyword, type(val), kwtype)
260 if isinstance(val, FunctionType):
261 val = classmethod(val)
262 type.__setattr__(cls, keyword, val)
263
264 def _new_param(cls, name, pdesc):
265 # each param desc should be uniquely assigned to one variable
266 assert(not hasattr(pdesc, 'name'))
267 pdesc.name = name
268 cls._params[name] = pdesc
269 if hasattr(pdesc, 'default'):
270 cls._set_param(name, pdesc.default, pdesc)
271
272 def _set_param(cls, name, value, param):
273 assert(param.name == name)
274 try:
275 value = param.convert(value)
276 except Exception, e:
277 msg = "%s\nError setting param %s.%s to %s\n" % \
278 (e, cls.__name__, name, value)
279 e.args = (msg, )
280 raise
281 cls._values[name] = value
282 # if param value is a SimObject, make it a child too, so that
283 # it gets cloned properly when the class is instantiated
284 if isSimObjectOrVector(value) and not value.has_parent():
285 cls._add_cls_child(name, value)
286
287 def _add_cls_child(cls, name, child):
288 # It's a little funky to have a class as a parent, but these
289 # objects should never be instantiated (only cloned, which
290 # clears the parent pointer), and this makes it clear that the
291 # object is not an orphan and can provide better error
292 # messages.
293 child.set_parent(cls, name)
294 cls._children[name] = child
295
296 def _new_port(cls, name, port):
297 # each port should be uniquely assigned to one variable
298 assert(not hasattr(port, 'name'))
299 port.name = name
300 cls._ports[name] = port
301
302 # same as _get_port_ref, effectively, but for classes
303 def _cls_get_port_ref(cls, attr):
304 # Return reference that can be assigned to another port
305 # via __setattr__. There is only ever one reference
306 # object per port, but we create them lazily here.
307 ref = cls._port_refs.get(attr)
308 if not ref:
309 ref = cls._ports[attr].makeRef(cls)
310 cls._port_refs[attr] = ref
311 return ref
312
313 # Set attribute (called on foo.attr = value when foo is an
314 # instance of class cls).
315 def __setattr__(cls, attr, value):
316 # normal processing for private attributes
317 if public_value(attr, value):
318 type.__setattr__(cls, attr, value)
319 return
320
321 if cls.keywords.has_key(attr):
322 cls._set_keyword(attr, value, cls.keywords[attr])
323 return
324
325 if cls._ports.has_key(attr):
326 cls._cls_get_port_ref(attr).connect(value)
327 return
328
329 if isSimObjectOrSequence(value) and cls._instantiated:
330 raise RuntimeError, \
331 "cannot set SimObject parameter '%s' after\n" \
332 " class %s has been instantiated or subclassed" \
333 % (attr, cls.__name__)
334
335 # check for param
336 param = cls._params.get(attr)
337 if param:
338 cls._set_param(attr, value, param)
339 return
340
341 if isSimObjectOrSequence(value):
342 # If RHS is a SimObject, it's an implicit child assignment.
343 cls._add_cls_child(attr, coerceSimObjectOrVector(value))
344 return
345
346 # no valid assignment... raise exception
347 raise AttributeError, \
348 "Class %s has no parameter \'%s\'" % (cls.__name__, attr)
349
350 def __getattr__(cls, attr):
351 if attr == 'cxx_class_path':
352 return cls.cxx_class.split('::')
353
354 if attr == 'cxx_class_name':
355 return cls.cxx_class_path[-1]
356
357 if attr == 'cxx_namespaces':
358 return cls.cxx_class_path[:-1]
359
360 if cls._values.has_key(attr):
361 return cls._values[attr]
362
363 if cls._children.has_key(attr):
364 return cls._children[attr]
365
366 raise AttributeError, \
367 "object '%s' has no attribute '%s'" % (cls.__name__, attr)
368
369 def __str__(cls):
370 return cls.__name__
371
372 # See ParamValue.cxx_predecls for description.
373 def cxx_predecls(cls, code):
374 code('#include "params/$cls.hh"')
375
376 # See ParamValue.swig_predecls for description.
377 def swig_predecls(cls, code):
378 code('%import "python/m5/internal/param_$cls.i"')
379
380 # Hook for exporting additional C++ methods to Python via SWIG.
381 # Default is none, override using @classmethod in class definition.
382 def export_methods(cls, code):
383 pass
384
385 # Generate the code needed as a prerequisite for the C++ methods
386 # exported via export_methods() to be compiled in the _wrap.cc
387 # file. Typically generates one or more #include statements. If
388 # any methods are exported, typically at least the C++ header
389 # declaring the relevant SimObject class must be included.
390 def export_method_cxx_predecls(cls, code):
391 pass
392
393 # Generate the code needed as a prerequisite for the C++ methods
394 # exported via export_methods() to be processed by SWIG.
395 # Typically generates one or more %include or %import statements.
396 # If any methods are exported, typically at least the C++ header
397 # declaring the relevant SimObject class must be included.
398 def export_method_swig_predecls(cls, code):
399 pass
400
401 # Generate the declaration for this object for wrapping with SWIG.
402 # Generates code that goes into a SWIG .i file. Called from
403 # src/SConscript.
404 def swig_decl(cls, code):
405 class_path = cls.cxx_class.split('::')
406 classname = class_path[-1]
407 namespaces = class_path[:-1]
408
409 # The 'local' attribute restricts us to the params declared in
410 # the object itself, not including inherited params (which
411 # will also be inherited from the base class's param struct
412 # here).
413 params = cls._params.local.values()
414 ports = cls._ports.local
415
416 code('%module(package="m5.internal") param_$cls')
417 code()
418 code('%{')
419 code('#include "sim/sim_object.hh"')
420 code('#include "params/$cls.hh"')
421 for param in params:
422 param.cxx_predecls(code)
423 code('#include "${{cls.cxx_header}}"')
424 cls.export_method_cxx_predecls(code)
425 code('''\
426/**
427 * This is a workaround for bug in swig. Prior to gcc 4.6.1 the STL
428 * headers like vector, string, etc. used to automatically pull in
429 * the cstddef header but starting with gcc 4.6.1 they no longer do.
430 * This leads to swig generated a file that does not compile so we
431 * explicitly include cstddef. Additionally, including version 2.0.4,
432 * swig uses ptrdiff_t without the std:: namespace prefix which is
433 * required with gcc 4.6.1. We explicitly provide access to it.
434 */
435#include <cstddef>
436using std::ptrdiff_t;
437''')
438 code('%}')
439 code()
440
441 for param in params:
442 param.swig_predecls(code)
443 cls.export_method_swig_predecls(code)
444
445 code()
446 if cls._base:
447 code('%import "python/m5/internal/param_${{cls._base}}.i"')
448 code()
449
450 for ns in namespaces:
451 code('namespace $ns {')
452
453 if namespaces:
454 code('// avoid name conflicts')
455 sep_string = '_COLONS_'
456 flat_name = sep_string.join(class_path)
457 code('%rename($flat_name) $classname;')
458
459 code()
460 code('// stop swig from creating/wrapping default ctor/dtor')
461 code('%nodefault $classname;')
462 code('class $classname')
463 if cls._base:
464 bases = [ cls._base.cxx_class ] + cls.cxx_bases
465 else:
466 bases = cls.cxx_bases
467 base_first = True
468 for base in bases:
469 if base_first:
470 code(' : public ${{base}}')
471 base_first = False
472 else:
473 code(' , public ${{base}}')
474
475 code('{')
476 code(' public:')
477 cls.export_methods(code)
478 code('};')
479
480 for ns in reversed(namespaces):
481 code('} // namespace $ns')
482
483 code()
484 code('%include "params/$cls.hh"')
485
486
487 # Generate the C++ declaration (.hh file) for this SimObject's
488 # param struct. Called from src/SConscript.
489 def cxx_param_decl(cls, code):
490 # The 'local' attribute restricts us to the params declared in
491 # the object itself, not including inherited params (which
492 # will also be inherited from the base class's param struct
493 # here).
494 params = cls._params.local.values()
495 ports = cls._ports.local
496 try:
497 ptypes = [p.ptype for p in params]
498 except:
499 print cls, p, p.ptype_str
500 print params
501 raise
502
503 class_path = cls._value_dict['cxx_class'].split('::')
504
505 code('''\
506#ifndef __PARAMS__${cls}__
507#define __PARAMS__${cls}__
508
509''')
510
511 # A forward class declaration is sufficient since we are just
512 # declaring a pointer.
513 for ns in class_path[:-1]:
514 code('namespace $ns {')
515 code('class $0;', class_path[-1])
516 for ns in reversed(class_path[:-1]):
517 code('} // namespace $ns')
518 code()
519
520 # The base SimObject has a couple of params that get
521 # automatically set from Python without being declared through
522 # the normal Param mechanism; we slip them in here (needed
523 # predecls now, actual declarations below)
524 if cls == SimObject:
525 code('''
526#ifndef PY_VERSION
527struct PyObject;
528#endif
529
530#include <string>
531
532class EventQueue;
533''')
534 for param in params:
535 param.cxx_predecls(code)
536 for port in ports.itervalues():
537 port.cxx_predecls(code)
538 code()
539
540 if cls._base:
541 code('#include "params/${{cls._base.type}}.hh"')
542 code()
543
544 for ptype in ptypes:
545 if issubclass(ptype, Enum):
546 code('#include "enums/${{ptype.__name__}}.hh"')
547 code()
548
549 # now generate the actual param struct
550 code("struct ${cls}Params")
551 if cls._base:
552 code(" : public ${{cls._base.type}}Params")
553 code("{")
554 if not hasattr(cls, 'abstract') or not cls.abstract:
555 if 'type' in cls.__dict__:
556 code(" ${{cls.cxx_type}} create();")
557
558 code.indent()
559 if cls == SimObject:
560 code('''
561 SimObjectParams()
562 {
563 extern EventQueue mainEventQueue;
564 eventq = &mainEventQueue;
565 }
566 virtual ~SimObjectParams() {}
567
568 std::string name;
569 PyObject *pyobj;
570 EventQueue *eventq;
571 ''')
572 for param in params:
573 param.cxx_decl(code)
574 for port in ports.itervalues():
575 port.cxx_decl(code)
576
577 code.dedent()
578 code('};')
579
580 code()
581 code('#endif // __PARAMS__${cls}__')
582 return code
583
584
585
586# The SimObject class is the root of the special hierarchy. Most of
587# the code in this class deals with the configuration hierarchy itself
588# (parent/child node relationships).
589class SimObject(object):
590 # Specify metaclass. Any class inheriting from SimObject will
591 # get this metaclass.
592 __metaclass__ = MetaSimObject
593 type = 'SimObject'
594 abstract = True
595 cxx_header = "sim/sim_object.hh"
596
597 cxx_bases = [ "Drainable", "Serializable" ]
598
599 @classmethod
600 def export_method_swig_predecls(cls, code):
601 code('''
602%include <std_string.i>
603
604%import "python/swig/drain.i"
605%import "python/swig/serialize.i"
606''')
607
608 @classmethod
609 def export_methods(cls, code):
610 code('''
611 void init();
612 void loadState(Checkpoint *cp);
613 void initState();
614 void regStats();
615 void resetStats();
616 void startup();
617''')
618
619 # Initialize new instance. For objects with SimObject-valued
620 # children, we need to recursively clone the classes represented
621 # by those param values as well in a consistent "deep copy"-style
622 # fashion. That is, we want to make sure that each instance is
623 # cloned only once, and that if there are multiple references to
624 # the same original object, we end up with the corresponding
625 # cloned references all pointing to the same cloned instance.
626 def __init__(self, **kwargs):
627 ancestor = kwargs.get('_ancestor')
628 memo_dict = kwargs.get('_memo')
629 if memo_dict is None:
630 # prepare to memoize any recursively instantiated objects
631 memo_dict = {}
632 elif ancestor:
633 # memoize me now to avoid problems with recursive calls
634 memo_dict[ancestor] = self
635
636 if not ancestor:
637 ancestor = self.__class__
638 ancestor._instantiated = True
639
640 # initialize required attributes
641 self._parent = None
642 self._name = None
643 self._ccObject = None # pointer to C++ object
644 self._ccParams = None
645 self._instantiated = False # really "cloned"
646
647 # Clone children specified at class level. No need for a
648 # multidict here since we will be cloning everything.
649 # Do children before parameter values so that children that
650 # are also param values get cloned properly.
651 self._children = {}
652 for key,val in ancestor._children.iteritems():
653 self.add_child(key, val(_memo=memo_dict))
654
655 # Inherit parameter values from class using multidict so
656 # individual value settings can be overridden but we still
657 # inherit late changes to non-overridden class values.
658 self._values = multidict(ancestor._values)
659 # clone SimObject-valued parameters
660 for key,val in ancestor._values.iteritems():
661 val = tryAsSimObjectOrVector(val)
662 if val is not None:
663 self._values[key] = val(_memo=memo_dict)
664
665 # clone port references. no need to use a multidict here
666 # since we will be creating new references for all ports.
667 self._port_refs = {}
668 for key,val in ancestor._port_refs.iteritems():
669 self._port_refs[key] = val.clone(self, memo_dict)
670 # apply attribute assignments from keyword args, if any
671 for key,val in kwargs.iteritems():
672 setattr(self, key, val)
673
674 # "Clone" the current instance by creating another instance of
675 # this instance's class, but that inherits its parameter values
676 # and port mappings from the current instance. If we're in a
677 # "deep copy" recursive clone, check the _memo dict to see if
678 # we've already cloned this instance.
679 def __call__(self, **kwargs):
680 memo_dict = kwargs.get('_memo')
681 if memo_dict is None:
682 # no memo_dict: must be top-level clone operation.
683 # this is only allowed at the root of a hierarchy
684 if self._parent:
685 raise RuntimeError, "attempt to clone object %s " \
686 "not at the root of a tree (parent = %s)" \
687 % (self, self._parent)
688 # create a new dict and use that.
689 memo_dict = {}
690 kwargs['_memo'] = memo_dict
691 elif memo_dict.has_key(self):
692 # clone already done & memoized
693 return memo_dict[self]
694 return self.__class__(_ancestor = self, **kwargs)
695
696 def _get_port_ref(self, attr):
697 # Return reference that can be assigned to another port
698 # via __setattr__. There is only ever one reference
699 # object per port, but we create them lazily here.
700 ref = self._port_refs.get(attr)
701 if ref == None:
702 ref = self._ports[attr].makeRef(self)
703 self._port_refs[attr] = ref
704 return ref
705
706 def __getattr__(self, attr):
707 if self._ports.has_key(attr):
708 return self._get_port_ref(attr)
709
710 if self._values.has_key(attr):
711 return self._values[attr]
712
713 if self._children.has_key(attr):
714 return self._children[attr]
715
716 # If the attribute exists on the C++ object, transparently
717 # forward the reference there. This is typically used for
718 # SWIG-wrapped methods such as init(), regStats(),
719 # resetStats(), startup(), drain(), and
720 # resume().
721 if self._ccObject and hasattr(self._ccObject, attr):
722 return getattr(self._ccObject, attr)
723
724 raise AttributeError, "object '%s' has no attribute '%s'" \
725 % (self.__class__.__name__, attr)
726
727 # Set attribute (called on foo.attr = value when foo is an
728 # instance of class cls).
729 def __setattr__(self, attr, value):
730 # normal processing for private attributes
731 if attr.startswith('_'):
732 object.__setattr__(self, attr, value)
733 return
734
735 if self._ports.has_key(attr):
736 # set up port connection
737 self._get_port_ref(attr).connect(value)
738 return
739
740 if isSimObjectOrSequence(value) and self._instantiated:
741 raise RuntimeError, \
742 "cannot set SimObject parameter '%s' after\n" \
743 " instance been cloned %s" % (attr, `self`)
744
745 param = self._params.get(attr)
746 if param:
747 try:
748 value = param.convert(value)
749 except Exception, e:
750 msg = "%s\nError setting param %s.%s to %s\n" % \
751 (e, self.__class__.__name__, attr, value)
752 e.args = (msg, )
753 raise
754 self._values[attr] = value
755 # implicitly parent unparented objects assigned as params
756 if isSimObjectOrVector(value) and not value.has_parent():
757 self.add_child(attr, value)
758 return
759
760 # if RHS is a SimObject, it's an implicit child assignment
761 if isSimObjectOrSequence(value):
762 self.add_child(attr, value)
763 return
764
765 # no valid assignment... raise exception
766 raise AttributeError, "Class %s has no parameter %s" \
767 % (self.__class__.__name__, attr)
768
769
770 # this hack allows tacking a '[0]' onto parameters that may or may
771 # not be vectors, and always getting the first element (e.g. cpus)
772 def __getitem__(self, key):
773 if key == 0:
774 return self
775 raise TypeError, "Non-zero index '%s' to SimObject" % key
776
777 # Also implemented by SimObjectVector
778 def clear_parent(self, old_parent):
779 assert self._parent is old_parent
780 self._parent = None
781
782 # Also implemented by SimObjectVector
783 def set_parent(self, parent, name):
784 self._parent = parent
785 self._name = name
786
787 # Return parent object of this SimObject, not implemented by SimObjectVector
788 # because the elements in a SimObjectVector may not share the same parent
789 def get_parent(self):
790 return self._parent
791
787 # Also implemented by SimObjectVector
788 def get_name(self):
789 return self._name
790
791 # Also implemented by SimObjectVector
792 def has_parent(self):
793 return self._parent is not None
794
795 # clear out child with given name. This code is not likely to be exercised.
796 # See comment in add_child.
797 def clear_child(self, name):
798 child = self._children[name]
799 child.clear_parent(self)
800 del self._children[name]
801
802 # Add a new child to this object.
803 def add_child(self, name, child):
804 child = coerceSimObjectOrVector(child)
805 if child.has_parent():
806 warn("add_child('%s'): child '%s' already has parent", name,
807 child.get_name())
808 if self._children.has_key(name):
809 # This code path had an undiscovered bug that would make it fail
810 # at runtime. It had been here for a long time and was only
811 # exposed by a buggy script. Changes here will probably not be
812 # exercised without specialized testing.
813 self.clear_child(name)
814 child.set_parent(self, name)
815 self._children[name] = child
816
817 # Take SimObject-valued parameters that haven't been explicitly
818 # assigned as children and make them children of the object that
819 # they were assigned to as a parameter value. This guarantees
820 # that when we instantiate all the parameter objects we're still
821 # inside the configuration hierarchy.
822 def adoptOrphanParams(self):
823 for key,val in self._values.iteritems():
824 if not isSimObjectVector(val) and isSimObjectSequence(val):
825 # need to convert raw SimObject sequences to
826 # SimObjectVector class so we can call has_parent()
827 val = SimObjectVector(val)
828 self._values[key] = val
829 if isSimObjectOrVector(val) and not val.has_parent():
830 warn("%s adopting orphan SimObject param '%s'", self, key)
831 self.add_child(key, val)
832
833 def path(self):
834 if not self._parent:
835 return '<orphan %s>' % self.__class__
836 ppath = self._parent.path()
837 if ppath == 'root':
838 return self._name
839 return ppath + "." + self._name
840
841 def __str__(self):
842 return self.path()
843
844 def ini_str(self):
845 return self.path()
846
847 def find_any(self, ptype):
848 if isinstance(self, ptype):
849 return self, True
850
851 found_obj = None
852 for child in self._children.itervalues():
853 if isinstance(child, ptype):
854 if found_obj != None and child != found_obj:
855 raise AttributeError, \
856 'parent.any matched more than one: %s %s' % \
857 (found_obj.path, child.path)
858 found_obj = child
859 # search param space
860 for pname,pdesc in self._params.iteritems():
861 if issubclass(pdesc.ptype, ptype):
862 match_obj = self._values[pname]
863 if found_obj != None and found_obj != match_obj:
864 raise AttributeError, \
865 'parent.any matched more than one: %s and %s' % (found_obj.path, match_obj.path)
866 found_obj = match_obj
867 return found_obj, found_obj != None
868
869 def find_all(self, ptype):
870 all = {}
871 # search children
872 for child in self._children.itervalues():
873 # a child could be a list, so ensure we visit each item
874 if isinstance(child, list):
875 children = child
876 else:
877 children = [child]
878
879 for child in children:
880 if isinstance(child, ptype) and not isproxy(child) and \
881 not isNullPointer(child):
882 all[child] = True
883 if isSimObject(child):
884 # also add results from the child itself
885 child_all, done = child.find_all(ptype)
886 all.update(dict(zip(child_all, [done] * len(child_all))))
887 # search param space
888 for pname,pdesc in self._params.iteritems():
889 if issubclass(pdesc.ptype, ptype):
890 match_obj = self._values[pname]
891 if not isproxy(match_obj) and not isNullPointer(match_obj):
892 all[match_obj] = True
893 return all.keys(), True
894
895 def unproxy(self, base):
896 return self
897
898 def unproxyParams(self):
899 for param in self._params.iterkeys():
900 value = self._values.get(param)
901 if value != None and isproxy(value):
902 try:
903 value = value.unproxy(self)
904 except:
905 print "Error in unproxying param '%s' of %s" % \
906 (param, self.path())
907 raise
908 setattr(self, param, value)
909
910 # Unproxy ports in sorted order so that 'append' operations on
911 # vector ports are done in a deterministic fashion.
912 port_names = self._ports.keys()
913 port_names.sort()
914 for port_name in port_names:
915 port = self._port_refs.get(port_name)
916 if port != None:
917 port.unproxy(self)
918
919 def print_ini(self, ini_file):
920 print >>ini_file, '[' + self.path() + ']' # .ini section header
921
922 instanceDict[self.path()] = self
923
924 if hasattr(self, 'type'):
925 print >>ini_file, 'type=%s' % self.type
926
927 if len(self._children.keys()):
928 print >>ini_file, 'children=%s' % \
929 ' '.join(self._children[n].get_name() \
930 for n in sorted(self._children.keys()))
931
932 for param in sorted(self._params.keys()):
933 value = self._values.get(param)
934 if value != None:
935 print >>ini_file, '%s=%s' % (param,
936 self._values[param].ini_str())
937
938 for port_name in sorted(self._ports.keys()):
939 port = self._port_refs.get(port_name, None)
940 if port != None:
941 print >>ini_file, '%s=%s' % (port_name, port.ini_str())
942
943 print >>ini_file # blank line between objects
944
945 # generate a tree of dictionaries expressing all the parameters in the
946 # instantiated system for use by scripts that want to do power, thermal
947 # visualization, and other similar tasks
948 def get_config_as_dict(self):
949 d = attrdict()
950 if hasattr(self, 'type'):
951 d.type = self.type
952 if hasattr(self, 'cxx_class'):
953 d.cxx_class = self.cxx_class
954 # Add the name and path of this object to be able to link to
955 # the stats
956 d.name = self.get_name()
957 d.path = self.path()
958
959 for param in sorted(self._params.keys()):
960 value = self._values.get(param)
961 if value != None:
962 try:
963 # Use native type for those supported by JSON and
964 # strings for everything else. skipkeys=True seems
965 # to not work as well as one would hope
966 if type(self._values[param].value) in \
967 [str, unicode, int, long, float, bool, None]:
968 d[param] = self._values[param].value
969 else:
970 d[param] = str(self._values[param])
971
972 except AttributeError:
973 pass
974
975 for n in sorted(self._children.keys()):
976 child = self._children[n]
977 # Use the name of the attribute (and not get_name()) as
978 # the key in the JSON dictionary to capture the hierarchy
979 # in the Python code that assembled this system
980 d[n] = child.get_config_as_dict()
981
982 for port_name in sorted(self._ports.keys()):
983 port = self._port_refs.get(port_name, None)
984 if port != None:
985 # Represent each port with a dictionary containing the
986 # prominent attributes
987 d[port_name] = port.get_config_as_dict()
988
989 return d
990
991 def getCCParams(self):
992 if self._ccParams:
993 return self._ccParams
994
995 cc_params_struct = getattr(m5.internal.params, '%sParams' % self.type)
996 cc_params = cc_params_struct()
997 cc_params.pyobj = self
998 cc_params.name = str(self)
999
1000 param_names = self._params.keys()
1001 param_names.sort()
1002 for param in param_names:
1003 value = self._values.get(param)
1004 if value is None:
1005 fatal("%s.%s without default or user set value",
1006 self.path(), param)
1007
1008 value = value.getValue()
1009 if isinstance(self._params[param], VectorParamDesc):
1010 assert isinstance(value, list)
1011 vec = getattr(cc_params, param)
1012 assert not len(vec)
1013 for v in value:
1014 vec.append(v)
1015 else:
1016 setattr(cc_params, param, value)
1017
1018 port_names = self._ports.keys()
1019 port_names.sort()
1020 for port_name in port_names:
1021 port = self._port_refs.get(port_name, None)
1022 if port != None:
1023 port_count = len(port)
1024 else:
1025 port_count = 0
1026 setattr(cc_params, 'port_' + port_name + '_connection_count',
1027 port_count)
1028 self._ccParams = cc_params
1029 return self._ccParams
1030
1031 # Get C++ object corresponding to this object, calling C++ if
1032 # necessary to construct it. Does *not* recursively create
1033 # children.
1034 def getCCObject(self):
1035 if not self._ccObject:
1036 # Make sure this object is in the configuration hierarchy
1037 if not self._parent and not isRoot(self):
1038 raise RuntimeError, "Attempt to instantiate orphan node"
1039 # Cycles in the configuration hierarchy are not supported. This
1040 # will catch the resulting recursion and stop.
1041 self._ccObject = -1
1042 params = self.getCCParams()
1043 self._ccObject = params.create()
1044 elif self._ccObject == -1:
1045 raise RuntimeError, "%s: Cycle found in configuration hierarchy." \
1046 % self.path()
1047 return self._ccObject
1048
1049 def descendants(self):
1050 yield self
1051 for child in self._children.itervalues():
1052 for obj in child.descendants():
1053 yield obj
1054
1055 # Call C++ to create C++ object corresponding to this object
1056 def createCCObject(self):
1057 self.getCCParams()
1058 self.getCCObject() # force creation
1059
1060 def getValue(self):
1061 return self.getCCObject()
1062
1063 # Create C++ port connections corresponding to the connections in
1064 # _port_refs
1065 def connectPorts(self):
1066 for portRef in self._port_refs.itervalues():
1067 portRef.ccConnect()
1068
1069# Function to provide to C++ so it can look up instances based on paths
1070def resolveSimObject(name):
1071 obj = instanceDict[name]
1072 return obj.getCCObject()
1073
1074def isSimObject(value):
1075 return isinstance(value, SimObject)
1076
1077def isSimObjectClass(value):
1078 return issubclass(value, SimObject)
1079
1080def isSimObjectVector(value):
1081 return isinstance(value, SimObjectVector)
1082
1083def isSimObjectSequence(value):
1084 if not isinstance(value, (list, tuple)) or len(value) == 0:
1085 return False
1086
1087 for val in value:
1088 if not isNullPointer(val) and not isSimObject(val):
1089 return False
1090
1091 return True
1092
1093def isSimObjectOrSequence(value):
1094 return isSimObject(value) or isSimObjectSequence(value)
1095
1096def isRoot(obj):
1097 from m5.objects import Root
1098 return obj and obj is Root.getInstance()
1099
1100def isSimObjectOrVector(value):
1101 return isSimObject(value) or isSimObjectVector(value)
1102
1103def tryAsSimObjectOrVector(value):
1104 if isSimObjectOrVector(value):
1105 return value
1106 if isSimObjectSequence(value):
1107 return SimObjectVector(value)
1108 return None
1109
1110def coerceSimObjectOrVector(value):
1111 value = tryAsSimObjectOrVector(value)
1112 if value is None:
1113 raise TypeError, "SimObject or SimObjectVector expected"
1114 return value
1115
1116baseClasses = allClasses.copy()
1117baseInstances = instanceDict.copy()
1118
1119def clear():
1120 global allClasses, instanceDict, noCxxHeader
1121
1122 allClasses = baseClasses.copy()
1123 instanceDict = baseInstances.copy()
1124 noCxxHeader = False
1125
1126# __all__ defines the list of symbols that get exported when
1127# 'from config import *' is invoked. Try to keep this reasonably
1128# short to avoid polluting other namespaces.
1129__all__ = [ 'SimObject' ]
792 # Also implemented by SimObjectVector
793 def get_name(self):
794 return self._name
795
796 # Also implemented by SimObjectVector
797 def has_parent(self):
798 return self._parent is not None
799
800 # clear out child with given name. This code is not likely to be exercised.
801 # See comment in add_child.
802 def clear_child(self, name):
803 child = self._children[name]
804 child.clear_parent(self)
805 del self._children[name]
806
807 # Add a new child to this object.
808 def add_child(self, name, child):
809 child = coerceSimObjectOrVector(child)
810 if child.has_parent():
811 warn("add_child('%s'): child '%s' already has parent", name,
812 child.get_name())
813 if self._children.has_key(name):
814 # This code path had an undiscovered bug that would make it fail
815 # at runtime. It had been here for a long time and was only
816 # exposed by a buggy script. Changes here will probably not be
817 # exercised without specialized testing.
818 self.clear_child(name)
819 child.set_parent(self, name)
820 self._children[name] = child
821
822 # Take SimObject-valued parameters that haven't been explicitly
823 # assigned as children and make them children of the object that
824 # they were assigned to as a parameter value. This guarantees
825 # that when we instantiate all the parameter objects we're still
826 # inside the configuration hierarchy.
827 def adoptOrphanParams(self):
828 for key,val in self._values.iteritems():
829 if not isSimObjectVector(val) and isSimObjectSequence(val):
830 # need to convert raw SimObject sequences to
831 # SimObjectVector class so we can call has_parent()
832 val = SimObjectVector(val)
833 self._values[key] = val
834 if isSimObjectOrVector(val) and not val.has_parent():
835 warn("%s adopting orphan SimObject param '%s'", self, key)
836 self.add_child(key, val)
837
838 def path(self):
839 if not self._parent:
840 return '<orphan %s>' % self.__class__
841 ppath = self._parent.path()
842 if ppath == 'root':
843 return self._name
844 return ppath + "." + self._name
845
846 def __str__(self):
847 return self.path()
848
849 def ini_str(self):
850 return self.path()
851
852 def find_any(self, ptype):
853 if isinstance(self, ptype):
854 return self, True
855
856 found_obj = None
857 for child in self._children.itervalues():
858 if isinstance(child, ptype):
859 if found_obj != None and child != found_obj:
860 raise AttributeError, \
861 'parent.any matched more than one: %s %s' % \
862 (found_obj.path, child.path)
863 found_obj = child
864 # search param space
865 for pname,pdesc in self._params.iteritems():
866 if issubclass(pdesc.ptype, ptype):
867 match_obj = self._values[pname]
868 if found_obj != None and found_obj != match_obj:
869 raise AttributeError, \
870 'parent.any matched more than one: %s and %s' % (found_obj.path, match_obj.path)
871 found_obj = match_obj
872 return found_obj, found_obj != None
873
874 def find_all(self, ptype):
875 all = {}
876 # search children
877 for child in self._children.itervalues():
878 # a child could be a list, so ensure we visit each item
879 if isinstance(child, list):
880 children = child
881 else:
882 children = [child]
883
884 for child in children:
885 if isinstance(child, ptype) and not isproxy(child) and \
886 not isNullPointer(child):
887 all[child] = True
888 if isSimObject(child):
889 # also add results from the child itself
890 child_all, done = child.find_all(ptype)
891 all.update(dict(zip(child_all, [done] * len(child_all))))
892 # search param space
893 for pname,pdesc in self._params.iteritems():
894 if issubclass(pdesc.ptype, ptype):
895 match_obj = self._values[pname]
896 if not isproxy(match_obj) and not isNullPointer(match_obj):
897 all[match_obj] = True
898 return all.keys(), True
899
900 def unproxy(self, base):
901 return self
902
903 def unproxyParams(self):
904 for param in self._params.iterkeys():
905 value = self._values.get(param)
906 if value != None and isproxy(value):
907 try:
908 value = value.unproxy(self)
909 except:
910 print "Error in unproxying param '%s' of %s" % \
911 (param, self.path())
912 raise
913 setattr(self, param, value)
914
915 # Unproxy ports in sorted order so that 'append' operations on
916 # vector ports are done in a deterministic fashion.
917 port_names = self._ports.keys()
918 port_names.sort()
919 for port_name in port_names:
920 port = self._port_refs.get(port_name)
921 if port != None:
922 port.unproxy(self)
923
924 def print_ini(self, ini_file):
925 print >>ini_file, '[' + self.path() + ']' # .ini section header
926
927 instanceDict[self.path()] = self
928
929 if hasattr(self, 'type'):
930 print >>ini_file, 'type=%s' % self.type
931
932 if len(self._children.keys()):
933 print >>ini_file, 'children=%s' % \
934 ' '.join(self._children[n].get_name() \
935 for n in sorted(self._children.keys()))
936
937 for param in sorted(self._params.keys()):
938 value = self._values.get(param)
939 if value != None:
940 print >>ini_file, '%s=%s' % (param,
941 self._values[param].ini_str())
942
943 for port_name in sorted(self._ports.keys()):
944 port = self._port_refs.get(port_name, None)
945 if port != None:
946 print >>ini_file, '%s=%s' % (port_name, port.ini_str())
947
948 print >>ini_file # blank line between objects
949
950 # generate a tree of dictionaries expressing all the parameters in the
951 # instantiated system for use by scripts that want to do power, thermal
952 # visualization, and other similar tasks
953 def get_config_as_dict(self):
954 d = attrdict()
955 if hasattr(self, 'type'):
956 d.type = self.type
957 if hasattr(self, 'cxx_class'):
958 d.cxx_class = self.cxx_class
959 # Add the name and path of this object to be able to link to
960 # the stats
961 d.name = self.get_name()
962 d.path = self.path()
963
964 for param in sorted(self._params.keys()):
965 value = self._values.get(param)
966 if value != None:
967 try:
968 # Use native type for those supported by JSON and
969 # strings for everything else. skipkeys=True seems
970 # to not work as well as one would hope
971 if type(self._values[param].value) in \
972 [str, unicode, int, long, float, bool, None]:
973 d[param] = self._values[param].value
974 else:
975 d[param] = str(self._values[param])
976
977 except AttributeError:
978 pass
979
980 for n in sorted(self._children.keys()):
981 child = self._children[n]
982 # Use the name of the attribute (and not get_name()) as
983 # the key in the JSON dictionary to capture the hierarchy
984 # in the Python code that assembled this system
985 d[n] = child.get_config_as_dict()
986
987 for port_name in sorted(self._ports.keys()):
988 port = self._port_refs.get(port_name, None)
989 if port != None:
990 # Represent each port with a dictionary containing the
991 # prominent attributes
992 d[port_name] = port.get_config_as_dict()
993
994 return d
995
996 def getCCParams(self):
997 if self._ccParams:
998 return self._ccParams
999
1000 cc_params_struct = getattr(m5.internal.params, '%sParams' % self.type)
1001 cc_params = cc_params_struct()
1002 cc_params.pyobj = self
1003 cc_params.name = str(self)
1004
1005 param_names = self._params.keys()
1006 param_names.sort()
1007 for param in param_names:
1008 value = self._values.get(param)
1009 if value is None:
1010 fatal("%s.%s without default or user set value",
1011 self.path(), param)
1012
1013 value = value.getValue()
1014 if isinstance(self._params[param], VectorParamDesc):
1015 assert isinstance(value, list)
1016 vec = getattr(cc_params, param)
1017 assert not len(vec)
1018 for v in value:
1019 vec.append(v)
1020 else:
1021 setattr(cc_params, param, value)
1022
1023 port_names = self._ports.keys()
1024 port_names.sort()
1025 for port_name in port_names:
1026 port = self._port_refs.get(port_name, None)
1027 if port != None:
1028 port_count = len(port)
1029 else:
1030 port_count = 0
1031 setattr(cc_params, 'port_' + port_name + '_connection_count',
1032 port_count)
1033 self._ccParams = cc_params
1034 return self._ccParams
1035
1036 # Get C++ object corresponding to this object, calling C++ if
1037 # necessary to construct it. Does *not* recursively create
1038 # children.
1039 def getCCObject(self):
1040 if not self._ccObject:
1041 # Make sure this object is in the configuration hierarchy
1042 if not self._parent and not isRoot(self):
1043 raise RuntimeError, "Attempt to instantiate orphan node"
1044 # Cycles in the configuration hierarchy are not supported. This
1045 # will catch the resulting recursion and stop.
1046 self._ccObject = -1
1047 params = self.getCCParams()
1048 self._ccObject = params.create()
1049 elif self._ccObject == -1:
1050 raise RuntimeError, "%s: Cycle found in configuration hierarchy." \
1051 % self.path()
1052 return self._ccObject
1053
1054 def descendants(self):
1055 yield self
1056 for child in self._children.itervalues():
1057 for obj in child.descendants():
1058 yield obj
1059
1060 # Call C++ to create C++ object corresponding to this object
1061 def createCCObject(self):
1062 self.getCCParams()
1063 self.getCCObject() # force creation
1064
1065 def getValue(self):
1066 return self.getCCObject()
1067
1068 # Create C++ port connections corresponding to the connections in
1069 # _port_refs
1070 def connectPorts(self):
1071 for portRef in self._port_refs.itervalues():
1072 portRef.ccConnect()
1073
1074# Function to provide to C++ so it can look up instances based on paths
1075def resolveSimObject(name):
1076 obj = instanceDict[name]
1077 return obj.getCCObject()
1078
1079def isSimObject(value):
1080 return isinstance(value, SimObject)
1081
1082def isSimObjectClass(value):
1083 return issubclass(value, SimObject)
1084
1085def isSimObjectVector(value):
1086 return isinstance(value, SimObjectVector)
1087
1088def isSimObjectSequence(value):
1089 if not isinstance(value, (list, tuple)) or len(value) == 0:
1090 return False
1091
1092 for val in value:
1093 if not isNullPointer(val) and not isSimObject(val):
1094 return False
1095
1096 return True
1097
1098def isSimObjectOrSequence(value):
1099 return isSimObject(value) or isSimObjectSequence(value)
1100
1101def isRoot(obj):
1102 from m5.objects import Root
1103 return obj and obj is Root.getInstance()
1104
1105def isSimObjectOrVector(value):
1106 return isSimObject(value) or isSimObjectVector(value)
1107
1108def tryAsSimObjectOrVector(value):
1109 if isSimObjectOrVector(value):
1110 return value
1111 if isSimObjectSequence(value):
1112 return SimObjectVector(value)
1113 return None
1114
1115def coerceSimObjectOrVector(value):
1116 value = tryAsSimObjectOrVector(value)
1117 if value is None:
1118 raise TypeError, "SimObject or SimObjectVector expected"
1119 return value
1120
1121baseClasses = allClasses.copy()
1122baseInstances = instanceDict.copy()
1123
1124def clear():
1125 global allClasses, instanceDict, noCxxHeader
1126
1127 allClasses = baseClasses.copy()
1128 instanceDict = baseInstances.copy()
1129 noCxxHeader = False
1130
1131# __all__ defines the list of symbols that get exported when
1132# 'from config import *' is invoked. Try to keep this reasonably
1133# short to avoid polluting other namespaces.
1134__all__ = [ 'SimObject' ]