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