1 # Copyright (c) 2012 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
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 # allows us to blithely call unproxy() on things without checking
118 # if they're really proxies or not
119 def unproxy(self
, base
):
122 # Regular parameter description.
123 class ParamDesc(object):
124 def __init__(self
, ptype_str
, ptype
, *args
, **kwargs
):
125 self
.ptype_str
= ptype_str
126 # remember ptype only if it is provided
134 self
.default
= args
[0]
137 raise TypeError, 'too many arguments'
139 if kwargs
.has_key('desc'):
140 assert(not hasattr(self
, 'desc'))
141 self
.desc
= kwargs
['desc']
144 if kwargs
.has_key('default'):
145 assert(not hasattr(self
, 'default'))
146 self
.default
= kwargs
['default']
147 del kwargs
['default']
150 raise TypeError, 'extra unknown kwargs %s' % kwargs
152 if not hasattr(self
, 'desc'):
153 raise TypeError, 'desc attribute missing'
155 def __getattr__(self
, attr
):
157 ptype
= SimObject
.allClasses
[self
.ptype_str
]
158 assert isSimObjectClass(ptype
)
162 raise AttributeError, "'%s' object has no attribute '%s'" % \
163 (type(self
).__name
__, attr
)
165 def convert(self
, value
):
166 if isinstance(value
, proxy
.BaseProxy
):
167 value
.set_param_desc(self
)
169 if not hasattr(self
, 'ptype') and isNullPointer(value
):
170 # deferred evaluation of SimObject; continue to defer if
171 # we're just assigning a null pointer
173 if isinstance(value
, self
.ptype
):
175 if isNullPointer(value
) and isSimObjectClass(self
.ptype
):
177 return self
.ptype(value
)
179 def cxx_predecls(self
, code
):
180 code('#include <cstddef>')
181 self
.ptype
.cxx_predecls(code
)
183 def swig_predecls(self
, code
):
184 self
.ptype
.swig_predecls(code
)
186 def cxx_decl(self
, code
):
187 code('${{self.ptype.cxx_type}} ${{self.name}};')
189 # Vector-valued parameter description. Just like ParamDesc, except
190 # that the value is a vector (list) of the specified type instead of a
193 class VectorParamValue(list):
194 __metaclass__
= MetaParamValue
195 def __setattr__(self
, attr
, value
):
196 raise AttributeError, \
197 "Not allowed to set %s on '%s'" % (attr
, type(self
).__name
__)
200 return ' '.join([v
.ini_str() for v
in self
])
203 return [ v
.getValue() for v
in self
]
205 def unproxy(self
, base
):
206 if len(self
) == 1 and isinstance(self
[0], proxy
.AllProxy
):
207 return self
[0].unproxy(base
)
209 return [v
.unproxy(base
) for v
in self
]
211 class SimObjectVector(VectorParamValue
):
212 # support clone operation
213 def __call__(self
, **kwargs
):
214 return SimObjectVector([v(**kwargs
) for v
in self
])
216 def clear_parent(self
, old_parent
):
218 v
.clear_parent(old_parent
)
220 def set_parent(self
, parent
, name
):
222 self
[0].set_parent(parent
, name
)
224 width
= int(math
.ceil(math
.log(len(self
))/math
.log(10)))
225 for i
,v
in enumerate(self
):
226 v
.set_parent(parent
, "%s%0*d" % (name
, width
, i
))
228 def has_parent(self
):
229 return reduce(lambda x
,y
: x
and y
, [v
.has_parent() for v
in self
])
231 # return 'cpu0 cpu1' etc. for print_ini()
233 return ' '.join([v
._name
for v
in self
])
235 # By iterating through the constituent members of the vector here
236 # we can nicely handle iterating over all a SimObject's children
237 # without having to provide lots of special functions on
238 # SimObjectVector directly.
239 def descendants(self
):
241 for obj
in v
.descendants():
244 def get_config_as_dict(self
):
247 a
.append(v
.get_config_as_dict())
250 class VectorParamDesc(ParamDesc
):
251 # Convert assigned value to appropriate type. If the RHS is not a
252 # list or tuple, it generates a single-element list.
253 def convert(self
, value
):
254 if isinstance(value
, (list, tuple)):
255 # list: coerce each element into new list
256 tmp_list
= [ ParamDesc
.convert(self
, v
) for v
in value
]
258 # singleton: coerce to a single-element list
259 tmp_list
= [ ParamDesc
.convert(self
, value
) ]
261 if isSimObjectSequence(tmp_list
):
262 return SimObjectVector(tmp_list
)
264 return VectorParamValue(tmp_list
)
266 def swig_module_name(self
):
267 return "%s_vector" % self
.ptype_str
269 def swig_predecls(self
, code
):
270 code('%import "${{self.swig_module_name()}}.i"')
272 def swig_decl(self
, code
):
273 code('%module(package="m5.internal") ${{self.swig_module_name()}}')
275 self
.ptype
.cxx_predecls(code
)
278 # Make sure the SWIGPY_SLICE_ARG is defined through this inclusion
279 code('%include "std_container.i"')
281 self
.ptype
.swig_predecls(code
)
283 code('%include "std_vector.i"')
286 ptype
= self
.ptype_str
287 cxx_type
= self
.ptype
.cxx_type
290 %typemap(in) std::vector< $cxx_type >::value_type {
291 if (SWIG_ConvertPtr($$input, (void **)&$$1, $$1_descriptor, 0) == -1) {
292 if (SWIG_ConvertPtr($$input, (void **)&$$1,
293 $$descriptor($cxx_type), 0) == -1) {
299 %typemap(in) std::vector< $cxx_type >::value_type * {
300 if (SWIG_ConvertPtr($$input, (void **)&$$1, $$1_descriptor, 0) == -1) {
301 if (SWIG_ConvertPtr($$input, (void **)&$$1,
302 $$descriptor($cxx_type *), 0) == -1) {
309 code('%template(vector_$ptype) std::vector< $cxx_type >;')
311 def cxx_predecls(self
, code
):
312 code('#include <vector>')
313 self
.ptype
.cxx_predecls(code
)
315 def cxx_decl(self
, code
):
316 code('std::vector< ${{self.ptype.cxx_type}} > ${{self.name}};')
318 class ParamFactory(object):
319 def __init__(self
, param_desc_class
, ptype_str
= None):
320 self
.param_desc_class
= param_desc_class
321 self
.ptype_str
= ptype_str
323 def __getattr__(self
, attr
):
325 attr
= self
.ptype_str
+ '.' + attr
326 return ParamFactory(self
.param_desc_class
, attr
)
328 # E.g., Param.Int(5, "number of widgets")
329 def __call__(self
, *args
, **kwargs
):
332 ptype
= allParams
[self
.ptype_str
]
334 # if name isn't defined yet, assume it's a SimObject, and
335 # try to resolve it later
337 return self
.param_desc_class(self
.ptype_str
, ptype
, *args
, **kwargs
)
339 Param
= ParamFactory(ParamDesc
)
340 VectorParam
= ParamFactory(VectorParamDesc
)
342 #####################################################################
346 # Though native Python types could be used to specify parameter types
347 # (the 'ptype' field of the Param and VectorParam classes), it's more
348 # flexible to define our own set of types. This gives us more control
349 # over how Python expressions are converted to values (via the
350 # __init__() constructor) and how these values are printed out (via
351 # the __str__() conversion method).
353 #####################################################################
355 # String-valued parameter. Just mixin the ParamValue class with the
356 # built-in str class.
357 class String(ParamValue
,str):
358 cxx_type
= 'std::string'
361 def cxx_predecls(self
, code
):
362 code('#include <string>')
365 def swig_predecls(cls
, code
):
366 code('%include "std_string.i"')
371 # superclass for "numeric" parameter values, to emulate math
372 # operations in a type-safe way. e.g., a Latency times an int returns
373 # a new Latency object.
374 class NumericParamValue(ParamValue
):
376 return str(self
.value
)
379 return float(self
.value
)
382 return long(self
.value
)
385 return int(self
.value
)
387 # hook for bounds checking
391 def __mul__(self
, other
):
392 newobj
= self
.__class
__(self
)
393 newobj
.value
*= other
399 def __div__(self
, other
):
400 newobj
= self
.__class
__(self
)
401 newobj
.value
/= other
405 def __sub__(self
, other
):
406 newobj
= self
.__class
__(self
)
407 newobj
.value
-= other
411 # Metaclass for bounds-checked integer parameters. See CheckedInt.
412 class CheckedIntType(MetaParamValue
):
413 def __init__(cls
, name
, bases
, dict):
414 super(CheckedIntType
, cls
).__init
__(name
, bases
, dict)
416 # CheckedInt is an abstract base class, so we actually don't
417 # want to do any processing on it... the rest of this code is
418 # just for classes that derive from CheckedInt.
419 if name
== 'CheckedInt':
422 if not (hasattr(cls
, 'min') and hasattr(cls
, 'max')):
423 if not (hasattr(cls
, 'size') and hasattr(cls
, 'unsigned')):
424 panic("CheckedInt subclass %s must define either\n" \
425 " 'min' and 'max' or 'size' and 'unsigned'\n",
429 cls
.max = 2 ** cls
.size
- 1
431 cls
.min = -(2 ** (cls
.size
- 1))
432 cls
.max = (2 ** (cls
.size
- 1)) - 1
434 # Abstract superclass for bounds-checked integer parameters. This
435 # class is subclassed to generate parameter classes with specific
436 # bounds. Initialization of the min and max bounds is done in the
437 # metaclass CheckedIntType.__init__.
438 class CheckedInt(NumericParamValue
):
439 __metaclass__
= CheckedIntType
442 if not self
.min <= self
.value
<= self
.max:
443 raise TypeError, 'Integer param out of bounds %d < %d < %d' % \
444 (self
.min, self
.value
, self
.max)
446 def __init__(self
, value
):
447 if isinstance(value
, str):
448 self
.value
= convert
.toInteger(value
)
449 elif isinstance(value
, (int, long, float, NumericParamValue
)):
450 self
.value
= long(value
)
452 raise TypeError, "Can't convert object of type %s to CheckedInt" \
453 % type(value
).__name
__
457 def cxx_predecls(cls
, code
):
458 # most derived types require this, so we just do it here once
459 code('#include "base/types.hh"')
462 def swig_predecls(cls
, code
):
463 # most derived types require this, so we just do it here once
464 code('%import "stdint.i"')
465 code('%import "base/types.hh"')
468 return long(self
.value
)
470 class Int(CheckedInt
): cxx_type
= 'int'; size
= 32; unsigned
= False
471 class Unsigned(CheckedInt
): cxx_type
= 'unsigned'; size
= 32; unsigned
= True
473 class Int8(CheckedInt
): cxx_type
= 'int8_t'; size
= 8; unsigned
= False
474 class UInt8(CheckedInt
): cxx_type
= 'uint8_t'; size
= 8; unsigned
= True
475 class Int16(CheckedInt
): cxx_type
= 'int16_t'; size
= 16; unsigned
= False
476 class UInt16(CheckedInt
): cxx_type
= 'uint16_t'; size
= 16; unsigned
= True
477 class Int32(CheckedInt
): cxx_type
= 'int32_t'; size
= 32; unsigned
= False
478 class UInt32(CheckedInt
): cxx_type
= 'uint32_t'; size
= 32; unsigned
= True
479 class Int64(CheckedInt
): cxx_type
= 'int64_t'; size
= 64; unsigned
= False
480 class UInt64(CheckedInt
): cxx_type
= 'uint64_t'; size
= 64; unsigned
= True
482 class Counter(CheckedInt
): cxx_type
= 'Counter'; size
= 64; unsigned
= True
483 class Tick(CheckedInt
): cxx_type
= 'Tick'; size
= 64; unsigned
= True
484 class TcpPort(CheckedInt
): cxx_type
= 'uint16_t'; size
= 16; unsigned
= True
485 class UdpPort(CheckedInt
): cxx_type
= 'uint16_t'; size
= 16; unsigned
= True
487 class Percent(CheckedInt
): cxx_type
= 'int'; min = 0; max = 100
489 class Float(ParamValue
, float):
492 def __init__(self
, value
):
493 if isinstance(value
, (int, long, float, NumericParamValue
, Float
)):
494 self
.value
= float(value
)
496 raise TypeError, "Can't convert object of type %s to Float" \
497 % type(value
).__name
__
500 return float(self
.value
)
502 class MemorySize(CheckedInt
):
503 cxx_type
= 'uint64_t'
506 def __init__(self
, value
):
507 if isinstance(value
, MemorySize
):
508 self
.value
= value
.value
510 self
.value
= convert
.toMemorySize(value
)
513 class MemorySize32(CheckedInt
):
514 cxx_type
= 'uint32_t'
517 def __init__(self
, value
):
518 if isinstance(value
, MemorySize
):
519 self
.value
= value
.value
521 self
.value
= convert
.toMemorySize(value
)
524 class Addr(CheckedInt
):
528 def __init__(self
, value
):
529 if isinstance(value
, Addr
):
530 self
.value
= value
.value
533 self
.value
= convert
.toMemorySize(value
)
535 self
.value
= long(value
)
537 def __add__(self
, other
):
538 if isinstance(other
, Addr
):
539 return self
.value
+ other
.value
541 return self
.value
+ other
544 class MetaRange(MetaParamValue
):
545 def __init__(cls
, name
, bases
, dict):
546 super(MetaRange
, cls
).__init
__(name
, bases
, dict)
549 cls
.cxx_type
= 'Range< %s >' % cls
.type.cxx_type
551 class Range(ParamValue
):
552 __metaclass__
= MetaRange
553 type = Int
# default; can be overridden in subclasses
554 def __init__(self
, *args
, **kwargs
):
555 def handle_kwargs(self
, kwargs
):
557 self
.second
= self
.type(kwargs
.pop('end'))
558 elif 'size' in kwargs
:
559 self
.second
= self
.first
+ self
.type(kwargs
.pop('size')) - 1
561 raise TypeError, "Either end or size must be specified"
564 self
.first
= self
.type(kwargs
.pop('start'))
565 handle_kwargs(self
, kwargs
)
569 self
.first
= self
.type(args
[0])
570 handle_kwargs(self
, kwargs
)
571 elif isinstance(args
[0], Range
):
572 self
.first
= self
.type(args
[0].first
)
573 self
.second
= self
.type(args
[0].second
)
574 elif isinstance(args
[0], (list, tuple)):
575 self
.first
= self
.type(args
[0][0])
576 self
.second
= self
.type(args
[0][1])
578 self
.first
= self
.type(0)
579 self
.second
= self
.type(args
[0]) - 1
582 self
.first
= self
.type(args
[0])
583 self
.second
= self
.type(args
[1])
585 raise TypeError, "Too many arguments specified"
588 raise TypeError, "too many keywords: %s" % kwargs
.keys()
591 return '%s:%s' % (self
.first
, self
.second
)
594 def cxx_predecls(cls
, code
):
595 cls
.type.cxx_predecls(code
)
596 code('#include "base/range.hh"')
599 def swig_predecls(cls
, code
):
600 cls
.type.swig_predecls(code
)
601 code('%import "python/swig/range.i"')
603 class AddrRange(Range
):
607 from m5
.internal
.range import AddrRange
610 value
.start
= long(self
.first
)
611 value
.end
= long(self
.second
)
614 class TickRange(Range
):
618 from m5
.internal
.range import TickRange
621 value
.start
= long(self
.first
)
622 value
.end
= long(self
.second
)
625 # Boolean parameter type. Python doesn't let you subclass bool, since
626 # it doesn't want to let you create multiple instances of True and
627 # False. Thus this is a little more complicated than String.
628 class Bool(ParamValue
):
630 def __init__(self
, value
):
632 self
.value
= convert
.toBool(value
)
634 self
.value
= bool(value
)
637 return bool(self
.value
)
640 return str(self
.value
)
647 def IncEthernetAddr(addr
, val
= 1):
648 bytes
= map(lambda x
: int(x
, 16), addr
.split(':'))
650 for i
in (5, 4, 3, 2, 1):
651 val
,rem
= divmod(bytes
[i
], 256)
656 assert(bytes
[0] <= 255)
657 return ':'.join(map(lambda x
: '%02x' % x
, bytes
))
659 _NextEthernetAddr
= "00:90:00:00:00:01"
660 def NextEthernetAddr():
661 global _NextEthernetAddr
663 value
= _NextEthernetAddr
664 _NextEthernetAddr
= IncEthernetAddr(_NextEthernetAddr
, 1)
667 class EthernetAddr(ParamValue
):
668 cxx_type
= 'Net::EthAddr'
671 def cxx_predecls(cls
, code
):
672 code('#include "base/inet.hh"')
675 def swig_predecls(cls
, code
):
676 code('%include "python/swig/inet.i"')
678 def __init__(self
, value
):
679 if value
== NextEthernetAddr
:
683 if not isinstance(value
, str):
684 raise TypeError, "expected an ethernet address and didn't get one"
686 bytes
= value
.split(':')
688 raise TypeError, 'invalid ethernet address %s' % value
691 if not 0 <= int(byte
) <= 0xff:
692 raise TypeError, 'invalid ethernet address %s' % value
696 def unproxy(self
, base
):
697 if self
.value
== NextEthernetAddr
:
698 return EthernetAddr(self
.value())
702 from m5
.internal
.params
import EthAddr
703 return EthAddr(self
.value
)
708 # When initializing an IpAddress, pass in an existing IpAddress, a string of
709 # the form "a.b.c.d", or an integer representing an IP.
710 class IpAddress(ParamValue
):
711 cxx_type
= 'Net::IpAddress'
714 def cxx_predecls(cls
, code
):
715 code('#include "base/inet.hh"')
718 def swig_predecls(cls
, code
):
719 code('%include "python/swig/inet.i"')
721 def __init__(self
, value
):
722 if isinstance(value
, IpAddress
):
726 self
.ip
= convert
.toIpAddress(value
)
728 self
.ip
= long(value
)
732 tup
= [(self
.ip
>> i
) & 0xff for i
in (24, 16, 8, 0)]
733 return '%d.%d.%d.%d' % tuple(tup
)
735 def __eq__(self
, other
):
736 if isinstance(other
, IpAddress
):
737 return self
.ip
== other
.ip
738 elif isinstance(other
, str):
740 return self
.ip
== convert
.toIpAddress(other
)
744 return self
.ip
== other
746 def __ne__(self
, other
):
747 return not (self
== other
)
750 if self
.ip
< 0 or self
.ip
>= (1 << 32):
751 raise TypeError, "invalid ip address %#08x" % self
.ip
754 from m5
.internal
.params
import IpAddress
755 return IpAddress(self
.ip
)
757 # When initializing an IpNetmask, pass in an existing IpNetmask, a string of
758 # the form "a.b.c.d/n" or "a.b.c.d/e.f.g.h", or an ip and netmask as
759 # positional or keyword arguments.
760 class IpNetmask(IpAddress
):
761 cxx_type
= 'Net::IpNetmask'
764 def cxx_predecls(cls
, code
):
765 code('#include "base/inet.hh"')
768 def swig_predecls(cls
, code
):
769 code('%include "python/swig/inet.i"')
771 def __init__(self
, *args
, **kwargs
):
772 def handle_kwarg(self
, kwargs
, key
, elseVal
= None):
774 setattr(self
, key
, kwargs
.pop(key
))
776 setattr(self
, key
, elseVal
)
778 raise TypeError, "No value set for %s" % key
781 handle_kwarg(self
, kwargs
, 'ip')
782 handle_kwarg(self
, kwargs
, 'netmask')
786 if not 'ip' in kwargs
and not 'netmask' in kwargs
:
787 raise TypeError, "Invalid arguments"
788 handle_kwarg(self
, kwargs
, 'ip', args
[0])
789 handle_kwarg(self
, kwargs
, 'netmask', args
[0])
790 elif isinstance(args
[0], IpNetmask
):
792 self
.netmask
= args
[0].netmask
794 (self
.ip
, self
.netmask
) = convert
.toIpNetmask(args
[0])
798 self
.netmask
= args
[1]
800 raise TypeError, "Too many arguments specified"
803 raise TypeError, "Too many keywords: %s" % kwargs
.keys()
808 return "%s/%d" % (super(IpNetmask
, self
).__str
__(), self
.netmask
)
810 def __eq__(self
, other
):
811 if isinstance(other
, IpNetmask
):
812 return self
.ip
== other
.ip
and self
.netmask
== other
.netmask
813 elif isinstance(other
, str):
815 return (self
.ip
, self
.netmask
) == convert
.toIpNetmask(other
)
823 if self
.netmask
< 0 or self
.netmask
> 32:
824 raise TypeError, "invalid netmask %d" % netmask
827 from m5
.internal
.params
import IpNetmask
828 return IpNetmask(self
.ip
, self
.netmask
)
830 # When initializing an IpWithPort, pass in an existing IpWithPort, a string of
831 # the form "a.b.c.d:p", or an ip and port as positional or keyword arguments.
832 class IpWithPort(IpAddress
):
833 cxx_type
= 'Net::IpWithPort'
836 def cxx_predecls(cls
, code
):
837 code('#include "base/inet.hh"')
840 def swig_predecls(cls
, code
):
841 code('%include "python/swig/inet.i"')
843 def __init__(self
, *args
, **kwargs
):
844 def handle_kwarg(self
, kwargs
, key
, elseVal
= None):
846 setattr(self
, key
, kwargs
.pop(key
))
848 setattr(self
, key
, elseVal
)
850 raise TypeError, "No value set for %s" % key
853 handle_kwarg(self
, kwargs
, 'ip')
854 handle_kwarg(self
, kwargs
, 'port')
858 if not 'ip' in kwargs
and not 'port' in kwargs
:
859 raise TypeError, "Invalid arguments"
860 handle_kwarg(self
, kwargs
, 'ip', args
[0])
861 handle_kwarg(self
, kwargs
, 'port', args
[0])
862 elif isinstance(args
[0], IpWithPort
):
864 self
.port
= args
[0].port
866 (self
.ip
, self
.port
) = convert
.toIpWithPort(args
[0])
872 raise TypeError, "Too many arguments specified"
875 raise TypeError, "Too many keywords: %s" % kwargs
.keys()
880 return "%s:%d" % (super(IpWithPort
, self
).__str
__(), self
.port
)
882 def __eq__(self
, other
):
883 if isinstance(other
, IpWithPort
):
884 return self
.ip
== other
.ip
and self
.port
== other
.port
885 elif isinstance(other
, str):
887 return (self
.ip
, self
.port
) == convert
.toIpWithPort(other
)
895 if self
.port
< 0 or self
.port
> 0xffff:
896 raise TypeError, "invalid port %d" % self
.port
899 from m5
.internal
.params
import IpWithPort
900 return IpWithPort(self
.ip
, self
.port
)
902 time_formats
= [ "%a %b %d %H:%M:%S %Z %Y",
903 "%a %b %d %H:%M:%S %Z %Y",
915 def parse_time(value
):
916 from time
import gmtime
, strptime
, struct_time
, time
917 from datetime
import datetime
, date
919 if isinstance(value
, struct_time
):
922 if isinstance(value
, (int, long)):
925 if isinstance(value
, (datetime
, date
)):
926 return value
.timetuple()
928 if isinstance(value
, str):
929 if value
in ('Now', 'Today'):
930 return time
.gmtime(time
.time())
932 for format
in time_formats
:
934 return strptime(value
, format
)
938 raise ValueError, "Could not parse '%s' as a time" % value
940 class Time(ParamValue
):
944 def cxx_predecls(cls
, code
):
945 code('#include <time.h>')
948 def swig_predecls(cls
, code
):
949 code('%include "python/swig/time.i"')
951 def __init__(self
, value
):
952 self
.value
= parse_time(value
)
955 from m5
.internal
.params
import tm
960 # UNIX is years since 1900
961 c_time
.tm_year
= py_time
.tm_year
- 1900;
963 # Python starts at 1, UNIX starts at 0
964 c_time
.tm_mon
= py_time
.tm_mon
- 1;
965 c_time
.tm_mday
= py_time
.tm_mday
;
966 c_time
.tm_hour
= py_time
.tm_hour
;
967 c_time
.tm_min
= py_time
.tm_min
;
968 c_time
.tm_sec
= py_time
.tm_sec
;
970 # Python has 0 as Monday, UNIX is 0 as sunday
971 c_time
.tm_wday
= py_time
.tm_wday
+ 1
972 if c_time
.tm_wday
> 6:
975 # Python starts at 1, Unix starts at 0
976 c_time
.tm_yday
= py_time
.tm_yday
- 1;
981 return time
.asctime(self
.value
)
986 def get_config_as_dict(self
):
989 # Enumerated types are a little more complex. The user specifies the
990 # type as Enum(foo) where foo is either a list or dictionary of
991 # alternatives (typically strings, but not necessarily so). (In the
992 # long run, the integer value of the parameter will be the list index
993 # or the corresponding dictionary value. For now, since we only check
994 # that the alternative is valid and then spit it into a .ini file,
995 # there's not much point in using the dictionary.)
997 # What Enum() must do is generate a new type encapsulating the
998 # provided list/dictionary so that specific values of the parameter
999 # can be instances of that type. We define two hidden internal
1000 # classes (_ListEnum and _DictEnum) to serve as base classes, then
1001 # derive the new type from the appropriate base class on the fly.
1004 # Metaclass for Enum types
1005 class MetaEnum(MetaParamValue
):
1006 def __new__(mcls
, name
, bases
, dict):
1007 assert name
not in allEnums
1009 cls
= super(MetaEnum
, mcls
).__new
__(mcls
, name
, bases
, dict)
1010 allEnums
[name
] = cls
1013 def __init__(cls
, name
, bases
, init_dict
):
1014 if init_dict
.has_key('map'):
1015 if not isinstance(cls
.map, dict):
1016 raise TypeError, "Enum-derived class attribute 'map' " \
1017 "must be of type dict"
1018 # build list of value strings from map
1019 cls
.vals
= cls
.map.keys()
1021 elif init_dict
.has_key('vals'):
1022 if not isinstance(cls
.vals
, list):
1023 raise TypeError, "Enum-derived class attribute 'vals' " \
1024 "must be of type list"
1025 # build string->value map from vals sequence
1027 for idx
,val
in enumerate(cls
.vals
):
1030 raise TypeError, "Enum-derived class must define "\
1031 "attribute 'map' or 'vals'"
1033 cls
.cxx_type
= 'Enums::%s' % name
1035 super(MetaEnum
, cls
).__init
__(name
, bases
, init_dict
)
1037 # Generate C++ class declaration for this enum type.
1038 # Note that we wrap the enum in a class/struct to act as a namespace,
1039 # so that the enum strings can be brief w/o worrying about collisions.
1040 def cxx_decl(cls
, code
):
1043 #ifndef __ENUM__${name}__
1044 #define __ENUM__${name}__
1050 for val
in cls
.vals
:
1051 code('$val = ${{cls.map[val]}},')
1052 code('Num_$name = ${{len(cls.vals)}},')
1056 extern const char *${name}Strings[Num_${name}];
1059 #endif // __ENUM__${name}__
1062 def cxx_def(cls
, code
):
1065 #include "enums/$name.hh"
1067 const char *${name}Strings[Num_${name}] =
1071 for val
in cls
.vals
:
1076 } // namespace Enums
1079 def swig_decl(cls
, code
):
1082 %module(package="m5.internal") enum_$name
1085 #include "enums/$name.hh"
1088 %include "enums/$name.hh"
1092 # Base class for enum types.
1093 class Enum(ParamValue
):
1094 __metaclass__
= MetaEnum
1097 def __init__(self
, value
):
1098 if value
not in self
.map:
1099 raise TypeError, "Enum param got bad value '%s' (not in %s)" \
1100 % (value
, self
.vals
)
1104 def cxx_predecls(cls
, code
):
1105 code('#include "enums/$0.hh"', cls
.__name
__)
1108 def swig_predecls(cls
, code
):
1109 code('%import "python/m5/internal/enum_$0.i"', cls
.__name
__)
1112 return int(self
.map[self
.value
])
1117 # how big does a rounding error need to be before we warn about it?
1118 frequency_tolerance
= 0.001 # 0.1%
1120 class TickParamValue(NumericParamValue
):
1124 def cxx_predecls(cls
, code
):
1125 code('#include "base/types.hh"')
1128 def swig_predecls(cls
, code
):
1129 code('%import "stdint.i"')
1130 code('%import "base/types.hh"')
1133 return long(self
.value
)
1135 class Latency(TickParamValue
):
1136 def __init__(self
, value
):
1137 if isinstance(value
, (Latency
, Clock
)):
1138 self
.ticks
= value
.ticks
1139 self
.value
= value
.value
1140 elif isinstance(value
, Frequency
):
1141 self
.ticks
= value
.ticks
1142 self
.value
= 1.0 / value
.value
1143 elif value
.endswith('t'):
1145 self
.value
= int(value
[:-1])
1148 self
.value
= convert
.toLatency(value
)
1150 def __getattr__(self
, attr
):
1151 if attr
in ('latency', 'period'):
1153 if attr
== 'frequency':
1154 return Frequency(self
)
1155 raise AttributeError, "Latency object has no attribute '%s'" % attr
1158 if self
.ticks
or self
.value
== 0:
1161 value
= ticks
.fromSeconds(self
.value
)
1164 # convert latency to ticks
1166 return '%d' % self
.getValue()
1168 class Frequency(TickParamValue
):
1169 def __init__(self
, value
):
1170 if isinstance(value
, (Latency
, Clock
)):
1171 if value
.value
== 0:
1174 self
.value
= 1.0 / value
.value
1175 self
.ticks
= value
.ticks
1176 elif isinstance(value
, Frequency
):
1177 self
.value
= value
.value
1178 self
.ticks
= value
.ticks
1181 self
.value
= convert
.toFrequency(value
)
1183 def __getattr__(self
, attr
):
1184 if attr
== 'frequency':
1186 if attr
in ('latency', 'period'):
1187 return Latency(self
)
1188 raise AttributeError, "Frequency object has no attribute '%s'" % attr
1190 # convert latency to ticks
1192 if self
.ticks
or self
.value
== 0:
1195 value
= ticks
.fromSeconds(1.0 / self
.value
)
1199 return '%d' % self
.getValue()
1201 # A generic frequency and/or Latency value. Value is stored as a latency,
1202 # but to avoid ambiguity this object does not support numeric ops (* or /).
1203 # An explicit conversion to a Latency or Frequency must be made first.
1204 class Clock(ParamValue
):
1208 def cxx_predecls(cls
, code
):
1209 code('#include "base/types.hh"')
1212 def swig_predecls(cls
, code
):
1213 code('%import "stdint.i"')
1214 code('%import "base/types.hh"')
1216 def __init__(self
, value
):
1217 if isinstance(value
, (Latency
, Clock
)):
1218 self
.ticks
= value
.ticks
1219 self
.value
= value
.value
1220 elif isinstance(value
, Frequency
):
1221 self
.ticks
= value
.ticks
1222 self
.value
= 1.0 / value
.value
1223 elif value
.endswith('t'):
1225 self
.value
= int(value
[:-1])
1228 self
.value
= convert
.anyToLatency(value
)
1230 def __getattr__(self
, attr
):
1231 if attr
== 'frequency':
1232 return Frequency(self
)
1233 if attr
in ('latency', 'period'):
1234 return Latency(self
)
1235 raise AttributeError, "Frequency object has no attribute '%s'" % attr
1238 return self
.period
.getValue()
1241 return self
.period
.ini_str()
1243 class NetworkBandwidth(float,ParamValue
):
1245 def __new__(cls
, value
):
1246 # convert to bits per second
1247 val
= convert
.toNetworkBandwidth(value
)
1248 return super(cls
, NetworkBandwidth
).__new
__(cls
, val
)
1251 return str(self
.val
)
1254 # convert to seconds per byte
1255 value
= 8.0 / float(self
)
1256 # convert to ticks per byte
1257 value
= ticks
.fromSeconds(value
)
1261 return '%f' % self
.getValue()
1263 class MemoryBandwidth(float,ParamValue
):
1265 def __new__(cls
, value
):
1266 # convert to bytes per second
1267 val
= convert
.toMemoryBandwidth(value
)
1268 return super(cls
, MemoryBandwidth
).__new
__(cls
, val
)
1271 return str(self
.val
)
1274 # convert to seconds per byte
1277 value
= 1.0 / float(self
)
1278 # convert to ticks per byte
1279 value
= ticks
.fromSeconds(value
)
1283 return '%f' % self
.getValue()
1286 # "Constants"... handy aliases for various values.
1289 # Special class for NULL pointers. Note the special check in
1290 # make_param_value() above that lets these be assigned where a
1291 # SimObject is required.
1292 # only one copy of a particular node
1293 class NullSimObject(object):
1294 __metaclass__
= Singleton
1299 def _instantiate(self
, parent
= None, path
= ''):
1305 def unproxy(self
, base
):
1308 def set_path(self
, parent
, name
):
1317 # The only instance you'll ever need...
1318 NULL
= NullSimObject()
1320 def isNullPointer(value
):
1321 return isinstance(value
, NullSimObject
)
1323 # Some memory range specifications use this as a default upper bound.
1326 AllMemory
= AddrRange(0, MaxAddr
)
1329 #####################################################################
1333 # Ports are used to interconnect objects in the memory system.
1335 #####################################################################
1337 # Port reference: encapsulates a reference to a particular port on a
1338 # particular SimObject.
1339 class PortRef(object):
1340 def __init__(self
, simobj
, name
, role
):
1341 assert(isSimObject(simobj
) or isSimObjectClass(simobj
))
1342 self
.simobj
= simobj
1345 self
.peer
= None # not associated with another port yet
1346 self
.ccConnected
= False # C++ port connection done?
1347 self
.index
= -1 # always -1 for non-vector ports
1350 return '%s.%s' % (self
.simobj
, self
.name
)
1353 # Return the number of connected ports, i.e. 0 is we have no
1354 # peer and 1 if we do.
1355 return int(self
.peer
!= None)
1357 # for config.ini, print peer's name (not ours)
1359 return str(self
.peer
)
1361 def __getattr__(self
, attr
):
1362 if attr
== 'peerObj':
1363 # shorthand for proxies
1364 return self
.peer
.simobj
1365 raise AttributeError, "'%s' object has no attribute '%s'" % \
1366 (self
.__class
__.__name
__, attr
)
1368 # Full connection is symmetric (both ways). Called via
1369 # SimObject.__setattr__ as a result of a port assignment, e.g.,
1370 # "obj1.portA = obj2.portB", or via VectorPortElementRef.__setitem__,
1371 # e.g., "obj1.portA[3] = obj2.portB".
1372 def connect(self
, other
):
1373 if isinstance(other
, VectorPortRef
):
1374 # reference to plain VectorPort is implicit append
1375 other
= other
._get
_next
()
1376 if self
.peer
and not proxy
.isproxy(self
.peer
):
1377 print "warning: overwriting port", self
, \
1378 "value", self
.peer
, "with", other
1379 self
.peer
.peer
= None
1381 if proxy
.isproxy(other
):
1382 other
.set_param_desc(PortParamDesc())
1383 elif isinstance(other
, PortRef
):
1384 if other
.peer
is not self
:
1388 "assigning non-port reference '%s' to port '%s'" \
1391 def clone(self
, simobj
, memo
):
1392 if memo
.has_key(self
):
1394 newRef
= copy
.copy(self
)
1396 newRef
.simobj
= simobj
1397 assert(isSimObject(newRef
.simobj
))
1398 if self
.peer
and not proxy
.isproxy(self
.peer
):
1399 peerObj
= self
.peer
.simobj(_memo
=memo
)
1400 newRef
.peer
= self
.peer
.clone(peerObj
, memo
)
1401 assert(not isinstance(newRef
.peer
, VectorPortRef
))
1404 def unproxy(self
, simobj
):
1405 assert(simobj
is self
.simobj
)
1406 if proxy
.isproxy(self
.peer
):
1408 realPeer
= self
.peer
.unproxy(self
.simobj
)
1410 print "Error in unproxying port '%s' of %s" % \
1411 (self
.name
, self
.simobj
.path())
1413 self
.connect(realPeer
)
1415 # Call C++ to create corresponding port connection between C++ objects
1416 def ccConnect(self
):
1417 from m5
.internal
.pyobject
import connectPorts
1419 if self
.role
== 'SLAVE':
1420 # do nothing and let the master take care of it
1423 if self
.ccConnected
: # already done this
1426 if not self
.peer
: # nothing to connect to
1429 # check that we connect a master to a slave
1430 if self
.role
== peer
.role
:
1432 "cannot connect '%s' and '%s' due to identical role '%s'" \
1433 % (peer
, self
, self
.role
)
1436 # self is always the master and peer the slave
1437 connectPorts(self
.simobj
.getCCObject(), self
.name
, self
.index
,
1438 peer
.simobj
.getCCObject(), peer
.name
, peer
.index
)
1440 print "Error connecting port %s.%s to %s.%s" % \
1441 (self
.simobj
.path(), self
.name
,
1442 peer
.simobj
.path(), peer
.name
)
1444 self
.ccConnected
= True
1445 peer
.ccConnected
= True
1447 # A reference to an individual element of a VectorPort... much like a
1448 # PortRef, but has an index.
1449 class VectorPortElementRef(PortRef
):
1450 def __init__(self
, simobj
, name
, role
, index
):
1451 PortRef
.__init
__(self
, simobj
, name
, role
)
1455 return '%s.%s[%d]' % (self
.simobj
, self
.name
, self
.index
)
1457 # A reference to a complete vector-valued port (not just a single element).
1458 # Can be indexed to retrieve individual VectorPortElementRef instances.
1459 class VectorPortRef(object):
1460 def __init__(self
, simobj
, name
, role
):
1461 assert(isSimObject(simobj
) or isSimObjectClass(simobj
))
1462 self
.simobj
= simobj
1468 return '%s.%s[:]' % (self
.simobj
, self
.name
)
1471 # Return the number of connected peers, corresponding the the
1472 # length of the elements.
1473 return len(self
.elements
)
1475 # for config.ini, print peer's name (not ours)
1477 return ' '.join([el
.ini_str() for el
in self
.elements
])
1479 def __getitem__(self
, key
):
1480 if not isinstance(key
, int):
1481 raise TypeError, "VectorPort index must be integer"
1482 if key
>= len(self
.elements
):
1483 # need to extend list
1484 ext
= [VectorPortElementRef(self
.simobj
, self
.name
, self
.role
, i
)
1485 for i
in range(len(self
.elements
), key
+1)]
1486 self
.elements
.extend(ext
)
1487 return self
.elements
[key
]
1489 def _get_next(self
):
1490 return self
[len(self
.elements
)]
1492 def __setitem__(self
, key
, value
):
1493 if not isinstance(key
, int):
1494 raise TypeError, "VectorPort index must be integer"
1495 self
[key
].connect(value
)
1497 def connect(self
, other
):
1498 if isinstance(other
, (list, tuple)):
1499 # Assign list of port refs to vector port.
1500 # For now, append them... not sure if that's the right semantics
1501 # or if it should replace the current vector.
1503 self
._get
_next
().connect(ref
)
1505 # scalar assignment to plain VectorPort is implicit append
1506 self
._get
_next
().connect(other
)
1508 def clone(self
, simobj
, memo
):
1509 if memo
.has_key(self
):
1511 newRef
= copy
.copy(self
)
1513 newRef
.simobj
= simobj
1514 assert(isSimObject(newRef
.simobj
))
1515 newRef
.elements
= [el
.clone(simobj
, memo
) for el
in self
.elements
]
1518 def unproxy(self
, simobj
):
1519 [el
.unproxy(simobj
) for el
in self
.elements
]
1521 def ccConnect(self
):
1522 [el
.ccConnect() for el
in self
.elements
]
1524 # Port description object. Like a ParamDesc object, this represents a
1525 # logical port in the SimObject class, not a particular port on a
1526 # SimObject instance. The latter are represented by PortRef objects.
1528 # Generate a PortRef for this port on the given SimObject with the
1530 def makeRef(self
, simobj
):
1531 return PortRef(simobj
, self
.name
, self
.role
)
1533 # Connect an instance of this port (on the given SimObject with
1534 # the given name) with the port described by the supplied PortRef
1535 def connect(self
, simobj
, ref
):
1536 self
.makeRef(simobj
).connect(ref
)
1538 # No need for any pre-declarations at the moment as we merely rely
1539 # on an unsigned int.
1540 def cxx_predecls(self
, code
):
1543 # Declare an unsigned int with the same name as the port, that
1544 # will eventually hold the number of connected ports (and thus the
1545 # number of elements for a VectorPort).
1546 def cxx_decl(self
, code
):
1547 code('unsigned int port_${{self.name}}_connection_count;')
1549 class MasterPort(Port
):
1550 # MasterPort("description")
1551 def __init__(self
, *args
):
1554 self
.role
= 'MASTER'
1556 raise TypeError, 'wrong number of arguments'
1558 class SlavePort(Port
):
1559 # SlavePort("description")
1560 def __init__(self
, *args
):
1565 raise TypeError, 'wrong number of arguments'
1567 # VectorPort description object. Like Port, but represents a vector
1568 # of connections (e.g., as on a Bus).
1569 class VectorPort(Port
):
1570 def __init__(self
, *args
):
1573 def makeRef(self
, simobj
):
1574 return VectorPortRef(simobj
, self
.name
, self
.role
)
1576 class VectorMasterPort(VectorPort
):
1577 # VectorMasterPort("description")
1578 def __init__(self
, *args
):
1581 self
.role
= 'MASTER'
1582 VectorPort
.__init
__(self
, *args
)
1584 raise TypeError, 'wrong number of arguments'
1586 class VectorSlavePort(VectorPort
):
1587 # VectorSlavePort("description")
1588 def __init__(self
, *args
):
1592 VectorPort
.__init
__(self
, *args
)
1594 raise TypeError, 'wrong number of arguments'
1596 # 'Fake' ParamDesc for Port references to assign to the _pdesc slot of
1597 # proxy objects (via set_param_desc()) so that proxy error messages
1599 class PortParamDesc(object):
1600 __metaclass__
= Singleton
1605 baseEnums
= allEnums
.copy()
1606 baseParams
= allParams
.copy()
1609 global allEnums
, allParams
1611 allEnums
= baseEnums
.copy()
1612 allParams
= baseParams
.copy()
1614 __all__
= ['Param', 'VectorParam',
1615 'Enum', 'Bool', 'String', 'Float',
1616 'Int', 'Unsigned', 'Int8', 'UInt8', 'Int16', 'UInt16',
1617 'Int32', 'UInt32', 'Int64', 'UInt64',
1618 'Counter', 'Addr', 'Tick', 'Percent',
1619 'TcpPort', 'UdpPort', 'EthernetAddr',
1620 'IpAddress', 'IpNetmask', 'IpWithPort',
1621 'MemorySize', 'MemorySize32',
1622 'Latency', 'Frequency', 'Clock',
1623 'NetworkBandwidth', 'MemoryBandwidth',
1624 'Range', 'AddrRange', 'TickRange',
1625 'MaxAddr', 'MaxTick', 'AllMemory',
1627 'NextEthernetAddr', 'NULL',
1628 'MasterPort', 'SlavePort',
1629 'VectorMasterPort', 'VectorSlavePort']