2 * Copyright (c) 2002-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: Steve Reinhardt
31 #ifndef __SIM_PARAM_HH__
32 #define __SIM_PARAM_HH__
39 #include "sim/startup.hh"
47 // The context of a parameter definition... usually a subclass of
48 // SimObjectBuilder (which derives from ParamContext), but abstracted
49 // here to support more global simulator control parameters as well.
51 class ParamContext : protected StartupCallback
55 // static list of all ParamContext objects, built as a side effect
56 // of the ParamContext constructor
57 static std::list<ParamContext *> *ctxList;
61 // .ini file (database) for parameter lookup... initialized on call
65 // .ini file section for parameter lookup
66 const std::string iniSection;
68 typedef std::vector<BaseParam *> ParamList;
70 // list of parameters defined in this context
73 ParamList *getParamList() {
75 paramList = new ParamList;
81 /// Initialization phases for ParamContext objects.
83 NoAutoInit = -1, ///< Don't initialize at all... params
84 /// will be parsed later (used by
85 /// SimObjectBuilder, which parses
86 /// params in SimObject::create().
87 OutputInitPhase = 0, ///< Output stream initialization
88 TraceInitPhase = 1, ///< Trace context initialization:
89 /// depends on output streams, but
90 /// needs to come before others so we
91 /// can use tracing in other
92 /// ParamContext init code
93 StatsInitPhase = 2, ///< Stats output initialization
94 DefaultInitPhase = 3 ///< Everything else
97 /// Records the initialization phase for this ParamContext.
101 /// @param _iniSection Name of .ini section corresponding to this context.
102 /// @param _initPhase Initialization phase (see InitPhase).
103 ParamContext(const std::string &_iniSection,
104 InitPhase _initPhase = DefaultInitPhase);
106 virtual ~ParamContext() {}
108 // add a parameter to the context... called from the parameter
109 // object's constructor (see BaseParam::BaseParam())
110 void addParam(BaseParam *);
112 // call parse() on all params in this context to convert string
113 // representations to parameter values
114 virtual void parseParams(IniFile &iniFile);
116 // Check parameter values for validity & consistency. Default
117 // implementation is no-op; derive subclass & override to add
118 // actual functionality here
119 virtual void checkParams();
121 // Clean up at end of execution: close file descriptors, etc.
122 // Default implementation is no-op; derive subclass & override to
123 // add actual functionality here
124 virtual void cleanup();
126 // dump parameter descriptions
127 void describeParams(std::ostream &);
129 // Display the parameters & values used
130 void showParams(std::ostream &);
132 // print context information for parameter error
133 virtual void printErrorProlog(std::ostream &);
135 // generate the name for this instance of this context (used as a
136 // prefix to create unique names in resolveSimObject()
137 virtual const std::string &getInstanceName() { return iniSection; }
139 // Parse all parameters registered with all ParamContext objects.
140 static void parseAllContexts(IniFile &iniFile);
142 // Check all parameters registered with all ParamContext objects.
143 // (calls checkParams() on each)
144 static void checkAllContexts();
146 // Print all parameter values on indicated ostream.
147 static void showAllContexts(std::ostream &os);
149 // Clean up all registered ParamContext objects. (calls cleanup()
151 static void cleanupAllContexts();
153 // print descriptions of all parameters registered with all
154 // ParamContext objects
155 static void describeAllContexts(std::ostream &os);
160 // Base class for all parameter objects
166 ParamContext *context;
168 std::string description; // text description for help message
169 bool wasSet; // true if parameter was set by user
170 bool hasDefault; // true if parameter has default value
172 BaseParam(ParamContext *_context, const std::string &_name,
173 const std::string &_description, bool _hasDefault)
174 : context(_context), name(_name), description(_description),
175 wasSet(false), hasDefault(_hasDefault)
177 context->addParam(this);
180 virtual ~BaseParam() {}
182 // a parameter is valid only if its value was set by the user or
183 // it has a default value
186 return (wasSet || hasDefault);
189 // set value by parsing string
190 virtual void parse(const std::string &s) = 0;
192 // display value to stream
193 virtual void showValue(std::ostream &) const = 0;
195 // display type to stream
196 virtual void showType(std::ostream &) const = 0;
198 // signal parse or usage error
199 virtual void die(const std::string &err) const;
203 // Template classes to specialize parameters to specific types.
205 // Param<T> is for single-valued (scalar) parameters of type T.
206 // VectorParam<T> is for multi-valued (vector) parameters of type T.
207 // These are specified in the .ini file as a space-delimited list of
211 class Param : public BaseParam
219 // Param with default value: set value to default
220 Param(ParamContext *context,
221 const std::string &name, const std::string &description, T dfltValue)
222 : BaseParam(context, name, description, true),
227 // Param with no default value: leave value uninitialized
228 Param(ParamContext *context,
229 const std::string &name, const std::string &description)
230 : BaseParam(context, name, description, false)
238 // if we attempt to reference an invalid parameter (i.e., one
239 // with no default value that was not set by the user), die.
245 // display value to stream
246 virtual void showValue(std::ostream &os) const;
248 // display type to stream
249 virtual void showType(std::ostream &) const;
251 // set value by parsing string
252 virtual void parse(const std::string &s);
257 // Template class for vector-valued parameters (lists)
260 class VectorParam : public BaseParam
264 std::vector<T> value;
268 typedef typename std::vector<T>::size_type size_type;
270 // Param with default value: set value to default
271 VectorParam(ParamContext *context, const std::string &name,
272 const std::string &description,
273 const std::vector<T> &dfltValue)
274 : BaseParam(context, name, description, true),
279 // Param with no default value: leave value uninitialized
280 VectorParam(ParamContext *context,
281 const std::string &name, const std::string &description)
282 : BaseParam(context, name, description, false)
286 virtual ~VectorParam() {}
288 // basic vector access methods
289 size_type size() const
296 const T &operator[](size_type n) const
303 // return reference to value vector
304 operator std::vector<T>&()
311 // display value to stream
312 virtual void showValue(std::ostream &os) const;
314 // display type to stream
315 virtual void showType(std::ostream &) const;
317 // set value by parsing string
318 virtual void parse(const std::string &s);
322 // Specialization of Param<int> and VectorParam<int> to handle
323 // enumerated types is done in two ways, using SimpleEnumParam and
324 // MappedEnumParam (and their vector counterparts,
325 // SimpleEnumVectorParam and MappedEnumVectorParam). SimpleEnumParam
326 // takes an array of strings and maps them to integers based on array
327 // index. MappedEnumParam takes an array of string-to-int mappings,
328 // allowing for mapping strings to non-contiguous integer values, or
329 // mapping multiple strings to the same integer value.
331 // Both SimpleEnumParam and MappedEnumParam are implemented using a
332 // single template class, EnumParam<Map>, which takes the type of the map
333 // as a parameter (const char * or EnumParamMap). Similarly,
334 // SimpleEnumVectorParam and MappedEnumVectorParam are both
335 // implemented using EnumVectorParam<Map>.
338 class EnumParam : public Param<int>
340 const int num_values;
345 // Param with default value: set value to default
346 EnumParam(ParamContext *context,
347 const std::string &name, const std::string &description,
348 const Map *_map, int _num_values,
350 : Param<int>(context, name, description, dfltValue),
351 num_values(_num_values), map(_map)
355 // Param with no default value: leave value uninitialized
356 EnumParam(ParamContext *context,
357 const std::string &name, const std::string &description,
358 const Map *_map, int _num_values)
359 : Param<int>(context, name, description),
360 num_values(_num_values), map(_map)
364 virtual ~EnumParam() {}
366 // display value to stream
367 virtual void showValue(std::ostream &os) const;
369 // display type to stream
370 virtual void showType(std::ostream &) const;
372 // set value by parsing string
373 virtual void parse(const std::string &s);
377 // Vector counterpart to SimpleEnumParam
380 class EnumVectorParam : public VectorParam<int>
382 const int num_values;
387 // Param with default value: set value to default
388 EnumVectorParam(ParamContext *context,
389 const std::string &name, const std::string &description,
390 const Map *_map, int _num_values,
391 std::vector<int> &dfltValue)
392 : VectorParam<int>(context, name, description, dfltValue),
393 num_values(_num_values), map(_map)
397 // Param with no default value: leave value uninitialized
398 EnumVectorParam(ParamContext *context,
399 const std::string &name, const std::string &description,
400 const Map *_map, int _num_values)
401 : VectorParam<int>(context, name, description),
402 num_values(_num_values), map(_map)
406 virtual ~EnumVectorParam() {}
408 // display value to stream
409 virtual void showValue(std::ostream &os) const;
411 // display type to stream
412 virtual void showType(std::ostream &) const;
414 // set value by parsing string
415 virtual void parse(const std::string &s);
418 // Specialize EnumParam for a particular enumeration type ENUM
419 // (automates casting to get value of enum type)
421 template <class ENUM>
422 class SimpleEnumParam : public EnumParam<const char *>
426 SimpleEnumParam(ParamContext *context,
427 const std::string &name, const std::string &description,
428 const char **_map, int _num_values,
430 : EnumParam<const char *>(context, name, description,
431 _map, _num_values, (int)dfltValue)
435 SimpleEnumParam(ParamContext *context,
436 const std::string &name, const std::string &description,
437 const char **_map, int _num_values)
438 : EnumParam<const char *>(context, name, description,
443 operator ENUM() const
452 // Specialize EnumParam for a particular enumeration type ENUM
453 // (automates casting to get value of enum type)
455 template <class ENUM>
456 class SimpleEnumVectorParam : public EnumVectorParam<const char *>
460 // skip default value constructor: too much pain to convert
461 // vector<ENUM> initializer to vector<int>
464 SimpleEnumVectorParam(ParamContext *context,
465 const std::string &name,
466 const std::string &description,
467 const char **_map, int _num_values)
468 : EnumVectorParam<const char *>(context, name, description,
473 ENUM operator[](size_type n)
477 return (ENUM)value[n];
483 // Handle enums via string-to-int map (see comment above).
486 // An array of string-to-int mappings must be supplied using the
493 // Specialize EnumParam for a particular enumeration type ENUM
494 // (automates casting to get value of enum type)
496 template <class ENUM>
497 class MappedEnumParam : public EnumParam<EnumParamMap>
501 MappedEnumParam(ParamContext *context,
502 const std::string &name, const std::string &description,
503 const EnumParamMap *_map, int _num_values,
505 : EnumParam<EnumParamMap>(context, name, description,
506 _map, _num_values, (int)dfltValue)
510 MappedEnumParam(ParamContext *context,
511 const std::string &name, const std::string &description,
512 const EnumParamMap *_map, int _num_values)
513 : EnumParam<EnumParamMap>(context, name, description,
522 return (ENUM)value[this->n];
527 // Specialize EnumParam for a particular enumeration type ENUM
528 // (automates casting to get value of enum type)
530 template <class ENUM>
531 class MappedEnumVectorParam : public EnumVectorParam<EnumParamMap>
535 // skip default value constructor: too much pain to convert
536 // vector<ENUM> initializer to vector<int>
539 MappedEnumVectorParam(ParamContext *context,
540 const std::string &name,
541 const std::string &description,
542 const EnumParamMap *_map, int _num_values)
543 : EnumVectorParam<EnumParamMap>(context, name, description,
548 ENUM operator[](size_type n)
552 return (ENUM)value[n];
558 // Parameters that point to other simulation objects (e.g. caches,
559 // busses, etc.) are handled by specializing SimObjectBaseParam to the
560 // specific subtype. The main purpose of SimObjectBaseParam is to
561 // provide a place to stick several helper functions common to all
562 // SimObject-derived parameters.
564 class SimObjectBaseParam : public BaseParam
568 SimObjectBaseParam(ParamContext *context, const std::string &name,
569 const std::string &description, bool hasDefault)
570 : BaseParam(context, name, description, hasDefault)
574 virtual ~SimObjectBaseParam() {}
576 // helper function for SimObjectParam<T>::showValue()
577 void showValue(std::ostream &os, SimObject *obj) const;
579 // helper function for SimObjectParam<T>::parse()
580 void parse(const std::string &s, SimObject *&value);
582 // helper function for SimObjectParam<T>::parse()
583 void parse(const std::string &s, std::vector<SimObject *>&value_vec);
588 // Parameter to a specific type of SimObject. Note that T must be a
589 // pointer to a class derived from SimObject (e.g., <CPU *>).
592 template <class T> class SimObjectParam;
595 class SimObjectParam<T *> : public SimObjectBaseParam
603 // initialization w/o default
604 SimObjectParam(ParamContext *context,
605 const std::string &name, const std::string &description)
606 : SimObjectBaseParam(context, name, description, false)
610 // initialization wit=h default
611 SimObjectParam(ParamContext *context,
612 const std::string &name, const std::string &description,
614 : SimObjectBaseParam(context, name, description, true),
619 virtual ~SimObjectParam() {}
621 // convert to pointer
629 T *operator->() const
636 // display value to stream
637 virtual void showValue(std::ostream &os) const
639 SimObjectBaseParam::showValue(os, value);
642 // display type to stream: see REGISTER_SIM_OBJECT macro in
643 // sim_object.hh for declaration
644 virtual void showType(std::ostream &os) const;
646 // set value by parsing string
647 virtual void parse(const std::string &s)
650 // first parse to generic SimObject *
651 SimObjectBaseParam::parse(s, so_ptr);
652 // now dynamic_cast to specific derived type
653 value = dynamic_cast<T *>(so_ptr);
654 // check for failure of dynamic_cast
655 if (value == NULL && so_ptr != NULL)
656 die("not of appropriate type");
662 // Vector counterpart to SimObjectParam<T>
665 template <class T> class SimObjectVectorParam;
668 class SimObjectVectorParam<T *> : public SimObjectBaseParam
672 std::vector<T *> value;
676 typedef typename std::vector<T *>::size_type size_type;
678 SimObjectVectorParam(ParamContext *context,
679 const std::string &name,
680 const std::string &description)
681 : SimObjectBaseParam(context, name, description, false)
685 SimObjectVectorParam(ParamContext *context,
686 const std::string &name,
687 const std::string &description,
688 std::vector<T *> dfltValue)
689 : SimObjectBaseParam(context, name, description, true),
694 virtual ~SimObjectVectorParam() {}
696 // basic vector access methods
697 size_type size() const
704 T *&operator[](size_type n)
711 // return reference to value vector
712 operator std::vector<T *>&()
719 // display value to stream
720 virtual void showValue(std::ostream &os) const
722 for (int i = 0; i < value.size(); i++) {
725 SimObjectBaseParam::showValue(os, value[i]);
729 // display type to stream: see
730 virtual void showType(std::ostream &os) const;
732 // set value by parsing string
733 virtual void parse(const std::string &s)
735 std::vector<SimObject *> so_ptr_vec;
736 // first parse to generic SimObject * vector (from SimObjectBaseParam)
737 SimObjectBaseParam::parse(s, so_ptr_vec);
739 value.resize(so_ptr_vec.size());
741 for (int i = 0; i < so_ptr_vec.size(); ++i) {
742 // now dynamic_cast to specific derived type
743 value[i] = dynamic_cast<T *>(so_ptr_vec[i]);
744 // check for failure of dynamic_cast
745 if (value[i] == NULL && so_ptr_vec[i] != NULL)
746 die("not of appropriate type");
752 // Macro to define showType() methods for SimObjectParam &
753 // SimObjectVectorParam. Can't do this automatically as it requires a
754 // string name for the type, which you can't get from a template
755 // argument. For concrete derived SimObject types, this macro is
756 // automatically invoked by REGISTER_SIM_OBJECT() (see sim_object.hh).
758 #define DEFINE_SIM_OBJECT_CLASS_NAME(CLASS_NAME, OBJ_CLASS) \
761 SimObjectParam<OBJ_CLASS *>::showType(std::ostream &os) const \
768 SimObjectVectorParam<OBJ_CLASS *>::showType(std::ostream &os) const \
770 os << "vector of " << CLASS_NAME; \
775 // Declarations for low-level parsing & displaying functions. These
776 // are used internally, but should not be used directly by clients of
777 // the parameter mechanism, but are declared here so they can be
778 // shared with the serialization code (see sim/serialize.cc).
779 template <class T> bool parseParam(const std::string &str, T &data);
780 template <class T> void showParam(std::ostream &os, const T &data);
782 #endif // _SIM_PARAM_HH_