python: Fix unproxing of VectorParams
[gem5.git] / src / python / m5 / SimObject.py
index 64f4ec5af24b251741d7f3ce88b71f7112d6c988..baeef73d9fb4ee8f020d0bec5cce86f5dec7efc4 100644 (file)
@@ -1,4 +1,18 @@
+# Copyright (c) 2017 ARM Limited
+# All rights reserved.
+#
+# The license below extends only to copyright in the software and shall
+# not be construed as granting a license to any other intellectual
+# property including but not limited to intellectual property relating
+# to a hardware implementation of the functionality of the software
+# licensed hereunder.  You may use the software subject to the license
+# terms below provided that you ensure that this notice is replicated
+# unmodified and in its entirety in all distributions of the software,
+# modified or unmodified, in source code or in binary form.
+#
 # Copyright (c) 2004-2006 The Regents of The University of Michigan
+# Copyright (c) 2010-20013 Advanced Micro Devices, Inc.
+# Copyright (c) 2013 Mark D. Hill and David A. Wood
 # All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without
 #
 # Authors: Steve Reinhardt
 #          Nathan Binkert
+#          Andreas Hansson
+#          Andreas Sandberg
 
-import sys, types
+import sys
+from types import FunctionType, MethodType, ModuleType
+from functools import wraps
+import inspect
 
-import proxy
 import m5
-from util import *
-
-# These utility functions have to come first because they're
-# referenced in params.py... otherwise they won't be defined when we
-# import params below, and the recursive import of this file from
-# params.py will not find these names.
-def isSimObject(value):
-    return isinstance(value, SimObject)
-
-def isSimObjectClass(value):
-    return issubclass(value, SimObject)
-
-def isSimObjectSequence(value):
-    if not isinstance(value, (list, tuple)) or len(value) == 0:
-        return False
-
-    for val in value:
-        if not isNullPointer(val) and not isSimObject(val):
-            return False
-
-    return True
-
-def isSimObjectOrSequence(value):
-    return isSimObject(value) or isSimObjectSequence(value)
+from m5.util import *
+from m5.util.pybind import *
 
 # Have to import params up top since Param is referenced on initial
 # load (when SimObject class references Param to create a class
 # variable, the 'name' param)...
-from params import *
+from m5.params import *
 # There are a few things we need that aren't in params.__all__ since
 # normal users don't need them
-from params import ParamDesc, VectorParamDesc, isNullPointer, SimObjVector
+from m5.params import ParamDesc, VectorParamDesc, \
+     isNullPointer, SimObjectVector, Port
 
-noDot = False
-try:
-    import pydot
-except:
-    noDot = True
+from m5.proxy import *
+from m5.proxy import isproxy
 
 #####################################################################
 #
@@ -116,22 +110,304 @@ allClasses = {}
 # dict to look up SimObjects based on path
 instanceDict = {}
 
