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