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