+# Did any of the SimObjects lack a header file?
+noCxxHeader = False
+
+def public_value(key, value):
+    return key.startswith('_') or \
+               isinstance(value, (FunctionType, MethodType, ModuleType,
+                                  classmethod, type))
+
+def createCxxConfigDirectoryEntryFile(code, name, simobj, is_header):
+    entry_class = 'CxxConfigDirectoryEntry_%s' % name
+    param_class = '%sCxxConfigParams' % name
+
+    code('#include "params/%s.hh"' % name)
+
+    if not is_header:
+        for param in simobj._params.values():
+            if isSimObjectClass(param.ptype):
+                code('#include "%s"' % param.ptype._value_dict['cxx_header'])
+                code('#include "params/%s.hh"' % param.ptype.__name__)
+            else:
+                param.ptype.cxx_ini_predecls(code)
+
+    if is_header:
+        member_prefix = ''
+        end_of_decl = ';'
+        code('#include "sim/cxx_config.hh"')
+        code()
+        code('class ${param_class} : public CxxConfigParams,'
+            ' public ${name}Params')
+        code('{')
+        code('  private:')
+        code.indent()
+        code('class DirectoryEntry : public CxxConfigDirectoryEntry')
+        code('{')
+        code('  public:')
+        code.indent()
+        code('DirectoryEntry();');
+        code()
+        code('CxxConfigParams *makeParamsObject() const')
+        code('{ return new ${param_class}; }')
+        code.dedent()
+        code('};')
+        code()
+        code.dedent()
+        code('  public:')
+        code.indent()
+    else:
+        member_prefix = '%s::' % param_class
+        end_of_decl = ''
+        code('#include "%s"' % simobj._value_dict['cxx_header'])
+        code('#include "base/str.hh"')
+        code('#include "cxx_config/${name}.hh"')
+
+        if simobj._ports.values() != []:
+            code('#include "mem/mem_object.hh"')
+            code('#include "mem/port.hh"')
+
+        code()
+        code('${member_prefix}DirectoryEntry::DirectoryEntry()');
+        code('{')
+
+        def cxx_bool(b):
+            return 'true' if b else 'false'
+
+        code.indent()
+        for param in simobj._params.values():
+            is_vector = isinstance(param, m5.params.VectorParamDesc)
+            is_simobj = issubclass(param.ptype, m5.SimObject.SimObject)
+
+            code('parameters["%s"] = new ParamDesc("%s", %s, %s);' %
+                (param.name, param.name, cxx_bool(is_vector),
+                cxx_bool(is_simobj)));
+
+        for port in simobj._ports.values():
+            is_vector = isinstance(port, m5.params.VectorPort)
+            is_master = port.role == 'MASTER'
+
+            code('ports["%s"] = new PortDesc("%s", %s, %s);' %
+                (port.name, port.name, cxx_bool(is_vector),
+                cxx_bool(is_master)))
+
+        code.dedent()
+        code('}')
+        code()
+
+    code('bool ${member_prefix}setSimObject(const std::string &name,')
+    code('    SimObject *simObject)${end_of_decl}')
+
+    if not is_header:
+        code('{')
+        code.indent()
+        code('bool ret = true;')
+        code()
+        code('if (false) {')
+        for param in simobj._params.values():
+            is_vector = isinstance(param, m5.params.VectorParamDesc)
+            is_simobj = issubclass(param.ptype, m5.SimObject.SimObject)
+
+            if is_simobj and not is_vector:
+                code('} else if (name == "${{param.name}}") {')
+                code.indent()
+                code('this->${{param.name}} = '
+                    'dynamic_cast<${{param.ptype.cxx_type}}>(simObject);')
+                code('if (simObject && !this->${{param.name}})')
+                code('   ret = false;')
+                code.dedent()
+        code('} else {')
+        code('    ret = false;')
+        code('}')
+        code()
+        code('return ret;')
+        code.dedent()
+        code('}')
+
+    code()
+    code('bool ${member_prefix}setSimObjectVector('
+        'const std::string &name,')
+    code('    const std::vector<SimObject *> &simObjects)${end_of_decl}')
+
+    if not is_header:
+        code('{')
+        code.indent()
+        code('bool ret = true;')
+        code()
+        code('if (false) {')
+        for param in simobj._params.values():
+            is_vector = isinstance(param, m5.params.VectorParamDesc)
+            is_simobj = issubclass(param.ptype, m5.SimObject.SimObject)
+
+            if is_simobj and is_vector:
+                code('} else if (name == "${{param.name}}") {')
+                code.indent()
+                code('this->${{param.name}}.clear();')
+                code('for (auto i = simObjects.begin(); '
+                    'ret && i != simObjects.end(); i ++)')
+                code('{')
+                code.indent()
+                code('${{param.ptype.cxx_type}} object = '
+                    'dynamic_cast<${{param.ptype.cxx_type}}>(*i);')
+                code('if (*i && !object)')
+                code('    ret = false;')
+                code('else')
+                code('    this->${{param.name}}.push_back(object);')
+                code.dedent()
+                code('}')
+                code.dedent()
+        code('} else {')
+        code('    ret = false;')
+        code('}')
+        code()
+        code('return ret;')
+        code.dedent()
+        code('}')
+
+    code()
+    code('void ${member_prefix}setName(const std::string &name_)'
+        '${end_of_decl}')
+
+    if not is_header:
+        code('{')
+        code.indent()
+        code('this->name = name_;')
+        code.dedent()
+        code('}')
+
+    if is_header:
+        code('const std::string &${member_prefix}getName()')
+        code('{ return this->name; }')
+
+    code()
+    code('bool ${member_prefix}setParam(const std::string &name,')
+    code('    const std::string &value, const Flags flags)${end_of_decl}')
+
+    if not is_header:
+        code('{')
+        code.indent()
+        code('bool ret = true;')
+        code()
+        code('if (false) {')
+        for param in simobj._params.values():
+            is_vector = isinstance(param, m5.params.VectorParamDesc)
+            is_simobj = issubclass(param.ptype, m5.SimObject.SimObject)
+
+            if not is_simobj and not is_vector:
+                code('} else if (name == "${{param.name}}") {')
+                code.indent()
+                param.ptype.cxx_ini_parse(code,
+                    'value', 'this->%s' % param.name, 'ret =')
+                code.dedent()
+        code('} else {')
+        code('    ret = false;')
+        code('}')
+        code()
+        code('return ret;')
+        code.dedent()
+        code('}')
+
+    code()
+    code('bool ${member_prefix}setParamVector('
+        'const std::string &name,')
+    code('    const std::vector<std::string> &values,')
+    code('    const Flags flags)${end_of_decl}')
+
+    if not is_header:
+        code('{')
+        code.indent()
+        code('bool ret = true;')
+        code()
+        code('if (false) {')
+        for param in simobj._params.values():
+            is_vector = isinstance(param, m5.params.VectorParamDesc)
+            is_simobj = issubclass(param.ptype, m5.SimObject.SimObject)
+
+            if not is_simobj and is_vector:
+                code('} else if (name == "${{param.name}}") {')
+                code.indent()
+                code('${{param.name}}.clear();')
+                code('for (auto i = values.begin(); '
+                    'ret && i != values.end(); i ++)')
+                code('{')
+                code.indent()
+                code('${{param.ptype.cxx_type}} elem;')
+                param.ptype.cxx_ini_parse(code,
+                    '*i', 'elem', 'ret =')
+                code('if (ret)')
+                code('    this->${{param.name}}.push_back(elem);')
+                code.dedent()
+                code('}')
+                code.dedent()
+        code('} else {')
+        code('    ret = false;')
+        code('}')
+        code()
+        code('return ret;')
+        code.dedent()
+        code('}')
+
+    code()
+    code('bool ${member_prefix}setPortConnectionCount('
+        'const std::string &name,')
+    code('    unsigned int count)${end_of_decl}')
+
+    if not is_header:
+        code('{')
+        code.indent()
+        code('bool ret = true;')
+        code()
+        code('if (false)')
+        code('    ;')
+        for port in simobj._ports.values():
+            code('else if (name == "${{port.name}}")')
+            code('    this->port_${{port.name}}_connection_count = count;')
+        code('else')
+        code('    ret = false;')
+        code()
+        code('return ret;')
+        code.dedent()
+        code('}')
+
+    code()
+    code('SimObject *${member_prefix}simObjectCreate()${end_of_decl}')
+
+    if not is_header:
+        code('{')
+        if hasattr(simobj, 'abstract') and simobj.abstract:
+            code('    return NULL;')
+        else:
+            code('    return this->create();')
+        code('}')
+
+    if is_header:
+        code()
+        code('static CxxConfigDirectoryEntry'
+            ' *${member_prefix}makeDirectoryEntry()')
+        code('{ return new DirectoryEntry; }')
+
+    if is_header:
+        code.dedent()
+        code('};')
+
 # The metaclass for SimObject.  This class controls how new classes
 # that derive from SimObject 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 MetaSimObject(type):
     # Attributes that can be set only at initialization time
-    init_keywords = { 'abstract' : types.BooleanType,
-                      'cxx_namespace' : types.StringType,
-                      'cxx_class' : types.StringType,
-                      'cxx_type' : types.StringType,
-                      'cxx_predecls' : types.ListType,
-                      'swig_objdecls' : types.ListType,
-                      'swig_predecls' : types.ListType,
-                      'type' : types.StringType }
+    init_keywords = {
+        'abstract' : bool,
+        'cxx_class' : str,
+        'cxx_type' : str,
+        'cxx_header' : str,
+        'type' : str,
+        'cxx_bases' : list,
+        'cxx_exports' : list,
+        'cxx_param_exports' : list,
+    }
     # Attributes that can be set any time
-    keywords = { 'check' : types.FunctionType }
+    keywords = { 'check' : FunctionType }
 
     # __new__ is called before __init__, and is where the statements
     # in the body of the class definition get loaded into the class's
@@ -139,22 +415,35 @@ class MetaSimObject(type):
     # and only allow "private" attributes to be passed to the base
     # __new__ (starting with underscore).
     def __new__(mcls, name, bases, dict):
-        assert name not in allClasses
+        assert name not in allClasses, "SimObject %s already present" % name
 
         # Copy "private" attributes, functions, and classes to the
         # official dict.  Everything else goes in _init_dict to be
         # filtered in __init__.
         cls_dict = {}
         value_dict = {}
+        cxx_exports = []
         for key,val in dict.items():
-            if key.startswith('_') or isinstance(val, (types.FunctionType,
-                                                       types.TypeType)):
+            try:
+                cxx_exports.append(getattr(val, "__pybind"))
+            except AttributeError:
+                pass
+
+            if public_value(key, val):
                 cls_dict[key] = val
             else:
                 # must be a param/port setting
                 value_dict[key] = val
         if 'abstract' not in value_dict:
             value_dict['abstract'] = False
+        if 'cxx_bases' not in value_dict:
+            value_dict['cxx_bases'] = []
+        if 'cxx_exports' not in value_dict:
+            value_dict['cxx_exports'] = cxx_exports
+        else:
+            value_dict['cxx_exports'] += cxx_exports
+        if 'cxx_param_exports' not in value_dict:
+            value_dict['cxx_param_exports'] = []
         cls_dict['_value_dict'] = value_dict
         cls = super(MetaSimObject, mcls).__new__(mcls, name, bases, cls_dict)
         if 'type' in value_dict:
@@ -175,13 +464,21 @@ class MetaSimObject(type):
 
         # class or instance attributes
         cls._values = multidict()   # param values
