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