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 __future__ import absolute_import, division, print_function, unicode_literals 37 38from collections import defaultdict 39import json 40import re 41import sys 42 43from regdb import RegisterDatabase, deduplicate_enums, deduplicate_register_types 44 45RE_number = re.compile('[0-9]+') 46 47def 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 71def json_canonicalize(filp, chips = None): 72 regdb = RegisterDatabase.from_json(json.load(filp)) 73 74 if chips is not None: 75 for regmap in regdb.register_mappings(): 76 assert not hasattr(regmap, 'chips') 77 regmap.chips = [chips] 78 79 deduplicate_enums(regdb) 80 deduplicate_register_types(regdb) 81 deduce_missing_register_types(regdb) 82 regdb.garbage_collect() 83 84 return regdb.encode_json_pretty() 85 86 87def main(): 88 print(json_canonicalize(open(sys.argv[1], 'r'), sys.argv[2])) 89 90if __name__ == '__main__': 91 main() 92 93# kate: space-indent on; indent-width 4; replace-tabs on; 94