+        cls._hr_values = multidict() # human readable param values
+        cls._children = multidict() # SimObject children
         cls._port_refs = multidict() # port ref objects
         cls._instantiated = False # really instantiated, cloned, or subclassed
 
-        # We don't support multiple inheritance.  If you want to, you
-        # must fix multidict to deal with it properly.
-        if len(bases) > 1:
-            raise TypeError, "SimObjects do not support multiple inheritance"
+        # We don't support multiple inheritance of sim objects.  If you want
+        # to, you must fix multidict to deal with it properly. Non sim-objects
+        # are ok, though
+        bTotal = 0
+        for c in bases:
+            if isinstance(c, MetaSimObject):
+                bTotal += 1
+            if bTotal > 1:
+                raise TypeError, \
+                      "SimObjects do not support multiple inheritance"
 
         base = bases[0]
 
@@ -190,46 +487,29 @@ class MetaSimObject(type):
         # the following is not true is when we define the SimObject
         # class itself (in which case the multidicts have no parent).
         if isinstance(base, MetaSimObject):
+            cls._base = base
             cls._params.parent = base._params
             cls._ports.parent = base._ports
             cls._values.parent = base._values
+            cls._hr_values.parent = base._hr_values
+            cls._children.parent = base._children
             cls._port_refs.parent = base._port_refs
             # mark base as having been subclassed
             base._instantiated = True
+        else:
+            cls._base = None
 
         # default keyword values
         if 'type' in cls._value_dict:
-            _type = cls._value_dict['type']
             if 'cxx_class' not in cls._value_dict:
-                cls._value_dict['cxx_class'] = _type
-
-            namespace = cls._value_dict.get('cxx_namespace', None)
-
-            _cxx_class = cls._value_dict['cxx_class']
-            if 'cxx_type' not in cls._value_dict:
-                t = _cxx_class + '*'
-                if namespace:
-                    t = '%s::%s' % (namespace, t)
-                cls._value_dict['cxx_type'] = t
-            if 'cxx_predecls' not in cls._value_dict:
-                # A forward class declaration is sufficient since we are
-                # just declaring a pointer.
-                decl = 'class %s;' % _cxx_class
-                if namespace:
-                    namespaces = namespace.split('::')
-                    namespaces.reverse()
-                    for namespace in namespaces:
-                        decl = 'namespace %s { %s }' % (namespace, decl)
-                cls._value_dict['cxx_predecls'] = [decl]
-
-            if 'swig_predecls' not in cls._value_dict:
-                # A forward class declaration is sufficient since we are
-                # just declaring a pointer.
-                cls._value_dict['swig_predecls'] = \
-                    cls._value_dict['cxx_predecls']
-
-        if 'swig_objdecls' not in cls._value_dict:
-            cls._value_dict['swig_objdecls'] = []
+                cls._value_dict['cxx_class'] = cls._value_dict['type']
+
+            cls._value_dict['cxx_type'] = '%s *' % cls._value_dict['cxx_class']
+
+            if 'cxx_header' not in cls._value_dict:
+                global noCxxHeader
+                noCxxHeader = True
+                warn("No header file specified for SimObject: %s", name)
 
         # Now process the _value_dict items.  They could be defining
         # new (or overriding existing) parameters or ports, setting
@@ -259,7 +539,7 @@ class MetaSimObject(type):
         if not isinstance(val, kwtype):
             raise TypeError, 'keyword %s has bad type %s (expecting %s)' % \
                   (keyword, type(val), kwtype)
-        if isinstance(val, types.FunctionType):
+        if isinstance(val, FunctionType):
             val = classmethod(val)
         type.__setattr__(cls, keyword, val)
 
@@ -274,20 +554,38 @@ class MetaSimObject(type):
     def _set_param(cls, name, value, param):
         assert(param.name == name)
         try:
-            cls._values[name] = param.convert(value)
+            hr_value = value
+            value = param.convert(value)
         except Exception, e:
             msg = "%s\nError setting param %s.%s to %s\n" % \
                   (e, cls.__name__, name, value)
             e.args = (msg, )
             raise
+        cls._values[name] = value
+        # if param value is a SimObject, make it a child too, so that
+        # it gets cloned properly when the class is instantiated
+        if isSimObjectOrVector(value) and not value.has_parent():
+            cls._add_cls_child(name, value)
+        # update human-readable values of the param if it has a literal
+        # value and is not an object or proxy.
+        if not (isSimObjectOrVector(value) or\
+                isinstance(value, m5.proxy.BaseProxy)):
+            cls._hr_values[name] = hr_value
+
+    def _add_cls_child(cls, name, child):
+        # It's a little funky to have a class as a parent, but these
+        # objects should never be instantiated (only cloned, which
+        # clears the parent pointer), and this makes it clear that the
+        # object is not an orphan and can provide better error
+        # messages.
+        child.set_parent(cls, name)
+        cls._children[name] = child
 
     def _new_port(cls, name, port):
         # each port should be uniquely assigned to one variable
         assert(not hasattr(port, 'name'))
         port.name = name
         cls._ports[name] = port
-        if hasattr(port, 'default'):
-            cls._cls_get_port_ref(name).connect(port.default)
 
     # same as _get_port_ref, effectively, but for classes
     def _cls_get_port_ref(cls, attr):
@@ -304,7 +602,7 @@ class MetaSimObject(type):
     # instance of class cls).
     def __setattr__(cls, attr, value):
         # normal processing for private attributes
-        if attr.startswith('_'):
+        if public_value(attr, value):
             type.__setattr__(cls, attr, value)
             return
 
@@ -330,11 +628,7 @@ class MetaSimObject(type):
 
         if isSimObjectOrSequence(value):
             # If RHS is a SimObject, it's an implicit child assignment.
-            # Classes don't have children, so we just put this object
-            # in _values; later, each instance will do a 'setattr(self,
-            # attr, _values[attr])' in SimObject.__init__ which will
-            # add this object as a child.
-            cls._values[attr] = value
+            cls._add_cls_child(attr, coerceSimObjectOrVector(value))
             return
 
         # no valid assignment... raise exception
@@ -342,30 +636,131 @@ class MetaSimObject(type):
               "Class %s has no parameter \'%s\'" % (cls.__name__, attr)
 
     def __getattr__(cls, attr):
+        if attr == 'cxx_class_path':
+            return cls.cxx_class.split('::')
+
+        if attr == 'cxx_class_name':
+            return cls.cxx_class_path[-1]
+
+        if attr == 'cxx_namespaces':
+            return cls.cxx_class_path[:-1]
+
         if cls._values.has_key(attr):
             return cls._values[attr]
 
+        if cls._children.has_key(attr):
+            return cls._children[attr]
+
         raise AttributeError, \
               "object '%s' has no attribute '%s'" % (cls.__name__, attr)
 
     def __str__(cls):
         return cls.__name__
 
-    def get_base(cls):
-        if str(cls) == 'SimObject':
-            return None
+    # See ParamValue.cxx_predecls for description.
+    def cxx_predecls(cls, code):
+        code('#include "params/$cls.hh"')
 
-        return  cls.__bases__[0].type
+    def pybind_predecls(cls, code):
+        code('#include "${{cls.cxx_header}}"')
 
