aboutsummaryrefslogtreecommitdiffstats
path: root/src/amd/registers/canonicalize.py
blob: 958f43894333de94d4d077b84f6592c4097c4a7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#
# Copyright 2019 Advanced Micro Devices, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# on the rights to use, copy, modify, merge, publish, distribute, sub
# license, and/or sell copies of the Software, and to permit persons to whom
# the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
# THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
# USE OR OTHER DEALINGS IN THE SOFTWARE.
#
"""
Helper script that was used during the generation of the JSON data.

  usage: python3 canonicalize.py FILE

Reads the register database from FILE, performs canonicalization
(de-duplication of enums and register types, implicitly sorting JSON by name)
and attempts to deduce missing register types.

Notes about deduced register types as well as the output JSON are printed on
stdout.
"""

from __future__ import absolute_import, division, print_function, unicode_literals

from collections import defaultdict
import json
import re
import sys

from regdb import RegisterDatabase, deduplicate_enums, deduplicate_register_types

RE_number = re.compile('[0-9]+')

def deduce_missing_register_types(regdb):
    """
    This is a heuristic for filling in missing register types based on
    sequentially named registers.
    """
    buckets = defaultdict(list)
    for regmap in regdb.register_mappings():
        buckets[RE_number.sub('0', regmap.name)].append(regmap)

    for bucket in buckets.values():
        if len(bucket) <= 1:
            continue

        regtypenames = set(
            regmap.type_ref for regmap in bucket if hasattr(regmap, 'type_ref')
        )
        if len(regtypenames) == 1:
            regtypename = regtypenames.pop()
            for regmap in bucket:
                if not hasattr(regmap, 'type_ref'):
                    print('Deducing {0} -> {1}'.format(regmap.name, regtypename), file=sys.stderr)
                regmap.type_ref = regtypename


def main():
    regdb_filename = sys.argv[1]
    with open(regdb_filename, 'r') as filp:
        regdb = RegisterDatabase.from_json(json.load(filp))

    if len(sys.argv) > 2:
        for regmap in regdb.register_mappings():
            assert not hasattr(regmap, 'chips')
            regmap.chips = [sys.argv[2]]

    deduplicate_enums(regdb)
    deduplicate_register_types(regdb)
    deduce_missing_register_types(regdb)
    regdb.garbage_collect()

    print(regdb.encode_json_pretty())


if __name__ == '__main__':
    main()

# kate: space-indent on; indent-width 4; replace-tabs on;