958f43894333de94d4d077b84f6592c4097c4a7c
[mesa.git] / src / amd / registers / canonicalize.py
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 """
24 Helper script that was used during the generation of the JSON data.
25
26 usage: python3 canonicalize.py FILE
27
28 Reads the register database from FILE, performs canonicalization
29 (de-duplication of enums and register types, implicitly sorting JSON by name)
30 and attempts to deduce missing register types.
31
32 Notes about deduced register types as well as the output JSON are printed on
33 stdout.
34 """
35
36 from __future__ import absolute_import, division, print_function, unicode_literals
37
38 from collections import defaultdict
39 import json
40 import re
41 import sys
42
43 from regdb import RegisterDatabase, deduplicate_enums, deduplicate_register_types
44
45 RE_number = re.compile('[0-9]+')
46
47 def deduce_missing_register_types(regdb):
48 """
49 This is a heuristic for filling in missing register types based on
50 sequentially named registers.
51 """
52 buckets = defaultdict(list)
53 for regmap in regdb.register_mappings():
54 buckets[RE_number.sub('0', regmap.name)].append(regmap)
55
56 for bucket in buckets.values():
57 if len(bucket) <= 1:
58 continue
59
60 regtypenames = set(
61 regmap.type_ref for regmap in bucket if hasattr(regmap, 'type_ref')
62 )
63 if len(regtypenames) == 1:
64 regtypename = regtypenames.pop()
65 for regmap in bucket:
66 if not hasattr(regmap, 'type_ref'):
67 print('Deducing {0} -> {1}'.format(regmap.name, regtypename), file=sys.stderr)
68 regmap.type_ref = regtypename
69
70
71 def main():
72 regdb_filename = sys.argv[1]
73 with open(regdb_filename, 'r') as filp:
74 regdb = RegisterDatabase.from_json(json.load(filp))
75
76 if len(sys.argv) > 2:
77 for regmap in regdb.register_mappings():
78 assert not hasattr(regmap, 'chips')
79 regmap.chips = [sys.argv[2]]
80
81 deduplicate_enums(regdb)
82 deduplicate_register_types(regdb)
83 deduce_missing_register_types(regdb)
84 regdb.garbage_collect()
85
86 print(regdb.encode_json_pretty())
87
88
89 if __name__ == '__main__':
90 main()
91
92 # kate: space-indent on; indent-width 4; replace-tabs on;