-    def cxx_decl(cls):
-        code = "#ifndef __PARAMS__%s\n" % cls
-        code += "#define __PARAMS__%s\n\n" % cls
+    def pybind_decl(cls, code):
+        class_path = cls.cxx_class.split('::')
+        namespaces, classname = class_path[:-1], class_path[-1]
+        py_class_name = '_COLONS_'.join(class_path) if namespaces else \
+                        classname;
 
-        # The 'dict' attribute restricts us to the params declared in
+        # The 'local' attribute restricts us to the params declared in
         # the object itself, not including inherited params (which
         # will also be inherited from the base class's param struct
-        # here).
-        params = cls._params.local.values()
+        # here). Sort the params based on their key
+        params = map(lambda (k, v): v, sorted(cls._params.local.items()))
+        ports = cls._ports.local
+
+        code('''#include "pybind11/pybind11.h"
+#include "pybind11/stl.h"
+
+#include "params/$cls.hh"
+#include "python/pybind11/core.hh"
+#include "sim/init.hh"
+#include "sim/sim_object.hh"
+
+#include "${{cls.cxx_header}}"
+
+''')
+
+        for param in params:
+            param.pybind_predecls(code)
+
+        code('''namespace py = pybind11;
+
+static void
+module_init(py::module &m_internal)
+{
+    py::module m = m_internal.def_submodule("param_${cls}");
+''')
+        code.indent()
+        if cls._base:
+            code('py::class_<${cls}Params, ${{cls._base.type}}Params, ' \
+                 'std::unique_ptr<${{cls}}Params, py::nodelete>>(' \
+                 'm, "${cls}Params")')
+        else:
+            code('py::class_<${cls}Params, ' \
+                 'std::unique_ptr<${cls}Params, py::nodelete>>(' \
+                 'm, "${cls}Params")')
+
+        code.indent()
+        if not hasattr(cls, 'abstract') or not cls.abstract:
+            code('.def(py::init<>())')
+            code('.def("create", &${cls}Params::create)')
+
+        param_exports = cls.cxx_param_exports + [
+            PyBindProperty(k)
+            for k, v in sorted(cls._params.local.items())
+        ] + [
+            PyBindProperty("port_%s_connection_count" % port.name)
+            for port in ports.itervalues()
+        ]
+        for exp in param_exports:
+            exp.export(code, "%sParams" % cls)
+
+        code(';')
+        code()
+        code.dedent()
+
+        bases = [ cls._base.cxx_class ] + cls.cxx_bases if cls._base else \
+                cls.cxx_bases
+        if bases:
+            base_str = ", ".join(bases)
+            code('py::class_<${{cls.cxx_class}}, ${base_str}, ' \
+                 'std::unique_ptr<${{cls.cxx_class}}, py::nodelete>>(' \
+                 'm, "${py_class_name}")')
+        else:
+            code('py::class_<${{cls.cxx_class}}, ' \
+                 'std::unique_ptr<${{cls.cxx_class}}, py::nodelete>>(' \
+                 'm, "${py_class_name}")')
+        code.indent()
+        for exp in cls.cxx_exports:
+            exp.export(code, cls.cxx_class)
+        code(';')
+        code.dedent()
+        code()
+        code.dedent()
+        code('}')
+        code()
+        code('static EmbeddedPyBind embed_obj("${0}", module_init, "${1}");',
+             cls, cls._base.type if cls._base else "")
+
+
+    # Generate the C++ declaration (.hh file) for this SimObject's
+    # param struct.  Called from src/SConscript.
+    def cxx_param_decl(cls, code):
+        # The 'local' attribute restricts us to the params declared in
+        # the object itself, not including inherited params (which
+        # will also be inherited from the base class's param struct
+        # here). Sort the params based on their key
+        params = map(lambda (k, v): v, sorted(cls._params.local.items()))
+        ports = cls._ports.local
         try:
             ptypes = [p.ptype for p in params]
         except:
@@ -373,112 +768,154 @@ class MetaSimObject(type):
             print params
             raise
 
-        # get a list of lists of predeclaration lines
-        predecls = []
-        predecls.extend(cls.cxx_predecls)
-        for p in params:
-            predecls.extend(p.cxx_predecls())
-        # remove redundant lines
-        predecls2 = []
-        for pd in predecls:
-            if pd not in predecls2:
-                predecls2.append(pd)
-        predecls2.sort()
-        code += "\n".join(predecls2)
-        code += "\n\n";
-
-        base = cls.get_base()
-        if base:
-            code += '#include "params/%s.hh"\n\n' % base
+        class_path = cls._value_dict['cxx_class'].split('::')
 
-        for ptype in ptypes:
-            if issubclass(ptype, Enum):
-                code += '#include "enums/%s.hh"\n' % ptype.__name__
-                code += "\n\n"
+        code('''\
+#ifndef __PARAMS__${cls}__
+#define __PARAMS__${cls}__
 
-        code += cls.cxx_struct(base, params)
+''')
 
-        # close #ifndef __PARAMS__* guard
-        code += "\n#endif\n"
-        return code
 
-    def cxx_struct(cls, base, params):
+        # The base SimObject has a couple of params that get
+        # automatically set from Python without being declared through
+        # the normal Param mechanism; we slip them in here (needed
+        # predecls now, actual declarations below)
         if cls == SimObject:
-            return '#include "sim/sim_object_params.hh"\n'
+            code('''#include <string>''')
+
+        # A forward class declaration is sufficient since we are just
+        # declaring a pointer.
+        for ns in class_path[:-1]:
+            code('namespace $ns {')
+        code('class $0;', class_path[-1])
+        for ns in reversed(class_path[:-1]):
+            code('} // namespace $ns')
+        code()
+
+        for param in params:
+            param.cxx_predecls(code)
+        for port in ports.itervalues():
+            port.cxx_predecls(code)
+        code()
+
+        if cls._base:
+            code('#include "params/${{cls._base.type}}.hh"')
+            code()
+
+        for ptype in ptypes:
+            if issubclass(ptype, Enum):
+                code('#include "enums/${{ptype.__name__}}.hh"')
+                code()
 
         # now generate the actual param struct
-        code = "struct %sParams" % cls
-        if base:
-            code += " : public %sParams" % base
-        code += "\n{\n"
+        code("struct ${cls}Params")
+        if cls._base:
+            code("    : public ${{cls._base.type}}Params")
+        code("{")
         if not hasattr(cls, 'abstract') or not cls.abstract:
             if 'type' in cls.__dict__:
-                code += "    %s create();\n" % cls.cxx_type
-        decls = [p.cxx_decl() for p in params]
-        decls.sort()
-        code += "".join(["    %s\n" % d for d in decls])
-        code += "};\n"
+                code("    ${{cls.cxx_type}} create();")
 
-        return code
+        code.indent()
+        if cls == SimObject:
+            code('''
+    SimObjectParams() {}
+    virtual ~SimObjectParams() {}
 
-    def cxx_type_decl(cls):
-        base = cls.get_base()
-        code = ''
+    std::string name;
+            ''')
 
-        if base:
-            code += '#include "%s_type.h"\n' % base
+        for param in params:
+            param.cxx_decl(code)
+        for port in ports.itervalues():
+            port.cxx_decl(code)
 
-        # now generate dummy code for inheritance
-        code += "struct %s" % cls.cxx_class
-        if base:
-            code += " : public %s" % base.cxx_class
-        code += "\n{};\n"
+        code.dedent()
+        code('};')
 
