Merge zizzer:/bk/m5 into isabel.reinhardt.house:/z/stever/bk/m5
[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
43 ostream &
44 builderStream()
45 {
46 static ofstream file;
47 static ostream *stream = NULL;
48
49 if (!stream) {
50 if (!outputDirectory.empty()) {
51 string filename = outputDirectory + "builder.txt";
52 file.open(filename.c_str());
53 stream = &file;
54 } else {
55 stream = outputStream;
56 }
57 }
58
59 return *stream;
60 }
61
62 SimObjectBuilder::SimObjectBuilder(const string &_configClass,
63 const string &_instanceName,
64 ConfigNode *_configNode,
65 const string &_simObjClassName)
66 : ParamContext(_configClass, true),
67 instanceName(_instanceName),
68 configNode(_configNode),
69 simObjClassName(_simObjClassName)
70 {
71 }
72
73 SimObjectBuilder::~SimObjectBuilder()
74 {
75 }
76
77 ///////////////////////////////////////////
78 //
79 // SimObjectBuilder member definitions
80 //
81 ///////////////////////////////////////////
82
83 // override ParamContext::parseParams() to check params based on
84 // instance name first. If not found, then check based on iniSection
85 // (as in default ParamContext implementation).
86 void
87 SimObjectBuilder::parseParams(IniFile &iniFile)
88 {
89 iniFilePtr = &iniFile; // set object member
90
91 ParamList::iterator i;
92
93 for (i = paramList->begin(); i != paramList->end(); ++i) {
94 string string_value;
95
96 if (iniFile.findDefault(instanceName, (*i)->name, string_value)) {
97 (*i)->parse(string_value);
98 }
99 else if (iniFile.findDefault(iniSection, (*i)->name, string_value)) {
100 (*i)->parse(string_value);
101 }
102 }
103 }
104
105
106 void
107 SimObjectBuilder::printErrorProlog(ostream &os)
108 {
109 os << "Error creating object '" << getInstanceName()
110 << "' of type '" << simObjClassName
111 << "', section '" << iniSection << "':" << endl;
112 }
113
114
115 ////////////////////////////////////////////////////////////////////////
116 //
117 // SimObjectClass member definitions
118 //
119 ////////////////////////////////////////////////////////////////////////
120
121 // Map of class names to SimObjectBuilder creation functions. Need to
122 // make this a pointer so we can force initialization on the first
123 // reference; otherwise, some SimObjectClass constructors may be invoked
124 // before the classMap constructor.
125 map<string,SimObjectClass::CreateFunc> *SimObjectClass::classMap = NULL;
126
127 // SimObjectClass constructor: add mapping to classMap
128 SimObjectClass::SimObjectClass(const string &className, CreateFunc createFunc)
129 {
130 if (classMap == NULL)
131 classMap = new map<string,SimObjectClass::CreateFunc>();
132
133 if ((*classMap)[className])
134 {
135 cerr << "Error: simulation object class " << className << " redefined"
136 << endl;
137 fatal("");
138 }
139
140 // add className --> createFunc to class map
141 (*classMap)[className] = createFunc;
142 }
143
144
145 //
146 //
147 SimObject *
148 SimObjectClass::createObject(IniFile &configDB,
149 const string &configClassName,
150 const string &objName,
151 ConfigNode *configNode)
152 {
153 // find simulation object class name from configuration class
154 // (specified by 'type=' parameter)
155 string simObjClassName;
156
157 if (!configNode->find("type", simObjClassName)) {
158 cerr << "Configuration class '" << configClassName << "' not found."
159 << endl;
160 abort();
161 }
162
163 // look up className to get appropriate createFunc
164 if (classMap->find(simObjClassName) == classMap->end()) {
165 cerr << "Simulator object class '" << simObjClassName << "' not found."
166 << endl;
167 abort();
168 }
169
170 CreateFunc createFunc = (*classMap)[simObjClassName];
171
172 // call createFunc with config hierarchy node to get object
173 // builder instance (context with parameters for object creation)
174 SimObjectBuilder *objectBuilder = (*createFunc)(configClassName,
175 objName, configNode,
176 simObjClassName);
177
178 assert(objectBuilder != NULL);
179
180 // parse all parameters in context to generate parameter values
181 objectBuilder->parseParams(configDB);
182
183 // now create the actual simulation object
184 SimObject *object = objectBuilder->create();
185
186 assert(object != NULL);
187
188 // echo object parameters to stats file (for documenting the
189 // config used to generate the associated stats)
190 builderStream() << "[" << object->name() << "]" << endl;
191 builderStream() << "type=" << simObjClassName << endl;
192 objectBuilder->showParams(builderStream());
193 builderStream() << endl;
194
195 // done with the SimObjectBuilder now
196 delete objectBuilder;
197
198 return object;
199 }
200
201
202 //
203 // static method:
204 //
205 void
206 SimObjectClass::describeAllClasses(ostream &os)
207 {
208 map<string,CreateFunc>::iterator iter;
209
210 for (iter = classMap->begin(); iter != classMap->end(); ++iter) {
211 const string &className = iter->first;
212 CreateFunc createFunc = iter->second;
213
214 os << "[" << className << "]\n";
215
216 // create dummy object builder just to instantiate parameters
217 SimObjectBuilder *objectBuilder = (*createFunc)("", "", NULL, "");
218
219 // now get the object builder to describe ite params
220 objectBuilder->describeParams(os);
221
222 os << endl;
223
224 // done with the object builder now
225 delete objectBuilder;
226 }
227 }