1# Copyright (c) 2012, 2015 ARM Limited
2# All rights reserved.
3#
4# Copyright (c) 2017, Centre National de la Recherche Scientifique (CNRS)
5#
6# The license below extends only to copyright in the software and shall
7# not be construed as granting a license to any other intellectual
8# property including but not limited to intellectual property relating
9# to a hardware implementation of the functionality of the software
10# licensed hereunder.  You may use the software subject to the license
11# terms below provided that you ensure that this notice is replicated
12# unmodified and in its entirety in all distributions of the software,
13# modified or unmodified, in source code or in binary form.
14#
15# Redistribution and use in source and binary forms, with or without
16# modification, are permitted provided that the following conditions are
17# met: redistributions of source code must retain the above copyright
18# notice, this list of conditions and the following disclaimer;
19# redistributions in binary form must reproduce the above copyright
20# notice, this list of conditions and the following disclaimer in the
21# documentation and/or other materials provided with the distribution;
22# neither the name of the copyright holders nor the names of its
23# contributors may be used to endorse or promote products derived from
24# this software without specific prior written permission.
25#
26# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37#
38# Authors: Andreas Sandberg
39#          Pierre-Yves Peneau
40
41from __future__ import print_function
42from __future__ import absolute_import
43
44import m5.objects
45import inspect
46import sys
47from m5.util import fatal
48from textwrap import TextWrapper
49
50# Dictionary of mapping names of real CPU models to classes.
51_platform_classes = {}
52
53# Platform aliases. The platforms listed here might not be compiled,
54# we make sure they exist before we add them to the platform list.
55_platform_aliases_all = [
56    ("RealView_PBX", "RealViewPBX"),
57    ("VExpress_GEM5", "VExpress_GEM5_V1"),
58    ]
59
60# Filtered list of aliases. Only aliases for existing platforms exist
61# in this list.
62_platform_aliases = {}
63
64def is_platform_class(cls):
65    """Determine if a class is a Platform that can be instantiated"""
66
67    # We can't use the normal inspect.isclass because the ParamFactory
68    # and ProxyFactory classes have a tendency to confuse it.
69    try:
70        return issubclass(cls, m5.objects.Platform) and \
71            not cls.abstract
72    except (TypeError, AttributeError):
73        return False
74
75def get(name):
76    """Get a platform class from a user provided class name."""
77
78    real_name = _platform_aliases.get(name, name)
79
80    try:
81        return _platform_classes[real_name]
82    except KeyError:
83        fatal("%s is not a valid Platform model." % (name,))
84
85def print_platform_list():
86    """Print a list of available Platform classes including their aliases."""
87
88    print("Available Platform classes:")
89    doc_wrapper = TextWrapper(initial_indent="\t\t", subsequent_indent="\t\t")
90    for name, cls in _platform_classes.items():
91        print("\t%s" % name)
92
93        # Try to extract the class documentation from the class help
94        # string.
95        doc = inspect.getdoc(cls)
96        if doc:
97            for line in doc_wrapper.wrap(doc):
98                print(line)
99
100    if _platform_aliases:
101        print("\Platform aliases:")
102        for alias, target in _platform_aliases.items():
103            print("\t%s => %s" % (alias, target))
104
105def platform_names():
106    """Return a list of valid Platform names."""
107    return list(_platform_classes.keys()) + list(_platform_aliases.keys())
108
109# Add all Platforms in the object hierarchy.
110for name, cls in inspect.getmembers(m5.objects, is_platform_class):
111    _platform_classes[name] = cls
112
113for alias, target in _platform_aliases_all:
114    if target in _platform_classes:
115        _platform_aliases[alias] = target
116