1#
2# Copyright 2019 Advanced Micro Devices, Inc.
3#
4# Permission is hereby granted, free of charge, to any person obtaining a
5# copy of this software and associated documentation files (the "Software"),
6# to deal in the Software without restriction, including without limitation
7# on the rights to use, copy, modify, merge, publish, distribute, sub
8# license, and/or sell copies of the Software, and to permit persons to whom
9# the Software is furnished to do so, subject to the following conditions:
10#
11# The above copyright notice and this permission notice (including the next
12# paragraph) shall be included in all copies or substantial portions of the
13# Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18# THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21# USE OR OTHER DEALINGS IN THE SOFTWARE.
22#
23"""
24Helper script that was used during the generation of the JSON data.
25
26  usage: python3 canonicalize.py FILE
27
28Reads the register database from FILE, performs canonicalization
29(de-duplication of enums and register types, implicitly sorting JSON by name)
30and attempts to deduce missing register types.
31
32Notes about deduced register types as well as the output JSON are printed on
33stdout.
34"""
35
36from collections import defaultdict
37import json
38import re
39import sys
40
41from regdb import RegisterDatabase, deduplicate_enums, deduplicate_register_types
42
43RE_number = re.compile('[0-9]+')
44
45def deduce_missing_register_types(regdb):
46    """
47    This is a heuristic for filling in missing register types based on
48    sequentially named registers.
49    """
50    buckets = defaultdict(list)
51    for regmap in regdb.register_mappings():
52        buckets[RE_number.sub('0', regmap.name)].append(regmap)
53
54    for bucket in buckets.values():
55        if len(bucket) <= 1:
56            continue
57
58        regtypenames = set(
59            regmap.type_ref for regmap in bucket if hasattr(regmap, 'type_ref')
60        )
61        if len(regtypenames) == 1:
62            regtypename = regtypenames.pop()
63            for regmap in bucket:
64                if not hasattr(regmap, 'type_ref'):
65                    print('Deducing {0} -> {1}'.format(regmap.name, regtypename), file=sys.stderr)
66                regmap.type_ref = regtypename
67
68
69def json_canonicalize(filp, chips = None):
70    regdb = RegisterDatabase.from_json(json.load(filp))
71
72    if chips is not None:
73        for regmap in regdb.register_mappings():
74            assert not hasattr(regmap, 'chips')
75            regmap.chips = [chips]
76
77    deduplicate_enums(regdb)
78    deduplicate_register_types(regdb)
79    deduce_missing_register_types(regdb)
80    regdb.garbage_collect()
81
82    return regdb.encode_json_pretty()
83
84
85def main():
86    print(json_canonicalize(open(sys.argv[1], 'r'), sys.argv[2]))
87
88if __name__ == '__main__':
89    main()
90
91# kate: space-indent on; indent-width 4; replace-tabs on;
92