SimObject.py revision 679
12651Ssaidi@eecs.umich.edufrom __future__ import generators
22651Ssaidi@eecs.umich.edu
32651Ssaidi@eecs.umich.eduimport os
42651Ssaidi@eecs.umich.eduimport re
52651Ssaidi@eecs.umich.eduimport sys
62651Ssaidi@eecs.umich.edu
72651Ssaidi@eecs.umich.edu#####################################################################
82651Ssaidi@eecs.umich.edu#
92651Ssaidi@eecs.umich.edu# M5 Python Configuration Utility
102651Ssaidi@eecs.umich.edu#
112651Ssaidi@eecs.umich.edu# The basic idea is to write simple Python programs that build Python
122651Ssaidi@eecs.umich.edu# objects corresponding to M5 SimObjects for the deisred simulation
132651Ssaidi@eecs.umich.edu# configuration.  For now, the Python emits a .ini file that can be
142651Ssaidi@eecs.umich.edu# parsed by M5.  In the future, some tighter integration between M5
152651Ssaidi@eecs.umich.edu# and the Python interpreter may allow bypassing the .ini file.
162651Ssaidi@eecs.umich.edu#
172651Ssaidi@eecs.umich.edu# Each SimObject class in M5 is represented by a Python class with the
182651Ssaidi@eecs.umich.edu# same name.  The Python inheritance tree mirrors the M5 C++ tree
192651Ssaidi@eecs.umich.edu# (e.g., SimpleCPU derives from BaseCPU in both cases, and all
202651Ssaidi@eecs.umich.edu# SimObjects inherit from a single SimObject base class).  To specify
212651Ssaidi@eecs.umich.edu# an instance of an M5 SimObject in a configuration, the user simply
222651Ssaidi@eecs.umich.edu# instantiates the corresponding Python object.  The parameters for
232651Ssaidi@eecs.umich.edu# that SimObject are given by assigning to attributes of the Python
242651Ssaidi@eecs.umich.edu# object, either using keyword assignment in the constructor or in
252651Ssaidi@eecs.umich.edu# separate assignment statements.  For example:
262651Ssaidi@eecs.umich.edu#
272651Ssaidi@eecs.umich.edu# cache = BaseCache('my_cache', root, size=64*K)
282651Ssaidi@eecs.umich.edu# cache.hit_latency = 3
292651Ssaidi@eecs.umich.edu# cache.assoc = 8
302651Ssaidi@eecs.umich.edu#
312651Ssaidi@eecs.umich.edu# (The first two constructor arguments specify the name of the created
322651Ssaidi@eecs.umich.edu# cache and its parent node in the hierarchy.)
332651Ssaidi@eecs.umich.edu#
342651Ssaidi@eecs.umich.edu# The magic lies in the mapping of the Python attributes for SimObject
352651Ssaidi@eecs.umich.edu# classes to the actual SimObject parameter specifications.  This
362651Ssaidi@eecs.umich.edu# allows parameter validity checking in the Python code.  Continuing
372651Ssaidi@eecs.umich.edu# the example above, the statements "cache.blurfl=3" or
382651Ssaidi@eecs.umich.edu# "cache.assoc='hello'" would both result in runtime errors in Python,
392651Ssaidi@eecs.umich.edu# since the BaseCache object has no 'blurfl' parameter and the 'assoc'
402651Ssaidi@eecs.umich.edu# parameter requires an integer, respectively.  This magic is done
412651Ssaidi@eecs.umich.edu# primarily by overriding the special __setattr__ method that controls
422651Ssaidi@eecs.umich.edu# assignment to object attributes.
432651Ssaidi@eecs.umich.edu#
442651Ssaidi@eecs.umich.edu# The Python module provides another class, ConfigNode, which is a
452651Ssaidi@eecs.umich.edu# superclass of SimObject.  ConfigNode implements the parent/child
462651Ssaidi@eecs.umich.edu# relationship for building the configuration hierarchy tree.
472651Ssaidi@eecs.umich.edu# Concrete instances of ConfigNode can be used to group objects in the
482651Ssaidi@eecs.umich.edu# hierarchy, but do not correspond to SimObjects themselves (like a
492651Ssaidi@eecs.umich.edu# .ini section with "children=" but no "type=".
502651Ssaidi@eecs.umich.edu#
512651Ssaidi@eecs.umich.edu# Once a set of Python objects have been instantiated in a hierarchy,
522651Ssaidi@eecs.umich.edu# calling 'instantiate(obj)' (where obj is the root of the hierarchy)
532651Ssaidi@eecs.umich.edu# will generate a .ini file.  See simple-4cpu.py for an example
542651Ssaidi@eecs.umich.edu# (corresponding to m5-test/simple-4cpu.ini).
552651Ssaidi@eecs.umich.edu#
562651Ssaidi@eecs.umich.edu#####################################################################
572651Ssaidi@eecs.umich.edu
582651Ssaidi@eecs.umich.edu#####################################################################
592651Ssaidi@eecs.umich.edu#
602651Ssaidi@eecs.umich.edu# ConfigNode/SimObject classes
612651Ssaidi@eecs.umich.edu#
622651Ssaidi@eecs.umich.edu# The Python class hierarchy rooted by ConfigNode (which is the base
632651Ssaidi@eecs.umich.edu# class of SimObject, which in turn is the base class of all other M5
642651Ssaidi@eecs.umich.edu# SimObject classes) has special attribute behavior.  In general, an
652651Ssaidi@eecs.umich.edu# object in this hierarchy has three categories of attribute-like
662651Ssaidi@eecs.umich.edu# things:
672651Ssaidi@eecs.umich.edu#
682651Ssaidi@eecs.umich.edu# 1. Regular Python methods and variables.  These must start with an
692651Ssaidi@eecs.umich.edu# underscore to be treated normally.
702651Ssaidi@eecs.umich.edu#
712651Ssaidi@eecs.umich.edu# 2. SimObject parameters.  These values are stored as normal Python
722651Ssaidi@eecs.umich.edu# attributes, but all assignments to these attributes are checked
732651Ssaidi@eecs.umich.edu# against the pre-defined set of parameters stored in the class's
742651Ssaidi@eecs.umich.edu# _param_dict dictionary.  Assignments to attributes that do not
752651Ssaidi@eecs.umich.edu# correspond to predefined parameters, or that are not of the correct
762651Ssaidi@eecs.umich.edu# type, incur runtime errors.
772651Ssaidi@eecs.umich.edu#
782651Ssaidi@eecs.umich.edu# 3. Hierarchy children.  The child nodes of a ConfigNode are stored
792651Ssaidi@eecs.umich.edu# in the node's _children dictionary, but can be accessed using the
802651Ssaidi@eecs.umich.edu# Python attribute dot-notation (just as they are printed out by the
812651Ssaidi@eecs.umich.edu# simulator).  Children cannot be created using attribute assigment;
822651Ssaidi@eecs.umich.edu# they must be added by specifying the parent node in the child's
832651Ssaidi@eecs.umich.edu# constructor or using the '+=' operator.
842651Ssaidi@eecs.umich.edu
852651Ssaidi@eecs.umich.edu# The SimObject parameters are the most complex, for a few reasons.
862651Ssaidi@eecs.umich.edu# First, both parameter descriptions and parameter values are
872651Ssaidi@eecs.umich.edu# inherited.  Thus parameter description lookup must go up the
882651Ssaidi@eecs.umich.edu# inheritance chain like normal attribute lookup, but this behavior
89# must be explicitly coded since the lookup occurs in each class's
90# _param_dict attribute.  Second, because parameter values can be set
91# on SimObject classes (to implement default values), the parameter
92# checking behavior must be enforced on class attribute assignments as
93# well as instance attribute assignments.  Finally, because we allow
94# class specialization via inheritance (e.g., see the L1Cache class in
95# the simple-4cpu.py example), we must do parameter checking even on
96# class instantiation.  To provide all these features, we use a
97# metaclass to define most of the SimObject parameter behavior for
98# this class hierarchy.
99#
100#####################################################################
101
102# The metaclass for ConfigNode (and thus for everything that dervies
103# from ConfigNode, including SimObject).  This class controls how new
104# classes that derive from ConfigNode are instantiated, and provides
105# inherited class behavior (just like a class controls how instances
106# of that class are instantiated, and provides inherited instance
107# behavior).
108class MetaConfigNode(type):
109
110    # __new__ is called before __init__, and is where the statements
111    # in the body of the class definition get loaded into the class's
112    # __dict__.  We intercept this to filter out parameter assignments
113    # and only allow "private" attributes to be passed to the base
114    # __new__ (starting with underscore).
115    def __new__(cls, name, bases, dict):
116        priv_keys = [k for k in dict.iterkeys() if k.startswith('_')]
117        priv_dict = {}
118        for k in priv_keys: priv_dict[k] = dict[k]; del dict[k]
119        # entries left in dict will get passed to __init__, where we'll
120        # deal with them as params.
121        return super(MetaConfigNode, cls).__new__(cls, name, bases, priv_dict)
122
123    # initialization: start out with an empty param dict (makes life
124    # simpler if we can assume _param_dict is always valid).  Also
125    # build inheritance list to simplify searching for inherited
126    # params.  Finally set parameters specified in class definition
127    # (if any).
128    def __init__(cls, name, bases, dict):
129        super(MetaConfigNode, cls).__init__(cls, name, bases, {})
130        # initialize _param_dict to empty
131        cls._param_dict = {}
132        # __mro__ is the ordered list of classes Python uses for
133        # method resolution.  We want to pick out the ones that have a
134        # _param_dict attribute for doing parameter lookups.
135        cls._param_bases = \
136                         [c for c in cls.__mro__ if hasattr(c, '_param_dict')]
137        # initialize attributes with values from class definition
138        for (pname, value) in dict.items():
139            try:
140                setattr(cls, pname, value)
141            except Exception, exc:
142                print "Error setting '%s' to '%s' on class '%s'\n" \
143                      % (pname, value, cls.__name__), exc
144
145    # set the class's parameter dictionary (called when loading
146    # class descriptions)
147    def set_param_dict(cls, param_dict):
148        # should only be called once (current one should be empty one
149        # from __init__)
150        assert not cls._param_dict
151        cls._param_dict = param_dict
152        # initialize attributes with default values
153        for (pname, param) in param_dict.items():
154            try:
155                setattr(cls, pname, param.default)
156            except Exception, exc:
157                print "Error setting '%s' default on class '%s'\n" \
158                      % (pname, cls.__name__), exc
159
160    # Lookup a parameter description by name in the given class.  Use
161    # the _param_bases list defined in __init__ to go up the
162    # inheritance hierarchy if necessary.
163    def lookup_param(cls, param_name):
164        for c in cls._param_bases:
165            param = c._param_dict.get(param_name)
166            if param: return param
167        return None
168
169    # Set attribute (called on foo.attr_name = value when foo is an
170    # instance of class cls).
171    def __setattr__(cls, attr_name, value):
172        # normal processing for private attributes
173        if attr_name.startswith('_'):
174            object.__setattr__(cls, attr_name, value)
175            return
176        # no '_': must be SimObject param
177        param = cls.lookup_param(attr_name)
178        if not param:
179            raise AttributeError, \
180                  "Class %s has no parameter %s" % (cls.__name__, attr_name)
181        # It's ok: set attribute by delegating to 'object' class.
182        # Note the use of param.make_value() to verify/canonicalize
183        # the assigned value
184        object.__setattr__(cls, attr_name, param.make_value(value))
185
186    # generator that iterates across all parameters for this class and
187    # all classes it inherits from
188    def all_param_names(cls):
189        for c in cls._param_bases:
190            for p in c._param_dict.iterkeys():
191                yield p
192
193# The ConfigNode class is the root of the special hierarchy.  Most of
194# the code in this class deals with the configuration hierarchy itself
195# (parent/child node relationships).
196class ConfigNode(object):
197    # Specify metaclass.  Any class inheriting from ConfigNode will
198    # get this metaclass.
199    __metaclass__ = MetaConfigNode
200
201    # Constructor.  Since bare ConfigNodes don't have parameters, just
202    # worry about the name and the parent/child stuff.
203    def __init__(self, _name, _parent=None):
204        # Type-check _name
205        if type(_name) != str:
206            if isinstance(_name, ConfigNode):
207                # special case message for common error of trying to
208                # coerce a SimObject to the wrong type
209                raise TypeError, \
210                      "Attempt to coerce %s to %s" \
211                      % (_name.__class__.__name__, self.__class__.__name__)
212            else:
213                raise TypeError, \
214                      "%s name must be string (was %s, %s)" \
215                      % (self.__class__.__name__, _name, type(_name))
216        # if specified, parent must be a subclass of ConfigNode
217        if _parent != None and not isinstance(_parent, ConfigNode):
218            raise TypeError, \
219                  "%s parent must be ConfigNode subclass (was %s, %s)" \
220                  % (self.__class__.__name__, _name, type(_name))
221        self._name = _name
222        self._parent = _parent
223        self._children = {}
224        if (_parent):
225            _parent.__addChild(self)
226        # Set up absolute path from root.
227        if (_parent and _parent._path != 'Universe'):
228            self._path = _parent._path + '.' + self._name
229        else:
230            self._path = self._name
231
232    # When printing (e.g. to .ini file), just give the name.
233    def __str__(self):
234        return self._name
235
236    # Catch attribute accesses that could be requesting children, and
237    # satisfy them.  Note that __getattr__ is called only if the
238    # regular attribute lookup fails, so private and parameter lookups
239    # will already be satisfied before we ever get here.
240    def __getattr__(self, name):
241        try:
242            return self._children[name]
243        except KeyError:
244            raise AttributeError, \
245                  "Node '%s' has no attribute or child '%s'" \
246                  % (self._name, name)
247
248    # Set attribute.  All attribute assignments go through here.  Must
249    # be private attribute (starts with '_') or valid parameter entry.
250    # Basically identical to MetaConfigClass.__setattr__(), except
251    # this handles instances rather than class attributes.
252    def __setattr__(self, attr_name, value):
253        if attr_name.startswith('_'):
254            object.__setattr__(self, attr_name, value)
255            return
256        # not private; look up as param
257        param = self.__class__.lookup_param(attr_name)
258        if not param:
259            raise AttributeError, \
260                  "Class %s has no parameter %s" \
261                  % (self.__class__.__name__, attr_name)
262        # It's ok: set attribute by delegating to 'object' class.
263        # Note the use of param.make_value() to verify/canonicalize
264        # the assigned value
265        object.__setattr__(self, attr_name, param.make_value(value))
266
267    # Add a child to this node.
268    def __addChild(self, new_child):
269        # set child's parent before calling this function
270        assert new_child._parent == self
271        if not isinstance(new_child, ConfigNode):
272            raise TypeError, \
273                  "ConfigNode child must also be of class ConfigNode"
274        if new_child._name in self._children:
275            raise AttributeError, \
276                  "Node '%s' already has a child '%s'" \
277                  % (self._name, new_child._name)
278        self._children[new_child._name] = new_child
279
280    # operator overload for '+='.  You can say "node += child" to add
281    # # a child that was created with parent=None.  An early attempt
282    # at # playing with syntax; turns out not to be that useful.
283    def __iadd__(self, new_child):
284        if new_child._parent != None:
285            raise AttributeError, \
286                  "Node '%s' already has a parent" % new_child._name
287        new_child._parent = self
288        self.__addChild(new_child)
289        return self
290
291    # Print instance info to .ini file.
292    def _instantiate(self):
293        print '[' + self._path + ']'	# .ini section header
294        if self._children:
295            # instantiate children in sorted order for backward
296            # compatibility (else we can end up with cpu1 before cpu0).
297            child_names = self._children.keys()
298            child_names.sort()
299            print 'children =',
300            for child_name in child_names:
301                print child_name,
302            print
303        self._instantiateParams()
304        print
305        # recursively dump out children
306        if self._children:
307            for child_name in child_names:
308                self._children[child_name]._instantiate()
309
310    # ConfigNodes have no parameters.  Overridden by SimObject.
311    def _instantiateParams(self):
312        pass
313
314# SimObject is a minimal extension of ConfigNode, implementing a
315# hierarchy node that corresponds to an M5 SimObject.  It prints out a
316# "type=" line to indicate its SimObject class, prints out the
317# assigned parameters corresponding to its class, and allows
318# parameters to be set by keyword in the constructor.  Note that most
319# of the heavy lifting for the SimObject param handling is done in the
320# MetaConfigNode metaclass.
321
322class SimObject(ConfigNode):
323    # initialization: like ConfigNode, but handle keyword-based
324    # parameter initializers.
325    def __init__(self, _name, _parent=None, **params):
326        ConfigNode.__init__(self, _name, _parent)
327        for param, value in params.items():
328            setattr(self, param, value)
329
330    # print type and parameter values to .ini file
331    def _instantiateParams(self):
332        print "type =", self.__class__._name
333        for pname in self.__class__.all_param_names():
334            value = getattr(self, pname)
335            if value != None:
336                print pname, '=', value
337
338#####################################################################
339#
340# Parameter description classes
341#
342# The _param_dict dictionary in each class maps parameter names to
343# either a Param or a VectorParam object.  These objects contain the
344# parameter description string, the parameter type, and the default
345# value (loaded from the PARAM section of the .odesc files).  The
346# make_value() method on these objects is used to force whatever value
347# is assigned to the parameter to the appropriate type.
348#
349# Note that the default values are loaded into the class's attribute
350# space when the parameter dictionary is initialized (in
351# MetaConfigNode.set_param_dict()); after that point they aren't
352# used.
353#
354#####################################################################
355
356# Force parameter value (rhs of '=') to ptype (or None, which means
357# not set).
358def make_param_value(ptype, value):
359    # nothing to do if None or already correct type
360    if value == None or isinstance(value, ptype):
361        return value
362    # this type conversion will raise an exception if it's illegal
363    return ptype(value)
364
365# Regular parameter.
366class Param(object):
367    # Constructor.  E.g., Param(Int, "number of widgets", 5)
368    def __init__(self, ptype, desc, default=None):
369        self.ptype = ptype
370        self.desc = desc
371        self.default = default
372
373    # Convert assigned value to appropriate type.
374    def make_value(self, value):
375        return make_param_value(self.ptype, value)
376
377# The _VectorParamValue class is a wrapper for vector-valued
378# parameters.  The leading underscore indicates that users shouldn't
379# see this class; it's magically generated by VectorParam.  The
380# parameter values are stored in the 'value' field as a Python list of
381# whatever type the parameter is supposed to be.  The only purpose of
382# storing these instead of a raw Python list is that we can override
383# the __str__() method to not print out '[' and ']' in the .ini file.
384class _VectorParamValue(object):
385    def __init__(self, list):
386        self.value = list
387
388    def __str__(self):
389        return ' '.join(map(str, self.value))
390
391# Vector-valued parameter description.  Just like Param, except that
392# the value is a vector (list) of the specified type instead of a
393# single value.
394class VectorParam(object):
395    # Constructor.  The resulting parameter will be a list of ptype.
396    def __init__(self, ptype, desc, default=None):
397        self.ptype = ptype
398        self.desc = desc
399        self.default = default
400
401    # Convert assigned value to appropriate type.  If the RHS is not a
402    # list or tuple, it generates a single-element list.
403    def make_value(self, value):
404        if value == None: return value
405        if isinstance(value, list) or isinstance(value, tuple):
406            # list: coerce each element into new list
407            val_list = [make_param_value(self.ptype, v) for v in
408                        iter(value)]
409        else:
410            # singleton: coerce & wrap in a list
411            val_list = [ make_param_value(self.ptype, value) ]
412        # wrap list in _VectorParamValue (see above)
413        return _VectorParamValue(val_list)
414
415#####################################################################
416#
417# Parameter Types
418#
419# Though native Python types could be used to specify parameter types
420# (the 'ptype' field of the Param and VectorParam classes), it's more
421# flexible to define our own set of types.  This gives us more control
422# over how Python expressions are converted to values (via the
423# __init__() constructor) and how these values are printed out (via
424# the __str__() conversion method).  Eventually we'll need these types
425# to correspond to distinct C++ types as well.
426#
427#####################################################################
428
429# Integer parameter type.
430class Int(object):
431    # Constructor.  Value must be Python int or long (long integer).
432    def __init__(self, value):
433        t = type(value)
434        if t == int or t == long:
435            self.value = value
436        else:
437            raise TypeError, "Int param got value %s %s" % (repr(value), t)
438
439    # Use Python string conversion.  Note that this puts an 'L' on the
440    # end of long integers; we can strip that off here if it gives us
441    # trouble.
442    def __str__(self):
443        return str(self.value)
444
445# Counter, Addr, and Tick are just aliases for Int for now.
446class Counter(Int):
447    pass
448
449class Addr(Int):
450    pass
451
452class Tick(Int):
453    pass
454
455# Boolean parameter type.
456class Bool(object):
457
458    # Constructor.  Typically the value will be one of the Python bool
459    # constants True or False (or the aliases true and false below).
460    # Also need to take integer 0 or 1 values since bool was not a
461    # distinct type in Python 2.2.  Parse a bunch of boolean-sounding
462    # strings too just for kicks.
463    def __init__(self, value):
464        t = type(value)
465        if t == bool:
466            self.value = value
467        elif t == int or t == long:
468            if value == 1:
469                self.value = True
470            elif value == 0:
471                self.value = False
472        elif t == str:
473            v = value.lower()
474            if v == "true" or v == "t" or v == "yes" or v == "y":
475                self.value = True
476            elif v == "false" or v == "f" or v == "no" or v == "n":
477                self.value = False
478        # if we didn't set it yet, it must not be something we understand
479        if not hasattr(self, 'value'):
480            raise TypeError, "Bool param got value %s %s" % (repr(value), t)
481
482    # Generate printable string version.
483    def __str__(self):
484        if self.value: return "true"
485        else: return "false"
486
487# String-valued parameter.
488class String(object):
489    # Constructor.  Value must be Python string.
490    def __init__(self, value):
491        t = type(value)
492        if t == str:
493            self.value = value
494        else:
495            raise TypeError, "String param got value %s %s" % (repr(value), t)
496
497    # Generate printable string version.  Not too tricky.
498    def __str__(self):
499        return self.value
500
501
502# Enumerated types are a little more complex.  The user specifies the
503# type as Enum(foo) where foo is either a list or dictionary of
504# alternatives (typically strings, but not necessarily so).  (In the
505# long run, the integer value of the parameter will be the list index
506# or the corresponding dictionary value.  For now, since we only check
507# that the alternative is valid and then spit it into a .ini file,
508# there's not much point in using the dictionary.)
509
510# What Enum() must do is generate a new type encapsulating the
511# provided list/dictionary so that specific values of the parameter
512# can be instances of that type.  We define two hidden internal
513# classes (_ListEnum and _DictEnum) to serve as base classes, then
514# derive the new type from the appropriate base class on the fly.
515
516
517# Base class for list-based Enum types.
518class _ListEnum(object):
519    # Constructor.  Value must be a member of the type's map list.
520    def __init__(self, value):
521        if value in self.map:
522            self.value = value
523            self.index = self.map.index(value)
524        else:
525            raise TypeError, "Enum param got bad value '%s' (not in %s)" \
526                  % (value, self.map)
527
528    # Generate printable string version of value.
529    def __str__(self):
530        return str(self.value)
531
532class _DictEnum(object):
533    # Constructor.  Value must be a key in the type's map dictionary.
534    def __init__(self, value):
535        if value in self.map:
536            self.value = value
537            self.index = self.map[value]
538        else:
539            raise TypeError, "Enum param got bad value '%s' (not in %s)" \
540                  % (value, self.map.keys())
541
542    # Generate printable string version of value.
543    def __str__(self):
544        return str(self.value)
545
546# Enum metaclass... calling Enum(foo) generates a new type (class)
547# that derives from _ListEnum or _DictEnum as appropriate.
548class Enum(type):
549    # counter to generate unique names for generated classes
550    counter = 1
551
552    def __new__(cls, map):
553        if isinstance(map, dict):
554            base = _DictEnum
555            keys = map.keys()
556        elif isinstance(map, list):
557            base = _ListEnum
558            keys = map
559        else:
560            raise TypeError, "Enum map must be list or dict (got %s)" % map
561        classname = "Enum%04d" % Enum.counter
562        Enum.counter += 1
563        # New class derives from selected base, and gets a 'map'
564        # attribute containing the specified list or dict.
565        return type.__new__(cls, classname, (base,), { 'map': map })
566
567
568#
569# "Constants"... handy aliases for various values.
570#
571
572# For compatibility with C++ bool constants.
573false = False
574true = True
575
576# Some memory range specifications use this as a default upper bound.
577MAX_ADDR = 2 ** 63
578
579# For power-of-two sizing, e.g. 64*K gives an integer value 65536.
580K = 1024
581M = K*K
582G = K*M
583
584#####################################################################
585#
586# Object description loading.
587#
588# The final step is to define the classes corresponding to M5 objects
589# and their parameters.  These classes are described in .odesc files
590# in the source tree.  This code walks the tree to find those files
591# and loads up the descriptions (by evaluating them in pieces as
592# Python code).
593#
594#
595# Because SimObject classes inherit from other SimObject classes, and
596# can use arbitrary other SimObject classes as parameter types, we
597# have to do this in three steps:
598#
599# 1. Walk the tree to find all the .odesc files.  Note that the base
600# of the filename *must* match the class name.  This step builds a
601# mapping from class names to file paths.
602#
603# 2. Start generating empty class definitions (via def_class()) using
604# the OBJECT field of the .odesc files to determine inheritance.
605# def_class() recurses on demand to define needed base classes before
606# derived classes.
607#
608# 3. Now that all of the classes are defined, go through the .odesc
609# files one more time loading the parameter descriptions.
610#
611#####################################################################
612
613# dictionary: maps object names to file paths
614odesc_file = {}
615
616# dictionary: maps object names to boolean flag indicating whether
617# class definition was loaded yet.  Since SimObject is defined in
618# m5.config.py, count it as loaded.
619odesc_loaded = { 'SimObject': True }
620
621# Find odesc files in namelist and initialize odesc_file and
622# odesc_loaded dictionaries.  Called via os.path.walk() (see below).
623def find_odescs(process, dirpath, namelist):
624    # Prune out SCCS directories so we don't process s.*.odesc files.
625    i = 0
626    while i < len(namelist):
627        if namelist[i] == "SCCS":
628            del namelist[i]
629        else:
630            i = i + 1
631    # Find .odesc files and record them.
632    for name in namelist:
633        if name.endswith('.odesc'):
634            objname = name[:name.rindex('.odesc')]
635            path = os.path.join(dirpath, name)
636            if odesc_file.has_key(objname):
637                print "Warning: duplicate object names:", \
638                      odesc_file[objname], path
639            odesc_file[objname] = path
640            odesc_loaded[objname] = False
641
642
643# Regular expression string for parsing .odesc files.
644file_re_string = r'''
645^OBJECT: \s* (\w+) \s* \( \s* (\w+) \s* \)
646\s*
647^PARAMS: \s*\n ( (\s+.*\n)* )
648'''
649
650# Compiled regular expression object.
651file_re = re.compile(file_re_string, re.MULTILINE | re.VERBOSE)
652
653# .odesc file parsing function.  Takes a filename and returns tuple of
654# object name, object base, and parameter description section.
655def parse_file(path):
656    f = open(path, 'r').read()
657    m = file_re.search(f)
658    if not m:
659        print "Can't parse", path
660        sys.exit(1)
661    return (m.group(1), m.group(2), m.group(3))
662
663# Define SimObject class based on description in specified filename.
664# Class itself is empty except for _name attribute; parameter
665# descriptions will be loaded later.  Will recurse to define base
666# classes as needed before defining specified class.
667def def_class(path):
668    # load & parse file
669    (obj, parent, params) = parse_file(path)
670    # check to see if base class is defined yet; define it if not
671    if not odesc_loaded.has_key(parent):
672        print "No .odesc file found for", parent
673        sys.exit(1)
674    if not odesc_loaded[parent]:
675        def_class(odesc_file[parent])
676    # define the class.
677    s = "class %s(%s): _name = '%s'" % (obj, parent, obj)
678    try:
679        # execute in global namespace, so new class will be globally
680        # visible
681        exec s in globals()
682    except Exception, exc:
683        print "Object error in %s:" % path, exc
684    # mark this file as loaded
685    odesc_loaded[obj] = True
686
687# Munge an arbitrary Python code string to get it to execute (mostly
688# dealing with indentation).  Stolen from isa_parser.py... see
689# comments there for a more detailed description.
690def fixPythonIndentation(s):
691    # get rid of blank lines first
692    s = re.sub(r'(?m)^\s*\n', '', s);
693    if (s != '' and re.match(r'[ \t]', s[0])):
694        s = 'if 1:\n' + s
695    return s
696
697# Load parameter descriptions from .odesc file.  Object class must
698# already be defined.
699def def_params(path):
700    # load & parse file
701    (obj_name, parent_name, param_code) = parse_file(path)
702    # initialize param dict
703    param_dict = {}
704    # execute parameter descriptions.
705    try:
706        # "in globals(), param_dict" makes exec use the current
707        # globals as the global namespace (so all of the Param
708        # etc. objects are visible) and param_dict as the local
709        # namespace (so the newly defined parameter variables will be
710        # entered into param_dict).
711        exec fixPythonIndentation(param_code) in globals(), param_dict
712    except Exception, exc:
713        print "Param error in %s:" % path, exc
714        return
715    # Convert object name string to Python class object
716    obj = eval(obj_name)
717    # Set the object's parameter description dictionary (see MetaConfigNode).
718    obj.set_param_dict(param_dict)
719
720
721# Walk directory tree to find .odesc files.
722# Someday we'll have to make the root path an argument instead of
723# hard-coding it.  For now the assumption is you're running this in
724# util/config.
725root = '../..'
726os.path.walk(root, find_odescs, None)
727
728# Iterate through file dictionary and define classes.
729for objname, path in odesc_file.iteritems():
730    if not odesc_loaded[objname]:
731        def_class(path)
732
733# Iterate through files again and load parameters.
734for path in odesc_file.itervalues():
735    def_params(path)
736
737#####################################################################
738
739# The final hook to generate .ini files.  Called from configuration
740# script once config is built.
741def instantiate(*objs):
742    for obj in objs:
743        obj._instantiate()
744