+        code()
+        code('#endif // __PARAMS__${cls}__')
         return code
 
-    def swig_decl(cls):
-        base = cls.get_base()
-
-        code = '%%module %s\n' % cls
-
-        code += '%{\n'
-        code += '#include "params/%s.hh"\n' % cls
-        code += '%}\n\n'
-
-        # The 'dict' attribute restricts us to the params declared in
-        # the object itself, not including inherited params (which
-        # will also be inherited from the base class's param struct
-        # here).
-        params = cls._params.local.values()
-        ptypes = [p.ptype for p in params]
-
-        # get a list of lists of predeclaration lines
-        predecls = []
-        predecls.extend([ p.swig_predecls() for p in params ])
-        # flatten
-        predecls = reduce(lambda x,y:x+y, predecls, [])
-        # remove redundant lines
-        predecls2 = []
-        for pd in predecls:
-            if pd not in predecls2:
-                predecls2.append(pd)
-        predecls2.sort()
-        code += "\n".join(predecls2)
-        code += "\n\n";
-
-        if base:
-            code += '%%import "params/%s.i"\n\n' % base
-
-        for ptype in ptypes:
-            if issubclass(ptype, Enum):
-                code += '%%import "enums/%s.hh"\n' % ptype.__name__
-                code += "\n\n"
-
-        code += '%%import "params/%s_type.hh"\n\n' % cls
-        code += '%%include "params/%s.hh"\n\n' % cls
-
+    # Generate the C++ declaration/definition files for this SimObject's
+    # param struct to allow C++ initialisation
+    def cxx_config_param_file(cls, code, is_header):
+        createCxxConfigDirectoryEntryFile(code, cls.__name__, cls, is_header)
         return code
 
+# This *temporary* definition is required to support calls from the
+# SimObject class definition to the MetaSimObject methods (in
+# particular _set_param, which gets called for parameters with default
+# values defined on the SimObject class itself).  It will get
+# overridden by the permanent definition (which requires that
+# SimObject be defined) lower in this file.
+def isSimObjectOrVector(value):
+    return False
+
+def cxxMethod(*args, **kwargs):
+    """Decorator to export C++ functions to Python"""
+
+    def decorate(func):
+        name = func.func_name
+        override = kwargs.get("override", False)
+        cxx_name = kwargs.get("cxx_name", name)
+
+        args, varargs, keywords, defaults = inspect.getargspec(func)
+        if varargs or keywords:
+            raise ValueError("Wrapped methods must not contain variable " \
+                             "arguments")
+
+        # Create tuples of (argument, default)
+        if defaults:
+            args = args[:-len(defaults)] + zip(args[-len(defaults):], defaults)
+        # Don't include self in the argument list to PyBind
+        args = args[1:]
+
+
+        @wraps(func)
+        def cxx_call(self, *args, **kwargs):
+            ccobj = self.getCCObject()
+            return getattr(ccobj, name)(*args, **kwargs)
+
+        @wraps(func)
+        def py_call(self, *args, **kwargs):
+            return self.func(*args, **kwargs)
+
+        f = py_call if override else cxx_call
+        f.__pybind = PyBindMethod(name, cxx_name=cxx_name, args=args)
+
+        return f
+
+    if len(args) == 0:
+        return decorate
+    elif len(args) == 1 and len(kwargs) == 0:
+        return decorate(*args)
+    else:
+        raise TypeError("One argument and no kwargs, or only kwargs expected")
+
+# This class holds information about each simobject parameter
+# that should be displayed on the command line for use in the
+# configuration system.
+class ParamInfo(object):
+  def __init__(self, type, desc, type_str, example, default_val, access_str):
+    self.type = type
+    self.desc = desc
+    self.type_str = type_str
+    self.example_str = example
+    self.default_val = default_val
+    # The string representation used to access this param through python.
+    # The method to access this parameter presented on the command line may
+    # be different, so this needs to be stored for later use.
+    self.access_str = access_str
+    self.created = True
+
+  # Make it so we can only set attributes at initialization time
+  # and effectively make this a const object.
+  def __setattr__(self, name, value):
+    if not "created" in self.__dict__:
+      self.__dict__[name] = value
+
 # The SimObject 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).
@@ -489,7 +926,89 @@ class SimObject(object):
     type = 'SimObject'
     abstract = True
 
-    swig_objdecls = [ '%include "python/swig/sim_object.i"' ]
+    cxx_header = "sim/sim_object.hh"
+    cxx_bases = [ "Drainable", "Serializable" ]
+    eventq_index = Param.UInt32(Parent.eventq_index, "Event Queue Index")
+
+    cxx_exports = [
+        PyBindMethod("init"),
+        PyBindMethod("initState"),
+        PyBindMethod("memInvalidate"),
+        PyBindMethod("memWriteback"),
+        PyBindMethod("regStats"),
+        PyBindMethod("resetStats"),
+        PyBindMethod("regProbePoints"),
+        PyBindMethod("regProbeListeners"),
+        PyBindMethod("startup"),
+    ]
+
+    cxx_param_exports = [
+        PyBindProperty("name"),
+    ]
+
+    @cxxMethod
+    def loadState(self, cp):
+        """Load SimObject state from a checkpoint"""
+        pass
+
+    # Returns a dict of all the option strings that can be
+    # generated as command line options for this simobject instance
+    # by tracing all reachable params in the top level instance and
+    # any children it contains.
+    def enumerateParams(self, flags_dict = {},
+                        cmd_line_str = "", access_str = ""):
+        if hasattr(self, "_paramEnumed"):
+            print "Cycle detected enumerating params"
+        else:
+            self._paramEnumed = True
+            # Scan the children first to pick up all the objects in this SimObj
+            for keys in self._children:
+                child = self._children[keys]
+                next_cmdline_str = cmd_line_str + keys
+                next_access_str = access_str + keys
+                if not isSimObjectVector(child):
+                    next_cmdline_str = next_cmdline_str + "."
+                    next_access_str = next_access_str + "."
+                flags_dict = child.enumerateParams(flags_dict,
+                                                   next_cmdline_str,
+                                                   next_access_str)
+
+            # Go through the simple params in the simobject in this level
+            # of the simobject hierarchy and save information about the
+            # parameter to be used for generating and processing command line
+            # options to the simulator to set these parameters.
+            for keys,values in self._params.items():
+                if values.isCmdLineSettable():
+                    type_str = ''
+                    ex_str = values.example_str()
+                    ptype = None
+                    if isinstance(values, VectorParamDesc):
+                        type_str = 'Vector_%s' % values.ptype_str
+                        ptype = values
+                    else:
+                        type_str = '%s' % values.ptype_str
+                        ptype = values.ptype
+
+                    if keys in self._hr_values\
+                       and keys in self._values\
+                       and not isinstance(self._values[keys],
+                                          m5.proxy.BaseProxy):
+                        cmd_str = cmd_line_str + keys
+                        acc_str = access_str + keys
+                        flags_dict[cmd_str] = ParamInfo(ptype,
+                                    self._params[keys].desc, type_str, ex_str,
+                                    values.pretty_print(self._hr_values[keys]),
+                                    acc_str)
+                    elif not keys in self._hr_values\
+                         and not keys in self._values:
+                        # Empty param
+                        cmd_str = cmd_line_str + keys
+                        acc_str = access_str + keys
+                        flags_dict[cmd_str] = ParamInfo(ptype,
+                                    self._params[keys].desc,
+                                    type_str, ex_str, '', acc_str)
+
+        return flags_dict
 
     # Initialize new instance.  For objects with SimObject-valued
     # children, we need to recursively clone the classes represented
