BPConfig.py revision 13731
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
33
34from m5 import fatal
35import m5.objects
36import inspect
37import sys
38from textwrap import TextWrapper
39
40# Dictionary of mapping names of real branch predictor models to classes.
41_bp_classes = {}
42
43
44def is_bp_class(cls):
45    """Determine if a class is a branch predictor that can be instantiated"""
46
47    # We can't use the normal inspect.isclass because the ParamFactory
48    # and ProxyFactory classes have a tendency to confuse it.
49    try:
50        return issubclass(cls, m5.objects.BranchPredictor) and \
51            not cls.abstract
52    except (TypeError, AttributeError):
53        return False
54
55def get(name):
56    """Get a BP class from a user provided class name or alias."""
57
58    try:
59        bp_class = _bp_classes[name]
60        return bp_class
61    except KeyError:
62        print("%s is not a valid BP model." % (name,))
63        sys.exit(1)
64
65def print_bp_list():
66    """Print a list of available BP classes."""
67
68    print("Available BranchPredictor classes:")
69    doc_wrapper = TextWrapper(initial_indent="\t\t", subsequent_indent="\t\t")
70    for name, cls in _bp_classes.items():
71        print("\t%s" % name)
72
73        # Try to extract the class documentation from the class help
74        # string.
75        doc = inspect.getdoc(cls)
76        if doc:
77            for line in doc_wrapper.wrap(doc):
78                print(line)
79
80def bp_names():
81    """Return a list of valid Branch Predictor names."""
82    return list(_bp_classes.keys())
83
84# Add all BPs in the object hierarchy.
85for name, cls in inspect.getmembers(m5.objects, is_bp_class):
86    _bp_classes[name] = cls
87
88