1 # Copyright (c) 2019 Inria
2 # Copyright (c) 2012, 2017-2018 ARM Limited
5 # The license below extends only to copyright in the software and shall
6 # not be construed as granting a license to any other intellectual
7 # property including but not limited to intellectual property relating
8 # to a hardware implementation of the functionality of the software
9 # licensed hereunder. You may use the software subject to the license
10 # terms below provided that you ensure that this notice is replicated
11 # unmodified and in its entirety in all distributions of the software,
12 # modified or unmodified, in source code or in binary form.
14 # Redistribution and use in source and binary forms, with or without
15 # modification, are permitted provided that the following conditions are
16 # met: redistributions of source code must retain the above copyright
17 # notice, this list of conditions and the following disclaimer;
18 # redistributions in binary form must reproduce the above copyright
19 # notice, this list of conditions and the following disclaimer in the
20 # documentation and/or other materials provided with the distribution;
21 # neither the name of the copyright holders nor the names of its
22 # contributors may be used to endorse or promote products derived from
23 # this software without specific prior written permission.
25 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 from __future__
import print_function
38 from __future__
import absolute_import
43 from textwrap
import TextWrapper
45 class ObjectList(object):
46 """ Creates a list of objects that are sub-classes of a given class. """
48 def _is_obj_class(self
, cls
):
49 """Determine if a class is a a sub class of the provided base class
50 that can be instantiated.
53 # We can't use the normal inspect.isclass because the ParamFactory
54 # and ProxyFactory classes have a tendency to confuse it.
56 return issubclass(cls
, self
.base_cls
) and not cls
.abstract
57 except (TypeError, AttributeError):
61 """Get a sub class from a user provided class name or alias."""
63 real_name
= self
._aliases
.get(name
, name
)
65 sub_cls
= self
._sub
_classes
[real_name
]
68 print("{} is not a valid sub-class of {}.".format(name
, \
73 """Print a list of available sub-classes and aliases."""
75 print("Available {} classes:".format(self
.base_cls
))
76 doc_wrapper
= TextWrapper(initial_indent
="\t\t",
77 subsequent_indent
="\t\t")
78 for name
, cls
in self
._sub
_classes
.items():
79 print("\t{}".format(name
))
81 # Try to extract the class documentation from the class help
83 doc
= inspect
.getdoc(cls
)
85 for line
in doc_wrapper
.wrap(doc
):
90 for alias
, target
in self
._aliases
.items():
91 print("\t{} => {}".format(alias
, target
))
94 """Return a list of valid sub-class names and aliases."""
95 return list(self
._sub
_classes
.keys()) + list(self
._aliases
.keys())
97 def _add_objects(self
):
98 """Add all sub-classes of the base class in the object hierarchy."""
99 for name
, cls
in inspect
.getmembers(m5
.objects
, self
._is
_obj
_class
):
100 self
._sub
_classes
[name
] = cls
102 def _add_aliases(self
, aliases
):
103 """Add all aliases of the sub-classes."""
104 if aliases
is not None:
105 for alias
, target
in aliases
:
106 if target
in self
._sub
_classes
:
107 self
._aliases
[alias
] = target
109 def __init__(self
, base_cls
, aliases
=None):
110 # Base class that will be used to determine if models are of this
112 self
.base_cls
= base_cls
113 # Dictionary that maps names of real models to classes
114 self
._sub
_classes
= {}
117 # Filtered list of aliases. Only aliases for existing objects exist
120 self
._add
_aliases
(aliases
)
122 class CPUList(ObjectList
):
123 def _is_obj_class(self
, cls
):
124 """Determine if a class is a CPU that can be instantiated"""
126 # We can't use the normal inspect.isclass because the ParamFactory
127 # and ProxyFactory classes have a tendency to confuse it.
129 return super(CPUList
, self
)._is
_obj
_class
(cls
) and \
130 not issubclass(cls
, m5
.objects
.CheckerCPU
)
131 except (TypeError, AttributeError):
134 def _add_objects(self
):
135 super(CPUList
, self
)._add
_objects
()
137 from m5
.defines
import buildEnv
138 from importlib
import import_module
139 for package
in [ "generic", buildEnv
['TARGET_ISA']]:
141 package
= import_module(".cores." + package
,
142 package
=__name__
.rpartition('.')[0])
144 # No timing models for this ISA
147 for mod_name
, module
in \
148 inspect
.getmembers(package
, inspect
.ismodule
):
149 for name
, cls
in inspect
.getmembers(module
,
151 self
._sub
_classes
[name
] = cls
153 bp_list
= ObjectList(getattr(m5
.objects
, 'BranchPredictor', None))
154 cpu_list
= CPUList(getattr(m5
.objects
, 'BaseCPU', None))
155 hwp_list
= ObjectList(getattr(m5
.objects
, 'BasePrefetcher', None))
156 indirect_bp_list
= ObjectList(getattr(m5
.objects
, 'IndirectPredictor', None))
157 mem_list
= ObjectList(getattr(m5
.objects
, 'AbstractMemory', None))
159 # Platform aliases. The platforms listed here might not be compiled,
160 # we make sure they exist before we add them to the platform list.
161 _platform_aliases_all
= [
162 ("VExpress_GEM5", "VExpress_GEM5_V1"),
164 platform_list
= ObjectList(getattr(m5
.objects
, 'Platform', None), \
165 _platform_aliases_all
)
167 def _subclass_tester(name
):
168 sub_class
= getattr(m5
.objects
, name
, None)
171 return sub_class
is not None and cls
is not None and \
172 issubclass(cls
, sub_class
)
176 is_kvm_cpu
= _subclass_tester("BaseKvmCPU")
177 is_noncaching_cpu
= _subclass_tester("NonCachingSimpleCPU")