python: Fix unproxing of VectorParams
[gem5.git] / src / python / m5 / SimObject.py
index 9f4c2c15533fa09ac422b7fd9a224b503f07e54c..baeef73d9fb4ee8f020d0bec5cce86f5dec7efc4 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2012 ARM Limited
+# Copyright (c) 2017 ARM Limited
 # All rights reserved.
 #
 # The license below extends only to copyright in the software and shall
 # Authors: Steve Reinhardt
 #          Nathan Binkert
 #          Andreas Hansson
+#          Andreas Sandberg
 
 import sys
 from types import FunctionType, MethodType, ModuleType
+from functools import wraps
+import inspect
 
 import m5
 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
@@ -114,18 +118,294 @@ def public_value(key, value):
                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' : bool,
-                      'cxx_class' : str,
-                      'cxx_type' : str,
-                      'cxx_header' : str,
-                      'type' : str,
-                      'cxx_bases' : list }
+    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' : FunctionType }
 
@@ -142,7 +422,13 @@ class MetaSimObject(type):
         # filtered in __init__.
         cls_dict = {}
         value_dict = {}
+        cxx_exports = []
         for key,val in dict.items():
+            try:
+                cxx_exports.append(getattr(val, "__pybind"))
+            except AttributeError:
+                pass
+
             if public_value(key, val):
                 cls_dict[key] = val
             else:
@@ -152,6 +438,12 @@ class MetaSimObject(type):
             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:
@@ -185,7 +477,8 @@ class MetaSimObject(type):
             if isinstance(c, MetaSimObject):
                 bTotal += 1
             if bTotal > 1:
-                raise TypeError, "SimObjects do not support multiple inheritance"
+                raise TypeError, \
+                      "SimObjects do not support multiple inheritance"
 
         base = bases[0]
 
@@ -218,20 +511,6 @@ class MetaSimObject(type):
                 noCxxHeader = True
                 warn("No header file specified for SimObject: %s", name)
 
-        # Export methods are automatically inherited via C++, so we
-        # don't want the method declarations to get inherited on the
-        # python side (and thus end up getting repeated in the wrapped
-        # versions of derived classes).  The code below basicallly
-        # suppresses inheritance by substituting in the base (null)
-        # versions of these methods unless a different version is
-        # explicitly supplied.
-        for method_name in ('export_methods', 'export_method_cxx_predecls',
-                            'export_method_swig_predecls'):
-            if method_name not in cls.__dict__:
-                base_method = getattr(MetaSimObject, method_name)
-                m = MethodType(base_method, cls, MetaSimObject)
-                setattr(cls, method_name, m)
-
         # Now process the _value_dict items.  They could be defining
         # new (or overriding existing) parameters or ports, setting
         # class keywords (e.g., 'abstract'), or setting parameter
@@ -382,115 +661,95 @@ class MetaSimObject(type):
     def cxx_predecls(cls, code):
         code('#include "params/$cls.hh"')
 
-    # See ParamValue.swig_predecls for description.
-    def swig_predecls(cls, code):
-        code('%import "python/m5/internal/param_$cls.i"')
-
-    # Hook for exporting additional C++ methods to Python via SWIG.
-    # Default is none, override using @classmethod in class definition.
-    def export_methods(cls, code):
-        pass
-
-    # Generate the code needed as a prerequisite for the C++ methods
-    # exported via export_methods() to be compiled in the _wrap.cc
-    # file.  Typically generates one or more #include statements.  If
-    # any methods are exported, typically at least the C++ header
-    # declaring the relevant SimObject class must be included.
-    def export_method_cxx_predecls(cls, code):
-        pass
-
-    # Generate the code needed as a prerequisite for the C++ methods
-    # exported via export_methods() to be processed by SWIG.
-    # Typically generates one or more %include or %import statements.
-    # If any methods are exported, typically at least the C++ header
-    # declaring the relevant SimObject class must be included.
-    def export_method_swig_predecls(cls, code):
-        pass
+    def pybind_predecls(cls, code):
+        code('#include "${{cls.cxx_header}}"')
 
