42#include <unordered_map>
46#include "debug/CxxConfig.hh"
61 std::string &object_type)
64 throw Exception(object_name,
"Can't find sim object");
66 if (!
configFile.getParam(object_name,
"type", object_type))
67 throw Exception(object_name,
"Sim object has no 'type' field");
73 "No sim object type %s is available", object_type));
87 if (from_name.find(renaming.
fromPrefix) == 0) {
89 from_name.substr(renaming.
fromPrefix.length());
102 if (to_name.find(renaming.
toPrefix) == 0) {
104 to_name.substr(renaming.
toPrefix.length());
114 std::ostringstream params;
116 auto i = param_values.begin();
117 auto end_i = param_values.end();
137 std::ostringstream params;
139 auto i = param_values.begin();
140 auto end_i = param_values.end();
144 params << (
i->first);
146 params << (
i->second);
159 const std::string &instance_name,
170 "Element not found for parameter: %s",
175 param_values,
flags))
178 "Bad parameter value: .%s=X=\"%s\"",
182 DPRINTF(CxxConfig,
"Setting parameter"
183 " %s.%s=%s\n", instance_name, param->
name,
185 }
else if (param->
isDict) {
186 std::unordered_map<std::string, std::string> param_values;
192 "Element not found for parameter: %s",
197 param_values,
flags))
200 "Bad parameter value: .%s=X=\"%s\"",
204 DPRINTF(CxxConfig,
"Setting parameter"
205 " %s.%s=%s\n", instance_name, param->
name,
208 std::string param_value;
214 "Element not found for parameter: %s",
222 "Bad parameter value: .%s=X=\"%s\"",
223 param->
name, param_value));
226 DPRINTF(CxxConfig,
"Setting parameter %s.%s=%s\n",
227 instance_name, param->
name, param_value);
235 std::string instance_name =
rename(object_name);
237 if (object_name ==
"Null")
245 throw Exception(instance_name,
"Cycle in configuration");
247 std::string object_type;
256 DPRINTF(CxxConfig,
"Configuring sim object references for: %s"
257 " (%s from object %s)\n", instance_name, object_type,
278 "Element not found: %s", param->
name));
283 for (
auto n = sub_object_names.begin();
284 n != sub_object_names.end(); ++
n)
290 sub_objects.push_back(sub_object);
297 "Can't assign sim object element %s from \"%s\"",
301 DPRINTF(CxxConfig,
"Setting sim object(s): %s.%s=%s\n",
302 object_name, param->
name,
305 std::string sub_object_name;
311 "Element not found: %s", param->
name));
322 "Can't assign sim object element %s from"
323 " \"%s\"", param->
name, sub_object_name));
327 DPRINTF(CxxConfig,
"Setting sim object(s):"
328 " %s.%s=%s\n", object_name, param->
name,
334 DPRINTF(CxxConfig,
"Creating SimObject: %s\n", instance_name);
339 " type: %s", object_type));
345 if (visit_children) {
347 configFile.getObjectChildren(object_name, children,
true);
350 for (
auto i = children.begin();
i != children.end(); ++
i)
354 delete object_params;
368 std::string instance_name =
rename(object_name);
374 std::string object_type;
378 DPRINTF(CxxConfig,
"Configuring parameters of object: %s (%s)\n",
379 instance_name, object_type);
386 object_params->
setName(instance_name);
397 object_params, param);
403 for (
auto i = entry.
ports.begin();
i != entry.
ports.end(); ++
i) {
408 DPRINTF(CxxConfig,
"Port not found: %s.%s,"
409 " assuming there are no connections\n",
410 instance_name, port->
name);
413 unsigned int peer_count = peers.size();
424 "Unconnected port: %s", port->
name));
427 DPRINTF(CxxConfig,
"Setting port connection count"
428 " for: %s.%s to %d\n",
429 instance_name, port->
name, peer_count);
441 DPRINTF(CxxConfig,
"Nulling sim object reference: %s.%s\n",
442 instance_name, param->
name);
455 "Error nulling sim object reference(s): %s",
461 delete object_params;
467 return object_params;
490 configFile.getObjectChildren(object_name, children,
true);
493 for (
auto i = children.begin();
i != children.end(); ++
i)
507 SimObject *requestor_object,
const std::string &request_port_name,
508 PortID request_port_index,
509 SimObject *responder_object,
const std::string &response_port_name,
510 PortID response_port_index)
518 request_port_name, request_port_index);
520 response_port_name, response_port_index);
524 "Request port: %s[%d] is already connected\n", request_port_name,
525 request_port_index));
530 "Response port: %s[%d] is already connected\n", response_port_name,
531 response_port_index));
534 DPRINTF(CxxConfig,
"Binding port %s.%s[%d]"
536 requestor_object->
name(), request_port_name, request_port_index,
537 responder_object->
name(), response_port_name, response_port_index);
539 request_port.
bind(response_port);
547 unsigned int request_port_index = 0;
549 for (
auto peer_i = peers.begin(); peer_i != peers.end();
552 const std::string &peer = *peer_i;
553 std::string response_object_name;
554 std::string response_port_name;
555 unsigned int response_port_index;
557 parsePort(peer, response_object_name, response_port_name,
558 response_port_index);
560 std::string response_instance_name =
rename(response_object_name);
565 "Can't find response port object: %s",
566 response_instance_name));
572 responder_object, response_port_name, response_port_index);
574 request_port_index++;
583 const std::string &instance_name =
object->name();
585 std::string object_name =
unRename(instance_name);
587 std::string object_type;
591 DPRINTF(CxxConfig,
"Binding ports of object: %s (%s)\n",
592 instance_name, object_type);
594 for (
auto i = entry.
ports.begin();
i != entry.
ports.end(); ++
i) {
597 DPRINTF(CxxConfig,
"Binding port: %s.%s\n", instance_name,
606 if (!port->
isVector && peers.size() > 1) {
608 "Too many connections to non-vector port %s (%d)\n",
609 port->
name, peers.size()));
619 std::string &path, std::string &port,
unsigned int &
index)
621 std::size_t dot_i = inp.rfind(
'.');
622 std::size_t open_square_i = inp.rfind(
'[');
624 if (dot_i == std::string::npos) {
625 DPRINTF(CxxConfig,
"Bad port string: %s\n", inp);
630 path = std::string(inp, 0, dot_i);
632 if (open_square_i == std::string::npos) {
634 port = std::string(inp, dot_i + 1, inp.length() - dot_i);
638 port = std::string(inp, dot_i + 1, (open_square_i - 1) - dot_i);
639 index = std::atoi(inp.c_str() + open_square_i + 1);
659 DPRINTF(CxxConfig,
"Initialising all objects\n");
662 DPRINTF(CxxConfig,
"Registering stats\n");
665 DPRINTF(CxxConfig,
"Registering probe points\n");
668 DPRINTF(CxxConfig,
"Connecting probe listeners\n");
675 DPRINTF(CxxConfig,
"Calling initState on all objects\n");
682 DPRINTF(CxxConfig,
"Starting up all objects\n");
703 os <<
'[' << (*i)->name() <<
"]\n";
712 (*i)->loadState(checkpoint);
719 DPRINTF(CxxConfig,
"Freeing sim object: %s\n", (*i)->name());
728 DPRINTF(CxxConfig,
"Freeing sim object params: %s\n",
739 const std::string ¶m_name,
const std::string ¶m_value)
745 " .%s=X=\"%s\"", param_name, param_value));
747 std::string instance_name =
rename(object_name);
749 DPRINTF(CxxConfig,
"Setting parameter %s.%s=%s\n",
750 instance_name, param_name, param_value);
756 const std::string ¶m_name,
765 std::string instance_name =
rename(object_name);
767 DPRINTF(CxxConfig,
"Setting parameter %s.%s=\"%s\"\n",
const bool isSimObject
Is this a SimObject, and so is to be set with setSimObject... or another from-string parameter set wi...
Similar to ParamDesc to describe ports.
const bool isRequestor
Is this a request or response port.
Config details entry for a SimObject.
std::map< std::string, ParamDesc * > parameters
All parameters (including SimObjects) in order.
virtual CxxConfigParams * makeParamsObject() const
Make a ...Param structure for the SimObject class of this entry.
std::map< std::string, PortDesc * > ports
Ports.
Config file wrapper providing a common interface to CxxConfigManager.
Exception for instantiate/post-instantiate errors.
std::set< std::string > inVisit
While configuring, inVisit contains names of SimObjects visited in this recursive configuration walk.
std::list< SimObject * > objectsInOrder
SimObjects in order.
void drainResume()
Resume from drain.
static void parsePort(const std::string &inp, std::string &path, std::string &port, unsigned int &index)
Parse a port string of the form 'path(.path)*.port[index]' into path, port and index.
void startup()
Call startup on all objects.
void deleteObjects()
Delete all objects and clear objectsByName and objectsByOrder.
CxxConfigManager(CxxConfigFileBase &configFile_)
void serialize(std::ostream &os)
Serialize (checkpoint) all objects to the given stream.
std::string rename(const std::string &from_name)
Apply the first matching renaming in renamings to the given name.
std::string unRename(const std::string &to_name)
Apply the first matching renaming in reverse (toPrefix -> fromPrefix for the given name.
CxxConfigParams::Flags flags
Flags to pass to affect param setting.
void setParamVector(const std::string &object_name, const std::string ¶m_name, const std::vector< std::string > ¶m_values)
CxxConfigFileBase & configFile
Configuration file being read.
void bindPort(SimObject *requestorObject, const std::string &requestPort, PortID requestPortIndex, SimObject *responderObject, const std::string &responsePort, PortID responsePortIndex)
Bind a single connection between two objects' ports.
void addRenaming(const Renaming &renaming)
Add a name prefix renaming to those currently applied.
unsigned int drain()
Drain all objects.
void initState()
Call initState on all objects.
std::list< Renaming > renamings
All the renamings applicable when instantiating objects.
void findTraversalOrder(const std::string &object_name)
Populate objectsInOrder with a preorder, depth first traversal from the given object name down throug...
void populateParams(const std::string &object_name, const std::string &instance_name, CxxConfigParams *object_params, const CxxConfigDirectoryEntry::ParamDesc *param)
Assign a parameter to the named object.
std::map< std::string, SimObject * > objectsByName
SimObject indexed by name.
void setParam(const std::string &object_name, const std::string ¶m_name, const std::string ¶m_value)
Convenience functions for calling set... member functions on a CxxConfigParams for an object.
void forEachObject(void(SimObject::*mem_func)())
Perform mem_func on each SimObject.
void bindRequestPort(SimObject *object, const CxxConfigDirectoryEntry::PortDesc &port, const std::vector< std::string > &peers)
Bind a single (possibly vectored) request port to peers from the unparsed list peers with elements in...
CxxConfigParams * findObjectParams(const std::string &object_name)
Find the parameters for the named object.
void bindObjectPorts(SimObject *object)
Bind the ports of a single SimObject.
std::map< std::string, CxxConfigParams * > objectParamsByName
...Params objects created by this manager
void loadState(CheckpointIn &checkpoint)
Load all objects' state from the given Checkpoint.
void findAllObjects()
Find all objects by iterating over the object names in the config file with findObject.
SimObject * findObject(const std::string &object_name, bool visit_children=false)
Walk the configuration starting with object object_name and fill in all the elements of this object o...
SimObjectResolver simObjectResolver
Singleton instance of SimObjectResolver.
void bindAllPorts()
Bind the ports of all the objects in objectInOrder order.
void instantiate(bool build_all=true)
Build all objects (if build_all is true, otherwise objects must have been individually findObject-ed ...
const CxxConfigDirectoryEntry & findObjectType(const std::string &object_name, std::string &object_type)
Find the type field for a named object and return both the name of the type to object_type and the ob...
Base for peer classes of SimObjectParams derived classes with parameter modifying member functions.
virtual bool setParam(const std::string &name, const std::string &value, const Flags flags)
Set a parameter with a value parsed from the given string.
virtual bool setPortConnectionCount(const std::string &name, unsigned int count)
Set the number of connections expected for the named port.
virtual const std::string & getName()
Get full path name string.
virtual bool setParamDict(const std::string &name, const std::unordered_map< std::string, std::string > &values, const Flags flags)
As setParamVector but for parameters given as dictionaries pre-separated into elements.
virtual bool setSimObjectVector(const std::string &name, const std::vector< SimObject * > &simObjects)
As setSimObjectVector but set a whole vector of references.
virtual SimObject * simObjectCreate()
Create the associated SimObject.
virtual void setName(const std::string &name_)
Example flag.
virtual bool setSimObject(const std::string &name, SimObject *simObject)
Set a SimObject valued parameter with a reference to the given SimObject.
virtual bool setParamVector(const std::string &name, const std::vector< std::string > &values, const Flags flags)
As setParam but for parameters given as vectors pre-separated into elements.
static DrainManager & instance()
Get the singleton DrainManager instance.
virtual std::string name() const
Ports are used to interface objects to each other.
bool isConnected() const
Is this port currently connected to a peer?
virtual void bind(Port &peer)
Attach to a peer port.
Abstract superclass for simulation objects.
C++-only configuration and instantiation support.
void resume()
Resume normal simulation in a Drained system.
bool tryDrain()
Try to drain the system.
virtual void initState()
initState() is called on each SimObject when not restoring from a checkpoint.
virtual void regProbeListeners()
Register probe listeners for this object.
virtual void startup()
startup() is the final initialization call before simulation.
virtual void regProbePoints()
Register probe points for this object.
virtual Port & getPort(const std::string &if_name, PortID idx=InvalidPortID)
Get a port with a given name and index.
virtual void init()
init() is called after all C++ SimObjects have been created and all ports are connected.
virtual void regStats()
Callback to set stat parameters.
Copyright (c) 2024 Arm Limited All rights reserved.
std::map< std::string, CxxConfigDirectoryEntry * > & cxxConfigDirectory()
Directory of all SimObject classes config details.
int16_t PortID
Port index/ID type, and a symbolic name for an invalid port id.
static std::string formatParamList(const std::vector< std::string > ¶m_values)
std::string csprintf(const char *format, const Args &...args)
Name substitution when instantiating any object whose name starts with fromPrefix.