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