-    # Generate the declaration for this object for wrapping with SWIG.
-    # Generates code that goes into a SWIG .i file.  Called from
-    # src/SConscript.
-    def swig_decl(cls, code):
+    def pybind_decl(cls, code):
         class_path = cls.cxx_class.split('::')
-        classname = class_path[-1]
-        namespaces = class_path[:-1]
+        namespaces, classname = class_path[:-1], class_path[-1]
+        py_class_name = '_COLONS_'.join(class_path) if namespaces else \
+                        classname;
 
         # 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('%module(package="m5.internal") param_$cls')
-        code()
-        code('%{')
-        code('#include "sim/sim_object.hh"')
-        code('#include "params/$cls.hh"')
-        for param in params:
-            param.cxx_predecls(code)
-        code('#include "${{cls.cxx_header}}"')
-        cls.export_method_cxx_predecls(code)
-        code('''\
-/**
-  * This is a workaround for bug in swig. Prior to gcc 4.6.1 the STL
-  * headers like vector, string, etc. used to automatically pull in
-  * the cstddef header but starting with gcc 4.6.1 they no longer do.
-  * This leads to swig generated a file that does not compile so we
-  * explicitly include cstddef. Additionally, including version 2.0.4,
-  * swig uses ptrdiff_t without the std:: namespace prefix which is
-  * required with gcc 4.6.1. We explicitly provide access to it.
-  */
-#include <cstddef>
-using std::ptrdiff_t;
-''')
-        code('%}')
-        code()
+        code('''#include "pybind11/pybind11.h"
+#include "pybind11/stl.h"
 
-        for param in params:
-            param.swig_predecls(code)
-        cls.export_method_swig_predecls(code)
+#include "params/$cls.hh"
+#include "python/pybind11/core.hh"
+#include "sim/init.hh"
+#include "sim/sim_object.hh"
 
-        code()
-        if cls._base:
-            code('%import "python/m5/internal/param_${{cls._base}}.i"')
-        code()
+#include "${{cls.cxx_header}}"
 
-        for ns in namespaces:
-            code('namespace $ns {')
+''')
 
-        if namespaces:
-            code('// avoid name conflicts')
-            sep_string = '_COLONS_'
-            flat_name = sep_string.join(class_path)
-            code('%rename($flat_name) $classname;')
+        for param in params:
+            param.pybind_predecls(code)
 
-        code()
-        code('// stop swig from creating/wrapping default ctor/dtor')
-        code('%nodefault $classname;')
-        code('class $classname')
+        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:
-            bases = [ cls._base.cxx_class ] + cls.cxx_bases
+            code('py::class_<${cls}Params, ${{cls._base.type}}Params, ' \
+                 'std::unique_ptr<${{cls}}Params, py::nodelete>>(' \
+                 'm, "${cls}Params")')
         else:
-            bases = cls.cxx_bases
-        base_first = True
-        for base in bases:
-            if base_first:
-                code('    : public ${{base}}')
-                base_first = False
-            else:
-                code('    , public ${{base}}')
-
-        code('{')
-        code('  public:')
-        cls.export_methods(code)
-        code('};')
+            code('py::class_<${cls}Params, ' \
+                 'std::unique_ptr<${cls}Params, py::nodelete>>(' \
+                 'm, "${cls}Params")')
 
-        for ns in reversed(namespaces):
-            code('} // namespace $ns')
+        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('%include "params/$cls.hh"')
+        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
@@ -499,8 +758,8 @@ using std::ptrdiff_t;
         # 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
         try:
             ptypes = [p.ptype for p in params]
@@ -517,6 +776,14 @@ using std::ptrdiff_t;
 
 ''')
 
+
+        # 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:
+            code('''#include <string>''')
+
         # A forward class declaration is sufficient since we are just
         # declaring a pointer.
         for ns in class_path[:-1]:
