summaryrefslogtreecommitdiff
path: root/src/sim
diff options
context:
space:
mode:
Diffstat (limited to 'src/sim')
-rw-r--r--src/sim/SConscript4
-rw-r--r--src/sim/cxx_config.cc45
-rw-r--r--src/sim/cxx_config.hh241
-rw-r--r--src/sim/cxx_config_ini.cc104
-rw-r--r--src/sim/cxx_config_ini.hh87
-rw-r--r--src/sim/cxx_manager.cc740
-rw-r--r--src/sim/cxx_manager.hh314
7 files changed, 1535 insertions, 0 deletions
diff --git a/src/sim/SConscript b/src/sim/SConscript
index 7987afa00..7583b53cb 100644
--- a/src/sim/SConscript
+++ b/src/sim/SConscript
@@ -43,6 +43,9 @@ SimObject('SubSystem.py')
Source('arguments.cc')
Source('async.cc')
Source('core.cc')
+Source('cxx_config.cc')
+Source('cxx_manager.cc')
+Source('cxx_config_ini.cc')
Source('debug.cc')
Source('py_interact.cc', skip_no_python=True)
Source('eventq.cc')
@@ -76,6 +79,7 @@ if env['TARGET_ISA'] != 'null':
DebugFlag('Checkpoint')
DebugFlag('Config')
+DebugFlag('CxxConfig')
DebugFlag('Drain')
DebugFlag('Event')
DebugFlag('Fault')
diff --git a/src/sim/cxx_config.cc b/src/sim/cxx_config.cc
new file mode 100644
index 000000000..f99afac8a
--- /dev/null
+++ b/src/sim/cxx_config.cc
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2014 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.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Andrew Bardsley
+ */
+
+#include "sim/cxx_config.hh"
+
+const std::string CxxConfigParams::invalidName = "<invalid>";
+
+/** Directory of all SimObject classes config details */
+std::map<std::string, CxxConfigDirectoryEntry *> cxx_config_directory;
diff --git a/src/sim/cxx_config.hh b/src/sim/cxx_config.hh
new file mode 100644
index 000000000..da2752b4b
--- /dev/null
+++ b/src/sim/cxx_config.hh
@@ -0,0 +1,241 @@
+/*
+ * Copyright (c) 2014 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.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Andrew Bardsley
+ */
+
+/**
+ * @file
+ *
+ * C++-only configuration and instantiation support. This allows a
+ * config to be read back from a .ini and instantiated without
+ * Python. Useful if you want to embed gem5 within a larger system
+ * without carrying the integration cost of the fully-featured
+ * configuration system.
+ *
+ * This file contains definitions needed to store summaries of a
+ * SimObject's parameter structure
+ */
+
+#ifndef __SIM_CXX_CONFIG_HH__
+#define __SIM_CXX_CONFIG_HH__
+
+#include <map>
+#include <string>
+#include <vector>
+
+#include "mem/port.hh"
+#include "params/SimObject.hh"
+#include "sim/sim_object.hh"
+
+class CxxConfigParams;
+
+/** Config details entry for a SimObject. Instances of this class contain
+ * enough configuration layout information to popular a ...Param structure
+ * and build a SimObject from it with the help of the 'set' functions in
+ * each ...Param class */
+class CxxConfigDirectoryEntry
+{
+ public:
+ /* Class to represent parameters and SimObject references within
+ * SimObjects */
+ class ParamDesc
+ {
+ public:
+ const std::string name;
+
+ /* Is this a vector or singleton parameters/SimObject */
+ const bool isVector;
+
+ /** Is this a SimObject, and so is to be set with setSimObject...
+ * or another from-string parameter set with setParam... */
+ const bool isSimObject;
+
+ ParamDesc(const std::string &name_,
+ bool isVector_, bool isSimObject_) :
+ name(name_), isVector(isVector_), isSimObject(isSimObject_)
+ { }
+ };
+
+ /** Similar to ParamDesc to describe ports */
+ class PortDesc
+ {
+ public:
+ const std::string name;
+
+ /* Is this a vector or singleton parameters/SimObject */
+ const bool isVector;
+
+ /** Is this a master or slave port */
+ const bool isMaster;
+
+ PortDesc(const std::string &name_,
+ bool isVector_, bool isMaster_) :
+ name(name_), isVector(isVector_), isMaster(isMaster_)
+ { }
+ };
+
+ /** All parameters (including SimObjects) in order */
+ std::map<std::string, ParamDesc *> parameters;
+
+ /** Ports */
+ std::map<std::string, PortDesc *> ports;
+
+ /** Make a ...Param structure for the SimObject class of this entry */
+ virtual CxxConfigParams *makeParamsObject() const { return NULL; }
+
+ virtual ~CxxConfigDirectoryEntry() { }
+};
+
+/** Base for peer classes of SimObjectParams derived classes with parameter
+ * modifying member functions. C++ configuration will offer objects of
+ * these classes to SimObjects as params rather than SimObjectParams
+ * objects */
+class CxxConfigParams
+{
+ private:
+ static const std::string invalidName;
+
+ public:
+ /** Flags passable to setParam... to smooth over any parsing difference
+ * between different config files */
+ typedef uint32_t FlagsType;
+ typedef ::Flags<FlagsType> Flags;
+
+ /** Example flag */
+ /* static const FlagsType MY_NEW_FLAG = 0x00000001; */
+
+ public:
+ /** Set future object's full path name */
+ virtual void setName(const std::string &name_) { }
+
+ /** Get full path name string */
+ virtual const std::string &getName() { return invalidName; }
+
+ /** Set a SimObject valued parameter with a reference to the given
+ * SimObject. This will return false if the parameter name is not
+ * valid or the object is of the wrong type */
+ virtual bool setSimObject(const std::string &name,
+ SimObject *simObject)
+ { return false; }
+
+ /** As setSimObjectVector but set a whole vector of references */
+ virtual bool setSimObjectVector(const std::string &name,
+ const std::vector<SimObject *> &simObjects)
+ { return false; }
+
+ /** Set a parameter with a value parsed from the given string. The
+ * parsing regime matches the format of .ini config files. Returns
+ * false if the parameter name is not valid or the string cannot be
+ * parsed as the type of the parameter */
+ virtual bool setParam(const std::string &name,
+ const std::string &value, const Flags flags)
+ { return false; }
+
+ /** As setParamVector but for parameters given as vectors pre-separated
+ * into elements */
+ virtual bool setParamVector(const std::string &name,
+ const std::vector<std::string> &values, const Flags flags)
+ { return false; }
+
+ /** Set the number of connections expected for the named port. Returns
+ * false if the port name is not valid */
+ virtual bool setPortConnectionCount(const std::string &name,
+ unsigned int count)
+ { return false; }
+
+ /** Create the associated SimObject */
+ virtual SimObject *simObjectCreate() { return NULL; }
+
+ CxxConfigParams() { }
+
+ virtual ~CxxConfigParams() { }
+};
+
+/** Config file wrapper providing a common interface to CxxConfigManager */
+class CxxConfigFileBase
+{
+ public:
+ CxxConfigFileBase() { }
+ virtual ~CxxConfigFileBase() { }
+
+ /** Get a single parameter value as a string returned in value.
+ * For booleans, the function expects "true" or "false" in value.
+ * For NULL SimObjects, it expects "Null" */
+ virtual bool getParam(const std::string &object_name,
+ const std::string &param_name,
+ std::string &value) const = 0;
+
+ /** Get a list/vector parameter */
+ virtual bool getParamVector(const std::string &object_name,
+ const std::string &param_name,
+ std::vector<std::string> &values) const = 0;
+
+ /** Get the peer (connected) ports of the named ports */
+ virtual bool getPortPeers(const std::string &object_name,
+ const std::string &port_name,
+ std::vector<std::string> &peers) const = 0;
+
+ /** Does an object with this path exist? */
+ virtual bool objectExists(const std::string &object_name) const = 0;
+
+ /** Get all SimObjects in the config */
+ virtual void getAllObjectNames(std::vector<std::string> &list) const = 0;
+
+ /** Get the names or paths of all the children SimObjects of this
+ * SimObject. If return_paths is true then full paths are returned.
+ * If false, only the last name component for each object is returned */
+ virtual void getObjectChildren(const std::string &object_name,
+ std::vector<std::string> &children,
+ bool return_paths = false) const = 0;
+
+ /** Load config file */
+ virtual bool load(const std::string &filename) = 0;
+
+ /** Get the flags which should be used to modify parameter parsing
+ * behaviour */
+ virtual CxxConfigParams::Flags getFlags() const { return 0; }
+};
+
+/** Directory of all SimObject classes config details */
+extern std::map<std::string, CxxConfigDirectoryEntry *>
+ cxx_config_directory;
+
+/** Initialise cxx_config_directory. This is defined in the
+ * auto-generated .../cxx_config/init.cc */
+void cxxConfigInit();
+
+#endif // __SIM_CXX_CONFIG_HH__
diff --git a/src/sim/cxx_config_ini.cc b/src/sim/cxx_config_ini.cc
new file mode 100644
index 000000000..3df6d72d4
--- /dev/null
+++ b/src/sim/cxx_config_ini.cc
@@ -0,0 +1,104 @@
+/*
+ * Copyright (c) 2014 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.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Andrew Bardsley
+ */
+
+#include "sim/cxx_config_ini.hh"
+
+bool
+CxxIniFile::getParam(const std::string &object_name,
+ const std::string &param_name,
+ std::string &value) const
+{
+ return iniFile.find(object_name, param_name, value);
+}
+
+bool
+CxxIniFile::getParamVector(const std::string &object_name,
+ const std::string &param_name,
+ std::vector<std::string> &values) const
+{
+ std::string value;
+ bool ret = iniFile.find(object_name, param_name, value);
+
+ if (ret) {
+ std::vector<std::string> sub_object_names;
+
+ tokenize(values, value, ' ', true);
+ }
+
+ return ret;
+}
+
+bool
+CxxIniFile::getPortPeers(const std::string &object_name,
+ const std::string &port_name,
+ std::vector<std::string> &peers) const
+{
+ return getParamVector(object_name, port_name, peers);
+}
+
+bool
+CxxIniFile::objectExists(const std::string &object) const
+{
+ return iniFile.sectionExists(object);
+}
+
+void
+CxxIniFile::getAllObjectNames(std::vector<std::string> &list) const
+{
+ iniFile.getSectionNames(list);
+}
+
+void
+CxxIniFile::getObjectChildren(const std::string &object_name,
+ std::vector<std::string> &children, bool return_paths) const
+{
+ if (!getParamVector(object_name, "children", children))
+ return;
+
+ if (return_paths && object_name != "root") {
+ for (auto i = children.begin(); i != children.end(); ++i)
+ *i = object_name + "." + *i;
+ }
+}
+
+bool
+CxxIniFile::load(const std::string &filename)
+{
+ return iniFile.load(filename);
+}
diff --git a/src/sim/cxx_config_ini.hh b/src/sim/cxx_config_ini.hh
new file mode 100644
index 000000000..9ea61976e
--- /dev/null
+++ b/src/sim/cxx_config_ini.hh
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2014 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.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Andrew Bardsley
+ */
+
+/**
+ * @file
+ *
+ * .ini file reading wrapper for use with CxxConfigManager
+ */
+
+#ifndef __SIM_CXX_CONFIG_INI_HH__
+#define __SIM_CXX_CONFIG_INI_HH__
+
+#include "base/inifile.hh"
+#include "base/str.hh"
+#include "sim/cxx_config.hh"
+
+/** CxxConfigManager interface for using .ini files */
+class CxxIniFile : public CxxConfigFileBase
+{
+ protected:
+ IniFile iniFile;
+
+ public:
+ CxxIniFile() { }
+
+ /* Most of these functions work by mapping 'object' onto 'section' */
+
+ bool getParam(const std::string &object_name,
+ const std::string &param_name,
+ std::string &value) const;
+
+ bool getParamVector(const std::string &object_name,
+ const std::string &param_name,
+ std::vector<std::string> &values) const;
+
+ bool getPortPeers(const std::string &object_name,
+ const std::string &port_name,
+ std::vector<std::string> &peers) const;
+
+ bool objectExists(const std::string &object_name) const;
+
+ void getAllObjectNames(std::vector<std::string> &list) const;
+
+ void getObjectChildren(const std::string &object_name,
+ std::vector<std::string> &children,
+ bool return_paths = false) const;
+
+ bool load(const std::string &filename);
+};
+
+#endif // __SIM_CXX_CONFIG_INI_HH__
diff --git a/src/sim/cxx_manager.cc b/src/sim/cxx_manager.cc
new file mode 100644
index 000000000..7f6a03398
--- /dev/null
+++ b/src/sim/cxx_manager.cc
@@ -0,0 +1,740 @@
+/*
+ * Copyright (c) 2014 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.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Andrew Bardsley
+ */
+
+#include <cstdlib>
+#include <sstream>
+
+#include "base/str.hh"
+#include "debug/CxxConfig.hh"
+#include "mem/mem_object.hh"
+#include "sim/cxx_manager.hh"
+#include "sim/serialize.hh"
+
+CxxConfigManager::CxxConfigManager(CxxConfigFileBase &configFile_) :
+ configFile(configFile_), flags(configFile_.getFlags()),
+ simObjectResolver(*this)
+{
+}
+
+const CxxConfigDirectoryEntry &
+CxxConfigManager::findObjectType(const std::string &object_name,
+ std::string &object_type)
+{
+ if (!configFile.objectExists(object_name))
+ throw Exception(object_name, "Can't find sim object");
+
+ if (!configFile.getParam(object_name, "type", object_type))
+ throw Exception(object_name, "Sim object has no 'type' field");
+
+ if (cxx_config_directory.find(object_type) ==
+ cxx_config_directory.end())
+ {
+ throw Exception(object_name, csprintf(
+ "No sim object type %s is available", object_type));
+ }
+
+ const CxxConfigDirectoryEntry *entry = cxx_config_directory[object_type];
+
+ return *entry;
+}
+
+std::string
+CxxConfigManager::rename(const std::string &from_name)
+{
+ for (auto i = renamings.begin(); i != renamings.end(); ++ i) {
+ const Renaming &renaming = *i;
+
+ if (from_name.find(renaming.fromPrefix) == 0) {
+ return renaming.toPrefix +
+ from_name.substr(renaming.fromPrefix.length());
+ }
+ }
+
+ return from_name;
+}
+
+std::string
+CxxConfigManager::unRename(const std::string &to_name)
+{
+ for (auto i = renamings.begin(); i != renamings.end(); ++ i) {
+ const Renaming &renaming = *i;
+
+ if (to_name.find(renaming.toPrefix) == 0) {
+ return renaming.fromPrefix +
+ to_name.substr(renaming.toPrefix.length());
+ }
+ }
+
+ return to_name;
+}
+
+static
+std::string formatParamList(const std::vector<std::string> &param_values)
+{
+ std::ostringstream params;
+
+ auto i = param_values.begin();
+ auto end_i = param_values.end();
+
+ params << '[';
+ while (i != end_i) {
+ params << (*i);
+ ++i;
+
+ if (i != end_i)
+ params << ", ";
+ }
+ params << ']';
+
+ return params.str();
+}
+
+SimObject *
+CxxConfigManager::findObject(const std::string &object_name,
+ bool visit_children)
+{
+ std::string instance_name = rename(object_name);
+
+ if (object_name == "Null")
+ return NULL;
+
+ /* Already constructed */
+ if (objectsByName.find(instance_name) != objectsByName.end())
+ return objectsByName[instance_name];
+
+ if (inVisit.find(instance_name) != inVisit.end())
+ throw Exception(instance_name, "Cycle in configuration");
+
+ std::string object_type;
+ const CxxConfigDirectoryEntry &entry =
+ findObjectType(object_name, object_type);
+
+ SimObject *object = NULL;
+
+ CxxConfigParams *object_params = findObjectParams(object_name);
+
+ try {
+ DPRINTF(CxxConfig, "Configuring sim object references for: %s"
+ " (%s from object %s)\n", instance_name, object_type,
+ object_name);
+
+ /* Remember the path back to the top of the recursion to detect
+ * cycles */
+ inVisit.insert(instance_name);
+
+ /* Resolve pointed-to SimObjects by recursing into them */
+ for (auto i = entry.parameters.begin();
+ i != entry.parameters.end(); ++i)
+ {
+ const CxxConfigDirectoryEntry::ParamDesc *param = (*i).second;
+
+ if (param->isSimObject) {
+ if (param->isVector) {
+ std::vector<std::string> sub_object_names;
+
+ if (!configFile.getParamVector(object_name, param->name,
+ sub_object_names))
+ {
+ throw Exception(object_name, csprintf(
+ "Element not found: %s", param->name));
+ }
+
+ std::vector<SimObject *> sub_objects;
+
+ for (auto n = sub_object_names.begin();
+ n != sub_object_names.end(); ++n)
+ {
+ SimObject *sub_object = findObject(*n,
+ visit_children);
+
+ if (sub_object)
+ sub_objects.push_back(sub_object);
+ }
+
+ if (!object_params->setSimObjectVector(param->name,
+ sub_objects))
+ {
+ throw Exception(object_name, csprintf(
+ "Can't assign sim object element %s from \"%s\"",
+ param->name, formatParamList(sub_object_names)));
+ }
+
+ DPRINTF(CxxConfig, "Setting sim object(s): %s.%s=%s\n",
+ object_name, param->name,
+ formatParamList(sub_object_names));
+ } else {
+ std::string sub_object_name;
+
+ if (!configFile.getParam(object_name, param->name,
+ sub_object_name))
+ {
+ throw Exception(object_name, csprintf(
+ "Element not found: %s", param->name));
+ }
+
+ SimObject *sub_object = findObject(sub_object_name,
+ visit_children);
+
+ if (sub_object) {
+ if (!object_params->setSimObject(param->name,
+ sub_object))
+ {
+ throw Exception(object_name, csprintf(
+ "Can't assign sim object element %s from"
+ " \"%s\"", param->name, sub_object_name));
+ }
+ }
+
+ DPRINTF(CxxConfig, "Setting sim object(s):"
+ " %s.%s=%s\n", object_name, param->name,
+ sub_object_name);
+ }
+ }
+ }
+
+ DPRINTF(CxxConfig, "Creating SimObject: %s\n", instance_name);
+ object = object_params->simObjectCreate();
+
+ if (!object) {
+ throw Exception(object_name, csprintf("Couldn't create object of"
+ " type: %s", object_type));
+ }
+
+ objectsByName[instance_name] = object;
+ objectParamsByName[instance_name] = object_params;
+
+ if (visit_children) {
+ std::vector<std::string> children;
+ configFile.getObjectChildren(object_name, children, true);
+
+ /* Visit all your children */
+ for (auto i = children.begin(); i != children.end(); ++i)
+ findObject(*i, visit_children);
+ }
+ } catch (Exception &) {
+ delete object_params;
+ throw;
+ }
+
+ /* Mark that we've exited object
+ * construction and so 'find'ing this object again won't be a
+ * configuration loop */
+ inVisit.erase(object_name);
+ return object;
+}
+
+CxxConfigParams *
+CxxConfigManager::findObjectParams(const std::string &object_name)
+{
+ std::string instance_name = rename(object_name);
+
+ /* Already constructed */
+ if (objectParamsByName.find(instance_name) != objectParamsByName.end())
+ return objectParamsByName[instance_name];
+
+ std::string object_type;
+ const CxxConfigDirectoryEntry &entry =
+ findObjectType(object_name, object_type);
+
+ DPRINTF(CxxConfig, "Configuring parameters of object: %s (%s)\n",
+ instance_name, object_type);
+
+ CxxConfigParams *object_params = entry.makeParamsObject();
+
+ try {
+ /* Fill in the implicit parameters that don't necessarily
+ * appear in config files */
+ object_params->setName(instance_name);
+
+ /* Fill in parameters */
+ for (auto i = entry.parameters.begin();
+ i != entry.parameters.end(); ++i)
+ {
+ const CxxConfigDirectoryEntry::ParamDesc *param = (*i).second;
+
+ if (!param->isSimObject) {
+ /* Only handle non-SimObject parameters here (see below) */
+
+ if (param->isVector) {
+ std::vector<std::string> param_values;
+
+ if (!configFile.getParamVector(object_name, param->name,
+ param_values))
+ {
+ throw Exception(object_name, csprintf(
+ "Element not found for parameter: %s",
+ param->name));
+ }
+
+ if (!object_params->setParamVector(param->name,
+ param_values, flags))
+ {
+ throw Exception(instance_name, csprintf(
+ "Bad parameter value: .%s=X=\"%s\"",
+ param->name, formatParamList(param_values)));
+ }
+
+ DPRINTF(CxxConfig, "Setting parameter"
+ " %s.%s=%s\n", instance_name, param->name,
+ formatParamList(param_values));
+ } else {
+ std::string param_value;
+
+ if (!configFile.getParam(object_name, param->name,
+ param_value))
+ {
+ throw Exception(object_name, csprintf(
+ "Element not found for parameter: %s",
+ param->name));
+ }
+
+ if (!object_params->setParam(param->name, param_value,
+ flags))
+ {
+ throw Exception(instance_name, csprintf(
+ "Bad parameter value: .%s=X=\"%s\"",
+ param->name, param_value));
+ }
+
+ DPRINTF(CxxConfig, "Setting parameter %s.%s=%s\n",
+ instance_name, param->name, param_value);
+ }
+ }
+ }
+
+ /* Find the number of ports that will need binding and set the
+ * appropriate port_..._connection_count parameters */
+ for (auto i = entry.ports.begin(); i != entry.ports.end(); ++i) {
+ const CxxConfigDirectoryEntry::PortDesc *port = (*i).second;
+ std::vector<std::string> peers;
+
+ if (!configFile.getPortPeers(object_name, port->name, peers)) {
+ DPRINTF(CxxConfig, "Port not found: %s.%s,"
+ " assuming there are no connections\n",
+ instance_name, port->name);
+ }
+
+ unsigned int peer_count = peers.size();
+
+ /* It would be more efficient to split the peer list and
+ * save the values for peer binding later but that would
+ * require another annoying intermediate structure to
+ * hold for little performance increase */
+
+ if (!object_params->setPortConnectionCount(port->name,
+ peer_count))
+ {
+ throw Exception(instance_name, csprintf(
+ "Unconnected port: %s", port->name));
+ }
+
+ DPRINTF(CxxConfig, "Setting port connection count"
+ " for: %s.%s to %d\n",
+ instance_name, port->name, peer_count);
+ }
+
+ /* Set pointed-to SimObjects to NULL */
+ for (auto i = entry.parameters.begin();
+ i != entry.parameters.end(); ++i)
+ {
+ const CxxConfigDirectoryEntry::ParamDesc *param = (*i).second;
+
+ if (param->isSimObject) {
+ bool ret;
+
+ DPRINTF(CxxConfig, "Nulling sim object reference: %s.%s\n",
+ instance_name, param->name);
+
+ if (param->isVector) {
+ /* Clear the reference list. */
+ std::vector<SimObject *> empty;
+ ret = object_params->setSimObjectVector(param->name,
+ empty);
+ } else {
+ ret = object_params->setSimObject(param->name, NULL);
+ }
+
+ if (!ret) {
+ throw Exception(instance_name, csprintf(
+ "Error nulling sim object reference(s): %s",
+ param->name));
+ }
+ }
+ }
+ } catch (Exception &) {
+ delete object_params;
+ throw;
+ }
+
+ objectParamsByName[instance_name] = object_params;
+
+ return object_params;
+}
+
+void
+CxxConfigManager::findAllObjects()
+{
+ std::vector<std::string> objects;
+ configFile.getAllObjectNames(objects);
+
+ /* Sort the object names to get a consistent initialisation order
+ * even with config file reorganisation */
+ std::sort(objects.begin(), objects.end());
+
+ for (auto i = objects.begin(); i != objects.end(); ++i)
+ findObject(*i);
+
+ /* Set the traversal order for further iterators */
+ objectsInOrder.clear();
+ findTraversalOrder("root");
+}
+
+void
+CxxConfigManager::findTraversalOrder(const std::string &object_name)
+{
+ SimObject *object = findObject(object_name);
+
+ if (object) {
+ objectsInOrder.push_back(object);
+
+ std::vector<std::string> children;
+ configFile.getObjectChildren(object_name, children, true);
+
+ /* Visit all your children */
+ for (auto i = children.begin(); i != children.end(); ++i)
+ findTraversalOrder(*i);
+ }
+}
+
+void
+CxxConfigManager::bindAllPorts()
+{
+ for (auto i = objectsInOrder.begin(); i != objectsInOrder.end(); ++i)
+ bindObjectPorts(*i);
+}
+
+void
+CxxConfigManager::bindPort(
+ SimObject *master_object, const std::string &master_port_name,
+ PortID master_port_index,
+ SimObject *slave_object, const std::string &slave_port_name,
+ PortID slave_port_index)
+{
+ MemObject *master_mem_object = dynamic_cast<MemObject *>(master_object);
+ MemObject *slave_mem_object = dynamic_cast<MemObject *>(slave_object);
+
+ if (!master_mem_object) {
+ throw Exception(master_object->name(), csprintf(
+ "Object isn't a mem object and so can have master port:"
+ " %s[%d]", master_port_name, master_port_index));
+ }
+
+ if (!slave_mem_object) {
+ throw Exception(slave_object->name(), csprintf(
+ "Object isn't a mem object and so can have slave port:"
+ " %s[%d]", slave_port_name, slave_port_index));
+ }
+
+ /* FIXME, check slave_port_index against connection_count
+ * defined for port, need getPortConnectionCount and a
+ * getCxxConfigDirectoryEntry for each object. */
+
+ /* It would be nice to be able to catch the errors from these calls. */
+ BaseMasterPort &master_port = master_mem_object->getMasterPort(
+ master_port_name, master_port_index);
+ BaseSlavePort &slave_port = slave_mem_object->getSlavePort(
+ slave_port_name, slave_port_index);
+
+ if (master_port.isConnected()) {
+ throw Exception(master_object->name(), csprintf(
+ "Master port: %s[%d] is already connected\n", master_port_name,
+ master_port_index));
+ }
+
+ if (slave_port.isConnected()) {
+ throw Exception(slave_object->name(), csprintf(
+ "Slave port: %s[%d] is already connected\n", slave_port_name,
+ slave_port_index));
+ }
+
+ DPRINTF(CxxConfig, "Binding port %s.%s[%d]"
+ " to %s:%s[%d]\n",
+ master_object->name(), master_port_name, master_port_index,
+ slave_object->name(), slave_port_name, slave_port_index);
+
+ master_port.bind(slave_port);
+}
+
+void
+CxxConfigManager::bindMasterPort(SimObject *object,
+ const CxxConfigDirectoryEntry::PortDesc &port,
+ const std::vector<std::string> &peers)
+{
+ unsigned int master_port_index = 0;
+
+ for (auto peer_i = peers.begin(); peer_i != peers.end();
+ ++peer_i)
+ {
+ const std::string &peer = *peer_i;
+ std::string slave_object_name;
+ std::string slave_port_name;
+ unsigned int slave_port_index;
+
+ parsePort(peer, slave_object_name, slave_port_name,
+ slave_port_index);
+
+ std::string slave_instance_name = rename(slave_object_name);
+
+ if (objectsByName.find(slave_instance_name) == objectsByName.end()) {
+ throw Exception(object->name(), csprintf(
+ "Can't find slave port object: %s", slave_instance_name));
+ }
+
+ SimObject *slave_object = objectsByName[slave_instance_name];
+
+ bindPort(object, port.name, master_port_index,
+ slave_object, slave_port_name, slave_port_index);
+
+ master_port_index++;
+ }
+}
+
+void
+CxxConfigManager::bindObjectPorts(SimObject *object)
+{
+ /* We may want to separate object->name() from the name in configuration
+ * later to allow (for example) repetition of fragments of configs */
+ const std::string &instance_name = object->name();
+
+ std::string object_name = unRename(instance_name);
+
+ std::string object_type;
+ const CxxConfigDirectoryEntry &entry =
+ findObjectType(object_name, object_type);
+
+ DPRINTF(CxxConfig, "Binding ports of object: %s (%s)\n",
+ instance_name, object_type);
+
+ for (auto i = entry.ports.begin(); i != entry.ports.end(); ++i) {
+ const CxxConfigDirectoryEntry::PortDesc *port = (*i).second;
+
+ DPRINTF(CxxConfig, "Binding port: %s.%s\n", instance_name,
+ port->name);
+
+ std::vector<std::string> peers;
+ configFile.getPortPeers(object_name, port->name, peers);
+
+ /* Only handle master ports as binding only needs to happen once
+ * for each observed pair of ports */
+ if (port->isMaster) {
+ if (!port->isVector && peers.size() > 1) {
+ throw Exception(instance_name, csprintf(
+ "Too many connections to non-vector port %s (%d)\n",
+ port->name, peers.size()));
+ }
+
+ bindMasterPort(object, *port, peers);
+ }
+ }
+}
+
+void
+CxxConfigManager::parsePort(const std::string &inp,
+ std::string &path, std::string &port, unsigned int &index)
+{
+ std::size_t dot_i = inp.rfind('.');
+ std::size_t open_square_i = inp.rfind('[');
+
+ if (dot_i == std::string::npos) {
+ DPRINTF(CxxConfig, "Bad port string: %s\n", inp);
+ path = "";
+ port = "";
+ index = 0;
+ } else {
+ path = std::string(inp, 0, dot_i);
+
+ if (open_square_i == std::string::npos) {
+ /* Singleton port */
+ port = std::string(inp, dot_i + 1, inp.length() - dot_i);
+ index = 0;
+ } else {
+ /* Vectored port elemnt */
+ port = std::string(inp, dot_i + 1, (open_square_i - 1) - dot_i);
+ index = std::atoi(inp.c_str() + open_square_i + 1);
+ }
+ }
+}
+
+void
+CxxConfigManager::forEachObject(void (SimObject::*mem_func)())
+{
+ for (auto i = objectsInOrder.begin(); i != objectsInOrder.end(); ++i)
+ ((*i)->*mem_func)();
+}
+
+void
+CxxConfigManager::instantiate(bool build_all)
+{
+ if (build_all) {
+ findAllObjects();
+ bindAllPorts();
+ }
+
+ DPRINTF(CxxConfig, "Initialising all objects\n");
+ forEachObject(&SimObject::init);
+
+ DPRINTF(CxxConfig, "Registering stats\n");
+ forEachObject(&SimObject::regStats);
+
+ DPRINTF(CxxConfig, "Registering probe points\n");
+ forEachObject(&SimObject::regProbePoints);
+
+ DPRINTF(CxxConfig, "Connecting probe listeners\n");
+ forEachObject(&SimObject::regProbeListeners);
+}
+
+void
+CxxConfigManager::initState()
+{
+ DPRINTF(CxxConfig, "Calling initState on all objects\n");
+ forEachObject(&SimObject::initState);
+}
+
+void
+CxxConfigManager::startup()
+{
+ DPRINTF(CxxConfig, "Starting up all objects\n");
+ forEachObject(&SimObject::startup);
+}
+
+unsigned int
+CxxConfigManager::drain(DrainManager *drain_manager)
+{
+ unsigned int ret = 0;
+
+ for (auto i = objectsInOrder.begin(); i != objectsInOrder.end(); ++ i)
+ ret += (*i)->drain(drain_manager);
+
+ return ret;
+}
+
+void
+CxxConfigManager::drainResume()
+{
+ forEachObject(&SimObject::drainResume);
+}
+
+void
+CxxConfigManager::serialize(std::ostream &os)
+{
+ for (auto i = objectsInOrder.begin(); i != objectsInOrder.end(); ++ i) {
+ // (*i)->nameOut(os); FIXME, change access spec. for nameOut
+ os << '[' << (*i)->name() << "]\n";
+ (*i)->serialize(os);
+ }
+}
+
+void
+CxxConfigManager::loadState(Checkpoint *checkpoint)
+{
+ for (auto i = objectsInOrder.begin(); i != objectsInOrder.end(); ++ i)
+ (*i)->loadState(checkpoint);
+}
+
+void
+CxxConfigManager::deleteObjects()
+{
+ for (auto i = objectsInOrder.rbegin(); i != objectsInOrder.rend(); ++i) {
+ DPRINTF(CxxConfig, "Freeing sim object: %s\n", (*i)->name());
+ delete *i;
+ }
+
+ for (auto i = objectParamsByName.rbegin();
+ i != objectParamsByName.rend(); ++i)
+ {
+ CxxConfigParams *params = (*i).second;
+
+ DPRINTF(CxxConfig, "Freeing sim object params: %s\n",
+ params->getName());
+ delete params;
+ }
+
+ objectsInOrder.clear();
+ objectsByName.clear();
+}
+
+void
+CxxConfigManager::setParam(const std::string &object_name,
+ const std::string &param_name, const std::string &param_value)
+{
+ CxxConfigParams *params = findObjectParams(object_name);
+
+ if (!params->setParam(param_name, param_value, flags)) {
+ throw Exception(object_name, csprintf("Bad parameter value:"
+ " .%s=X=\"%s\"", param_name, param_value));
+ } else {
+ std::string instance_name = rename(object_name);
+
+ DPRINTF(CxxConfig, "Setting parameter %s.%s=%s\n",
+ instance_name, param_name, param_value);
+ }
+}
+
+void
+CxxConfigManager::setParamVector(const std::string &object_name,
+ const std::string &param_name,
+ const std::vector<std::string> &param_values)
+{
+ CxxConfigParams *params = findObjectParams(object_name);
+
+ if (!params->setParamVector(param_name, param_values, flags)) {
+ throw Exception(object_name, csprintf("Bad vector parameter value:"
+ " .%s=X=\"%s\"", param_name, formatParamList(param_values)));
+ } else {
+ std::string instance_name = rename(object_name);
+
+ DPRINTF(CxxConfig, "Setting parameter %s.%s=\"%s\"\n",
+ instance_name, param_name, formatParamList(param_values));
+ }
+}
+
+void CxxConfigManager::addRenaming(const Renaming &renaming)
+{
+ renamings.push_back(renaming);
+}
diff --git a/src/sim/cxx_manager.hh b/src/sim/cxx_manager.hh
new file mode 100644
index 000000000..caa115f03
--- /dev/null
+++ b/src/sim/cxx_manager.hh
@@ -0,0 +1,314 @@
+/*
+ * Copyright (c) 2014 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.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Andrew Bardsley
+ */
+
+/**
+ * @file
+ *
+ * C++-only configuration and instantiation support. This allows a
+ * config to be read back from a config file and instantiated without
+ * Python. Useful if you want to embed gem5 within a larger system
+ * without carrying the integration cost of the fully-featured
+ * configuration system.
+ *
+ * This file contains the config loading/storing manager class
+ */
+
+#ifndef __SIM_CXX_MANAGER_HH__
+#define __SIM_CXX_MANAGER_HH__
+
+#include <list>
+#include <map>
+#include <set>
+#include <string>
+#include <vector>
+
+#include "base/cprintf.hh"
+#include "sim/cxx_config.hh"
+
+class Checkpoint;
+
+/** This class allows a config file to be read into gem5 (generating the
+ * appropriate SimObjects) from C++ */
+class CxxConfigManager
+{
+ protected:
+ /** Configuration file being read */
+ CxxConfigFileBase &configFile;
+
+ /** Flags to pass to affect param setting */
+ CxxConfigParams::Flags flags;
+
+ public:
+ /** Exception for instantiate/post-instantiate errors */
+ class Exception : public std::exception
+ {
+ public:
+ std::string name;
+ std::string message;
+
+ public:
+ Exception(const std::string &name_, const std::string &message_) :
+ name(name_), message(message_)
+ { }
+
+ const char *what() const throw() { return message.c_str(); }
+
+ ~Exception() throw() { }
+ };
+
+ /** Name substitution when instantiating any object whose name starts
+ * with fromPrefix. Where both renamed and unrenamed names are used
+ * in the code, `object' as part of a name usually refers to the
+ * unrenamed name (the name as it appears in the config file) and
+ * `instance' is part of the renamed name */
+ struct Renaming
+ {
+ std::string fromPrefix;
+ std::string toPrefix;
+
+ Renaming(const std::string &from_prefix,
+ const std::string &to_prefix) :
+ fromPrefix(from_prefix),
+ toPrefix(to_prefix)
+ { }
+ };
+
+ public:
+ /** SimObject indexed by name */
+ std::map<std::string, SimObject *> objectsByName;
+
+ /** ...Params objects created by this manager */
+ std::map<std::string, CxxConfigParams *> objectParamsByName;
+
+ /** SimObjects in order. This is populated by findAllObjects */
+ std::list<SimObject *> objectsInOrder;
+
+ protected:
+ /** While configuring, inVisit contains names of SimObjects visited in
+ * this recursive configuration walk */
+ std::set<std::string> inVisit;
+
+ /** All the renamings applicable when instantiating objects */
+ std::list<Renaming> renamings;
+
+ /** Bind a single connection between two objects' ports */
+ void bindPort(SimObject *masterObject, const std::string &masterPort,
+ PortID masterPortIndex, SimObject *slaveObject,
+ const std::string &slavePort, PortID slavePortIndex);
+
+ /** Bind a single (possibly vectored) master port to peers from the
+ * unparsed list peers with elements in the .ini connection format:
+ * path(.path)*.port[index] */
+ void bindMasterPort(SimObject *object,
+ const CxxConfigDirectoryEntry::PortDesc &port,
+ const std::vector<std::string> &peers);
+
+ /** Apply the first matching renaming in renamings to the given name */
+ std::string rename(const std::string &from_name);
+
+ /** Apply the first matching renaming in reverse (toPrefix -> fromPrefix
+ * for the given name */
+ std::string unRename(const std::string &to_name);
+
+ protected:
+ /** Bind the ports of all the objects in objectInOrder order.
+ * Also */
+ void bindAllPorts();
+
+ /** Class for resolving SimObject names to SimObjects usable by the
+ * checkpoint restore mechanism */
+ class SimObjectResolver : public ::SimObjectResolver
+ {
+ protected:
+ CxxConfigManager &configManager;
+
+ public:
+ SimObjectResolver(CxxConfigManager &configManager_) :
+ configManager(configManager_)
+ { }
+
+ SimObject *resolveSimObject(const std::string &name)
+ { return &(configManager.getObject<SimObject>(name)); }
+ };
+
+ /** Singleton instance of SimObjectResolver */
+ SimObjectResolver simObjectResolver;
+
+ public:
+ CxxConfigManager(CxxConfigFileBase &configFile_);
+
+ /** Find the type field for a named object and return both the
+ * name of the type to object_type and the object's directory
+ * entry as the return value */
+ const CxxConfigDirectoryEntry &findObjectType(
+ const std::string &object_name, std::string &object_type);
+
+ /** Add a name prefix renaming to those currently applied. Call this
+ * before trying to instantiate any object as the name mappings are
+ * not applied to the config tree read from the config file but are
+ * applied while processing instantiations */
+ void addRenaming(const Renaming &renaming);
+
+ public:
+ /** Bind the ports of a single SimObject */
+ void bindObjectPorts(SimObject *object);
+
+ /** Walk the configuration starting with object object_name and fill
+ * in all the elements of this object on the way. This involves:
+ * <ul>
+ * <li>Calling findObjectParams to make the ...Params object
+ * If findObjectParams has already been called for this object,
+ * the ...Params object generated by that called (stored in
+ * (objectParamsByName[object_name] will be used)</li>
+ * <li>Populating the ...Params object references to other
+ * SimObjects by recursively descending into the trees formed
+ * by SimObject references</li>
+ * <li>Building the final SimObject and adding it to
+ * objectsByName</li>
+ * <li>If visit_children is true, recursively visit all this
+ * object's children and build/find them too</li>
+ * </ul>
+ * After the first call, this function will return
+ * objectsByName[object_name] */
+ SimObject *findObject(const std::string &object_name,
+ bool visit_children = false);
+
+ /** Find the parameters for the named object. Returns NULL if the
+ * object isn't in the configuration. For the first call with a
+ * particular object name, a new CxxConfigParams descended object
+ * is made with the configuration file contents for this object.
+ * This involves populating that ...Params object with:
+ * <ul>
+ * <li>parameter values from the configuration file</li>
+ * <li>port connection connection counts from the connection counts
+ * indicated by the number of peer ports in the configuration
+ * file</li>
+ * <li>nulled (or vector<>::clear'ed) SimObject references for
+ * SimObject-values parameters</li>
+ * </ul>
+ * The ...Params object is then added to objectParamsByName
+ * After the first call, this function will return
+ * objectParamsByName[object_name] */
+ CxxConfigParams *findObjectParams(const std::string &object_name);
+
+ /** Populate objectsInOrder with a preorder, depth first traversal from
+ * the given object name down through all its children */
+ void findTraversalOrder(const std::string &object_name);
+
+ /** Find an object from objectsByName with a type-checking cast.
+ * This function is provided for manipulating objects after
+ * instantiate as it assumes the named object exists. */
+ template<typename SimObjectType>
+ SimObjectType &
+ getObject(const std::string &object_name)
+ {
+ if (objectsByName.find(object_name) == objectsByName.end()) {
+ throw Exception("", csprintf("No sim object named: %s",
+ object_name));
+ }
+
+ SimObjectType *object = dynamic_cast<SimObjectType *>(
+ objectsByName[object_name]);
+
+ if (!object) {
+ throw Exception("", csprintf("Sim object: %s has the wrong"
+ " type", object_name));
+ }
+
+ return *object;
+ }
+
+ /** Perform mem_func on each SimObject */
+ void forEachObject(void (SimObject::*mem_func)());
+
+ /** Find all objects by iterating over the object names in the config
+ * file with findObject. Also populate the traversal order */
+ void findAllObjects();
+
+ /** Parse a port string of the form 'path(.path)*.port[index]' into
+ * path, port and index */
+ static void parsePort(const std::string &inp,
+ std::string &path, std::string &port, unsigned int &index);
+
+ /** Build all objects (if build_all is true, otherwise objects must
+ * have been individually findObject-ed and added to the traversal
+ * order) and perform all the configuration specific actions up to,
+ * but not including initState.
+ *
+ * If you want to set some parameters before completing instantiation,
+ * call findObjectParams on the objects you want to modify, then call
+ * instantiate */
+ void instantiate(bool build_all = true);
+
+ /** Call initState on all objects */
+ void initState();
+
+ /** Call startup on all objects */
+ void startup();
+
+ /** Drain all objects */
+ unsigned int drain(DrainManager *drain_manager);
+
+ /** Resume from drain */
+ void drainResume();
+
+ /** Serialize (checkpoint) all objects to the given stream */
+ void serialize(std::ostream &os);
+
+ /** Load all objects' state from the given Checkpoint */
+ void loadState(Checkpoint *checkpoint);
+
+ /** Delete all objects and clear objectsByName and objectsByOrder */
+ void deleteObjects();
+
+ /** Get the resolver used to map SimObject names to SimObjects for
+ * checkpoint restore */
+ SimObjectResolver &getSimObjectResolver() { return simObjectResolver; }
+
+ /** Convenience functions for calling set... member functions on a
+ * CxxConfigParams for an object. These functions throw Exception
+ * rather than return a bool on failure */
+ void setParam(const std::string &object_name,
+ const std::string &param_name, const std::string &param_value);
+ void setParamVector(const std::string &object_name,
+ const std::string &param_name,
+ const std::vector<std::string> &param_values);
+};
+
+#endif // __SIM_CXX_MANAGER_HH__