arch-power: Fix disassembly for compare instructions
[gem5.git] / src / sim / cxx_manager.hh
1 /*
2 * Copyright (c) 2014 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /**
39 * @file
40 *
41 * C++-only configuration and instantiation support. This allows a
42 * config to be read back from a config file and instantiated without
43 * Python. Useful if you want to embed gem5 within a larger system
44 * without carrying the integration cost of the fully-featured
45 * configuration system.
46 *
47 * This file contains the config loading/storing manager class
48 */
49
50 #ifndef __SIM_CXX_MANAGER_HH__
51 #define __SIM_CXX_MANAGER_HH__
52
53 #include <list>
54 #include <map>
55 #include <set>
56 #include <string>
57 #include <vector>
58
59 #include "base/cprintf.hh"
60 #include "sim/cxx_config.hh"
61
62 class CheckpointIn;
63
64 /** This class allows a config file to be read into gem5 (generating the
65 * appropriate SimObjects) from C++ */
66 class CxxConfigManager
67 {
68 protected:
69 /** Configuration file being read */
70 CxxConfigFileBase &configFile;
71
72 /** Flags to pass to affect param setting */
73 CxxConfigParams::Flags flags;
74
75 public:
76 /** Exception for instantiate/post-instantiate errors */
77 class Exception : public std::exception
78 {
79 public:
80 std::string name;
81 std::string message;
82
83 public:
84 Exception(const std::string &name_, const std::string &message_) :
85 name(name_), message(message_)
86 { }
87
88 const char *what() const throw() { return message.c_str(); }
89
90 ~Exception() throw() { }
91 };
92
93 /** Name substitution when instantiating any object whose name starts
94 * with fromPrefix. Where both renamed and unrenamed names are used
95 * in the code, `object' as part of a name usually refers to the
96 * unrenamed name (the name as it appears in the config file) and
97 * `instance' is part of the renamed name */
98 struct Renaming
99 {
100 std::string fromPrefix;
101 std::string toPrefix;
102
103 Renaming(const std::string &from_prefix,
104 const std::string &to_prefix) :
105 fromPrefix(from_prefix),
106 toPrefix(to_prefix)
107 { }
108 };
109
110 public:
111 /** SimObject indexed by name */
112 std::map<std::string, SimObject *> objectsByName;
113
114 /** ...Params objects created by this manager */
115 std::map<std::string, CxxConfigParams *> objectParamsByName;
116
117 /** SimObjects in order. This is populated by findAllObjects */
118 std::list<SimObject *> objectsInOrder;
119
120 protected:
121 /** While configuring, inVisit contains names of SimObjects visited in
122 * this recursive configuration walk */
123 std::set<std::string> inVisit;
124
125 /** All the renamings applicable when instantiating objects */
126 std::list<Renaming> renamings;
127
128 /** Bind a single connection between two objects' ports */
129 void bindPort(SimObject *requestorObject, const std::string &requestPort,
130 PortID requestPortIndex, SimObject *responderObject,
131 const std::string &responsePort, PortID responsePortIndex);
132
133 /** Bind a single (possibly vectored) request port to peers from the
134 * unparsed list peers with elements in the .ini connection format:
135 * path(.path)*.port[index] */
136 void bindRequestPort(SimObject *object,
137 const CxxConfigDirectoryEntry::PortDesc &port,
138 const std::vector<std::string> &peers);
139
140 /** Apply the first matching renaming in renamings to the given name */
141 std::string rename(const std::string &from_name);
142
143 /** Apply the first matching renaming in reverse (toPrefix -> fromPrefix
144 * for the given name */
145 std::string unRename(const std::string &to_name);
146
147 protected:
148 /** Bind the ports of all the objects in objectInOrder order.
149 * Also */
150 void bindAllPorts();
151
152 /** Class for resolving SimObject names to SimObjects usable by the
153 * checkpoint restore mechanism */
154 class SimObjectResolver : public ::SimObjectResolver
155 {
156 protected:
157 CxxConfigManager &configManager;
158
159 public:
160 SimObjectResolver(CxxConfigManager &configManager_) :
161 configManager(configManager_)
162 { }
163
164 SimObject *resolveSimObject(const std::string &name)
165 { return &(configManager.getObject<SimObject>(name)); }
166 };
167
168 /** Singleton instance of SimObjectResolver */
169 SimObjectResolver simObjectResolver;
170
171 public:
172 CxxConfigManager(CxxConfigFileBase &configFile_);
173
174 /** Find the type field for a named object and return both the
175 * name of the type to object_type and the object's directory
176 * entry as the return value */
177 const CxxConfigDirectoryEntry &findObjectType(
178 const std::string &object_name, std::string &object_type);
179
180 /** Add a name prefix renaming to those currently applied. Call this
181 * before trying to instantiate any object as the name mappings are
182 * not applied to the config tree read from the config file but are
183 * applied while processing instantiations */
184 void addRenaming(const Renaming &renaming);
185
186 public:
187 /** Bind the ports of a single SimObject */
188 void bindObjectPorts(SimObject *object);
189
190 /** Walk the configuration starting with object object_name and fill
191 * in all the elements of this object on the way. This involves:
192 * <ul>
193 * <li>Calling findObjectParams to make the ...Params object
194 * If findObjectParams has already been called for this object,
195 * the ...Params object generated by that called (stored in
196 * (objectParamsByName[object_name] will be used)</li>
197 * <li>Populating the ...Params object references to other
198 * SimObjects by recursively descending into the trees formed
199 * by SimObject references</li>
200 * <li>Building the final SimObject and adding it to
201 * objectsByName</li>
202 * <li>If visit_children is true, recursively visit all this
203 * object's children and build/find them too</li>
204 * </ul>
205 * After the first call, this function will return
206 * objectsByName[object_name] */
207 SimObject *findObject(const std::string &object_name,
208 bool visit_children = false);
209
210 /** Find the parameters for the named object. Returns NULL if the
211 * object isn't in the configuration. For the first call with a
212 * particular object name, a new CxxConfigParams descended object
213 * is made with the configuration file contents for this object.
214 * This involves populating that ...Params object with:
215 * <ul>
216 * <li>parameter values from the configuration file</li>
217 * <li>port connection connection counts from the connection counts
218 * indicated by the number of peer ports in the configuration
219 * file</li>
220 * <li>nulled (or vector<>::clear'ed) SimObject references for
221 * SimObject-values parameters</li>
222 * </ul>
223 * The ...Params object is then added to objectParamsByName
224 * After the first call, this function will return
225 * objectParamsByName[object_name] */
226 CxxConfigParams *findObjectParams(const std::string &object_name);
227
228 /** Populate objectsInOrder with a preorder, depth first traversal from
229 * the given object name down through all its children */
230 void findTraversalOrder(const std::string &object_name);
231
232 /** Find an object from objectsByName with a type-checking cast.
233 * This function is provided for manipulating objects after
234 * instantiate as it assumes the named object exists. */
235 template<typename SimObjectType>
236 SimObjectType &
237 getObject(const std::string &object_name)
238 {
239 if (objectsByName.find(object_name) == objectsByName.end()) {
240 throw Exception("", csprintf("No sim object named: %s",
241 object_name));
242 }
243
244 SimObjectType *object = dynamic_cast<SimObjectType *>(
245 objectsByName[object_name]);
246
247 if (!object) {
248 throw Exception("", csprintf("Sim object: %s has the wrong"
249 " type", object_name));
250 }
251
252 return *object;
253 }
254
255 /** Perform mem_func on each SimObject */
256 void forEachObject(void (SimObject::*mem_func)());
257
258 /** Find all objects by iterating over the object names in the config
259 * file with findObject. Also populate the traversal order */
260 void findAllObjects();
261
262 /** Parse a port string of the form 'path(.path)*.port[index]' into
263 * path, port and index */
264 static void parsePort(const std::string &inp,
265 std::string &path, std::string &port, unsigned int &index);
266
267 /** Build all objects (if build_all is true, otherwise objects must
268 * have been individually findObject-ed and added to the traversal
269 * order) and perform all the configuration specific actions up to,
270 * but not including initState.
271 *
272 * If you want to set some parameters before completing instantiation,
273 * call findObjectParams on the objects you want to modify, then call
274 * instantiate */
275 void instantiate(bool build_all = true);
276
277 /** Call initState on all objects */
278 void initState();
279
280 /** Call startup on all objects */
281 void startup();
282
283 /** Drain all objects */
284 unsigned int drain();
285
286 /** Resume from drain */
287 void drainResume();
288
289 /** Serialize (checkpoint) all objects to the given stream */
290 void serialize(std::ostream &os);
291
292 /** Load all objects' state from the given Checkpoint */
293 void loadState(CheckpointIn &checkpoint);
294
295 /** Delete all objects and clear objectsByName and objectsByOrder */
296 void deleteObjects();
297
298 /** Get the resolver used to map SimObject names to SimObjects for
299 * checkpoint restore */
300 SimObjectResolver &getSimObjectResolver() { return simObjectResolver; }
301
302 /** Convenience functions for calling set... member functions on a
303 * CxxConfigParams for an object. These functions throw Exception
304 * rather than return a bool on failure */
305 void setParam(const std::string &object_name,
306 const std::string &param_name, const std::string &param_value);
307 void setParamVector(const std::string &object_name,
308 const std::string &param_name,
309 const std::vector<std::string> &param_values);
310 };
311
312 #endif // __SIM_CXX_MANAGER_HH__