diff options
author | Nathan Binkert <binkertn@umich.edu> | 2005-01-15 04:12:25 -0500 |
---|---|---|
committer | Nathan Binkert <binkertn@umich.edu> | 2005-01-15 04:12:25 -0500 |
commit | 3ece6ab029756fa40679677d124e14acd1349416 (patch) | |
tree | 917c6adc11d19f9da82bc51511e6fb0cace8cce9 /sim/pyconfig/m5config.py | |
parent | dfecc891509d318ffaefee95b6c6a1ec83b6f28d (diff) | |
download | gem5-3ece6ab029756fa40679677d124e14acd1349416.tar.xz |
New and improved configuration mechanism. No more writing of
wierd ini files. The ini files are still used as an intermediate step,
but a sophisticated python library exists to help build them more
easily.
SConscript:
add the new embedded file stuff
remove all of the old object description junk
base/inifile.cc:
base/inifile.hh:
get rid of findDefault and findAppend since they were the source
of much evil.
base/trace.cc:
For now, if we don't have the dprintf_stream set up, dump
to standard out. We probably want a command line option
for this.
dev/alpha_console.cc:
PioDevice now takes a platform parameter.
All PioDevices must have a pio_latency parameter. We stick
a dummy parameter in here for now until we get rid of the
builder stuff.
dev/alpha_console.hh:
don't need Platform anymore
dev/baddev.cc:
PioDevice now takes a platform parameter.
All PioDevices must have a pio_latency parameter. We stick
a dummy parameter in here for now until we get rid of the
builder stuff. Same for the platform parameter, though we just
pass the PioDevice a null parameter since it isn't used by
this device and it's quicker.
dev/baddev.hh:
fix #include guards
dev/etherlink.cc:
rename parameters.
dev/ethertap.cc:
rename parameters
dev/ide_ctrl.cc:
All devices need an address even if it will get overwritten later.
dev/ide_disk.cc:
use an enum for the drive ID stuff.
rename disk_delay -> delay
Actually, I think that we should implement "cable select" and
have the controller tell the drive what it is.
dev/io_device.cc:
dev/io_device.hh:
All IO devices take a Platform *
dev/ns_gige.cc:
all devices need an io_bus. rename header_bus to io_bus
We don't need stuff for the interrupt controller since
it's all in the platform now.
dev/ns_gige.hh:
We don't need stuff for the interrupt controller now since
it's all in the platform.
dev/pciconfigall.cc:
Pass a dummy NULL to the PioDevice for the platform since
we don't need one.
dev/pcidev.cc:
Move a bunch of common functionality into the PciDev
dev/platform.hh:
remove unneeded code
dev/tsunami.cc:
remove unused param
dev/tsunami_cchip.cc:
pass platform pointer
dev/tsunami_io.cc:
dev/tsunami_pchip.cc:
dev/uart.cc:
pass platform variable
dev/uart.hh:
don't need to keep a platform pointer. it's in the base class
kern/linux/linux_system.cc:
kern/tru64/tru64_system.cc:
rename some parameters
sim/builder.cc:
clean up builder code. use more parameters from the
config node. all sections with a type= are now created,
the old mechanisms no longer work
sim/builder.hh:
remove some extra variables since they are found in the ConfigNode
sim/main.cc:
add a quick hack command line argument -X to dump out the
embedded files. (probably should be fixed up a little.)
accept .mpy files
printing to the streams has to happen after the hierarchy
is built since we're moving away from param contexts
sim/param.cc:
add parsing support for ranges
sim/process.cc:
isValid isn't very useful anymore. interpret the names
stdout, stderr, cout, cerr for the file descriptors
sim/pyconfig/SConscript:
Add Action handlers for creating an embedded python file
and for creating an embedded C file.
use these action handlers to embed all objects found in the objects
tree into the binary along with the importer and the m5config stuff
sim/pyconfig/m5config.py:
Major changes to the original configuration file generator. These
changes largely involve implementing copy-on-write like semantics
for all of the SimObjects. Real documentation must be written.
sim/universe.cc:
Universe becomes a SimObject since we don't really have the notion of
param contexts in the python code.
--HG--
rename : sim/pyconfig/m5configbase.py => sim/pyconfig/m5config.py
extra : convert_revision : c353453e5beb91c37f15755998fc0d8858c6829a
Diffstat (limited to 'sim/pyconfig/m5config.py')
-rw-r--r-- | sim/pyconfig/m5config.py | 1250 |
1 files changed, 1250 insertions, 0 deletions
diff --git a/sim/pyconfig/m5config.py b/sim/pyconfig/m5config.py new file mode 100644 index 000000000..39c2be7ab --- /dev/null +++ b/sim/pyconfig/m5config.py @@ -0,0 +1,1250 @@ +# Copyright (c) 2004 The Regents of The University of Michigan +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer; +# redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution; +# neither the name of the copyright holders nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +from __future__ import generators +import os, re, sys, types + +env = {} +env.update(os.environ) +def defined(key): + return env.has_key(key) + +def define(key, value = True): + env[key] = value + +def issequence(value): + return isinstance(value, tuple) or isinstance(value, list) + +class Singleton(type): + def __call__(cls, *args, **kwargs): + if hasattr(cls, '_instance'): + return cls._instance + + cls._instance = super(Singleton, cls).__call__(*args, **kwargs) + return cls._instance + +#class MetaTempObject(type): +# classes = {} +# def __new__(mcls, name, bases, dict): +# _bases = [] +# for base in bases: +# if base.__name__ is 'TempObject': +# _bases.append('SimObject') +# else: +# _bases.append(base.__name__) +# newdict = { '_bases' : _bases, '_name' : name, '_params' : dict } +# cls = type.__new__(mcls, name, bases, newdict) +# mcls.classes[name] = cls +# return cls +# +#class TempObject(object): +# __metaclass__ = MetaTempObject + +def defined(s): + try: + eval(s) + return True + except NameError: + return False + +if os.environ.has_key('FULL_SYSTEM'): + FULL_SYSTEM = True + +##################################################################### +# +# M5 Python Configuration Utility +# +# The basic idea is to write simple Python programs that build Python +# objects corresponding to M5 SimObjects for the deisred simulation +# configuration. For now, the Python emits a .ini file that can be +# parsed by M5. In the future, some tighter integration between M5 +# and the Python interpreter may allow bypassing the .ini file. +# +# Each SimObject class in M5 is represented by a Python class with the +# same name. The Python inheritance tree mirrors the M5 C++ tree +# (e.g., SimpleCPU derives from BaseCPU in both cases, and all +# SimObjects inherit from a single SimObject base class). To specify +# an instance of an M5 SimObject in a configuration, the user simply +# instantiates the corresponding Python object. The parameters for +# that SimObject are given by assigning to attributes of the Python +# object, either using keyword assignment in the constructor or in +# separate assignment statements. For example: +# +# cache = BaseCache('my_cache', root, size=64*K) +# cache.hit_latency = 3 +# cache.assoc = 8 +# +# (The first two constructor arguments specify the name of the created +# cache and its parent node in the hierarchy.) +# +# The magic lies in the mapping of the Python attributes for SimObject +# classes to the actual SimObject parameter specifications. This +# allows parameter validity checking in the Python code. Continuing +# the example above, the statements "cache.blurfl=3" or +# "cache.assoc='hello'" would both result in runtime errors in Python, +# since the BaseCache object has no 'blurfl' parameter and the 'assoc' +# parameter requires an integer, respectively. This magic is done +# primarily by overriding the special __setattr__ method that controls +# assignment to object attributes. +# +# The Python module provides another class, ConfigNode, which is a +# superclass of SimObject. ConfigNode implements the parent/child +# relationship for building the configuration hierarchy tree. +# Concrete instances of ConfigNode can be used to group objects in the +# hierarchy, but do not correspond to SimObjects themselves (like a +# .ini section with "children=" but no "type=". +# +# Once a set of Python objects have been instantiated in a hierarchy, +# calling 'instantiate(obj)' (where obj is the root of the hierarchy) +# will generate a .ini file. See simple-4cpu.py for an example +# (corresponding to m5-test/simple-4cpu.ini). +# +##################################################################### + +##################################################################### +# +# ConfigNode/SimObject classes +# +# The Python class hierarchy rooted by ConfigNode (which is the base +# class of SimObject, which in turn is the base class of all other M5 +# SimObject classes) has special attribute behavior. In general, an +# object in this hierarchy has three categories of attribute-like +# things: +# +# 1. Regular Python methods and variables. These must start with an +# underscore to be treated normally. +# +# 2. SimObject parameters. These values are stored as normal Python +# attributes, but all assignments to these attributes are checked +# against the pre-defined set of parameters stored in the class's +# _params dictionary. Assignments to attributes that do not +# correspond to predefined parameters, or that are not of the correct +# type, incur runtime errors. +# +# 3. Hierarchy children. The child nodes of a ConfigNode are stored +# in the node's _children dictionary, but can be accessed using the +# Python attribute dot-notation (just as they are printed out by the +# simulator). Children cannot be created using attribute assigment; +# they must be added by specifying the parent node in the child's +# constructor or using the '+=' operator. + +# The SimObject parameters are the most complex, for a few reasons. +# First, both parameter descriptions and parameter values are +# inherited. Thus parameter description lookup must go up the +# inheritance chain like normal attribute lookup, but this behavior +# must be explicitly coded since the lookup occurs in each class's +# _params attribute. Second, because parameter values can be set +# on SimObject classes (to implement default values), the parameter +# checking behavior must be enforced on class attribute assignments as +# well as instance attribute assignments. Finally, because we allow +# class specialization via inheritance (e.g., see the L1Cache class in +# the simple-4cpu.py example), we must do parameter checking even on +# class instantiation. To provide all these features, we use a +# metaclass to define most of the SimObject parameter behavior for +# this class hierarchy. +# +##################################################################### + +class Proxy(object): + def __init__(self, path = ()): + self._object = None + self._path = path + + def __getattr__(self, attr): + return Proxy(self._path + (attr, )) + + def __setattr__(self, attr, value): + if not attr.startswith('_'): + raise AttributeError, 'cannot set attribute %s' % attr + super(Proxy, self).__setattr__(attr, value) + + def _convert(self): + obj = self._object + for attr in self._path: + obj = obj.__getattribute__(attr) + return obj + +Super = Proxy() + +def isSubClass(value, cls): + try: + return issubclass(value, cls) + except: + return False + +def isParam(self): + return isinstance(self, _Param) + +def isConfigNode(value): + try: + return issubclass(value, ConfigNode) + except: + return False + +def isSimObject(value): + try: + return issubclass(value, SimObject) + except: + return False + +def isSimObjSequence(value): + if not issequence(value): + return False + + for val in value: + if not isNullPointer(val) and not isConfigNode(val): + return False + + return True + +# The metaclass for ConfigNode (and thus for everything that derives +# from ConfigNode, including SimObject). This class controls how new +# classes that derive from ConfigNode are instantiated, and provides +# inherited class behavior (just like a class controls how instances +# of that class are instantiated, and provides inherited instance +# behavior). +class MetaConfigNode(type): + keywords = { 'abstract' : types.BooleanType, + 'check' : types.FunctionType, + '_init' : types.FunctionType, + 'type' : (types.NoneType, types.StringType) } + + # __new__ is called before __init__, and is where the statements + # in the body of the class definition get loaded into the class's + # __dict__. We intercept this to filter out parameter assignments + # and only allow "private" attributes to be passed to the base + # __new__ (starting with underscore). + def __new__(mcls, name, bases, dict): + priv = { 'abstract' : False, + # initialize _params and _values dicts to empty + '_params' : {}, + '_values' : {}, + '_disable' : {} } + + for key,val in dict.items(): + if mcls.keywords.has_key(key): + if not isinstance(val, mcls.keywords[key]): + raise TypeError, \ + 'keyword %s has the wrong type %s should be %s' % \ + (key, type(val), mcls.keywords[key]) + + if isinstance(val, types.FunctionType): + val = classmethod(val) + priv[key] = val + del dict[key] + + elif key.startswith('_'): + priv[key] = val + del dict[key] + + elif not isNullPointer(val) and isConfigNode(val): + dict[key] = val() + + elif isSimObjSequence(val): + dict[key] = [ v() for v in val ] + + # If your parent has a value in it that's a config node, clone it. + for base in bases: + if not isConfigNode(base): + continue + + for name,value in base._values.iteritems(): + if dict.has_key(name): + continue + + if isConfigNode(value): + priv['_values'][name] = value() + elif isSimObjSequence(value): + priv['_values'][name] = [ val() for val in value ] + + # entries left in dict will get passed to __init__, where we'll + # deal with them as params. + return super(MetaConfigNode, mcls).__new__(mcls, name, bases, priv) + + # initialization: start out with an empty _params dict (makes life + # simpler if we can assume _params is always valid). + def __init__(cls, name, bases, dict): + super(MetaConfigNode, cls).__init__(cls, name, bases, {}) + + cls._bases = [c for c in cls.__mro__ if isConfigNode(c)] + + # initialize attributes with values from class definition + for pname,value in dict.iteritems(): + setattr(cls, pname, value) + + if hasattr(cls, '_init'): + cls._init() + del cls._init + + def _isvalue(cls, name): + for c in cls._bases: + if c._params.has_key(name): + return True + + for c in cls._bases: + if c._values.has_key(name): + return True + + return False + + # generator that iterates across all parameters for this class and + # all classes it inherits from + def _getparams(cls): + params = {} + for c in cls._bases: + for p,v in c._params.iteritems(): + if not params.has_key(p): + params[p] = v + return params + + # Lookup a parameter description by name in the given class. + def _getparam(cls, name, default = AttributeError): + for c in cls._bases: + if c._params.has_key(name): + return c._params[name] + if isSubClass(default, Exception): + raise default, \ + "object '%s' has no attribute '%s'" % (cls.__name__, name) + else: + return default + + def _setparam(cls, name, value): + cls._params[name] = value + + def _hasvalue(cls, name): + for c in cls._bases: + if c._values.has_key(name): + return True + + return False + + def _getvalues(cls): + values = {} + for i,c in enumerate(cls._bases): + for p,v in c._values.iteritems(): + if not values.has_key(p): + values[p] = v + return values + + def _getvalue(cls, name, default = AttributeError): + value = None + for c in cls._bases: + if c._values.has_key(name): + value = c._values[name] + break + if value is not None: + return value + + param = cls._getparam(name, None) + if param is not None and hasattr(param, 'default'): + param.valid(param.default) + value = param.default + cls._setvalue(name, value) + return value + + if isSubClass(default, Exception): + raise default, 'value for %s not found' % name + else: + return default + + def _setvalue(cls, name, value): + cls._values[name] = value + + def _getdisable(cls, name): + for c in cls._bases: + if c._disable.has_key(name): + return c._disable[name] + return False + + def _setdisable(cls, name, value): + cls._disable[name] = value + + def __getattr__(cls, attr): + if cls._isvalue(attr): + return Value(cls, attr) + + raise AttributeError, \ + "object '%s' has no attribute '%s'" % (cls.__name__, cls) + + + # Set attribute (called on foo.attr = value when foo is an + # instance of class cls). + def __setattr__(cls, attr, value): + # normal processing for private attributes + if attr.startswith('_'): + type.__setattr__(cls, attr, value) + return + + if cls.keywords.has_key(attr): + raise TypeError, \ + "keyword '%s' can only be set in a simobj definition" % attr + + if isParam(value): + cls._setparam(attr, value) + return + + # must be SimObject param + param = cls._getparam(attr, None) + if param: + # It's ok: set attribute by delegating to 'object' class. + # Note the use of param.make_value() to verify/canonicalize + # the assigned value + param.valid(value) + cls._setvalue(attr, value) + elif isConfigNode(value) or isSimObjSequence(value): + cls._setvalue(attr, value) + else: + for p,v in cls._getparams().iteritems(): + print p,v + raise AttributeError, \ + "Class %s has no parameter %s" % (cls.__name__, attr) + + def add_child(cls, instance, name, child): + if isNullPointer(child) or instance.top_child_names.has_key(name): + return + + if issequence(child): + kid = [] + for i,c in enumerate(child): + n = '%s%d' % (name, i) + k = c.instantiate(n, instance) + + instance.children.append(k) + instance.child_names[n] = k + instance.child_objects[c] = k + kid.append(k) + else: + kid = child.instantiate(name, instance) + instance.children.append(kid) + instance.child_names[name] = kid + instance.child_objects[child] = kid + + instance.top_child_names[name] = kid + + # Print instance info to .ini file. + def instantiate(cls, name, parent = None): + instance = Node(name, cls, cls.type, parent) + + if hasattr(cls, 'check'): + cls.check() + + for key,value in cls._getvalues().iteritems(): + if cls._getdisable(key): + continue + + if isConfigNode(value): + cls.add_child(instance, key, value) + if issequence(value): + list = [ v for v in value if isConfigNode(v) ] + if len(list): + cls.add_child(instance, key, list) + + for pname,param in cls._getparams().iteritems(): + try: + if cls._getdisable(pname): + continue + + value = cls._getvalue(pname) + + if isConfigNode(value): + value = instance.child_objects[value] + elif issequence(value): + v = [] + for val in value: + if isConfigNode(val): + v.append(instance.child_objects[val]) + else: + v.append(val) + value = v + + p = NodeParam(pname, param, value) + instance.params.append(p) + instance.param_names[pname] = p + except: + print 'Exception while evaluating %s.%s' % \ + (instance.path, pname) + raise + + return instance + + def _convert(cls, value): + realvalue = value + if isinstance(value, Node): + realvalue = value.realtype + + if isinstance(realvalue, Proxy): + return value + + if realvalue == None or isNullPointer(realvalue): + return value + + if isSubClass(realvalue, cls): + return value + + raise TypeError, 'object %s type %s wrong type, should be %s' % \ + (repr(realvalue), realvalue, cls) + + def _string(cls, value): + if isNullPointer(value): + return 'Null' + return Node._string(value) + +# The ConfigNode class is the root of the special hierarchy. Most of +# the code in this class deals with the configuration hierarchy itself +# (parent/child node relationships). +class ConfigNode(object): + # Specify metaclass. Any class inheriting from ConfigNode will + # get this metaclass. + __metaclass__ = MetaConfigNode + type = None + + def __new__(cls, **kwargs): + return MetaConfigNode(cls.__name__, (cls, ), kwargs) + + # Set attribute. All attribute assignments go through here. Must + # be private attribute (starts with '_') or valid parameter entry. + # Basically identical to MetaConfigClass.__setattr__(), except + # this sets attributes on specific instances rather than on classes. + #def __setattr__(self, attr, value): + # if attr.startswith('_'): + # object.__setattr__(self, attr, value) + # return + # not private; look up as param + # param = self.__class__.lookup_param(attr) + # if not param: + # raise AttributeError, \ + # "Class %s has no parameter %s" \ + # % (self.__class__.__name__, attr) + # It's ok: set attribute by delegating to 'object' class. + # Note the use of param.make_value() to verify/canonicalize + # the assigned value. + # v = param.convert(value) + # object.__setattr__(self, attr, v) + +# SimObject is a minimal extension of ConfigNode, implementing a +# hierarchy node that corresponds to an M5 SimObject. It prints out a +# "type=" line to indicate its SimObject class, prints out the +# assigned parameters corresponding to its class, and allows +# parameters to be set by keyword in the constructor. Note that most +# of the heavy lifting for the SimObject param handling is done in the +# MetaConfigNode metaclass. +class SimObject(ConfigNode): + def _sim_code(cls): + name = cls.__name__ + param_names = cls._params.keys() + param_names.sort() + code = "BEGIN_DECLARE_SIM_OBJECT_PARAMS(%s)\n" % name + decls = [" " + cls._params[pname].sim_decl(pname) \ + for pname in param_names] + code += "\n".join(decls) + "\n" + code += "END_DECLARE_SIM_OBJECT_PARAMS(%s)\n\n" % name + code += "BEGIN_INIT_SIM_OBJECT_PARAMS(%s)\n" % name + inits = [" " + cls._params[pname].sim_init(pname) \ + for pname in param_names] + code += ",\n".join(inits) + "\n" + code += "END_INIT_SIM_OBJECT_PARAMS(%s)\n\n" % name + return code + _sim_code = classmethod(_sim_code) + +class NodeParam(object): + def __init__(self, name, param, value): + self.name = name + self.param = param + self.ptype = param.ptype + self.convert = param.convert + self.string = param.string + self.value = value + +class Node(object): + all = {} + def __init__(self, name, realtype, type, parent): + self.name = name + self.realtype = realtype + self.type = type + self.parent = parent + self.children = [] + self.child_names = {} + self.child_objects = {} + self.top_child_names = {} + self.params = [] + self.param_names = {} + + path = [ self.name ] + node = self.parent + while node is not None: + if node.name != 'root': + path.insert(0, node.name) + else: + assert(node.parent is None) + node = node.parent + self.path = '.'.join(path) + + def find(self, realtype, path): + rtype = eval(realtype) + if not path: + if issubclass(self.realtype, rtype): + return self, True + + obj = None + for child in self.children: + if issubclass(child.realtype, rtype): + if obj is not None: + raise AttributeError, \ + 'Super matched more than one: %s %s' % \ + (obj.path, child.path) + obj = child + return obj, obj is not None + + try: + obj = self + for node in path[:-1]: + obj = obj.child_names[node] + + last = path[-1] + if obj.child_names.has_key(last): + value = obj.child_names[last] + if issubclass(value.realtype, rtype): + return value, True + elif obj.param_names.has_key(last): + value = obj.param_names[last] + rtype._convert(value.value) + return value.value, True + except KeyError: + pass + + return None, False + + def unproxy(self, ptype, value): + if not isinstance(value, Proxy): + return value + + if value is None: + raise AttributeError, 'Error while fixing up %s' % self.path + + obj = self + done = False + while not done: + if obj is None: + raise AttributeError, \ + 'Parent of %s type %s not found at path %s' \ + % (self.name, ptype, value._path) + found, done = obj.find(ptype, value._path) + if isinstance(found, Proxy): + done = false + obj = obj.parent + + return found + + def fixup(self): + self.all[self.path] = self + + for param in self.params: + ptype = param.ptype + pval = param.value + + try: + if issequence(pval): + param.value = [ self.unproxy(ptype, pv) for pv in pval ] + else: + param.value = self.unproxy(ptype, pval) + except: + print 'Error while fixing up %s:%s' % (self.path, param.name) + raise + + for child in self.children: + assert(child != self) + child.fixup() + + # print type and parameter values to .ini file + def display(self): + print '[' + self.path + ']' # .ini section header + + if isSimObject(self.realtype): + print 'type = %s' % self.type + + if self.children: + # instantiate children in same order they were added for + # backward compatibility (else we can end up with cpu1 + # before cpu0). + print 'children =', ' '.join([ c.name for c in self.children]) + + for param in self.params: + try: + if param.value is None: + raise AttributeError, 'Parameter with no value' + + value = param.convert(param.value) + string = param.string(value) + except: + print 'exception in %s:%s' % (self.path, param.name) + raise + + print '%s = %s' % (param.name, string) + + print + + # recursively dump out children + for c in self.children: + c.display() + + def _string(cls, value): + if not isinstance(value, Node): + raise AttributeError, 'expecting %s got %s' % (Node, value) + return value.path + _string = classmethod(_string) + +##################################################################### +# +# Parameter description classes +# +# The _params dictionary in each class maps parameter names to +# either a Param or a VectorParam object. These objects contain the +# parameter description string, the parameter type, and the default +# value (loaded from the PARAM section of the .odesc files). The +# _convert() method on these objects is used to force whatever value +# is assigned to the parameter to the appropriate type. +# +# Note that the default values are loaded into the class's attribute +# space when the parameter dictionary is initialized (in +# MetaConfigNode._setparams()); after that point they aren't used. +# +##################################################################### + +def isNullPointer(value): + return isinstance(value, NullSimObject) + +class Value(object): + def __init__(self, obj, attr): + super(Value, self).__setattr__('attr', attr) + super(Value, self).__setattr__('obj', obj) + + def _getattr(self): + return self.obj._getvalue(self.attr) + + def __setattr__(self, attr, value): + if attr == 'disable': + self.obj._setdisable(self.attr, value) + else: + setattr(self._getattr(), attr, value) + + def __getattr__(self, attr): + if attr == 'disable': + return self.obj._getdisable(self.attr) + else: + return getattr(self._getattr(), attr) + + def __getitem__(self, index): + return self._getattr().__getitem__(index) + + def __call__(self, *args, **kwargs): + return self._getattr().__call__(*args, **kwargs) + + def __nonzero__(self): + return bool(self._getattr()) + + def __str__(self): + return str(self._getattr()) + +# Regular parameter. +class _Param(object): + def __init__(self, ptype, *args, **kwargs): + self.ptype = ptype + + if args: + if len(args) == 1: + self.desc = args[0] + elif len(args) == 2: + self.default = args[0] + self.desc = args[1] + else: + raise TypeError, 'too many arguments' + + if kwargs.has_key('desc'): + assert(not hasattr(self, 'desc')) + self.desc = kwargs['desc'] + del kwargs['desc'] + + if kwargs.has_key('default'): + assert(not hasattr(self, 'default')) + self.default = kwargs['default'] + del kwargs['default'] + + if kwargs: + raise TypeError, 'extra unknown kwargs %s' % kwargs + + if not hasattr(self, 'desc'): + raise TypeError, 'desc attribute missing' + + def valid(self, value): + if not isinstance(value, Proxy): + ptype = eval(self.ptype) + ptype._convert(value) + + def convert(self, value): + ptype = eval(self.ptype) + return ptype._convert(value) + + def string(self, value): + ptype = eval(self.ptype) + return ptype._string(value) + + def get(self, name, instance, owner): + # nothing to do if None or already correct type. Also allow NULL + # pointer to be assigned where a SimObject is expected. + try: + if value == None or isinstance(value, self.ptype) or \ + isConfigNode(self.ptype) and \ + (isNullPointer(value) or issubclass(value, self.ptype)): + return value + + except TypeError: + # this type conversion will raise an exception if it's illegal + return self.ptype(value) + + def set(self, name, instance, value): + instance.__dict__[name] = value + + def sim_decl(self, name): + return 'Param<%s> %s;' % (self.ptype.__name__, name) + + def sim_init(self, name): + if self.default == None: + return 'INIT_PARAM(%s, "%s")' % (name, self.desc) + else: + return 'INIT_PARAM_DFLT(%s, "%s", %s)' % \ + (name, self.desc, str(self.default)) + +class _ParamProxy(object): + def __init__(self, type): + self.ptype = type + + # E.g., Param.Int(5, "number of widgets") + def __call__(self, *args, **kwargs): + return _Param(self.ptype, *args, **kwargs) + + def __getattr__(self, attr): + if attr == '__bases__': + raise AttributeError, '' + cls = type(self) + return cls(attr) + + def __setattr__(self, attr, value): + if attr != 'ptype': + raise AttributeError, \ + 'Attribute %s not available in %s' % (attr, self.__class__) + super(_ParamProxy, self).__setattr__(attr, value) + + +Param = _ParamProxy(None) + +# Vector-valued parameter description. Just like Param, except that +# the value is a vector (list) of the specified type instead of a +# single value. +class _VectorParam(_Param): + def __init__(self, type, *args, **kwargs): + _Param.__init__(self, type, *args, **kwargs) + + def valid(self, value): + if value == None: + return True + + ptype = eval(self.ptype) + if issequence(value): + for val in value: + if not isinstance(val, Proxy): + ptype._convert(val) + elif not isinstance(value, Proxy): + ptype._convert(value) + + # Convert assigned value to appropriate type. If the RHS is not a + # list or tuple, it generates a single-element list. + def convert(self, value): + if value == None: + return [] + + ptype = eval(self.ptype) + if issequence(value): + # list: coerce each element into new list + return [ ptype._convert(v) for v in value ] + else: + # singleton: coerce & wrap in a list + return ptype._convert(value) + + def string(self, value): + ptype = eval(self.ptype) + if issequence(value): + return ' '.join([ ptype._string(v) for v in value]) + else: + return ptype._string(value) + + def sim_decl(self, name): + return 'VectorParam<%s> %s;' % (self.ptype.__name__, name) + +class _VectorParamProxy(_ParamProxy): + # E.g., VectorParam.Int(5, "number of widgets") + def __call__(self, *args, **kwargs): + return _VectorParam(self.ptype, *args, **kwargs) + +VectorParam = _VectorParamProxy(None) + +##################################################################### +# +# Parameter Types +# +# Though native Python types could be used to specify parameter types +# (the 'ptype' field of the Param and VectorParam classes), it's more +# flexible to define our own set of types. This gives us more control +# over how Python expressions are converted to values (via the +# __init__() constructor) and how these values are printed out (via +# the __str__() conversion method). Eventually we'll need these types +# to correspond to distinct C++ types as well. +# +##################################################################### +# Integer parameter type. +class _CheckedInt(object): + def _convert(cls, value): + t = type(value) + if t == bool: + return int(value) + + if t != int and t != long and t != float and t != str: + raise TypeError, 'Integer parameter of invalid type %s' % t + + if t == str or t == float: + value = long(value) + + if not cls._min <= value <= cls._max: + raise TypeError, 'Integer parameter out of bounds %d < %d < %d' % \ + (cls._min, value, cls._max) + + return value + _convert = classmethod(_convert) + + def _string(cls, value): + return str(value) + _string = classmethod(_string) + +class CheckedInt(type): + def __new__(cls, name, min, max): + # New class derives from _CheckedInt base with proper bounding + # parameters + dict = { '_name' : name, '_min' : min, '_max' : max } + return type.__new__(cls, name, (_CheckedInt, ), dict) + +class CheckedIntType(CheckedInt): + def __new__(cls, name, size, unsigned): + dict = {} + if unsigned: + min = 0 + max = 2 ** size - 1 + else: + min = -(2 ** (size - 1)) + max = (2 ** (size - 1)) - 1 + + return super(cls, CheckedIntType).__new__(cls, name, min, max) + +Int = CheckedIntType('int', 32, False) +Unsigned = CheckedIntType('unsigned', 32, True) + +Int8 = CheckedIntType('int8_t', 8, False) +UInt8 = CheckedIntType('uint8_t', 8, True) +Int16 = CheckedIntType('int16_t', 16, False) +UInt16 = CheckedIntType('uint16_t', 16, True) +Int32 = CheckedIntType('int32_t', 32, False) +UInt32 = CheckedIntType('uint32_t', 32, True) +Int64 = CheckedIntType('int64_t', 64, False) +UInt64 = CheckedIntType('uint64_t', 64, True) + +Counter = CheckedIntType('Counter', 64, True) +Addr = CheckedIntType('Addr', 64, True) +Tick = CheckedIntType('Tick', 64, True) + +Percent = CheckedInt('int', 0, 100) + +class Pair(object): + def __init__(self, first, second): + self.first = first + self.second = second + +class _Range(object): + def _convert(cls, value): + if not isinstance(value, Pair): + raise TypeError, 'value %s is not a Pair' % value + return Pair(cls._type._convert(value.first), + cls._type._convert(value.second)) + _convert = classmethod(_convert) + + def _string(cls, value): + return '%s:%s' % (cls._type._string(value.first), + cls._type._string(value.second)) + _string = classmethod(_string) + +def RangeSize(start, size): + return Pair(start, start + size - 1) + +class Range(type): + def __new__(cls, type): + dict = { '_name' : 'Range<%s>' + type._name, '_type' : type } + cname = 'Range_' + type.__name__ + return super(cls, Range).__new__(cls, cname, (_Range, ), dict) + +AddrRange = Range(Addr) + +# Boolean parameter type. +class Bool(object): + _name = 'bool' + def _convert(value): + t = type(value) + if t == bool: + return value + + if t == int or t == long: + return bool(value) + + if t == str: + v = value.lower() + if v == "true" or v == "t" or v == "yes" or v == "y": + return True + elif v == "false" or v == "f" or v == "no" or v == "n": + return False + + raise TypeError, 'Bool parameter (%s) of invalid type %s' % (v, t) + _convert = staticmethod(_convert) + + def _string(value): + if value: + return "true" + else: + return "false" + _string = staticmethod(_string) + +# String-valued parameter. +class String(object): + _name = 'string' + + # Constructor. Value must be Python string. + def _convert(cls,value): + if value is None: + return '' + if isinstance(value, str): + return value + + raise TypeError, \ + "String param got value %s %s" % (repr(value), type(value)) + _convert = classmethod(_convert) + + # Generate printable string version. Not too tricky. + def _string(cls, value): + return value + _string = classmethod(_string) + + +def IncEthernetAddr(addr, val = 1): + bytes = map(lambda x: int(x, 16), addr.split(':')) + bytes[5] += val + for i in (5, 4, 3, 2, 1): + val,rem = divmod(bytes[i], 256) + bytes[i] = rem + if val == 0: + break + bytes[i - 1] += val + assert(bytes[0] <= 255) + return ':'.join(map(lambda x: '%02x' % x, bytes)) + +class NextEthernetAddr(object): + __metaclass__ = Singleton + addr = "00:90:00:00:00:01" + + def __init__(self, inc = 1): + self.value = self.addr + self.addr = IncEthernetAddr(self.addr, inc) + +class EthernetAddr(object): + _name = 'EthAddr' + + def _convert(cls, value): + if value == NextEthernetAddr: + return value + + if not isinstance(value, str): + raise TypeError, "expected an ethernet address and didn't get one" + + bytes = value.split(':') + if len(bytes) != 6: + raise TypeError, 'invalid ethernet address %s' % value + + for byte in bytes: + if not 0 <= int(byte) <= 256: + raise TypeError, 'invalid ethernet address %s' % value + + return value + _convert = classmethod(_convert) + + def _string(cls, value): + if value == NextEthernetAddr: + value = value().value + return value + _string = classmethod(_string) + +# Special class for NULL pointers. Note the special check in +# make_param_value() above that lets these be assigned where a +# SimObject is required. +# only one copy of a particular node +class NullSimObject(object): + __metaclass__ = Singleton + _name = 'NULL' + + def __call__(cls): + return cls + + def _sim_code(cls): + pass + _sim_code = classmethod(_sim_code) + + def _instantiate(self, parent = None, path = ''): + pass + + def _convert(cls, value): + if value == Nxone: + return + + if isinstance(value, cls): + return value + + raise TypeError, 'object %s %s of the wrong type, should be %s' % \ + (repr(value), type(value), cls) + _convert = classmethod(_convert) + + def _string(): + return 'NULL' + _string = staticmethod(_string) + +# The only instance you'll ever need... +Null = NULL = NullSimObject() + +# Enumerated types are a little more complex. The user specifies the +# type as Enum(foo) where foo is either a list or dictionary of +# alternatives (typically strings, but not necessarily so). (In the +# long run, the integer value of the parameter will be the list index +# or the corresponding dictionary value. For now, since we only check +# that the alternative is valid and then spit it into a .ini file, +# there's not much point in using the dictionary.) + +# What Enum() must do is generate a new type encapsulating the +# provided list/dictionary so that specific values of the parameter +# can be instances of that type. We define two hidden internal +# classes (_ListEnum and _DictEnum) to serve as base classes, then +# derive the new type from the appropriate base class on the fly. + + +# Base class for Enum types. +class _Enum(object): + def _convert(self, value): + if value not in self.map: + raise TypeError, "Enum param got bad value '%s' (not in %s)" \ + % (value, self.map) + return value + _convert = classmethod(_convert) + + # Generate printable string version of value. + def _string(self, value): + return str(value) + _string = classmethod(_string) + +# Enum metaclass... calling Enum(foo) generates a new type (class) +# that derives from _ListEnum or _DictEnum as appropriate. +class Enum(type): + # counter to generate unique names for generated classes + counter = 1 + + def __new__(cls, *args): + if len(args) > 1: + enum_map = args + else: + enum_map = args[0] + + if isinstance(enum_map, dict): + map = enum_map + elif issequence(enum_map): + map = {} + for idx,val in enumerate(enum_map): + map[val] = idx + else: + raise TypeError, "Enum map must be list or dict (got %s)" % map + + classname = "Enum%04d" % Enum.counter + Enum.counter += 1 + + # New class derives from _Enum base, and gets a 'map' + # attribute containing the specified list or dict. + return type.__new__(cls, classname, (_Enum, ), { 'map': map }) + + +# +# "Constants"... handy aliases for various values. +# + +# For compatibility with C++ bool constants. +false = False +true = True + +# Some memory range specifications use this as a default upper bound. +MAX_ADDR = Addr._max + +# For power-of-two sizing, e.g. 64*K gives an integer value 65536. +K = 1024 +M = K*K +G = K*M + +##################################################################### + +# Munge an arbitrary Python code string to get it to execute (mostly +# dealing with indentation). Stolen from isa_parser.py... see +# comments there for a more detailed description. +#def fixPythonIndentation(s): +# # get rid of blank lines first +# s = re.sub(r'(?m)^\s*\n', '', s); +# if (s != '' and re.match(r'[ \t]', s[0])): +# s = 'if 1:\n' + s +# return s + +# Hook to generate C++ parameter code. +def gen_sim_code(file): + for objname in sim_object_list: + print >> file, eval("%s._sim_code()" % objname) + +# The final hook to generate .ini files. Called from configuration +# script once config is built. +def instantiate(root): + if not issubclass(root, Root): + raise AttributeError, 'Can only instantiate the Root of the tree' + + instance = root.instantiate('root') + instance.fixup() + instance.display() + +from objects import * + |