Clean up/standardize handling of various output files.
[gem5.git] / sim / builder.cc
1 /*
2 * Copyright (c) 2003-2004 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <assert.h>
30
31 #include "base/inifile.hh"
32 #include "base/misc.hh"
33 #include "sim/builder.hh"
34 #include "sim/configfile.hh"
35 #include "sim/config_node.hh"
36 #include "sim/host.hh"
37 #include "sim/sim_object.hh"
38 #include "sim/universe.hh"
39
40 using namespace std;
41
42 SimObjectBuilder::SimObjectBuilder(const string &_configClass,
43 const string &_instanceName,
44 ConfigNode *_configNode,
45 const string &_simObjClassName)
46 : ParamContext(_configClass, true),
47 instanceName(_instanceName),
48 configNode(_configNode),
49 simObjClassName(_simObjClassName)
50 {
51 }
52
53 SimObjectBuilder::~SimObjectBuilder()
54 {
55 }
56
57 ///////////////////////////////////////////
58 //
59 // SimObjectBuilder member definitions
60 //
61 ///////////////////////////////////////////
62
63 // override ParamContext::parseParams() to check params based on
64 // instance name first. If not found, then check based on iniSection
65 // (as in default ParamContext implementation).
66 void
67 SimObjectBuilder::parseParams(IniFile &iniFile)
68 {
69 iniFilePtr = &iniFile; // set object member
70
71 ParamList::iterator i;
72
73 for (i = paramList->begin(); i != paramList->end(); ++i) {
74 string string_value;
75
76 if (iniFile.findDefault(instanceName, (*i)->name, string_value)) {
77 (*i)->parse(string_value);
78 }
79 else if (iniFile.findDefault(iniSection, (*i)->name, string_value)) {
80 (*i)->parse(string_value);
81 }
82 }
83 }
84
85
86 void
87 SimObjectBuilder::printErrorProlog(ostream &os)
88 {
89 os << "Error creating object '" << getInstanceName()
90 << "' of type '" << simObjClassName
91 << "', section '" << iniSection << "':" << endl;
92 }
93
94
95 ////////////////////////////////////////////////////////////////////////
96 //
97 // SimObjectClass member definitions
98 //
99 ////////////////////////////////////////////////////////////////////////
100
101 // Map of class names to SimObjectBuilder creation functions. Need to
102 // make this a pointer so we can force initialization on the first
103 // reference; otherwise, some SimObjectClass constructors may be invoked
104 // before the classMap constructor.
105 map<string,SimObjectClass::CreateFunc> *SimObjectClass::classMap = NULL;
106
107 // SimObjectClass constructor: add mapping to classMap
108 SimObjectClass::SimObjectClass(const string &className, CreateFunc createFunc)
109 {
110 if (classMap == NULL)
111 classMap = new map<string,SimObjectClass::CreateFunc>();
112
113 if ((*classMap)[className])
114 {
115 cerr << "Error: simulation object class " << className << " redefined"
116 << endl;
117 fatal("");
118 }
119
120 // add className --> createFunc to class map
121 (*classMap)[className] = createFunc;
122 }
123
124
125 //
126 //
127 SimObject *
128 SimObjectClass::createObject(IniFile &configDB,
129 const string &configClassName,
130 const string &objName,
131 ConfigNode *configNode)
132 {
133 // find simulation object class name from configuration class
134 // (specified by 'type=' parameter)
135 string simObjClassName;
136
137 if (!configNode->find("type", simObjClassName)) {
138 cerr << "Configuration class '" << configClassName << "' not found."
139 << endl;
140 abort();
141 }
142
143 // look up className to get appropriate createFunc
144 if (classMap->find(simObjClassName) == classMap->end()) {
145 cerr << "Simulator object class '" << simObjClassName << "' not found."
146 << endl;
147 abort();
148 }
149
150 CreateFunc createFunc = (*classMap)[simObjClassName];
151
152 // call createFunc with config hierarchy node to get object
153 // builder instance (context with parameters for object creation)
154 SimObjectBuilder *objectBuilder = (*createFunc)(configClassName,
155 objName, configNode,
156 simObjClassName);
157
158 assert(objectBuilder != NULL);
159
160 // parse all parameters in context to generate parameter values
161 objectBuilder->parseParams(configDB);
162
163 // now create the actual simulation object
164 SimObject *object = objectBuilder->create();
165
166 assert(object != NULL);
167
168 // echo object parameters to stats file (for documenting the
169 // config used to generate the associated stats)
170 *configStream << "[" << object->name() << "]" << endl;
171 *configStream << "type=" << simObjClassName << endl;
172 objectBuilder->showParams(*configStream);
173 *configStream << endl;
174
175 // done with the SimObjectBuilder now
176 delete objectBuilder;
177
178 return object;
179 }
180
181
182 //
183 // static method:
184 //
185 void
186 SimObjectClass::describeAllClasses(ostream &os)
187 {
188 map<string,CreateFunc>::iterator iter;
189
190 for (iter = classMap->begin(); iter != classMap->end(); ++iter) {
191 const string &className = iter->first;
192 CreateFunc createFunc = iter->second;
193
194 os << "[" << className << "]\n";
195
196 // create dummy object builder just to instantiate parameters
197 SimObjectBuilder *objectBuilder = (*createFunc)("", "", NULL, "");
198
199 // now get the object builder to describe ite params
200 objectBuilder->describeParams(os);
201
202 os << endl;
203
204 // done with the object builder now
205 delete objectBuilder;
206 }
207 }