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