If we're running the quick tests, remove the SPARC_FS build since we don't have any...
[gem5.git] / src / sim / builder.cc
1 /*
2 * Copyright (c) 2003-2005 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 * Authors: Nathan Binkert
29 */
30
31 #include <assert.h>
32
33 #include "base/inifile.hh"
34 #include "base/misc.hh"
35 #include "sim/builder.hh"
36 #include "sim/host.hh"
37 #include "sim/sim_object.hh"
38 #include "sim/root.hh"
39
40 using namespace std;
41
42 SimObjectBuilder::SimObjectBuilder(const std::string &_iniSection)
43 : ParamContext(_iniSection)
44 {
45 }
46
47 SimObjectBuilder::~SimObjectBuilder()
48 {
49 }
50
51 ///////////////////////////////////////////
52 //
53 // SimObjectBuilder member definitions
54 //
55 ///////////////////////////////////////////
56
57 // override ParamContext::parseParams() to check params based on
58 // instance name first. If not found, then check based on iniSection
59 // (as in default ParamContext implementation).
60 void
61 SimObjectBuilder::parseParams(IniFile &iniFile)
62 {
63 iniFilePtr = &iniFile; // set object member
64
65 ParamList::iterator i;
66
67 for (i = paramList->begin(); i != paramList->end(); ++i) {
68 string string_value;
69 if (iniFile.find(iniSection, (*i)->name, string_value))
70 (*i)->parse(string_value);
71 }
72 }
73
74
75 void
76 SimObjectBuilder::printErrorProlog(ostream &os)
77 {
78 ccprintf(os, "Error creating object '%s':\n", iniSection);
79 }
80
81
82 ////////////////////////////////////////////////////////////////////////
83 //
84 // SimObjectClass member definitions
85 //
86 ////////////////////////////////////////////////////////////////////////
87
88 // Map of class names to SimObjectBuilder creation functions. Need to
89 // make this a pointer so we can force initialization on the first
90 // reference; otherwise, some SimObjectClass constructors may be invoked
91 // before the classMap constructor.
92 map<string,SimObjectClass::CreateFunc> *SimObjectClass::classMap = NULL;
93
94 // SimObjectClass constructor: add mapping to classMap
95 SimObjectClass::SimObjectClass(const string &className, CreateFunc createFunc)
96 {
97 if (classMap == NULL)
98 classMap = new map<string,SimObjectClass::CreateFunc>();
99
100 if ((*classMap)[className])
101 panic("Error: simulation object class '%s' redefined\n", className);
102
103 // add className --> createFunc to class map
104 (*classMap)[className] = createFunc;
105 }
106
107
108 //
109 //
110 SimObject *
111 SimObjectClass::createObject(IniFile &configDB, const std::string &iniSection)
112 {
113 string type;
114 if (!configDB.find(iniSection, "type", type)) {
115 // no C++ type associated with this object
116 return NULL;
117 }
118
119 // look up className to get appropriate createFunc
120 if (classMap->find(type) == classMap->end())
121 panic("Simulator object type '%s' not found.\n", type);
122
123
124 CreateFunc createFunc = (*classMap)[type];
125
126 // call createFunc with config hierarchy node to get object
127 // builder instance (context with parameters for object creation)
128 SimObjectBuilder *objectBuilder = (*createFunc)(iniSection);
129
130 assert(objectBuilder != NULL);
131
132 // parse all parameters in context to generate parameter values
133 objectBuilder->parseParams(configDB);
134
135 // now create the actual simulation object
136 SimObject *object = objectBuilder->create();
137
138 assert(object != NULL);
139
140 // echo object parameters to stats file (for documenting the
141 // config used to generate the associated stats)
142 ccprintf(*configStream, "[%s]\n", object->name());
143 ccprintf(*configStream, "type=%s\n", type);
144 objectBuilder->showParams(*configStream);
145 ccprintf(*configStream, "\n");
146
147 // done with the SimObjectBuilder now
148 delete objectBuilder;
149
150 return object;
151 }
152
153
154 //
155 // static method:
156 //
157 void
158 SimObjectClass::describeAllClasses(ostream &os)
159 {
160 map<string,CreateFunc>::iterator iter;
161
162 for (iter = classMap->begin(); iter != classMap->end(); ++iter) {
163 const string &className = iter->first;
164 CreateFunc createFunc = iter->second;
165
166 os << "[" << className << "]\n";
167
168 // create dummy object builder just to instantiate parameters
169 SimObjectBuilder *objectBuilder = (*createFunc)("");
170
171 // now get the object builder to describe ite params
172 objectBuilder->describeParams(os);
173
174 os << endl;
175
176 // done with the object builder now
177 delete objectBuilder;
178 }
179 }