Merge zizzer.eecs.umich.edu:/m5/Bitkeeper/m5
[gem5.git] / sim / builder.hh
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 #ifndef __BUILDER_HH__
30 #define __BUILDER_HH__
31
32 #include <iosfwd>
33 #include <list>
34 #include <map>
35 #include <vector>
36
37 #include "sim/param.hh"
38
39 class SimObject;
40
41 std::ostream &
42 builderStream();
43
44 //
45 // A SimObjectBuilder serves as an evaluation context for a set of
46 // parameters that describe a specific instance of a SimObject. This
47 // evaluation context corresponds to a section in the .ini file (as
48 // with the base ParamContext) plus an optional node in the
49 // configuration hierarchy (the configNode member) for resolving
50 // SimObject references. SimObjectBuilder is an abstract superclass;
51 // derived classes specialize the class for particular subclasses of
52 // SimObject (e.g., BaseCache).
53 //
54 // For typical usage, see the definition of
55 // SimObjectClass::createObject().
56 //
57 class SimObjectBuilder : public ParamContext
58 {
59 private:
60 // name of the instance we are creating
61 std::string instanceName;
62
63 // The corresponding node in the configuration hierarchy.
64 // (optional: may be null if the created object is not in the
65 // hierarchy)
66 ConfigNode *configNode;
67
68 // The external SimObject class name (for error messages)
69 std::string simObjClassName;
70
71 public:
72 SimObjectBuilder(const std::string &_configClass,
73 const std::string &_instanceName,
74 ConfigNode *_configNode,
75 const std::string &_simObjClassName);
76
77 virtual ~SimObjectBuilder();
78
79 // call parse() on all params in this context to convert string
80 // representations to parameter values
81 virtual void parseParams(IniFile &iniFile);
82
83 // parameter error prolog (override of ParamContext)
84 virtual void printErrorProlog(std::ostream &);
85
86 // generate the name for this SimObject instance (derived from the
87 // configuration hierarchy node label and position)
88 virtual const std::string &getInstanceName() { return instanceName; }
89
90 // return the configuration hierarchy node for this context.
91 virtual ConfigNode *getConfigNode() { return configNode; }
92
93 // Create the actual SimObject corresponding to the parameter
94 // values in this context. This function is overridden in derived
95 // classes to call a specific constructor for a particular
96 // subclass of SimObject.
97 virtual SimObject *create() = 0;
98 };
99
100
101 //
102 // Handy macros for initializing parameter members of classes derived
103 // from SimObjectBuilder. Assumes that the name of the parameter
104 // member object is the same as the textual parameter name seen by the
105 // user. (Note that '#p' is expanded by the preprocessor to '"p"'.)
106 //
107 #define INIT_PARAM(p, desc) p(this, #p, desc)
108 #define INIT_PARAM_DFLT(p, desc, dflt) p(this, #p, desc, dflt)
109
110 //
111 // Initialize an enumeration variable... assumes that 'map' is the
112 // name of an array of mappings (char * for SimpleEnumParam, or
113 // EnumParamMap for MappedEnumParam).
114 //
115 #define INIT_ENUM_PARAM(p, desc, map) \
116 p(this, #p, desc, map, sizeof(map)/sizeof(map[0]))
117 #define INIT_ENUM_PARAM_DFLT(p, desc, map, dflt) \
118 p(this, #p, desc, map, sizeof(map)/sizeof(map[0]), dflt)
119
120 //
121 // An instance of SimObjectClass corresponds to a class derived from
122 // SimObject. The SimObjectClass instance serves to bind the string
123 // name (found in the config file) to a function that creates an
124 // instance of the appropriate derived class.
125 //
126 // This would be much cleaner in Smalltalk or Objective-C, where types
127 // are first-class objects themselves.
128 //
129 class SimObjectClass
130 {
131 public:
132 // Type CreateFunc is a pointer to a function that creates a new
133 // simulation object builder based on a .ini-file parameter
134 // section (specified by the first string argument), a unique name
135 // for the object (specified by the second string argument), and
136 // an optional config hierarchy node (specified by the third
137 // argument). A pointer to the new SimObjectBuilder is returned.
138 typedef SimObjectBuilder *(*CreateFunc)(const std::string &configClassName,
139 const std::string &objName,
140 ConfigNode *configNode,
141 const std::string &simObjClassName);
142
143 static std::map<std::string,CreateFunc> *classMap;
144
145 // Constructor. For example:
146 //
147 // SimObjectClass baseCacheClass("BaseCache", newBaseCacheBuilder);
148 //
149 SimObjectClass(const std::string &className, CreateFunc createFunc);
150
151 // create SimObject given name of class and pointer to
152 // configuration hierarchy node
153 static SimObject *createObject(IniFile &configDB,
154 const std::string &configClassName,
155 const std::string &objName,
156 ConfigNode *configNode);
157
158 // print descriptions of all parameters registered with all
159 // SimObject classes
160 static void describeAllClasses(std::ostream &os);
161 };
162
163 //
164 // Macros to encapsulate the magic of declaring & defining
165 // SimObjectBuilder and SimObjectClass objects
166 //
167
168 #define BEGIN_DECLARE_SIM_OBJECT_PARAMS(OBJ_CLASS) \
169 class OBJ_CLASS##Builder : public SimObjectBuilder \
170 { \
171 public:
172
173 #define END_DECLARE_SIM_OBJECT_PARAMS(OBJ_CLASS) \
174 \
175 OBJ_CLASS##Builder(const std::string &configClass, \
176 const std::string &instanceName, \
177 ConfigNode *configNode, \
178 const std::string &simObjClassName); \
179 virtual ~OBJ_CLASS##Builder() {} \
180 \
181 OBJ_CLASS *create(); \
182 };
183
184 #define BEGIN_INIT_SIM_OBJECT_PARAMS(OBJ_CLASS) \
185 OBJ_CLASS##Builder::OBJ_CLASS##Builder(const std::string &configClass, \
186 const std::string &instanceName, \
187 ConfigNode *configNode, \
188 const std::string &simObjClassName) \
189 : SimObjectBuilder(configClass, instanceName, \
190 configNode, simObjClassName),
191
192
193 #define END_INIT_SIM_OBJECT_PARAMS(OBJ_CLASS) \
194 { \
195 }
196
197 #define CREATE_SIM_OBJECT(OBJ_CLASS) \
198 OBJ_CLASS *OBJ_CLASS##Builder::create()
199
200 #define REGISTER_SIM_OBJECT(CLASS_NAME, OBJ_CLASS) \
201 SimObjectBuilder * \
202 new##OBJ_CLASS##Builder(const std::string &configClass, \
203 const std::string &instanceName, \
204 ConfigNode *configNode, \
205 const std::string &simObjClassName) \
206 { \
207 return new OBJ_CLASS##Builder(configClass, instanceName, \
208 configNode, simObjClassName); \
209 } \
210 \
211 SimObjectClass the##OBJ_CLASS##Class(CLASS_NAME, \
212 new##OBJ_CLASS##Builder); \
213 \
214 /* see param.hh */ \
215 DEFINE_SIM_OBJECT_CLASS_NAME(CLASS_NAME, OBJ_CLASS)
216
217
218 #endif // __BUILDER_HH__