@@ -514,20 +1033,30 @@ class SimObject(object):
 
         # initialize required attributes
         self._parent = None
-        self._children = {}
+        self._name = None
         self._ccObject = None  # pointer to C++ object
         self._ccParams = None
         self._instantiated = False # really "cloned"
 
+        # Clone children specified at class level.  No need for a
+        # multidict here since we will be cloning everything.
+        # Do children before parameter values so that children that
+        # are also param values get cloned properly.
+        self._children = {}
+        for key,val in ancestor._children.iteritems():
+            self.add_child(key, val(_memo=memo_dict))
+
         # Inherit parameter values from class using multidict so
-        # individual value settings can be overridden.
+        # individual value settings can be overridden but we still
+        # inherit late changes to non-overridden class values.
         self._values = multidict(ancestor._values)
+        self._hr_values = multidict(ancestor._hr_values)
         # clone SimObject-valued parameters
         for key,val in ancestor._values.iteritems():
-            if isSimObject(val):
-                setattr(self, key, val(_memo=memo_dict))
-            elif isSimObjectSequence(val) and len(val):
-                setattr(self, key, [ v(_memo=memo_dict) for v in val ])
+            val = tryAsSimObjectOrVector(val)
+            if val is not None:
+                self._values[key] = val(_memo=memo_dict)
+
         # clone port references.  no need to use a multidict here
         # since we will be creating new references for all ports.
         self._port_refs = {}
@@ -564,7 +1093,7 @@ class SimObject(object):
         # via __setattr__.  There is only ever one reference
         # object per port, but we create them lazily here.
         ref = self._port_refs.get(attr)
-        if not ref:
+        if ref == None:
             ref = self._ports[attr].makeRef(self)
             self._port_refs[attr] = ref
         return ref
@@ -576,9 +1105,24 @@ class SimObject(object):
         if self._values.has_key(attr):
             return self._values[attr]
 
-        raise AttributeError, "object '%s' has no attribute '%s'" \
+        if self._children.has_key(attr):
+            return self._children[attr]
+
+        # If the attribute exists on the C++ object, transparently
+        # forward the reference there.  This is typically used for
+        # methods exported to Python (e.g., init(), and startup())
+        if self._ccObject and hasattr(self._ccObject, attr):
+            return getattr(self._ccObject, attr)
+
+        err_string = "object '%s' has no attribute '%s'" \
               % (self.__class__.__name__, attr)
 
+        if not self._ccObject:
+            err_string += "\n  (C++ object is not yet constructed," \
+                          " so wrapped C++ methods are unavailable.)"
+
+        raise AttributeError, err_string
+
     # Set attribute (called on foo.attr = value when foo is an
     # instance of class cls).
     def __setattr__(self, attr, value):
@@ -592,26 +1136,32 @@ class SimObject(object):
             self._get_port_ref(attr).connect(value)
             return
 
-        if isSimObjectOrSequence(value) and self._instantiated:
-            raise RuntimeError, \
-                  "cannot set SimObject parameter '%s' after\n" \
-                  "    instance been cloned %s" % (attr, `self`)
-
-        # must be SimObject param
         param = self._params.get(attr)
         if param:
             try:
+                hr_value = value
                 value = param.convert(value)
             except Exception, e:
                 msg = "%s\nError setting param %s.%s to %s\n" % \
                       (e, self.__class__.__name__, attr, value)
                 e.args = (msg, )
                 raise
-            self._set_child(attr, value)
+            self._values[attr] = value
+            # implicitly parent unparented objects assigned as params
+            if isSimObjectOrVector(value) and not value.has_parent():
+                self.add_child(attr, value)
+            # set the human-readable value dict if this is a param
+            # with a literal value and is not being set as an object
+            # or proxy.
+            if not (isSimObjectOrVector(value) or\
+                    isinstance(value, m5.proxy.BaseProxy)):
+                self._hr_values[attr] = hr_value
+
             return
 
+        # if RHS is a SimObject, it's an implicit child assignment
         if isSimObjectOrSequence(value):
-            self._set_child(attr, value)
+            self.add_child(attr, value)
             return
 
         # no valid assignment... raise exception
@@ -624,47 +1174,82 @@ class SimObject(object):
     def __getitem__(self, key):
         if key == 0:
             return self
-        raise TypeError, "Non-zero index '%s' to SimObject" % key
+        raise IndexError, "Non-zero index '%s' to SimObject" % key
 
-    # clear out children with given name, even if it's a vector
-    def clear_child(self, name):
-        if not self._children.has_key(name):
-            return
-        child = self._children[name]
-        if isinstance(child, SimObjVector):
-            for i in xrange(len(child)):
-                del self._children["s%d" % (name, i)]
-        del self._children[name]
+    # this hack allows us to iterate over a SimObject that may
+    # not be a vector, so we can call a loop over it and get just one
+    # element.
+    def __len__(self):
+        return 1
 
-    def add_child(self, name, value):
-        self._children[name] = value
+    # Also implemented by SimObjectVector
+    def clear_parent(self, old_parent):
+        assert self._parent is old_parent
+        self._parent = None
 
-    def _maybe_set_parent(self, parent, name):
-        if not self._parent:
-            self._parent = parent
-            self._name = name
-            parent.add_child(name, self)
+    # Also implemented by SimObjectVector
+    def set_parent(self, parent, name):
+        self._parent = parent
+        self._name = name
 
-    def _set_child(self, attr, value):
-        # if RHS is a SimObject, it's an implicit child assignment
-        # clear out old child with this name, if any
-        self.clear_child(attr)
-
-        if isSimObject(value):
-            value._maybe_set_parent(self, attr)
-        elif isSimObjectSequence(value):
-            value = SimObjVector(value)
-            if len(value) == 1:
-                value[0]._maybe_set_parent(self, attr)
-            else:
-                for i,v in enumerate(value):
-                    v._maybe_set_parent(self, "%s%d" % (attr, i))
+    # Return parent object of this SimObject, not implemented by
+    # SimObjectVector because the elements in a SimObjectVector may not share
+    # the same parent
+    def get_parent(self):
+        return self._parent
 
