misc: Merged release-staging-v19.0.0.0 into develop
[gem5.git] / configs / common / ObjectList.py
1 # Copyright (c) 2019 Inria
2 # Copyright (c) 2012, 2017-2018 ARM Limited
3 # All rights reserved.
4 #
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.
13 #
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.
24 #
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.
36
37 from __future__ import print_function
38 from __future__ import absolute_import
39
40 import m5.objects
41 import inspect
42 import sys
43 from textwrap import TextWrapper
44
45 class ObjectList(object):
46 """ Creates a list of objects that are sub-classes of a given class. """
47
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.
51 """
52
53 # We can't use the normal inspect.isclass because the ParamFactory
54 # and ProxyFactory classes have a tendency to confuse it.
55 try:
56 return issubclass(cls, self.base_cls) and not cls.abstract
57 except (TypeError, AttributeError):
58 return False
59
60 def get(self, name):
61 """Get a sub class from a user provided class name or alias."""
62
63 real_name = self._aliases.get(name, name)
64 try:
65 sub_cls = self._sub_classes[real_name]
66 return sub_cls
67 except KeyError:
68 print("{} is not a valid sub-class of {}.".format(name, \
69 self.base_cls))
70 raise
71
72 def print(self):
73 """Print a list of available sub-classes and aliases."""
74
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))
80
81 # Try to extract the class documentation from the class help
82 # string.
83 doc = inspect.getdoc(cls)
84 if doc:
85 for line in doc_wrapper.wrap(doc):
86 print(line)
87
88 if self._aliases:
89 print("\Aliases:")
90 for alias, target in self._aliases.items():
91 print("\t{} => {}".format(alias, target))
92
93 def get_names(self):
94 """Return a list of valid sub-class names and aliases."""
95 return list(self._sub_classes.keys()) + list(self._aliases.keys())
96
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
101
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
108
109 def __init__(self, base_cls, aliases=None):
110 # Base class that will be used to determine if models are of this
111 # object class
112 self.base_cls = base_cls
113 # Dictionary that maps names of real models to classes
114 self._sub_classes = {}
115 self._add_objects()
116
117 # Filtered list of aliases. Only aliases for existing objects exist
118 # in this list.
119 self._aliases = {}
120 self._add_aliases(aliases)
121
122 class CPUList(ObjectList):
123 def _is_obj_class(self, cls):
124 """Determine if a class is a CPU that can be instantiated"""
125
126 # We can't use the normal inspect.isclass because the ParamFactory
127 # and ProxyFactory classes have a tendency to confuse it.
128 try:
129 return super(CPUList, self)._is_obj_class(cls) and \
130 not issubclass(cls, m5.objects.CheckerCPU)
131 except (TypeError, AttributeError):
132 return False
133
134 def _add_objects(self):
135 super(CPUList, self)._add_objects()
136
137 from m5.defines import buildEnv
138 from importlib import import_module
139 for package in [ "generic", buildEnv['TARGET_ISA']]:
140 try:
141 package = import_module(".cores." + package,
142 package=__name__.rpartition('.')[0])
143 except ImportError:
144 # No timing models for this ISA
145 continue
146
147 for mod_name, module in \
148 inspect.getmembers(package, inspect.ismodule):
149 for name, cls in inspect.getmembers(module,
150 self._is_obj_class):
151 self._sub_classes[name] = cls
152
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))
158
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"),
163 ]
164 platform_list = ObjectList(getattr(m5.objects, 'Platform', None), \
165 _platform_aliases_all)
166
167 def _subclass_tester(name):
168 sub_class = getattr(m5.objects, name, None)
169
170 def tester(cls):
171 return sub_class is not None and cls is not None and \
172 issubclass(cls, sub_class)
173
174 return tester
175
176 is_kvm_cpu = _subclass_tester("BaseKvmCPU")
177 is_noncaching_cpu = _subclass_tester("NonCachingSimpleCPU")