1# Copyright (c) 2018 Metempsy Technology Consulting
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright
9# notice, this list of conditions and the following disclaimer in the
10# documentation and/or other materials provided with the distribution;
11# neither the name of the copyright holders nor the names of its
12# contributors may be used to endorse or promote products derived from
13# this software without specific prior written permission.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# Authors: Pau Cabre
28
29# This file is a copy of MemConfig.py / CpuConfig.py, but modified to
30# hanle branch predictors instead of memory controllers / CPUs
31
32from __future__ import print_function
33from __future__ import absolute_import
34
35from m5 import fatal
36import m5.objects
37import inspect
38import sys
39from textwrap import TextWrapper
40
41# Dictionary of mapping names of real branch predictor models to classes.
42_hwp_classes = {}
43
44
45def is_hwp_class(cls):
46    """Determine if a class is a prefetcher that can be instantiated"""
47
48    # We can't use the normal inspect.isclass because the ParamFactory
49    # and ProxyFactory classes have a tendency to confuse it.
50    try:
51        return issubclass(cls, m5.objects.BasePrefetcher) and \
52            not cls.abstract
53    except (TypeError, AttributeError):
54        return False
55
56def get(name):
57    """Get a HWP class from a user provided class name or alias."""
58
59    try:
60        hwp_class = _hwp_classes[name]
61        return hwp_class
62    except KeyError:
63        print("%s is not a valid HWP model." % (name,))
64        sys.exit(1)
65
66def print_hwp_list():
67    """Print a list of available HWP classes."""
68
69    print("Available Hardware Prefetcher classes:")
70    doc_wrapper = TextWrapper(initial_indent="\t\t", subsequent_indent="\t\t")
71    for name, cls in _hwp_classes.items():
72        print("\t%s" % name)
73
74        # Try to extract the class documentation from the class help
75        # string.
76        doc = inspect.getdoc(cls)
77        if doc:
78            for line in doc_wrapper.wrap(doc):
79                print(line)
80
81def hwp_names():
82    """Return a list of valid Hardware Prefetcher names."""
83    return list(_hwp_classes.keys())
84
85# Add all HWPs in the object hierarchy.
86for name, cls in inspect.getmembers(m5.objects, is_hwp_class):
87    _hwp_classes[name] = cls
88
89