-        self._values[attr] = value
+    # Also implemented by SimObjectVector
+    def get_name(self):
+        return self._name
+
+    # Also implemented by SimObjectVector
+    def has_parent(self):
+        return self._parent is not None
+
+    # clear out child with given name. This code is not likely to be exercised.
+    # See comment in add_child.
+    def clear_child(self, name):
+        child = self._children[name]
+        child.clear_parent(self)
+        del self._children[name]
+
+    # Add a new child to this object.
+    def add_child(self, name, child):
+        child = coerceSimObjectOrVector(child)
+        if child.has_parent():
+            warn("add_child('%s'): child '%s' already has parent", name,
+                child.get_name())
+        if self._children.has_key(name):
+            # This code path had an undiscovered bug that would make it fail
+            # at runtime. It had been here for a long time and was only
+            # exposed by a buggy script. Changes here will probably not be
+            # exercised without specialized testing.
+            self.clear_child(name)
+        child.set_parent(self, name)
+        self._children[name] = child
+
+    # Take SimObject-valued parameters that haven't been explicitly
+    # assigned as children and make them children of the object that
+    # they were assigned to as a parameter value.  This guarantees
+    # that when we instantiate all the parameter objects we're still
+    # inside the configuration hierarchy.
+    def adoptOrphanParams(self):
+        for key,val in self._values.iteritems():
+            if not isSimObjectVector(val) and isSimObjectSequence(val):
+                # need to convert raw SimObject sequences to
+                # SimObjectVector class so we can call has_parent()
+                val = SimObjectVector(val)
+                self._values[key] = val
+            if isSimObjectOrVector(val) and not val.has_parent():
+                warn("%s adopting orphan SimObject param '%s'", self, key)
+                self.add_child(key, val)
 
     def path(self):
         if not self._parent:
-            return 'root'
+            return '<orphan %s>' % self.__class__
+        elif isinstance(self._parent, MetaSimObject):
+            return str(self.__class__)
+
         ppath = self._parent.path()
         if ppath == 'root':
             return self._name
@@ -673,6 +1258,9 @@ class SimObject(object):
     def __str__(self):
         return self.path()
 
+    def config_value(self):
+        return self.path()
+
     def ini_str(self):
         return self.path()
 
@@ -682,7 +1270,11 @@ class SimObject(object):
 
         found_obj = None
         for child in self._children.itervalues():
-            if isinstance(child, ptype):
+            visited = False
+            if hasattr(child, '_visited'):
+              visited = getattr(child, '_visited')
+
+            if isinstance(child, ptype) and not visited:
                 if found_obj != None and child != found_obj:
                     raise AttributeError, \
                           'parent.any matched more than one: %s %s' % \
@@ -694,17 +1286,46 @@ class SimObject(object):
                 match_obj = self._values[pname]
                 if found_obj != None and found_obj != match_obj:
                     raise AttributeError, \
-                          'parent.any matched more than one: %s' % obj.path
+                          'parent.any matched more than one: %s and %s' % \
+                          (found_obj.path, match_obj.path)
                 found_obj = match_obj
         return found_obj, found_obj != None
 
+    def find_all(self, ptype):
+        all = {}
+        # search children
+        for child in self._children.itervalues():
+            # a child could be a list, so ensure we visit each item
+            if isinstance(child, list):
+                children = child
+            else:
+                children = [child]
+
+            for child in children:
+                if isinstance(child, ptype) and not isproxy(child) and \
+                        not isNullPointer(child):
+                    all[child] = True
+                if isSimObject(child):
+                    # also add results from the child itself
+                    child_all, done = child.find_all(ptype)
+                    all.update(dict(zip(child_all, [done] * len(child_all))))
+        # search param space
+        for pname,pdesc in self._params.iteritems():
+            if issubclass(pdesc.ptype, ptype):
+                match_obj = self._values[pname]
+                if not isproxy(match_obj) and not isNullPointer(match_obj):
+                    all[match_obj] = True
+        # Also make sure to sort the keys based on the objects' path to
+        # ensure that the order is the same on all hosts
+        return sorted(all.keys(), key = lambda o: o.path()), True
+
     def unproxy(self, base):
         return self
 
-    def unproxy_all(self):
+    def unproxyParams(self):
         for param in self._params.iterkeys():
             value = self._values.get(param)
-            if value != None and proxy.isproxy(value):
+            if value != None and isproxy(value):
                 try:
                     value = value.unproxy(self)
                 except:
@@ -722,12 +1343,6 @@ class SimObject(object):
             if port != None:
                 port.unproxy(self)
 
-        # Unproxy children in sorted order for determinism also.
-        child_names = self._children.keys()
-        child_names.sort()
-        for child in child_names:
-            self._children[child].unproxy_all()
-
     def print_ini(self, ini_file):
         print >>ini_file, '[' + self.path() + ']'       # .ini section header
 
@@ -736,38 +1351,65 @@ class SimObject(object):
         if hasattr(self, 'type'):
             print >>ini_file, 'type=%s' % self.type
 
-        child_names = self._children.keys()
-        child_names.sort()
-        if len(child_names):
-            print >>ini_file, 'children=%s' % ' '.join(child_names)
+        if len(self._children.keys()):
+            print >>ini_file, 'children=%s' % \
+                  ' '.join(self._children[n].get_name() \
+                  for n in sorted(self._children.keys()))
 
-        param_names = self._params.keys()
-        param_names.sort()
-        for param in param_names:
+        for param in sorted(self._params.keys()):
             value = self._values.get(param)
             if value != None:
                 print >>ini_file, '%s=%s' % (param,
                                              self._values[param].ini_str())
 
-        port_names = self._ports.keys()
-        port_names.sort()
-        for port_name in port_names:
+        for port_name in sorted(self._ports.keys()):
             port = self._port_refs.get(port_name, None)
             if port != None:
                 print >>ini_file, '%s=%s' % (port_name, port.ini_str())
 
         print >>ini_file        # blank line between objects
 
-        for child in child_names:
-            self._children[child].print_ini(ini_file)
+    # generate a tree of dictionaries expressing all the parameters in the
+    # instantiated system for use by scripts that want to do power, thermal
+    # visualization, and other similar tasks
+    def get_config_as_dict(self):
+        d = attrdict()
+        if hasattr(self, 'type'):
+            d.type = self.type
+        if hasattr(self, 'cxx_class'):
+            d.cxx_class = self.cxx_class
+        # Add the name and path of this object to be able to link to
+        # the stats
+        d.name = self.get_name()
+        d.path = self.path()
+
+        for param in sorted(self._params.keys()):
+            value = self._values.get(param)
+            if value != None:
+                d[param] = value.config_value()
+
+        for n in sorted(self._children.keys()):
+            child = self._children[n]
+            # Use the name of the attribute (and not get_name()) as
+            # the key in the JSON dictionary to capture the hierarchy
+            # in the Python code that assembled this system
+            d[n] = child.get_config_as_dict()
+
+        for port_name in sorted(self._ports.keys()):
+            port = self._port_refs.get(port_name, None)
+            if port != None:
+                # Represent each port with a dictionary containing the
+                # prominent attributes
+                d[port_name] = port.get_config_as_dict()
+
+        return d
 
     def getCCParams(self):
         if self._ccParams:
             return self._ccParams
 
-        cc_params_struct = getattr(m5.objects.params, '%sParams' % self.type)
+        cc_params_struct = getattr(m5.internal.params, '%sParams' % self.type)
         cc_params = cc_params_struct()
-        cc_params.pyobj = self
         cc_params.name = str(self)
 
         param_names = self._params.keys()
@@ -775,15 +1417,22 @@ class SimObject(object):
         for param in param_names:
             value = self._values.get(param)
             if value is None:
-                continue
+                fatal("%s.%s without default or user set value",
+                      self.path(), param)
 
             value = value.getValue()
             if isinstance(self._params[param], VectorParamDesc):
                 assert isinstance(value, list)
                 vec = getattr(cc_params, param)
                 assert not len(vec)
