2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Nathan Binkert
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"
42 SimObjectBuilder::SimObjectBuilder(const std::string
&_iniSection
)
43 : ParamContext(_iniSection
)
47 SimObjectBuilder::~SimObjectBuilder()
51 ///////////////////////////////////////////
53 // SimObjectBuilder member definitions
55 ///////////////////////////////////////////
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).
61 SimObjectBuilder::parseParams(IniFile
&iniFile
)
63 iniFilePtr
= &iniFile
; // set object member
65 ParamList::iterator i
;
67 for (i
= paramList
->begin(); i
!= paramList
->end(); ++i
) {
69 if (iniFile
.find(iniSection
, (*i
)->name
, string_value
))
70 (*i
)->parse(string_value
);
76 SimObjectBuilder::printErrorProlog(ostream
&os
)
78 ccprintf(os
, "Error creating object '%s':\n", iniSection
);
82 ////////////////////////////////////////////////////////////////////////
84 // SimObjectClass member definitions
86 ////////////////////////////////////////////////////////////////////////
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
;
94 // SimObjectClass constructor: add mapping to classMap
95 SimObjectClass::SimObjectClass(const string
&className
, CreateFunc createFunc
)
98 classMap
= new map
<string
,SimObjectClass::CreateFunc
>();
100 if ((*classMap
)[className
])
101 panic("Error: simulation object class '%s' redefined\n", className
);
103 // add className --> createFunc to class map
104 (*classMap
)[className
] = createFunc
;
111 SimObjectClass::createObject(IniFile
&configDB
, const std::string
&iniSection
)
114 if (!configDB
.find(iniSection
, "type", type
)) {
115 // no C++ type associated with this object
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
);
124 CreateFunc createFunc
= (*classMap
)[type
];
126 // call createFunc with config hierarchy node to get object
127 // builder instance (context with parameters for object creation)
128 SimObjectBuilder
*objectBuilder
= (*createFunc
)(iniSection
);
130 assert(objectBuilder
!= NULL
);
132 // parse all parameters in context to generate parameter values
133 objectBuilder
->parseParams(configDB
);
135 // now create the actual simulation object
136 SimObject
*object
= objectBuilder
->create();
138 assert(object
!= NULL
);
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");
147 // done with the SimObjectBuilder now
148 delete objectBuilder
;
158 SimObjectClass::describeAllClasses(ostream
&os
)
160 map
<string
,CreateFunc
>::iterator iter
;
162 for (iter
= classMap
->begin(); iter
!= classMap
->end(); ++iter
) {
163 const string
&className
= iter
->first
;
164 CreateFunc createFunc
= iter
->second
;
166 os
<< "[" << className
<< "]\n";
168 // create dummy object builder just to instantiate parameters
169 SimObjectBuilder
*objectBuilder
= (*createFunc
)("");
171 // now get the object builder to describe ite params
172 objectBuilder
->describeParams(os
);
176 // done with the object builder now
177 delete objectBuilder
;