2 # Copyright 2019 Advanced Micro Devices, Inc.
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:
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
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.
24 Helper script that was used during the generation of the JSON data.
26 usage: python3 canonicalize.py FILE
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.
32 Notes about deduced register types as well as the output JSON are printed on
36 from __future__
import absolute_import
, division
, print_function
, unicode_literals
38 from collections
import defaultdict
43 from regdb
import RegisterDatabase
, deduplicate_enums
, deduplicate_register_types
45 RE_number
= re
.compile('[0-9]+')
47 def deduce_missing_register_types(regdb
):
49 This is a heuristic for filling in missing register types based on
50 sequentially named registers.
52 buckets
= defaultdict(list)
53 for regmap
in regdb
.register_mappings():
54 buckets
[RE_number
.sub('0', regmap
.name
)].append(regmap
)
56 for bucket
in buckets
.values():
61 regmap
.type_ref
for regmap
in bucket
if hasattr(regmap
, 'type_ref')
63 if len(regtypenames
) == 1:
64 regtypename
= regtypenames
.pop()
66 if not hasattr(regmap
, 'type_ref'):
67 print('Deducing {0} -> {1}'.format(regmap
.name
, regtypename
), file=sys
.stderr
)
68 regmap
.type_ref
= regtypename
72 regdb_filename
= sys
.argv
[1]
73 with
open(regdb_filename
, 'r') as filp
:
74 regdb
= RegisterDatabase
.from_json(json
.load(filp
))
77 for regmap
in regdb
.register_mappings():
78 assert not hasattr(regmap
, 'chips')
79 regmap
.chips
= [sys
.argv
[2]]
81 deduplicate_enums(regdb
)
82 deduplicate_register_types(regdb
)
83 deduce_missing_register_types(regdb
)
84 regdb
.garbage_collect()
86 print(regdb
.encode_json_pretty())
89 if __name__
== '__main__':
92 # kate: space-indent on; indent-width 4; replace-tabs on;