-                for v in value:
-                    vec.append(v)
+                # Some types are exposed as opaque types. They support
+                # the append operation unlike the automatically
+                # wrapped types.
+                if isinstance(vec, list):
+                    setattr(cc_params, param, list(value))
+                else:
+                    for v in value:
+                        getattr(cc_params, param).append(v)
             else:
                 setattr(cc_params, param, value)
 
@@ -792,7 +1441,11 @@ class SimObject(object):
         for port_name in port_names:
             port = self._port_refs.get(port_name, None)
             if port != None:
-                setattr(cc_params, port_name, port)
+                port_count = len(port)
+            else:
+                port_count = 0
+            setattr(cc_params, 'port_' + port_name + '_connection_count',
+                    port_count)
         self._ccParams = cc_params
         return self._ccParams
 
@@ -801,117 +1454,108 @@ class SimObject(object):
     # children.
     def getCCObject(self):
         if not self._ccObject:
-            # Cycles in the configuration heirarchy are not supported. This
+            # Make sure this object is in the configuration hierarchy
+            if not self._parent and not isRoot(self):
+                raise RuntimeError, "Attempt to instantiate orphan node"
+            # Cycles in the configuration hierarchy are not supported. This
             # will catch the resulting recursion and stop.
             self._ccObject = -1
-            params = self.getCCParams()
-            self._ccObject = params.create()
+            if not self.abstract:
+                params = self.getCCParams()
+                self._ccObject = params.create()
         elif self._ccObject == -1:
-            raise RuntimeError, "%s: Cycle found in configuration heirarchy." \
+            raise RuntimeError, "%s: Cycle found in configuration hierarchy." \
                   % self.path()
         return self._ccObject
 
-    # Call C++ to create C++ object corresponding to this object and
-    # (recursively) all its children
+    def descendants(self):
+        yield self
+        # The order of the dict is implementation dependent, so sort
+        # it based on the key (name) to ensure the order is the same
+        # on all hosts
+        for (name, child) in sorted(self._children.iteritems()):
+            for obj in child.descendants():
+                yield obj
+
+    # Call C++ to create C++ object corresponding to this object
     def createCCObject(self):
         self.getCCParams()
         self.getCCObject() # force creation
-        for child in self._children.itervalues():
-            child.createCCObject()
 
     def getValue(self):
         return self.getCCObject()
 
     # Create C++ port connections corresponding to the connections in
-    # _port_refs (& recursively for all children)
+    # _port_refs
     def connectPorts(self):
-        for portRef in self._port_refs.itervalues():
+        # Sort the ports based on their attribute name to ensure the
+        # order is the same on all hosts
+        for (attr, portRef) in sorted(self._port_refs.iteritems()):
             portRef.ccConnect()
-        for child in self._children.itervalues():
-            child.connectPorts()
-
-    def startDrain(self, drain_event, recursive):
-        count = 0
-        if isinstance(self, SimObject):
-            count += self._ccObject.drain(drain_event)
-        if recursive:
-            for child in self._children.itervalues():
-                count += child.startDrain(drain_event, True)
-        return count
-
-    def resume(self):
-        if isinstance(self, SimObject):
-            self._ccObject.resume()
-        for child in self._children.itervalues():
-            child.resume()
 
-    def getMemoryMode(self):
-        if not isinstance(self, m5.objects.System):
-            return None
+# Function to provide to C++ so it can look up instances based on paths
+def resolveSimObject(name):
+    obj = instanceDict[name]
+    return obj.getCCObject()
 
-        return self._ccObject.getMemoryMode()
+def isSimObject(value):
+    return isinstance(value, SimObject)
 
-    def changeTiming(self, mode):
-        if isinstance(self, m5.objects.System):
-            # i don't know if there's a better way to do this - calling
-            # setMemoryMode directly from self._ccObject results in calling
-            # SimObject::setMemoryMode, not the System::setMemoryMode
-            self._ccObject.setMemoryMode(mode)
-        for child in self._children.itervalues():
-            child.changeTiming(mode)
-
-    def takeOverFrom(self, old_cpu):
-        self._ccObject.takeOverFrom(old_cpu._ccObject)
-
-    # generate output file for 'dot' to display as a pretty graph.
-    # this code is currently broken.
-    def outputDot(self, dot):
-        label = "{%s|" % self.path
-        if isSimObject(self.realtype):
-            label +=  '%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).
-            for c in self.children:
-                dot.add_edge(pydot.Edge(self.path,c.path, style="bold"))
-
-        simobjs = []
-        for param in self.params:
-            try:
-                if param.value is None:
-                    raise AttributeError, 'Parameter with no value'
+def isSimObjectClass(value):
+    return issubclass(value, SimObject)
 
-                value = param.value
-                string = param.string(value)
-            except Exception, e:
-                msg = 'exception in %s:%s\n%s' % (self.name, param.name, e)
-                e.args = (msg, )
-                raise
+def isSimObjectVector(value):
+    return isinstance(value, SimObjectVector)
 
-            if isSimObject(param.ptype) and string != "Null":
-                simobjs.append(string)
-            else:
-                label += '%s = %s\\n' % (param.name, string)
+def isSimObjectSequence(value):
+    if not isinstance(value, (list, tuple)) or len(value) == 0:
+        return False
 
-        for so in simobjs:
-            label += "|<%s> %s" % (so, so)
-            dot.add_edge(pydot.Edge("%s:%s" % (self.path, so), so,
-                                    tailport="w"))
-        label += '}'
-        dot.add_node(pydot.Node(self.path,shape="Mrecord",label=label))
+    for val in value:
+        if not isNullPointer(val) and not isSimObject(val):
+            return False
 
-        # recursively dump out children
-        for c in self.children:
-            c.outputDot(dot)
+    return True
 
-# Function to provide to C++ so it can look up instances based on paths
-def resolveSimObject(name):
-    obj = instanceDict[name]
-    return obj.getCCObject()
+def isSimObjectOrSequence(value):
+    return isSimObject(value) or isSimObjectSequence(value)
+
+def isRoot(obj):
+    from m5.objects import Root
+    return obj and obj is Root.getInstance()
+
+def isSimObjectOrVector(value):
+    return isSimObject(value) or isSimObjectVector(value)
+
+def tryAsSimObjectOrVector(value):
+    if isSimObjectOrVector(value):
+        return value
+    if isSimObjectSequence(value):
+        return SimObjectVector(value)
+    return None
+
+def coerceSimObjectOrVector(value):
+    value = tryAsSimObjectOrVector(value)
+    if value is None:
+        raise TypeError, "SimObject or SimObjectVector expected"
+    return value
+
+baseClasses = allClasses.copy()
+baseInstances = instanceDict.copy()
+
+def clear():
+    global allClasses, instanceDict, noCxxHeader
+
+    allClasses = baseClasses.copy()
+    instanceDict = baseInstances.copy()
+    noCxxHeader = False
 
 # __all__ defines the list of symbols that get exported when
 # 'from config import *' is invoked.  Try to keep this reasonably
 # short to avoid polluting other namespaces.
-__all__ = [ 'SimObject' ]
+__all__ = [
+    'SimObject',
+    'cxxMethod',
+    'PyBindMethod',
+    'PyBindProperty',
+]