Minor restructuring of Python config code, mostly to avoid walking
[gem5.git] / util / config / m5configbase.py
1 from __future__ import generators
2
3 import os
4 import re
5 import sys
6
7 #####################################################################
8 #
9 # M5 Python Configuration Utility
10 #
11 # The basic idea is to write simple Python programs that build Python
12 # objects corresponding to M5 SimObjects for the deisred simulation
13 # configuration. For now, the Python emits a .ini file that can be
14 # parsed by M5. In the future, some tighter integration between M5
15 # and the Python interpreter may allow bypassing the .ini file.
16 #
17 # Each SimObject class in M5 is represented by a Python class with the
18 # same name. The Python inheritance tree mirrors the M5 C++ tree
19 # (e.g., SimpleCPU derives from BaseCPU in both cases, and all
20 # SimObjects inherit from a single SimObject base class). To specify
21 # an instance of an M5 SimObject in a configuration, the user simply
22 # instantiates the corresponding Python object. The parameters for
23 # that SimObject are given by assigning to attributes of the Python
24 # object, either using keyword assignment in the constructor or in
25 # separate assignment statements. For example:
26 #
27 # cache = BaseCache('my_cache', root, size=64*K)
28 # cache.hit_latency = 3
29 # cache.assoc = 8
30 #
31 # (The first two constructor arguments specify the name of the created
32 # cache and its parent node in the hierarchy.)
33 #
34 # The magic lies in the mapping of the Python attributes for SimObject
35 # classes to the actual SimObject parameter specifications. This
36 # allows parameter validity checking in the Python code. Continuing
37 # the example above, the statements "cache.blurfl=3" or
38 # "cache.assoc='hello'" would both result in runtime errors in Python,
39 # since the BaseCache object has no 'blurfl' parameter and the 'assoc'
40 # parameter requires an integer, respectively. This magic is done
41 # primarily by overriding the special __setattr__ method that controls
42 # assignment to object attributes.
43 #
44 # The Python module provides another class, ConfigNode, which is a
45 # superclass of SimObject. ConfigNode implements the parent/child
46 # relationship for building the configuration hierarchy tree.
47 # Concrete instances of ConfigNode can be used to group objects in the
48 # hierarchy, but do not correspond to SimObjects themselves (like a
49 # .ini section with "children=" but no "type=".
50 #
51 # Once a set of Python objects have been instantiated in a hierarchy,
52 # calling 'instantiate(obj)' (where obj is the root of the hierarchy)
53 # will generate a .ini file. See simple-4cpu.py for an example
54 # (corresponding to m5-test/simple-4cpu.ini).
55 #
56 #####################################################################
57
58 #####################################################################
59 #
60 # ConfigNode/SimObject classes
61 #
62 # The Python class hierarchy rooted by ConfigNode (which is the base
63 # class of SimObject, which in turn is the base class of all other M5
64 # SimObject classes) has special attribute behavior. In general, an
65 # object in this hierarchy has three categories of attribute-like
66 # things:
67 #
68 # 1. Regular Python methods and variables. These must start with an
69 # underscore to be treated normally.
70 #
71 # 2. SimObject parameters. These values are stored as normal Python
72 # attributes, but all assignments to these attributes are checked
73 # against the pre-defined set of parameters stored in the class's
74 # _param_dict dictionary. Assignments to attributes that do not
75 # correspond to predefined parameters, or that are not of the correct
76 # type, incur runtime errors.
77 #
78 # 3. Hierarchy children. The child nodes of a ConfigNode are stored
79 # in the node's _children dictionary, but can be accessed using the
80 # Python attribute dot-notation (just as they are printed out by the
81 # simulator). Children cannot be created using attribute assigment;
82 # they must be added by specifying the parent node in the child's
83 # constructor or using the '+=' operator.
84
85 # The SimObject parameters are the most complex, for a few reasons.
86 # First, both parameter descriptions and parameter values are
87 # inherited. Thus parameter description lookup must go up the
88 # inheritance chain like normal attribute lookup, but this behavior
89 # must be explicitly coded since the lookup occurs in each class's
90 # _param_dict attribute. Second, because parameter values can be set
91 # on SimObject classes (to implement default values), the parameter
92 # checking behavior must be enforced on class attribute assignments as
93 # well as instance attribute assignments. Finally, because we allow
94 # class specialization via inheritance (e.g., see the L1Cache class in
95 # the simple-4cpu.py example), we must do parameter checking even on
96 # class instantiation. To provide all these features, we use a
97 # metaclass to define most of the SimObject parameter behavior for
98 # this class hierarchy.
99 #
100 #####################################################################
101
102 # The metaclass for ConfigNode (and thus for everything that derives
103 # from ConfigNode, including SimObject). This class controls how new
104 # classes that derive from ConfigNode are instantiated, and provides
105 # inherited class behavior (just like a class controls how instances
106 # of that class are instantiated, and provides inherited instance
107 # behavior).
108 class MetaConfigNode(type):
109
110 # __new__ is called before __init__, and is where the statements
111 # in the body of the class definition get loaded into the class's
112 # __dict__. We intercept this to filter out parameter assignments
113 # and only allow "private" attributes to be passed to the base
114 # __new__ (starting with underscore).
115 def __new__(cls, name, bases, dict):
116 priv_keys = [k for k in dict.iterkeys() if k.startswith('_')]
117 priv_dict = {}
118 for k in priv_keys: priv_dict[k] = dict[k]; del dict[k]
119 # entries left in dict will get passed to __init__, where we'll
120 # deal with them as params.
121 return super(MetaConfigNode, cls).__new__(cls, name, bases, priv_dict)
122
123 # initialization: start out with an empty param dict (makes life
124 # simpler if we can assume _param_dict is always valid). Also
125 # build inheritance list to simplify searching for inherited
126 # params. Finally set parameters specified in class definition
127 # (if any).
128 def __init__(cls, name, bases, dict):
129 super(MetaConfigNode, cls).__init__(cls, name, bases, {})
130 # initialize _param_dict to empty
131 cls._param_dict = {}
132 # __mro__ is the ordered list of classes Python uses for
133 # method resolution. We want to pick out the ones that have a
134 # _param_dict attribute for doing parameter lookups.
135 cls._param_bases = \
136 [c for c in cls.__mro__ if hasattr(c, '_param_dict')]
137 # initialize attributes with values from class definition
138 for (pname, value) in dict.items():
139 try:
140 setattr(cls, pname, value)
141 except Exception, exc:
142 print "Error setting '%s' to '%s' on class '%s'\n" \
143 % (pname, value, cls.__name__), exc
144
145 # set the class's parameter dictionary (called when loading
146 # class descriptions)
147 def set_param_dict(cls, param_dict):
148 # should only be called once (current one should be empty one
149 # from __init__)
150 assert not cls._param_dict
151 cls._param_dict = param_dict
152 # initialize attributes with default values
153 for (pname, param) in param_dict.items():
154 try:
155 setattr(cls, pname, param.default)
156 except Exception, exc:
157 print "Error setting '%s' default on class '%s'\n" \
158 % (pname, cls.__name__), exc
159
160 # Set the class's parameter dictionary given a code string of
161 # parameter initializers (as from an object description file).
162 # Note that the caller must pass in the namespace in which to
163 # execute the code (usually the caller's globals()), since if we
164 # call globals() from inside this function all we get is this
165 # module's internal scope.
166 def init_params(cls, init_code, ctx):
167 dict = {}
168 try:
169 exec fixPythonIndentation(init_code) in ctx, dict
170 except Exception, exc:
171 print "Error in %s.init_params:" % cls.__name__, exc
172 raise
173 cls.set_param_dict(dict)
174
175 # Lookup a parameter description by name in the given class. Use
176 # the _param_bases list defined in __init__ to go up the
177 # inheritance hierarchy if necessary.
178 def lookup_param(cls, param_name):
179 for c in cls._param_bases:
180 param = c._param_dict.get(param_name)
181 if param: return param
182 return None
183
184 # Set attribute (called on foo.attr_name = value when foo is an
185 # instance of class cls).
186 def __setattr__(cls, attr_name, value):
187 # normal processing for private attributes
188 if attr_name.startswith('_'):
189 object.__setattr__(cls, attr_name, value)
190 return
191 # no '_': must be SimObject param
192 param = cls.lookup_param(attr_name)
193 if not param:
194 raise AttributeError, \
195 "Class %s has no parameter %s" % (cls.__name__, attr_name)
196 # It's ok: set attribute by delegating to 'object' class.
197 # Note the use of param.make_value() to verify/canonicalize
198 # the assigned value
199 object.__setattr__(cls, attr_name, param.make_value(value))
200
201 # generator that iterates across all parameters for this class and
202 # all classes it inherits from
203 def all_param_names(cls):
204 for c in cls._param_bases:
205 for p in c._param_dict.iterkeys():
206 yield p
207
208 # The ConfigNode class is the root of the special hierarchy. Most of
209 # the code in this class deals with the configuration hierarchy itself
210 # (parent/child node relationships).
211 class ConfigNode(object):
212 # Specify metaclass. Any class inheriting from ConfigNode will
213 # get this metaclass.
214 __metaclass__ = MetaConfigNode
215
216 # Constructor. Since bare ConfigNodes don't have parameters, just
217 # worry about the name and the parent/child stuff.
218 def __init__(self, _name, _parent=None):
219 # Type-check _name
220 if type(_name) != str:
221 if isinstance(_name, ConfigNode):
222 # special case message for common error of trying to
223 # coerce a SimObject to the wrong type
224 raise TypeError, \
225 "Attempt to coerce %s to %s" \
226 % (_name.__class__.__name__, self.__class__.__name__)
227 else:
228 raise TypeError, \
229 "%s name must be string (was %s, %s)" \
230 % (self.__class__.__name__, _name, type(_name))
231 # if specified, parent must be a subclass of ConfigNode
232 if _parent != None and not isinstance(_parent, ConfigNode):
233 raise TypeError, \
234 "%s parent must be ConfigNode subclass (was %s, %s)" \
235 % (self.__class__.__name__, _name, type(_name))
236 self._name = _name
237 self._parent = _parent
238 if (_parent):
239 _parent._add_child(self)
240 self._children = {}
241 # keep a list of children in addition to the dictionary keys
242 # so we can remember the order they were added and print them
243 # out in that order.
244 self._child_list = []
245
246 # When printing (e.g. to .ini file), just give the name.
247 def __str__(self):
248 return self._name
249
250 # Catch attribute accesses that could be requesting children, and
251 # satisfy them. Note that __getattr__ is called only if the
252 # regular attribute lookup fails, so private and parameter lookups
253 # will already be satisfied before we ever get here.
254 def __getattr__(self, name):
255 try:
256 return self._children[name]
257 except KeyError:
258 raise AttributeError, \
259 "Node '%s' has no attribute or child '%s'" \
260 % (self._name, name)
261
262 # Set attribute. All attribute assignments go through here. Must
263 # be private attribute (starts with '_') or valid parameter entry.
264 # Basically identical to MetaConfigClass.__setattr__(), except
265 # this sets attributes on specific instances rather than on classes.
266 def __setattr__(self, attr_name, value):
267 if attr_name.startswith('_'):
268 object.__setattr__(self, attr_name, value)
269 return
270 # not private; look up as param
271 param = self.__class__.lookup_param(attr_name)
272 if not param:
273 raise AttributeError, \
274 "Class %s has no parameter %s" \
275 % (self.__class__.__name__, attr_name)
276 # It's ok: set attribute by delegating to 'object' class.
277 # Note the use of param.make_value() to verify/canonicalize
278 # the assigned value.
279 v = param.make_value(value)
280 object.__setattr__(self, attr_name, v)
281
282 # A little convenient magic: if the parameter is a ConfigNode
283 # (or vector of ConfigNodes, or anything else with a
284 # '_set_parent_if_none' function attribute) that does not have
285 # a parent (and so is not part of the configuration
286 # hierarchy), then make this node its parent.
287 if hasattr(v, '_set_parent_if_none'):
288 v._set_parent_if_none(self)
289
290 def _path(self):
291 # Return absolute path from root.
292 if not self._parent and self._name != 'Universe':
293 print >> sys.stderr, "Warning:", self._name, "has no parent"
294 parent_path = self._parent and self._parent._path()
295 if parent_path and parent_path != 'Universe':
296 return parent_path + '.' + self._name
297 else:
298 return self._name
299
300 # Add a child to this node.
301 def _add_child(self, new_child):
302 # set child's parent before calling this function
303 assert new_child._parent == self
304 if not isinstance(new_child, ConfigNode):
305 raise TypeError, \
306 "ConfigNode child must also be of class ConfigNode"
307 if new_child._name in self._children:
308 raise AttributeError, \
309 "Node '%s' already has a child '%s'" \
310 % (self._name, new_child._name)
311 self._children[new_child._name] = new_child
312 self._child_list += [new_child]
313
314 # operator overload for '+='. You can say "node += child" to add
315 # a child that was created with parent=None. An early attempt
316 # at playing with syntax; turns out not to be that useful.
317 def __iadd__(self, new_child):
318 if new_child._parent != None:
319 raise AttributeError, \
320 "Node '%s' already has a parent" % new_child._name
321 new_child._parent = self
322 self._add_child(new_child)
323 return self
324
325 # Set this instance's parent to 'parent' if it doesn't already
326 # have one. See ConfigNode.__setattr__().
327 def _set_parent_if_none(self, parent):
328 if self._parent == None:
329 parent += self
330
331 # Print instance info to .ini file.
332 def _instantiate(self):
333 print '[' + self._path() + ']' # .ini section header
334 if self._child_list:
335 # instantiate children in same order they were added for
336 # backward compatibility (else we can end up with cpu1
337 # before cpu0).
338 print 'children =', ' '.join([c._name for c in self._child_list])
339 self._instantiateParams()
340 print
341 # recursively dump out children
342 for c in self._child_list:
343 c._instantiate()
344
345 # ConfigNodes have no parameters. Overridden by SimObject.
346 def _instantiateParams(self):
347 pass
348
349 # SimObject is a minimal extension of ConfigNode, implementing a
350 # hierarchy node that corresponds to an M5 SimObject. It prints out a
351 # "type=" line to indicate its SimObject class, prints out the
352 # assigned parameters corresponding to its class, and allows
353 # parameters to be set by keyword in the constructor. Note that most
354 # of the heavy lifting for the SimObject param handling is done in the
355 # MetaConfigNode metaclass.
356
357 class SimObject(ConfigNode):
358 # initialization: like ConfigNode, but handle keyword-based
359 # parameter initializers.
360 def __init__(self, _name, _parent=None, **params):
361 ConfigNode.__init__(self, _name, _parent)
362 for param, value in params.items():
363 setattr(self, param, value)
364
365 # print type and parameter values to .ini file
366 def _instantiateParams(self):
367 print "type =", self.__class__._name
368 for pname in self.__class__.all_param_names():
369 value = getattr(self, pname)
370 if value != None:
371 print pname, '=', value
372
373 def _sim_code(cls):
374 name = cls.__name__
375 param_names = cls._param_dict.keys()
376 param_names.sort()
377 code = "BEGIN_DECLARE_SIM_OBJECT_PARAMS(%s)\n" % name
378 decls = [" " + cls._param_dict[pname].sim_decl(pname) \
379 for pname in param_names]
380 code += "\n".join(decls) + "\n"
381 code += "END_DECLARE_SIM_OBJECT_PARAMS(%s)\n\n" % name
382 code += "BEGIN_INIT_SIM_OBJECT_PARAMS(%s)\n" % name
383 inits = [" " + cls._param_dict[pname].sim_init(pname) \
384 for pname in param_names]
385 code += ",\n".join(inits) + "\n"
386 code += "END_INIT_SIM_OBJECT_PARAMS(%s)\n\n" % name
387 return code
388 _sim_code = classmethod(_sim_code)
389
390 #####################################################################
391 #
392 # Parameter description classes
393 #
394 # The _param_dict dictionary in each class maps parameter names to
395 # either a Param or a VectorParam object. These objects contain the
396 # parameter description string, the parameter type, and the default
397 # value (loaded from the PARAM section of the .odesc files). The
398 # make_value() method on these objects is used to force whatever value
399 # is assigned to the parameter to the appropriate type.
400 #
401 # Note that the default values are loaded into the class's attribute
402 # space when the parameter dictionary is initialized (in
403 # MetaConfigNode.set_param_dict()); after that point they aren't
404 # used.
405 #
406 #####################################################################
407
408 def isNullPointer(value):
409 return isinstance(value, NullSimObject)
410
411 # Regular parameter.
412 class Param(object):
413 # Constructor. E.g., Param(Int, "number of widgets", 5)
414 def __init__(self, ptype, desc, default=None):
415 self.ptype = ptype
416 self.ptype_name = self.ptype.__name__
417 self.desc = desc
418 self.default = default
419
420 # Convert assigned value to appropriate type. Force parameter
421 # value (rhs of '=') to ptype (or None, which means not set).
422 def make_value(self, value):
423 # nothing to do if None or already correct type. Also allow NULL
424 # pointer to be assigned where a SimObject is expected.
425 if value == None or isinstance(value, self.ptype) or \
426 isNullPointer(value) and issubclass(self.ptype, ConfigNode):
427 return value
428 # this type conversion will raise an exception if it's illegal
429 return self.ptype(value)
430
431 def sim_decl(self, name):
432 return 'Param<%s> %s;' % (self.ptype_name, name)
433
434 def sim_init(self, name):
435 if self.default == None:
436 return 'INIT_PARAM(%s, "%s")' % (name, self.desc)
437 else:
438 return 'INIT_PARAM_DFLT(%s, "%s", %s)' % \
439 (name, self.desc, str(self.default))
440
441 # The _VectorParamValue class is a wrapper for vector-valued
442 # parameters. The leading underscore indicates that users shouldn't
443 # see this class; it's magically generated by VectorParam. The
444 # parameter values are stored in the 'value' field as a Python list of
445 # whatever type the parameter is supposed to be. The only purpose of
446 # storing these instead of a raw Python list is that we can override
447 # the __str__() method to not print out '[' and ']' in the .ini file.
448 class _VectorParamValue(object):
449 def __init__(self, value):
450 assert isinstance(value, list) or value == None
451 self.value = value
452
453 def __str__(self):
454 return ' '.join(map(str, self.value))
455
456 # Set member instance's parents to 'parent' if they don't already
457 # have one. Extends "magic" parenting of ConfigNodes to vectors
458 # of ConfigNodes as well. See ConfigNode.__setattr__().
459 def _set_parent_if_none(self, parent):
460 if self.value and hasattr(self.value[0], '_set_parent_if_none'):
461 for v in self.value:
462 v._set_parent_if_none(parent)
463
464 # Vector-valued parameter description. Just like Param, except that
465 # the value is a vector (list) of the specified type instead of a
466 # single value.
467 class VectorParam(Param):
468
469 # Inherit Param constructor. However, the resulting parameter
470 # will be a list of ptype rather than a single element of ptype.
471 def __init__(self, ptype, desc, default=None):
472 Param.__init__(self, ptype, desc, default)
473
474 # Convert assigned value to appropriate type. If the RHS is not a
475 # list or tuple, it generates a single-element list.
476 def make_value(self, value):
477 if value == None: return value
478 if isinstance(value, list) or isinstance(value, tuple):
479 # list: coerce each element into new list
480 val_list = [Param.make_value(self, v) for v in iter(value)]
481 else:
482 # singleton: coerce & wrap in a list
483 val_list = [Param.make_value(self, value)]
484 # wrap list in _VectorParamValue (see above)
485 return _VectorParamValue(val_list)
486
487 def sim_decl(self, name):
488 return 'VectorParam<%s> %s;' % (self.ptype_name, name)
489
490 # sim_init inherited from Param
491
492 #####################################################################
493 #
494 # Parameter Types
495 #
496 # Though native Python types could be used to specify parameter types
497 # (the 'ptype' field of the Param and VectorParam classes), it's more
498 # flexible to define our own set of types. This gives us more control
499 # over how Python expressions are converted to values (via the
500 # __init__() constructor) and how these values are printed out (via
501 # the __str__() conversion method). Eventually we'll need these types
502 # to correspond to distinct C++ types as well.
503 #
504 #####################################################################
505
506 # Integer parameter type.
507 class Int(object):
508 # Constructor. Value must be Python int or long (long integer).
509 def __init__(self, value):
510 t = type(value)
511 if t == int or t == long:
512 self.value = value
513 else:
514 raise TypeError, "Int param got value %s %s" % (repr(value), t)
515
516 # Use Python string conversion. Note that this puts an 'L' on the
517 # end of long integers; we can strip that off here if it gives us
518 # trouble.
519 def __str__(self):
520 return str(self.value)
521
522 # Counter, Addr, and Tick are just aliases for Int for now.
523 class Counter(Int):
524 pass
525
526 class Addr(Int):
527 pass
528
529 class Tick(Int):
530 pass
531
532 # Boolean parameter type.
533 class Bool(object):
534
535 # Constructor. Typically the value will be one of the Python bool
536 # constants True or False (or the aliases true and false below).
537 # Also need to take integer 0 or 1 values since bool was not a
538 # distinct type in Python 2.2. Parse a bunch of boolean-sounding
539 # strings too just for kicks.
540 def __init__(self, value):
541 t = type(value)
542 if t == bool:
543 self.value = value
544 elif t == int or t == long:
545 if value == 1:
546 self.value = True
547 elif value == 0:
548 self.value = False
549 elif t == str:
550 v = value.lower()
551 if v == "true" or v == "t" or v == "yes" or v == "y":
552 self.value = True
553 elif v == "false" or v == "f" or v == "no" or v == "n":
554 self.value = False
555 # if we didn't set it yet, it must not be something we understand
556 if not hasattr(self, 'value'):
557 raise TypeError, "Bool param got value %s %s" % (repr(value), t)
558
559 # Generate printable string version.
560 def __str__(self):
561 if self.value: return "true"
562 else: return "false"
563
564 # String-valued parameter.
565 class String(object):
566 # Constructor. Value must be Python string.
567 def __init__(self, value):
568 t = type(value)
569 if t == str:
570 self.value = value
571 else:
572 raise TypeError, "String param got value %s %s" % (repr(value), t)
573
574 # Generate printable string version. Not too tricky.
575 def __str__(self):
576 return self.value
577
578 # Special class for NULL pointers. Note the special check in
579 # make_param_value() above that lets these be assigned where a
580 # SimObject is required.
581 class NullSimObject(object):
582 # Constructor. No parameters, nothing to do.
583 def __init__(self):
584 pass
585
586 def __str__(self):
587 return "NULL"
588
589 # The only instance you'll ever need...
590 NULL = NullSimObject()
591
592 # Enumerated types are a little more complex. The user specifies the
593 # type as Enum(foo) where foo is either a list or dictionary of
594 # alternatives (typically strings, but not necessarily so). (In the
595 # long run, the integer value of the parameter will be the list index
596 # or the corresponding dictionary value. For now, since we only check
597 # that the alternative is valid and then spit it into a .ini file,
598 # there's not much point in using the dictionary.)
599
600 # What Enum() must do is generate a new type encapsulating the
601 # provided list/dictionary so that specific values of the parameter
602 # can be instances of that type. We define two hidden internal
603 # classes (_ListEnum and _DictEnum) to serve as base classes, then
604 # derive the new type from the appropriate base class on the fly.
605
606
607 # Base class for list-based Enum types.
608 class _ListEnum(object):
609 # Constructor. Value must be a member of the type's map list.
610 def __init__(self, value):
611 if value in self.map:
612 self.value = value
613 self.index = self.map.index(value)
614 else:
615 raise TypeError, "Enum param got bad value '%s' (not in %s)" \
616 % (value, self.map)
617
618 # Generate printable string version of value.
619 def __str__(self):
620 return str(self.value)
621
622 class _DictEnum(object):
623 # Constructor. Value must be a key in the type's map dictionary.
624 def __init__(self, value):
625 if value in self.map:
626 self.value = value
627 self.index = self.map[value]
628 else:
629 raise TypeError, "Enum param got bad value '%s' (not in %s)" \
630 % (value, self.map.keys())
631
632 # Generate printable string version of value.
633 def __str__(self):
634 return str(self.value)
635
636 # Enum metaclass... calling Enum(foo) generates a new type (class)
637 # that derives from _ListEnum or _DictEnum as appropriate.
638 class Enum(type):
639 # counter to generate unique names for generated classes
640 counter = 1
641
642 def __new__(cls, map):
643 if isinstance(map, dict):
644 base = _DictEnum
645 keys = map.keys()
646 elif isinstance(map, list):
647 base = _ListEnum
648 keys = map
649 else:
650 raise TypeError, "Enum map must be list or dict (got %s)" % map
651 classname = "Enum%04d" % Enum.counter
652 Enum.counter += 1
653 # New class derives from selected base, and gets a 'map'
654 # attribute containing the specified list or dict.
655 return type.__new__(cls, classname, (base,), { 'map': map })
656
657
658 #
659 # "Constants"... handy aliases for various values.
660 #
661
662 # For compatibility with C++ bool constants.
663 false = False
664 true = True
665
666 # Some memory range specifications use this as a default upper bound.
667 MAX_ADDR = 2**64 - 1
668
669 # For power-of-two sizing, e.g. 64*K gives an integer value 65536.
670 K = 1024
671 M = K*K
672 G = K*M
673
674 #####################################################################
675
676 # Munge an arbitrary Python code string to get it to execute (mostly
677 # dealing with indentation). Stolen from isa_parser.py... see
678 # comments there for a more detailed description.
679 def fixPythonIndentation(s):
680 # get rid of blank lines first
681 s = re.sub(r'(?m)^\s*\n', '', s);
682 if (s != '' and re.match(r'[ \t]', s[0])):
683 s = 'if 1:\n' + s
684 return s
685
686 # Hook to generate C++ parameter code.
687 def gen_sim_code(file):
688 for objname in sim_object_list:
689 print >> file, eval("%s._sim_code()" % objname)
690
691 # The final hook to generate .ini files. Called from configuration
692 # script once config is built.
693 def instantiate(*objs):
694 for obj in objs:
695 obj._instantiate()
696
697