@@ -526,18 +793,6 @@ using std::ptrdiff_t;
             code('} // namespace $ns')
         code()
 
-        # 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:
-            code('''
-#ifndef PY_VERSION
-struct PyObject;
-#endif
-
-#include <string>
-''')
         for param in params:
             param.cxx_predecls(code)
         for port in ports.itervalues():
@@ -569,8 +824,8 @@ struct PyObject;
     virtual ~SimObjectParams() {}
 
     std::string name;
-    PyObject *pyobj;
             ''')
+
         for param in params:
             param.cxx_decl(code)
         for port in ports.itervalues():
@@ -583,6 +838,11 @@ struct PyObject;
         code('#endif // __PARAMS__${cls}__')
         return code
 
+    # 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
@@ -593,6 +853,47 @@ struct PyObject;
 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.
@@ -629,27 +930,26 @@ class SimObject(object):
     cxx_bases = [ "Drainable", "Serializable" ]
     eventq_index = Param.UInt32(Parent.eventq_index, "Event Queue Index")
 
-    @classmethod
-    def export_method_swig_predecls(cls, code):
-        code('''
-%include <std_string.i>
-
-%import "python/swig/drain.i"
-%import "python/swig/serialize.i"
-''')
-
-    @classmethod
-    def export_methods(cls, code):
-        code('''
-    void init();
-    void loadState(Checkpoint *cp);
-    void initState();
-    void regStats();
-    void resetStats();
-    void regProbePoints();
-    void regProbeListeners();
-    void startup();
-''')
+    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
@@ -691,7 +991,8 @@ class SimObject(object):
 
                     if keys in self._hr_values\
                        and keys in self._values\
-                       and not isinstance(self._values[keys], m5.proxy.BaseProxy):
+                       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,
@@ -809,9 +1110,7 @@ class SimObject(object):
 
         # If the attribute exists on the C++ object, transparently
         # forward the reference there.  This is typically used for
-        # SWIG-wrapped methods such as init(), regStats(),
-        # resetStats(), startup(), drain(), and
-        # resume().
+        # methods exported to Python (e.g., init(), and startup())
         if self._ccObject and hasattr(self._ccObject, attr):
             return getattr(self._ccObject, attr)
 
@@ -893,8 +1192,9 @@ class SimObject(object):
         self._parent = parent
         self._name = name
 
-    # Return parent object of this SimObject, not implemented by SimObjectVector
-    # because the elements in a SimObjectVector may not share the same parent
+    # 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
 
@@ -947,6 +1247,9 @@ class SimObject(object):
     def path(self):
         if not self._parent:
             return '<orphan %s>' % self.__class__
+        elif isinstance(self._parent, MetaSimObject):
+            return str(self.__class__)
+
         ppath = self._parent.path()
         if ppath == 'root':
             return self._name
@@ -983,7 +1286,8 @@ 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 and %s' % (found_obj.path, match_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
 
@@ -1011,7 +1315,9 @@ class SimObject(object):
                 match_obj = self._values[pname]
                 if not isproxy(match_obj) and not isNullPointer(match_obj):
                     all[match_obj] = True
-        return all.keys(), 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
@@ -1104,7 +1410,6 @@ class SimObject(object):
 
         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()
@@ -1120,8 +1425,14 @@ class SimObject(object):
                 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)
 
@@ -1159,7 +1470,10 @@ class SimObject(object):
 
     def descendants(self):
         yield self
-        for child in self._children.itervalues():
+        # 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
 
@@ -1174,7 +1488,9 @@ class SimObject(object):
     # Create C++ port connections corresponding to the connections in
     # _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()
 
 # Function to provide to C++ so it can look up instances based on paths
@@ -1237,4 +1553,9 @@ def clear():
 # __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',
+]