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