1 # Copyright (c) 2012-2014 ARM Limited
4 # The license below extends only to copyright in the software and shall
5 # not be construed as granting a license to any other intellectual
6 # property including but not limited to intellectual property relating
7 # to a hardware implementation of the functionality of the software
8 # licensed hereunder. You may use the software subject to the license
9 # terms below provided that you ensure that this notice is replicated
10 # unmodified and in its entirety in all distributions of the software,
11 # modified or unmodified, in source code or in binary form.
13 # Copyright (c) 2004-2006 The Regents of The University of Michigan
14 # Copyright (c) 2010-2011 Advanced Micro Devices, Inc.
15 # All rights reserved.
17 # Redistribution and use in source and binary forms, with or without
18 # modification, are permitted provided that the following conditions are
19 # met: redistributions of source code must retain the above copyright
20 # notice, this list of conditions and the following disclaimer;
21 # redistributions in binary form must reproduce the above copyright
22 # notice, this list of conditions and the following disclaimer in the
23 # documentation and/or other materials provided with the distribution;
24 # neither the name of the copyright holders nor the names of its
25 # contributors may be used to endorse or promote products derived from
26 # this software without specific prior written permission.
28 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 # Authors: Steve Reinhardt
45 #####################################################################
47 # Parameter description classes
49 # The _params dictionary in each class maps parameter names to either
50 # a Param or a VectorParam object. These objects contain the
51 # parameter description string, the parameter type, and the default
52 # value (if any). The convert() method on these objects is used to
53 # force whatever value is assigned to the parameter to the appropriate
56 # Note that the default values are loaded into the class's attribute
57 # space when the parameter dictionary is initialized (in
58 # MetaSimObject._new_param()); after that point they aren't used.
60 #####################################################################
73 def isSimObject(*args
, **kwargs
):
74 return SimObject
.isSimObject(*args
, **kwargs
)
76 def isSimObjectSequence(*args
, **kwargs
):
77 return SimObject
.isSimObjectSequence(*args
, **kwargs
)
79 def isSimObjectClass(*args
, **kwargs
):
80 return SimObject
.isSimObjectClass(*args
, **kwargs
)
84 class MetaParamValue(type):
85 def __new__(mcls
, name
, bases
, dct
):
86 cls
= super(MetaParamValue
, mcls
).__new
__(mcls
, name
, bases
, dct
)
87 assert name
not in allParams
92 # Dummy base class to identify types that are legitimate for SimObject
94 class ParamValue(object):
95 __metaclass__
= MetaParamValue
96 cmd_line_settable
= False
98 # Generate the code needed as a prerequisite for declaring a C++
99 # object of this type. Typically generates one or more #include
100 # statements. Used when declaring parameters of this type.
102 def cxx_predecls(cls
, code
):
105 # Generate the code needed as a prerequisite for including a
106 # reference to a C++ object of this type in a SWIG .i file.
107 # Typically generates one or more %import or %include statements.
109 def swig_predecls(cls
, code
):
112 # default for printing to .ini file is regular string conversion.
113 # will be overridden in some cases
117 # default for printing to .json file is regular string conversion.
118 # will be overridden in some cases, mostly to use native Python
119 # types where there are similar JSON types
120 def config_value(self
):
123 # Prerequisites for .ini parsing with cxx_ini_parse
125 def cxx_ini_predecls(cls
, code
):
128 # parse a .ini file entry for this param from string expression
129 # src into lvalue dest (of the param's C++ type)
131 def cxx_ini_parse(cls
, code
, src
, dest
, ret
):
132 code('// Unhandled param type: %s' % cls
.__name
__)
133 code('%s false;' % ret
)
135 # allows us to blithely call unproxy() on things without checking
136 # if they're really proxies or not
137 def unproxy(self
, base
):
140 # Produce a human readable version of the stored value
141 def pretty_print(self
, value
):
144 # Regular parameter description.
145 class ParamDesc(object):
146 def __init__(self
, ptype_str
, ptype
, *args
, **kwargs
):
147 self
.ptype_str
= ptype_str
148 # remember ptype only if it is provided
156 self
.default
= args
[0]
159 raise TypeError, 'too many arguments'
161 if kwargs
.has_key('desc'):
162 assert(not hasattr(self
, 'desc'))
163 self
.desc
= kwargs
['desc']
166 if kwargs
.has_key('default'):
167 assert(not hasattr(self
, 'default'))
168 self
.default
= kwargs
['default']
169 del kwargs
['default']
172 raise TypeError, 'extra unknown kwargs %s' % kwargs
174 if not hasattr(self
, 'desc'):
175 raise TypeError, 'desc attribute missing'
177 def __getattr__(self
, attr
):
179 ptype
= SimObject
.allClasses
[self
.ptype_str
]
180 assert isSimObjectClass(ptype
)
184 raise AttributeError, "'%s' object has no attribute '%s'" % \
185 (type(self
).__name
__, attr
)
187 def example_str(self
):
188 if hasattr(self
.ptype
, "ex_str"):
189 return self
.ptype
.ex_str
191 return self
.ptype_str
193 # Is the param available to be exposed on the command line
194 def isCmdLineSettable(self
):
195 if hasattr(self
.ptype
, "cmd_line_settable"):
196 return self
.ptype
.cmd_line_settable
200 def convert(self
, value
):
201 if isinstance(value
, proxy
.BaseProxy
):
202 value
.set_param_desc(self
)
204 if not hasattr(self
, 'ptype') and isNullPointer(value
):
205 # deferred evaluation of SimObject; continue to defer if
206 # we're just assigning a null pointer
208 if isinstance(value
, self
.ptype
):
210 if isNullPointer(value
) and isSimObjectClass(self
.ptype
):
212 return self
.ptype(value
)
214 def pretty_print(self
, value
):
215 if isinstance(value
, proxy
.BaseProxy
):
217 if isNullPointer(value
):
219 return self
.ptype(value
).pretty_print(value
)
221 def cxx_predecls(self
, code
):
222 code('#include <cstddef>')
223 self
.ptype
.cxx_predecls(code
)
225 def swig_predecls(self
, code
):
226 self
.ptype
.swig_predecls(code
)
228 def cxx_decl(self
, code
):
229 code('${{self.ptype.cxx_type}} ${{self.name}};')
231 # Vector-valued parameter description. Just like ParamDesc, except
232 # that the value is a vector (list) of the specified type instead of a
235 class VectorParamValue(list):
236 __metaclass__
= MetaParamValue
237 def __setattr__(self
, attr
, value
):
238 raise AttributeError, \
239 "Not allowed to set %s on '%s'" % (attr
, type(self
).__name
__)
241 def config_value(self
):
242 return [v
.config_value() for v
in self
]
245 return ' '.join([v
.ini_str() for v
in self
])
248 return [ v
.getValue() for v
in self
]
250 def unproxy(self
, base
):
251 if len(self
) == 1 and isinstance(self
[0], proxy
.AllProxy
):
252 return self
[0].unproxy(base
)
254 return [v
.unproxy(base
) for v
in self
]
256 class SimObjectVector(VectorParamValue
):
257 # support clone operation
258 def __call__(self
, **kwargs
):
259 return SimObjectVector([v(**kwargs
) for v
in self
])
261 def clear_parent(self
, old_parent
):
263 v
.clear_parent(old_parent
)
265 def set_parent(self
, parent
, name
):
267 self
[0].set_parent(parent
, name
)
269 width
= int(math
.ceil(math
.log(len(self
))/math
.log(10)))
270 for i
,v
in enumerate(self
):
271 v
.set_parent(parent
, "%s%0*d" % (name
, width
, i
))
273 def has_parent(self
):
274 return reduce(lambda x
,y
: x
and y
, [v
.has_parent() for v
in self
])
276 # return 'cpu0 cpu1' etc. for print_ini()
278 return ' '.join([v
._name
for v
in self
])
280 # By iterating through the constituent members of the vector here
281 # we can nicely handle iterating over all a SimObject's children
282 # without having to provide lots of special functions on
283 # SimObjectVector directly.
284 def descendants(self
):
286 for obj
in v
.descendants():
289 def get_config_as_dict(self
):
292 a
.append(v
.get_config_as_dict())
295 # If we are replacing an item in the vector, make sure to set the
296 # parent reference of the new SimObject to be the same as the parent
297 # of the SimObject being replaced. Useful to have if we created
298 # a SimObjectVector of temporary objects that will be modified later in
299 # configuration scripts.
300 def __setitem__(self
, key
, value
):
302 if value
.has_parent():
303 warn("SimObject %s already has a parent" % value
.get_name() +\
304 " that is being overwritten by a SimObjectVector")
305 value
.set_parent(val
.get_parent(), val
._name
)
306 super(SimObjectVector
, self
).__setitem
__(key
, value
)
308 # Enumerate the params of each member of the SimObject vector. Creates
309 # strings that will allow indexing into the vector by the python code and
310 # allow it to be specified on the command line.
311 def enumerateParams(self
, flags_dict
= {},
314 if hasattr(self
, "_paramEnumed"):
315 print "Cycle detected enumerating params at %s?!" % (cmd_line_str
)
319 # Each entry in the SimObjectVector should be an
320 # instance of a SimObject
321 flags_dict
= vals
.enumerateParams(flags_dict
,
322 cmd_line_str
+ "%d." % x
,
323 access_str
+ "[%d]." % x
)
328 class VectorParamDesc(ParamDesc
):
329 # Convert assigned value to appropriate type. If the RHS is not a
330 # list or tuple, it generates a single-element list.
331 def convert(self
, value
):
332 if isinstance(value
, (list, tuple)):
333 # list: coerce each element into new list
334 tmp_list
= [ ParamDesc
.convert(self
, v
) for v
in value
]
335 elif isinstance(value
, str):
336 # If input is a csv string
337 tmp_list
= [ ParamDesc
.convert(self
, v
) \
338 for v
in value
.strip('[').strip(']').split(',') ]
340 # singleton: coerce to a single-element list
341 tmp_list
= [ ParamDesc
.convert(self
, value
) ]
343 if isSimObjectSequence(tmp_list
):
344 return SimObjectVector(tmp_list
)
346 return VectorParamValue(tmp_list
)
348 # Produce a human readable example string that describes
349 # how to set this vector parameter in the absence of a default
351 def example_str(self
):
352 s
= super(VectorParamDesc
, self
).example_str()
353 help_str
= "[" + s
+ "," + s
+ ", ...]"
356 # Produce a human readable representation of the value of this vector param.
357 def pretty_print(self
, value
):
358 if isinstance(value
, (list, tuple)):
359 tmp_list
= [ ParamDesc
.pretty_print(self
, v
) for v
in value
]
360 elif isinstance(value
, str):
361 tmp_list
= [ ParamDesc
.pretty_print(self
, v
) for v
in value
.split(',') ]
363 tmp_list
= [ ParamDesc
.pretty_print(self
, value
) ]
367 # This is a helper function for the new config system
368 def __call__(self
, value
):
369 if isinstance(value
, (list, tuple)):
370 # list: coerce each element into new list
371 tmp_list
= [ ParamDesc
.convert(self
, v
) for v
in value
]
372 elif isinstance(value
, str):
373 # If input is a csv string
374 tmp_list
= [ ParamDesc
.convert(self
, v
) \
375 for v
in value
.strip('[').strip(']').split(',') ]
377 # singleton: coerce to a single-element list
378 tmp_list
= [ ParamDesc
.convert(self
, value
) ]
380 return VectorParamValue(tmp_list
)
382 def swig_module_name(self
):
383 return "%s_vector" % self
.ptype_str
385 def swig_predecls(self
, code
):
386 code('%import "${{self.swig_module_name()}}.i"')
388 def swig_decl(self
, code
):
389 code('%module(package="m5.internal") ${{self.swig_module_name()}}')
391 self
.ptype
.cxx_predecls(code
)
394 # Make sure the SWIGPY_SLICE_ARG is defined through this inclusion
395 code('%include "std_container.i"')
397 self
.ptype
.swig_predecls(code
)
399 code('%include "std_vector.i"')
402 ptype
= self
.ptype_str
403 cxx_type
= self
.ptype
.cxx_type
405 code('%template(vector_$ptype) std::vector< $cxx_type >;')
407 def cxx_predecls(self
, code
):
408 code('#include <vector>')
409 self
.ptype
.cxx_predecls(code
)
411 def cxx_decl(self
, code
):
412 code('std::vector< ${{self.ptype.cxx_type}} > ${{self.name}};')
414 class ParamFactory(object):
415 def __init__(self
, param_desc_class
, ptype_str
= None):
416 self
.param_desc_class
= param_desc_class
417 self
.ptype_str
= ptype_str
419 def __getattr__(self
, attr
):
421 attr
= self
.ptype_str
+ '.' + attr
422 return ParamFactory(self
.param_desc_class
, attr
)
424 # E.g., Param.Int(5, "number of widgets")
425 def __call__(self
, *args
, **kwargs
):
428 ptype
= allParams
[self
.ptype_str
]
430 # if name isn't defined yet, assume it's a SimObject, and
431 # try to resolve it later
433 return self
.param_desc_class(self
.ptype_str
, ptype
, *args
, **kwargs
)
435 Param
= ParamFactory(ParamDesc
)
436 VectorParam
= ParamFactory(VectorParamDesc
)
438 #####################################################################
442 # Though native Python types could be used to specify parameter types
443 # (the 'ptype' field of the Param and VectorParam classes), it's more
444 # flexible to define our own set of types. This gives us more control
445 # over how Python expressions are converted to values (via the
446 # __init__() constructor) and how these values are printed out (via
447 # the __str__() conversion method).
449 #####################################################################
451 # String-valued parameter. Just mixin the ParamValue class with the
452 # built-in str class.
453 class String(ParamValue
,str):
454 cxx_type
= 'std::string'
455 cmd_line_settable
= True
458 def cxx_predecls(self
, code
):
459 code('#include <string>')
462 def swig_predecls(cls
, code
):
463 code('%include "std_string.i"')
465 def __call__(self
, value
):
470 def cxx_ini_parse(self
, code
, src
, dest
, ret
):
471 code('%s = %s;' % (dest
, src
))
472 code('%s true;' % ret
)
477 # superclass for "numeric" parameter values, to emulate math
478 # operations in a type-safe way. e.g., a Latency times an int returns
479 # a new Latency object.
480 class NumericParamValue(ParamValue
):
482 return str(self
.value
)
485 return float(self
.value
)
488 return long(self
.value
)
491 return int(self
.value
)
493 # hook for bounds checking
497 def __mul__(self
, other
):
498 newobj
= self
.__class
__(self
)
499 newobj
.value
*= other
505 def __div__(self
, other
):
506 newobj
= self
.__class
__(self
)
507 newobj
.value
/= other
511 def __sub__(self
, other
):
512 newobj
= self
.__class
__(self
)
513 newobj
.value
-= other
517 def config_value(self
):
521 def cxx_ini_predecls(cls
, code
):
522 # Assume that base/str.hh will be included anyway
523 # code('#include "base/str.hh"')
526 # The default for parsing PODs from an .ini entry is to extract from an
527 # istringstream and let overloading choose the right type according to
530 def cxx_ini_parse(self
, code
, src
, dest
, ret
):
531 code('%s to_number(%s, %s);' % (ret
, src
, dest
))
533 # Metaclass for bounds-checked integer parameters. See CheckedInt.
534 class CheckedIntType(MetaParamValue
):
535 def __init__(cls
, name
, bases
, dict):
536 super(CheckedIntType
, cls
).__init
__(name
, bases
, dict)
538 # CheckedInt is an abstract base class, so we actually don't
539 # want to do any processing on it... the rest of this code is
540 # just for classes that derive from CheckedInt.
541 if name
== 'CheckedInt':
544 if not (hasattr(cls
, 'min') and hasattr(cls
, 'max')):
545 if not (hasattr(cls
, 'size') and hasattr(cls
, 'unsigned')):
546 panic("CheckedInt subclass %s must define either\n" \
547 " 'min' and 'max' or 'size' and 'unsigned'\n",
551 cls
.max = 2 ** cls
.size
- 1
553 cls
.min = -(2 ** (cls
.size
- 1))
554 cls
.max = (2 ** (cls
.size
- 1)) - 1
556 # Abstract superclass for bounds-checked integer parameters. This
557 # class is subclassed to generate parameter classes with specific
558 # bounds. Initialization of the min and max bounds is done in the
559 # metaclass CheckedIntType.__init__.
560 class CheckedInt(NumericParamValue
):
561 __metaclass__
= CheckedIntType
562 cmd_line_settable
= True
565 if not self
.min <= self
.value
<= self
.max:
566 raise TypeError, 'Integer param out of bounds %d < %d < %d' % \
567 (self
.min, self
.value
, self
.max)
569 def __init__(self
, value
):
570 if isinstance(value
, str):
571 self
.value
= convert
.toInteger(value
)
572 elif isinstance(value
, (int, long, float, NumericParamValue
)):
573 self
.value
= long(value
)
575 raise TypeError, "Can't convert object of type %s to CheckedInt" \
576 % type(value
).__name
__
579 def __call__(self
, value
):
584 def cxx_predecls(cls
, code
):
585 # most derived types require this, so we just do it here once
586 code('#include "base/types.hh"')
589 def swig_predecls(cls
, code
):
590 # most derived types require this, so we just do it here once
591 code('%import "stdint.i"')
592 code('%import "base/types.hh"')
595 return long(self
.value
)
597 class Int(CheckedInt
): cxx_type
= 'int'; size
= 32; unsigned
= False
598 class Unsigned(CheckedInt
): cxx_type
= 'unsigned'; size
= 32; unsigned
= True
600 class Int8(CheckedInt
): cxx_type
= 'int8_t'; size
= 8; unsigned
= False
601 class UInt8(CheckedInt
): cxx_type
= 'uint8_t'; size
= 8; unsigned
= True
602 class Int16(CheckedInt
): cxx_type
= 'int16_t'; size
= 16; unsigned
= False
603 class UInt16(CheckedInt
): cxx_type
= 'uint16_t'; size
= 16; unsigned
= True
604 class Int32(CheckedInt
): cxx_type
= 'int32_t'; size
= 32; unsigned
= False
605 class UInt32(CheckedInt
): cxx_type
= 'uint32_t'; size
= 32; unsigned
= True
606 class Int64(CheckedInt
): cxx_type
= 'int64_t'; size
= 64; unsigned
= False
607 class UInt64(CheckedInt
): cxx_type
= 'uint64_t'; size
= 64; unsigned
= True
609 class Counter(CheckedInt
): cxx_type
= 'Counter'; size
= 64; unsigned
= True
610 class Tick(CheckedInt
): cxx_type
= 'Tick'; size
= 64; unsigned
= True
611 class TcpPort(CheckedInt
): cxx_type
= 'uint16_t'; size
= 16; unsigned
= True
612 class UdpPort(CheckedInt
): cxx_type
= 'uint16_t'; size
= 16; unsigned
= True
614 class Percent(CheckedInt
): cxx_type
= 'int'; min = 0; max = 100
616 class Cycles(CheckedInt
):
622 from m5
.internal
.core
import Cycles
623 return Cycles(self
.value
)
626 def cxx_ini_predecls(cls
, code
):
627 # Assume that base/str.hh will be included anyway
628 # code('#include "base/str.hh"')
632 def cxx_ini_parse(cls
, code
, src
, dest
, ret
):
633 code('uint64_t _temp;')
634 code('bool _ret = to_number(%s, _temp);' % src
)
636 code(' %s = Cycles(_temp);' % dest
)
637 code('%s _ret;' % ret
)
639 class Float(ParamValue
, float):
641 cmd_line_settable
= True
643 def __init__(self
, value
):
644 if isinstance(value
, (int, long, float, NumericParamValue
, Float
, str)):
645 self
.value
= float(value
)
647 raise TypeError, "Can't convert object of type %s to Float" \
648 % type(value
).__name
__
650 def __call__(self
, value
):
655 return float(self
.value
)
657 def config_value(self
):
661 def cxx_ini_predecls(cls
, code
):
662 code('#include <sstream>')
665 def cxx_ini_parse(self
, code
, src
, dest
, ret
):
666 code('%s (std::istringstream(%s) >> %s).eof();' % (ret
, src
, dest
))
668 class MemorySize(CheckedInt
):
669 cxx_type
= 'uint64_t'
673 def __init__(self
, value
):
674 if isinstance(value
, MemorySize
):
675 self
.value
= value
.value
677 self
.value
= convert
.toMemorySize(value
)
680 class MemorySize32(CheckedInt
):
681 cxx_type
= 'uint32_t'
685 def __init__(self
, value
):
686 if isinstance(value
, MemorySize
):
687 self
.value
= value
.value
689 self
.value
= convert
.toMemorySize(value
)
692 class Addr(CheckedInt
):
696 def __init__(self
, value
):
697 if isinstance(value
, Addr
):
698 self
.value
= value
.value
701 # Often addresses are referred to with sizes. Ex: A device
702 # base address is at "512MB". Use toMemorySize() to convert
703 # these into addresses. If the address is not specified with a
704 # "size", an exception will occur and numeric translation will
706 self
.value
= convert
.toMemorySize(value
)
707 except (TypeError, ValueError):
708 # Convert number to string and use long() to do automatic
709 # base conversion (requires base=0 for auto-conversion)
710 self
.value
= long(str(value
), base
=0)
713 def __add__(self
, other
):
714 if isinstance(other
, Addr
):
715 return self
.value
+ other
.value
717 return self
.value
+ other
718 def pretty_print(self
, value
):
720 val
= convert
.toMemorySize(value
)
723 return "0x%x" % long(val
)
725 class AddrRange(ParamValue
):
726 cxx_type
= 'AddrRange'
728 def __init__(self
, *args
, **kwargs
):
729 # Disable interleaving by default
730 self
.intlvHighBit
= 0
734 def handle_kwargs(self
, kwargs
):
735 # An address range needs to have an upper limit, specified
736 # either explicitly with an end, or as an offset using the
739 self
.end
= Addr(kwargs
.pop('end'))
740 elif 'size' in kwargs
:
741 self
.end
= self
.start
+ Addr(kwargs
.pop('size')) - 1
743 raise TypeError, "Either end or size must be specified"
745 # Now on to the optional bit
746 if 'intlvHighBit' in kwargs
:
747 self
.intlvHighBit
= int(kwargs
.pop('intlvHighBit'))
748 if 'intlvBits' in kwargs
:
749 self
.intlvBits
= int(kwargs
.pop('intlvBits'))
750 if 'intlvMatch' in kwargs
:
751 self
.intlvMatch
= int(kwargs
.pop('intlvMatch'))
754 self
.start
= Addr(kwargs
.pop('start'))
755 handle_kwargs(self
, kwargs
)
759 self
.start
= Addr(args
[0])
760 handle_kwargs(self
, kwargs
)
761 elif isinstance(args
[0], (list, tuple)):
762 self
.start
= Addr(args
[0][0])
763 self
.end
= Addr(args
[0][1])
766 self
.end
= Addr(args
[0]) - 1
769 self
.start
= Addr(args
[0])
770 self
.end
= Addr(args
[1])
772 raise TypeError, "Too many arguments specified"
775 raise TypeError, "Too many keywords: %s" % kwargs
.keys()
778 return '%s:%s' % (self
.start
, self
.end
)
781 # Divide the size by the size of the interleaving slice
782 return (long(self
.end
) - long(self
.start
) + 1) >> self
.intlvBits
785 def cxx_predecls(cls
, code
):
786 Addr
.cxx_predecls(code
)
787 code('#include "base/addr_range.hh"')
790 def swig_predecls(cls
, code
):
791 Addr
.swig_predecls(code
)
794 def cxx_ini_predecls(cls
, code
):
795 code('#include <sstream>')
798 def cxx_ini_parse(cls
, code
, src
, dest
, ret
):
799 code('uint64_t _start, _end;')
801 code('std::istringstream _stream(${src});')
802 code('_stream >> _start;')
803 code('_stream.get(_sep);')
804 code('_stream >> _end;')
805 code('bool _ret = !_stream.fail() &&'
806 '_stream.eof() && _sep == \':\';')
808 code(' ${dest} = AddrRange(_start, _end);')
812 # Go from the Python class to the wrapped C++ class generated
814 from m5
.internal
.range import AddrRange
816 return AddrRange(long(self
.start
), long(self
.end
),
817 int(self
.intlvHighBit
), int(self
.intlvBits
),
818 int(self
.intlvMatch
))
820 # Boolean parameter type. Python doesn't let you subclass bool, since
821 # it doesn't want to let you create multiple instances of True and
822 # False. Thus this is a little more complicated than String.
823 class Bool(ParamValue
):
825 cmd_line_settable
= True
827 def __init__(self
, value
):
829 self
.value
= convert
.toBool(value
)
831 self
.value
= bool(value
)
833 def __call__(self
, value
):
838 return bool(self
.value
)
841 return str(self
.value
)
843 # implement truth value testing for Bool parameters so that these params
844 # evaluate correctly during the python configuration phase
845 def __nonzero__(self
):
846 return bool(self
.value
)
853 def config_value(self
):
857 def cxx_ini_predecls(cls
, code
):
858 # Assume that base/str.hh will be included anyway
859 # code('#include "base/str.hh"')
863 def cxx_ini_parse(cls
, code
, src
, dest
, ret
):
864 code('%s to_bool(%s, %s);' % (ret
, src
, dest
))
866 def IncEthernetAddr(addr
, val
= 1):
867 bytes
= map(lambda x
: int(x
, 16), addr
.split(':'))
869 for i
in (5, 4, 3, 2, 1):
870 val
,rem
= divmod(bytes
[i
], 256)
875 assert(bytes
[0] <= 255)
876 return ':'.join(map(lambda x
: '%02x' % x
, bytes
))
878 _NextEthernetAddr
= "00:90:00:00:00:01"
879 def NextEthernetAddr():
880 global _NextEthernetAddr
882 value
= _NextEthernetAddr
883 _NextEthernetAddr
= IncEthernetAddr(_NextEthernetAddr
, 1)
886 class EthernetAddr(ParamValue
):
887 cxx_type
= 'Net::EthAddr'
888 ex_str
= "00:90:00:00:00:01"
889 cmd_line_settable
= True
892 def cxx_predecls(cls
, code
):
893 code('#include "base/inet.hh"')
896 def swig_predecls(cls
, code
):
897 code('%include "python/swig/inet.i"')
899 def __init__(self
, value
):
900 if value
== NextEthernetAddr
:
904 if not isinstance(value
, str):
905 raise TypeError, "expected an ethernet address and didn't get one"
907 bytes
= value
.split(':')
909 raise TypeError, 'invalid ethernet address %s' % value
912 if not 0 <= int(byte
, base
=16) <= 0xff:
913 raise TypeError, 'invalid ethernet address %s' % value
917 def __call__(self
, value
):
921 def unproxy(self
, base
):
922 if self
.value
== NextEthernetAddr
:
923 return EthernetAddr(self
.value())
927 from m5
.internal
.params
import EthAddr
928 return EthAddr(self
.value
)
934 def cxx_ini_parse(self
, code
, src
, dest
, ret
):
935 code('%s = Net::EthAddr(%s);' % (dest
, src
))
936 code('%s true;' % ret
)
938 # When initializing an IpAddress, pass in an existing IpAddress, a string of
939 # the form "a.b.c.d", or an integer representing an IP.
940 class IpAddress(ParamValue
):
941 cxx_type
= 'Net::IpAddress'
943 cmd_line_settable
= True
946 def cxx_predecls(cls
, code
):
947 code('#include "base/inet.hh"')
950 def swig_predecls(cls
, code
):
951 code('%include "python/swig/inet.i"')
953 def __init__(self
, value
):
954 if isinstance(value
, IpAddress
):
958 self
.ip
= convert
.toIpAddress(value
)
960 self
.ip
= long(value
)
963 def __call__(self
, value
):
968 tup
= [(self
.ip
>> i
) & 0xff for i
in (24, 16, 8, 0)]
969 return '%d.%d.%d.%d' % tuple(tup
)
971 def __eq__(self
, other
):
972 if isinstance(other
, IpAddress
):
973 return self
.ip
== other
.ip
974 elif isinstance(other
, str):
976 return self
.ip
== convert
.toIpAddress(other
)
980 return self
.ip
== other
982 def __ne__(self
, other
):
983 return not (self
== other
)
986 if self
.ip
< 0 or self
.ip
>= (1 << 32):
987 raise TypeError, "invalid ip address %#08x" % self
.ip
990 from m5
.internal
.params
import IpAddress
991 return IpAddress(self
.ip
)
993 # When initializing an IpNetmask, pass in an existing IpNetmask, a string of
994 # the form "a.b.c.d/n" or "a.b.c.d/e.f.g.h", or an ip and netmask as
995 # positional or keyword arguments.
996 class IpNetmask(IpAddress
):
997 cxx_type
= 'Net::IpNetmask'
998 ex_str
= "127.0.0.0/24"
999 cmd_line_settable
= True
1002 def cxx_predecls(cls
, code
):
1003 code('#include "base/inet.hh"')
1006 def swig_predecls(cls
, code
):
1007 code('%include "python/swig/inet.i"')
1009 def __init__(self
, *args
, **kwargs
):
1010 def handle_kwarg(self
, kwargs
, key
, elseVal
= None):
1012 setattr(self
, key
, kwargs
.pop(key
))
1014 setattr(self
, key
, elseVal
)
1016 raise TypeError, "No value set for %s" % key
1019 handle_kwarg(self
, kwargs
, 'ip')
1020 handle_kwarg(self
, kwargs
, 'netmask')
1022 elif len(args
) == 1:
1024 if not 'ip' in kwargs
and not 'netmask' in kwargs
:
1025 raise TypeError, "Invalid arguments"
1026 handle_kwarg(self
, kwargs
, 'ip', args
[0])
1027 handle_kwarg(self
, kwargs
, 'netmask', args
[0])
1028 elif isinstance(args
[0], IpNetmask
):
1029 self
.ip
= args
[0].ip
1030 self
.netmask
= args
[0].netmask
1032 (self
.ip
, self
.netmask
) = convert
.toIpNetmask(args
[0])
1034 elif len(args
) == 2:
1036 self
.netmask
= args
[1]
1038 raise TypeError, "Too many arguments specified"
1041 raise TypeError, "Too many keywords: %s" % kwargs
.keys()
1045 def __call__(self
, value
):
1046 self
.__init
__(value
)
1050 return "%s/%d" % (super(IpNetmask
, self
).__str
__(), self
.netmask
)
1052 def __eq__(self
, other
):
1053 if isinstance(other
, IpNetmask
):
1054 return self
.ip
== other
.ip
and self
.netmask
== other
.netmask
1055 elif isinstance(other
, str):
1057 return (self
.ip
, self
.netmask
) == convert
.toIpNetmask(other
)
1065 if self
.netmask
< 0 or self
.netmask
> 32:
1066 raise TypeError, "invalid netmask %d" % netmask
1069 from m5
.internal
.params
import IpNetmask
1070 return IpNetmask(self
.ip
, self
.netmask
)
1072 # When initializing an IpWithPort, pass in an existing IpWithPort, a string of
1073 # the form "a.b.c.d:p", or an ip and port as positional or keyword arguments.
1074 class IpWithPort(IpAddress
):
1075 cxx_type
= 'Net::IpWithPort'
1076 ex_str
= "127.0.0.1:80"
1077 cmd_line_settable
= True
1080 def cxx_predecls(cls
, code
):
1081 code('#include "base/inet.hh"')
1084 def swig_predecls(cls
, code
):
1085 code('%include "python/swig/inet.i"')
1087 def __init__(self
, *args
, **kwargs
):
1088 def handle_kwarg(self
, kwargs
, key
, elseVal
= None):
1090 setattr(self
, key
, kwargs
.pop(key
))
1092 setattr(self
, key
, elseVal
)
1094 raise TypeError, "No value set for %s" % key
1097 handle_kwarg(self
, kwargs
, 'ip')
1098 handle_kwarg(self
, kwargs
, 'port')
1100 elif len(args
) == 1:
1102 if not 'ip' in kwargs
and not 'port' in kwargs
:
1103 raise TypeError, "Invalid arguments"
1104 handle_kwarg(self
, kwargs
, 'ip', args
[0])
1105 handle_kwarg(self
, kwargs
, 'port', args
[0])
1106 elif isinstance(args
[0], IpWithPort
):
1107 self
.ip
= args
[0].ip
1108 self
.port
= args
[0].port
1110 (self
.ip
, self
.port
) = convert
.toIpWithPort(args
[0])
1112 elif len(args
) == 2:
1116 raise TypeError, "Too many arguments specified"
1119 raise TypeError, "Too many keywords: %s" % kwargs
.keys()
1123 def __call__(self
, value
):
1124 self
.__init
__(value
)
1128 return "%s:%d" % (super(IpWithPort
, self
).__str
__(), self
.port
)
1130 def __eq__(self
, other
):
1131 if isinstance(other
, IpWithPort
):
1132 return self
.ip
== other
.ip
and self
.port
== other
.port
1133 elif isinstance(other
, str):
1135 return (self
.ip
, self
.port
) == convert
.toIpWithPort(other
)
1143 if self
.port
< 0 or self
.port
> 0xffff:
1144 raise TypeError, "invalid port %d" % self
.port
1147 from m5
.internal
.params
import IpWithPort
1148 return IpWithPort(self
.ip
, self
.port
)
1150 time_formats
= [ "%a %b %d %H:%M:%S %Z %Y",
1151 "%a %b %d %H:%M:%S %Y",
1152 "%Y/%m/%d %H:%M:%S",
1155 "%m/%d/%Y %H:%M:%S",
1158 "%m/%d/%y %H:%M:%S",
1163 def parse_time(value
):
1164 from time
import gmtime
, strptime
, struct_time
, time
1165 from datetime
import datetime
, date
1167 if isinstance(value
, struct_time
):
1170 if isinstance(value
, (int, long)):
1171 return gmtime(value
)
1173 if isinstance(value
, (datetime
, date
)):
1174 return value
.timetuple()
1176 if isinstance(value
, str):
1177 if value
in ('Now', 'Today'):
1178 return time
.gmtime(time
.time())
1180 for format
in time_formats
:
1182 return strptime(value
, format
)
1186 raise ValueError, "Could not parse '%s' as a time" % value
1188 class Time(ParamValue
):
1192 def cxx_predecls(cls
, code
):
1193 code('#include <time.h>')
1196 def swig_predecls(cls
, code
):
1197 code('%include "python/swig/time.i"')
1199 def __init__(self
, value
):
1200 self
.value
= parse_time(value
)
1202 def __call__(self
, value
):
1203 self
.__init
__(value
)
1207 from m5
.internal
.params
import tm
1210 py_time
= self
.value
1212 # UNIX is years since 1900
1213 c_time
.tm_year
= py_time
.tm_year
- 1900;
1215 # Python starts at 1, UNIX starts at 0
1216 c_time
.tm_mon
= py_time
.tm_mon
- 1;
1217 c_time
.tm_mday
= py_time
.tm_mday
;
1218 c_time
.tm_hour
= py_time
.tm_hour
;
1219 c_time
.tm_min
= py_time
.tm_min
;
1220 c_time
.tm_sec
= py_time
.tm_sec
;
1222 # Python has 0 as Monday, UNIX is 0 as sunday
1223 c_time
.tm_wday
= py_time
.tm_wday
+ 1
1224 if c_time
.tm_wday
> 6:
1225 c_time
.tm_wday
-= 7;
1227 # Python starts at 1, Unix starts at 0
1228 c_time
.tm_yday
= py_time
.tm_yday
- 1;
1233 return time
.asctime(self
.value
)
1238 def get_config_as_dict(self
):
1243 def cxx_ini_predecls(cls
, code
):
1244 code('#include <time.h>')
1247 def cxx_ini_parse(cls
, code
, src
, dest
, ret
):
1248 code('char *_parse_ret = strptime((${src}).c_str(),')
1249 code(' "%a %b %d %H:%M:%S %Y", &(${dest}));')
1250 code('${ret} _parse_ret && *_parse_ret == \'\\0\';');
1252 # Enumerated types are a little more complex. The user specifies the
1253 # type as Enum(foo) where foo is either a list or dictionary of
1254 # alternatives (typically strings, but not necessarily so). (In the
1255 # long run, the integer value of the parameter will be the list index
1256 # or the corresponding dictionary value. For now, since we only check
1257 # that the alternative is valid and then spit it into a .ini file,
1258 # there's not much point in using the dictionary.)
1260 # What Enum() must do is generate a new type encapsulating the
1261 # provided list/dictionary so that specific values of the parameter
1262 # can be instances of that type. We define two hidden internal
1263 # classes (_ListEnum and _DictEnum) to serve as base classes, then
1264 # derive the new type from the appropriate base class on the fly.
1267 # Metaclass for Enum types
1268 class MetaEnum(MetaParamValue
):
1269 def __new__(mcls
, name
, bases
, dict):
1270 assert name
not in allEnums
1272 cls
= super(MetaEnum
, mcls
).__new
__(mcls
, name
, bases
, dict)
1273 allEnums
[name
] = cls
1276 def __init__(cls
, name
, bases
, init_dict
):
1277 if init_dict
.has_key('map'):
1278 if not isinstance(cls
.map, dict):
1279 raise TypeError, "Enum-derived class attribute 'map' " \
1280 "must be of type dict"
1281 # build list of value strings from map
1282 cls
.vals
= cls
.map.keys()
1284 elif init_dict
.has_key('vals'):
1285 if not isinstance(cls
.vals
, list):
1286 raise TypeError, "Enum-derived class attribute 'vals' " \
1287 "must be of type list"
1288 # build string->value map from vals sequence
1290 for idx
,val
in enumerate(cls
.vals
):
1293 raise TypeError, "Enum-derived class must define "\
1294 "attribute 'map' or 'vals'"
1296 cls
.cxx_type
= 'Enums::%s' % name
1298 super(MetaEnum
, cls
).__init
__(name
, bases
, init_dict
)
1300 # Generate C++ class declaration for this enum type.
1301 # Note that we wrap the enum in a class/struct to act as a namespace,
1302 # so that the enum strings can be brief w/o worrying about collisions.
1303 def cxx_decl(cls
, code
):
1304 wrapper_name
= cls
.wrapper_name
1305 wrapper
= 'struct' if cls
.wrapper_is_struct
else 'namespace'
1306 name
= cls
.__name
__ if cls
.enum_name
is None else cls
.enum_name
1307 idem_macro
= '__ENUM__%s__%s__' % (wrapper_name
, name
)
1313 $wrapper $wrapper_name {
1317 for val
in cls
.vals
:
1318 code('$val = ${{cls.map[val]}},')
1319 code('Num_$name = ${{len(cls.vals)}}')
1323 if cls
.wrapper_is_struct
:
1324 code(' static const char *${name}Strings[Num_${name}];')
1327 code('extern const char *${name}Strings[Num_${name}];')
1331 code('#endif // $idem_macro')
1333 def cxx_def(cls
, code
):
1334 wrapper_name
= cls
.wrapper_name
1335 file_name
= cls
.__name
__
1336 name
= cls
.__name
__ if cls
.enum_name
is None else cls
.enum_name
1338 code('#include "enums/$file_name.hh"')
1339 if cls
.wrapper_is_struct
:
1340 code('const char *${wrapper_name}::${name}Strings'
1343 code('namespace Enums {')
1345 code(' const char *${name}Strings[Num_${name}] =')
1349 for val
in cls
.vals
:
1354 if not cls
.wrapper_is_struct
:
1355 code('} // namespace $wrapper_name')
1358 def swig_decl(cls
, code
):
1361 %module(package="m5.internal") enum_$name
1364 #include "enums/$name.hh"
1367 %include "enums/$name.hh"
1371 # Base class for enum types.
1372 class Enum(ParamValue
):
1373 __metaclass__
= MetaEnum
1375 cmd_line_settable
= True
1377 # The name of the wrapping namespace or struct
1378 wrapper_name
= 'Enums'
1380 # If true, the enum is wrapped in a struct rather than a namespace
1381 wrapper_is_struct
= False
1383 # If not None, use this as the enum name rather than this class name
1386 def __init__(self
, value
):
1387 if value
not in self
.map:
1388 raise TypeError, "Enum param got bad value '%s' (not in %s)" \
1389 % (value
, self
.vals
)
1392 def __call__(self
, value
):
1393 self
.__init
__(value
)
1397 def cxx_predecls(cls
, code
):
1398 code('#include "enums/$0.hh"', cls
.__name
__)
1401 def swig_predecls(cls
, code
):
1402 code('%import "python/m5/internal/enum_$0.i"', cls
.__name
__)
1405 def cxx_ini_parse(cls
, code
, src
, dest
, ret
):
1406 code('if (false) {')
1407 for elem_name
in cls
.map.iterkeys():
1408 code('} else if (%s == "%s") {' % (src
, elem_name
))
1410 code('%s = Enums::%s;' % (dest
, elem_name
))
1411 code('%s true;' % ret
)
1414 code(' %s false;' % ret
)
1418 return int(self
.map[self
.value
])
1423 # how big does a rounding error need to be before we warn about it?
1424 frequency_tolerance
= 0.001 # 0.1%
1426 class TickParamValue(NumericParamValue
):
1429 cmd_line_settable
= True
1432 def cxx_predecls(cls
, code
):
1433 code('#include "base/types.hh"')
1436 def swig_predecls(cls
, code
):
1437 code('%import "stdint.i"')
1438 code('%import "base/types.hh"')
1440 def __call__(self
, value
):
1441 self
.__init
__(value
)
1445 return long(self
.value
)
1448 def cxx_ini_predecls(cls
, code
):
1449 code('#include <sstream>')
1451 # Ticks are expressed in seconds in JSON files and in plain
1452 # Ticks in .ini files. Switch based on a config flag
1454 def cxx_ini_parse(self
, code
, src
, dest
, ret
):
1455 code('${ret} to_number(${src}, ${dest});')
1457 class Latency(TickParamValue
):
1460 def __init__(self
, value
):
1461 if isinstance(value
, (Latency
, Clock
)):
1462 self
.ticks
= value
.ticks
1463 self
.value
= value
.value
1464 elif isinstance(value
, Frequency
):
1465 self
.ticks
= value
.ticks
1466 self
.value
= 1.0 / value
.value
1467 elif value
.endswith('t'):
1469 self
.value
= int(value
[:-1])
1472 self
.value
= convert
.toLatency(value
)
1474 def __call__(self
, value
):
1475 self
.__init
__(value
)
1478 def __getattr__(self
, attr
):
1479 if attr
in ('latency', 'period'):
1481 if attr
== 'frequency':
1482 return Frequency(self
)
1483 raise AttributeError, "Latency object has no attribute '%s'" % attr
1486 if self
.ticks
or self
.value
== 0:
1489 value
= ticks
.fromSeconds(self
.value
)
1492 def config_value(self
):
1493 return self
.getValue()
1495 # convert latency to ticks
1497 return '%d' % self
.getValue()
1499 class Frequency(TickParamValue
):
1502 def __init__(self
, value
):
1503 if isinstance(value
, (Latency
, Clock
)):
1504 if value
.value
== 0:
1507 self
.value
= 1.0 / value
.value
1508 self
.ticks
= value
.ticks
1509 elif isinstance(value
, Frequency
):
1510 self
.value
= value
.value
1511 self
.ticks
= value
.ticks
1514 self
.value
= convert
.toFrequency(value
)
1516 def __call__(self
, value
):
1517 self
.__init
__(value
)
1520 def __getattr__(self
, attr
):
1521 if attr
== 'frequency':
1523 if attr
in ('latency', 'period'):
1524 return Latency(self
)
1525 raise AttributeError, "Frequency object has no attribute '%s'" % attr
1527 # convert latency to ticks
1529 if self
.ticks
or self
.value
== 0:
1532 value
= ticks
.fromSeconds(1.0 / self
.value
)
1535 def config_value(self
):
1536 return self
.getValue()
1539 return '%d' % self
.getValue()
1541 # A generic Frequency and/or Latency value. Value is stored as a
1542 # latency, just like Latency and Frequency.
1543 class Clock(TickParamValue
):
1544 def __init__(self
, value
):
1545 if isinstance(value
, (Latency
, Clock
)):
1546 self
.ticks
= value
.ticks
1547 self
.value
= value
.value
1548 elif isinstance(value
, Frequency
):
1549 self
.ticks
= value
.ticks
1550 self
.value
= 1.0 / value
.value
1551 elif value
.endswith('t'):
1553 self
.value
= int(value
[:-1])
1556 self
.value
= convert
.anyToLatency(value
)
1558 def __call__(self
, value
):
1559 self
.__init
__(value
)
1563 return "%s" % Latency(self
)
1565 def __getattr__(self
, attr
):
1566 if attr
== 'frequency':
1567 return Frequency(self
)
1568 if attr
in ('latency', 'period'):
1569 return Latency(self
)
1570 raise AttributeError, "Frequency object has no attribute '%s'" % attr
1573 return self
.period
.getValue()
1575 def config_value(self
):
1576 return self
.period
.config_value()
1579 return self
.period
.ini_str()
1581 class Voltage(float,ParamValue
):
1584 cmd_line_settable
= False
1586 def __new__(cls
, value
):
1587 # convert to voltage
1588 val
= convert
.toVoltage(value
)
1589 return super(cls
, Voltage
).__new
__(cls
, val
)
1591 def __call__(self
, value
):
1592 val
= convert
.toVoltage(value
)
1597 return str(self
.getValue())
1604 return '%f' % self
.getValue()
1607 def cxx_ini_predecls(cls
, code
):
1608 code('#include <sstream>')
1611 def cxx_ini_parse(self
, code
, src
, dest
, ret
):
1612 code('%s (std::istringstream(%s) >> %s).eof();' % (ret
, src
, dest
))
1614 class Current(float, ParamValue
):
1617 cmd_line_settable
= False
1619 def __new__(cls
, value
):
1620 # convert to current
1621 val
= convert
.toCurrent(value
)
1622 return super(cls
, Current
).__new
__(cls
, val
)
1624 def __call__(self
, value
):
1625 val
= convert
.toCurrent(value
)
1630 return str(self
.getValue())
1637 return '%f' % self
.getValue()
1640 def cxx_ini_predecls(cls
, code
):
1641 code('#include <sstream>')
1644 def cxx_ini_parse(self
, code
, src
, dest
, ret
):
1645 code('%s (std::istringstream(%s) >> %s).eof();' % (ret
, src
, dest
))
1647 class NetworkBandwidth(float,ParamValue
):
1650 cmd_line_settable
= True
1652 def __new__(cls
, value
):
1653 # convert to bits per second
1654 val
= convert
.toNetworkBandwidth(value
)
1655 return super(cls
, NetworkBandwidth
).__new
__(cls
, val
)
1658 return str(self
.val
)
1660 def __call__(self
, value
):
1661 val
= convert
.toNetworkBandwidth(value
)
1666 # convert to seconds per byte
1667 value
= 8.0 / float(self
)
1668 # convert to ticks per byte
1669 value
= ticks
.fromSeconds(value
)
1673 return '%f' % self
.getValue()
1675 def config_value(self
):
1676 return '%f' % self
.getValue()
1679 def cxx_ini_predecls(cls
, code
):
1680 code('#include <sstream>')
1683 def cxx_ini_parse(self
, code
, src
, dest
, ret
):
1684 code('%s (std::istringstream(%s) >> %s).eof();' % (ret
, src
, dest
))
1686 class MemoryBandwidth(float,ParamValue
):
1689 cmd_line_settable
= True
1691 def __new__(cls
, value
):
1692 # convert to bytes per second
1693 val
= convert
.toMemoryBandwidth(value
)
1694 return super(cls
, MemoryBandwidth
).__new
__(cls
, val
)
1696 def __call__(self
, value
):
1697 val
= convert
.toMemoryBandwidth(value
)
1702 # convert to seconds per byte
1705 value
= 1.0 / float(self
)
1706 # convert to ticks per byte
1707 value
= ticks
.fromSeconds(value
)
1711 return '%f' % self
.getValue()
1713 def config_value(self
):
1714 return '%f' % self
.getValue()
1717 def cxx_ini_predecls(cls
, code
):
1718 code('#include <sstream>')
1721 def cxx_ini_parse(self
, code
, src
, dest
, ret
):
1722 code('%s (std::istringstream(%s) >> %s).eof();' % (ret
, src
, dest
))
1725 # "Constants"... handy aliases for various values.
1728 # Special class for NULL pointers. Note the special check in
1729 # make_param_value() above that lets these be assigned where a
1730 # SimObject is required.
1731 # only one copy of a particular node
1732 class NullSimObject(object):
1733 __metaclass__
= Singleton
1738 def _instantiate(self
, parent
= None, path
= ''):
1744 def unproxy(self
, base
):
1747 def set_path(self
, parent
, name
):
1753 def config_value(self
):
1759 # The only instance you'll ever need...
1760 NULL
= NullSimObject()
1762 def isNullPointer(value
):
1763 return isinstance(value
, NullSimObject
)
1765 # Some memory range specifications use this as a default upper bound.
1768 AllMemory
= AddrRange(0, MaxAddr
)
1771 #####################################################################
1775 # Ports are used to interconnect objects in the memory system.
1777 #####################################################################
1779 # Port reference: encapsulates a reference to a particular port on a
1780 # particular SimObject.
1781 class PortRef(object):
1782 def __init__(self
, simobj
, name
, role
):
1783 assert(isSimObject(simobj
) or isSimObjectClass(simobj
))
1784 self
.simobj
= simobj
1787 self
.peer
= None # not associated with another port yet
1788 self
.ccConnected
= False # C++ port connection done?
1789 self
.index
= -1 # always -1 for non-vector ports
1792 return '%s.%s' % (self
.simobj
, self
.name
)
1795 # Return the number of connected ports, i.e. 0 is we have no
1796 # peer and 1 if we do.
1797 return int(self
.peer
!= None)
1799 # for config.ini, print peer's name (not ours)
1801 return str(self
.peer
)
1804 def get_config_as_dict(self
):
1805 return {'role' : self
.role
, 'peer' : str(self
.peer
)}
1807 def __getattr__(self
, attr
):
1808 if attr
== 'peerObj':
1809 # shorthand for proxies
1810 return self
.peer
.simobj
1811 raise AttributeError, "'%s' object has no attribute '%s'" % \
1812 (self
.__class
__.__name
__, attr
)
1814 # Full connection is symmetric (both ways). Called via
1815 # SimObject.__setattr__ as a result of a port assignment, e.g.,
1816 # "obj1.portA = obj2.portB", or via VectorPortElementRef.__setitem__,
1817 # e.g., "obj1.portA[3] = obj2.portB".
1818 def connect(self
, other
):
1819 if isinstance(other
, VectorPortRef
):
1820 # reference to plain VectorPort is implicit append
1821 other
= other
._get
_next
()
1822 if self
.peer
and not proxy
.isproxy(self
.peer
):
1823 fatal("Port %s is already connected to %s, cannot connect %s\n",
1824 self
, self
.peer
, other
);
1826 if proxy
.isproxy(other
):
1827 other
.set_param_desc(PortParamDesc())
1828 elif isinstance(other
, PortRef
):
1829 if other
.peer
is not self
:
1833 "assigning non-port reference '%s' to port '%s'" \
1836 # Allow a master/slave port pair to be spliced between
1837 # a port and its connected peer. Useful operation for connecting
1838 # instrumentation structures into a system when it is necessary
1839 # to connect the instrumentation after the full system has been
1841 def splice(self
, new_master_peer
, new_slave_peer
):
1842 if self
.peer
and not proxy
.isproxy(self
.peer
):
1843 if isinstance(new_master_peer
, PortRef
) and \
1844 isinstance(new_slave_peer
, PortRef
):
1845 old_peer
= self
.peer
1846 if self
.role
== 'SLAVE':
1847 self
.peer
= new_master_peer
1848 old_peer
.peer
= new_slave_peer
1849 new_master_peer
.connect(self
)
1850 new_slave_peer
.connect(old_peer
)
1851 elif self
.role
== 'MASTER':
1852 self
.peer
= new_slave_peer
1853 old_peer
.peer
= new_master_peer
1854 new_slave_peer
.connect(self
)
1855 new_master_peer
.connect(old_peer
)
1857 panic("Port %s has unknown role, "+\
1858 "cannot splice in new peers\n", self
)
1861 "Splicing non-port references '%s','%s' to port '%s'"\
1862 % (new_peer
, peers_new_peer
, self
)
1864 fatal("Port %s not connected, cannot splice in new peers\n", self
)
1866 def clone(self
, simobj
, memo
):
1867 if memo
.has_key(self
):
1869 newRef
= copy
.copy(self
)
1871 newRef
.simobj
= simobj
1872 assert(isSimObject(newRef
.simobj
))
1873 if self
.peer
and not proxy
.isproxy(self
.peer
):
1874 peerObj
= self
.peer
.simobj(_memo
=memo
)
1875 newRef
.peer
= self
.peer
.clone(peerObj
, memo
)
1876 assert(not isinstance(newRef
.peer
, VectorPortRef
))
1879 def unproxy(self
, simobj
):
1880 assert(simobj
is self
.simobj
)
1881 if proxy
.isproxy(self
.peer
):
1883 realPeer
= self
.peer
.unproxy(self
.simobj
)
1885 print "Error in unproxying port '%s' of %s" % \
1886 (self
.name
, self
.simobj
.path())
1888 self
.connect(realPeer
)
1890 # Call C++ to create corresponding port connection between C++ objects
1891 def ccConnect(self
):
1892 from m5
.internal
.pyobject
import connectPorts
1894 if self
.role
== 'SLAVE':
1895 # do nothing and let the master take care of it
1898 if self
.ccConnected
: # already done this
1901 if not self
.peer
: # nothing to connect to
1904 # check that we connect a master to a slave
1905 if self
.role
== peer
.role
:
1907 "cannot connect '%s' and '%s' due to identical role '%s'" \
1908 % (peer
, self
, self
.role
)
1911 # self is always the master and peer the slave
1912 connectPorts(self
.simobj
.getCCObject(), self
.name
, self
.index
,
1913 peer
.simobj
.getCCObject(), peer
.name
, peer
.index
)
1915 print "Error connecting port %s.%s to %s.%s" % \
1916 (self
.simobj
.path(), self
.name
,
1917 peer
.simobj
.path(), peer
.name
)
1919 self
.ccConnected
= True
1920 peer
.ccConnected
= True
1922 # A reference to an individual element of a VectorPort... much like a
1923 # PortRef, but has an index.
1924 class VectorPortElementRef(PortRef
):
1925 def __init__(self
, simobj
, name
, role
, index
):
1926 PortRef
.__init
__(self
, simobj
, name
, role
)
1930 return '%s.%s[%d]' % (self
.simobj
, self
.name
, self
.index
)
1932 # A reference to a complete vector-valued port (not just a single element).
1933 # Can be indexed to retrieve individual VectorPortElementRef instances.
1934 class VectorPortRef(object):
1935 def __init__(self
, simobj
, name
, role
):
1936 assert(isSimObject(simobj
) or isSimObjectClass(simobj
))
1937 self
.simobj
= simobj
1943 return '%s.%s[:]' % (self
.simobj
, self
.name
)
1946 # Return the number of connected peers, corresponding the the
1947 # length of the elements.
1948 return len(self
.elements
)
1950 # for config.ini, print peer's name (not ours)
1952 return ' '.join([el
.ini_str() for el
in self
.elements
])
1955 def get_config_as_dict(self
):
1956 return {'role' : self
.role
,
1957 'peer' : [el
.ini_str() for el
in self
.elements
]}
1959 def __getitem__(self
, key
):
1960 if not isinstance(key
, int):
1961 raise TypeError, "VectorPort index must be integer"
1962 if key
>= len(self
.elements
):
1963 # need to extend list
1964 ext
= [VectorPortElementRef(self
.simobj
, self
.name
, self
.role
, i
)
1965 for i
in range(len(self
.elements
), key
+1)]
1966 self
.elements
.extend(ext
)
1967 return self
.elements
[key
]
1969 def _get_next(self
):
1970 return self
[len(self
.elements
)]
1972 def __setitem__(self
, key
, value
):
1973 if not isinstance(key
, int):
1974 raise TypeError, "VectorPort index must be integer"
1975 self
[key
].connect(value
)
1977 def connect(self
, other
):
1978 if isinstance(other
, (list, tuple)):
1979 # Assign list of port refs to vector port.
1980 # For now, append them... not sure if that's the right semantics
1981 # or if it should replace the current vector.
1983 self
._get
_next
().connect(ref
)
1985 # scalar assignment to plain VectorPort is implicit append
1986 self
._get
_next
().connect(other
)
1988 def clone(self
, simobj
, memo
):
1989 if memo
.has_key(self
):
1991 newRef
= copy
.copy(self
)
1993 newRef
.simobj
= simobj
1994 assert(isSimObject(newRef
.simobj
))
1995 newRef
.elements
= [el
.clone(simobj
, memo
) for el
in self
.elements
]
1998 def unproxy(self
, simobj
):
1999 [el
.unproxy(simobj
) for el
in self
.elements
]
2001 def ccConnect(self
):
2002 [el
.ccConnect() for el
in self
.elements
]
2004 # Port description object. Like a ParamDesc object, this represents a
2005 # logical port in the SimObject class, not a particular port on a
2006 # SimObject instance. The latter are represented by PortRef objects.
2008 # Generate a PortRef for this port on the given SimObject with the
2010 def makeRef(self
, simobj
):
2011 return PortRef(simobj
, self
.name
, self
.role
)
2013 # Connect an instance of this port (on the given SimObject with
2014 # the given name) with the port described by the supplied PortRef
2015 def connect(self
, simobj
, ref
):
2016 self
.makeRef(simobj
).connect(ref
)
2018 # No need for any pre-declarations at the moment as we merely rely
2019 # on an unsigned int.
2020 def cxx_predecls(self
, code
):
2023 # Declare an unsigned int with the same name as the port, that
2024 # will eventually hold the number of connected ports (and thus the
2025 # number of elements for a VectorPort).
2026 def cxx_decl(self
, code
):
2027 code('unsigned int port_${{self.name}}_connection_count;')
2029 class MasterPort(Port
):
2030 # MasterPort("description")
2031 def __init__(self
, *args
):
2034 self
.role
= 'MASTER'
2036 raise TypeError, 'wrong number of arguments'
2038 class SlavePort(Port
):
2039 # SlavePort("description")
2040 def __init__(self
, *args
):
2045 raise TypeError, 'wrong number of arguments'
2047 # VectorPort description object. Like Port, but represents a vector
2048 # of connections (e.g., as on a XBar).
2049 class VectorPort(Port
):
2050 def __init__(self
, *args
):
2053 def makeRef(self
, simobj
):
2054 return VectorPortRef(simobj
, self
.name
, self
.role
)
2056 class VectorMasterPort(VectorPort
):
2057 # VectorMasterPort("description")
2058 def __init__(self
, *args
):
2061 self
.role
= 'MASTER'
2062 VectorPort
.__init
__(self
, *args
)
2064 raise TypeError, 'wrong number of arguments'
2066 class VectorSlavePort(VectorPort
):
2067 # VectorSlavePort("description")
2068 def __init__(self
, *args
):
2072 VectorPort
.__init
__(self
, *args
)
2074 raise TypeError, 'wrong number of arguments'
2076 # 'Fake' ParamDesc for Port references to assign to the _pdesc slot of
2077 # proxy objects (via set_param_desc()) so that proxy error messages
2079 class PortParamDesc(object):
2080 __metaclass__
= Singleton
2085 baseEnums
= allEnums
.copy()
2086 baseParams
= allParams
.copy()
2089 global allEnums
, allParams
2091 allEnums
= baseEnums
.copy()
2092 allParams
= baseParams
.copy()
2094 __all__
= ['Param', 'VectorParam',
2095 'Enum', 'Bool', 'String', 'Float',
2096 'Int', 'Unsigned', 'Int8', 'UInt8', 'Int16', 'UInt16',
2097 'Int32', 'UInt32', 'Int64', 'UInt64',
2098 'Counter', 'Addr', 'Tick', 'Percent',
2099 'TcpPort', 'UdpPort', 'EthernetAddr',
2100 'IpAddress', 'IpNetmask', 'IpWithPort',
2101 'MemorySize', 'MemorySize32',
2102 'Latency', 'Frequency', 'Clock', 'Voltage',
2103 'NetworkBandwidth', 'MemoryBandwidth',
2105 'MaxAddr', 'MaxTick', 'AllMemory',
2107 'NextEthernetAddr', 'NULL',
2108 'MasterPort', 'SlavePort',
2109 'VectorMasterPort', 'VectorSlavePort']