17ec681f3Smrg#
27ec681f3Smrg# Copyright 2019 Advanced Micro Devices, Inc.
37ec681f3Smrg#
47ec681f3Smrg# Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg# copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg# to deal in the Software without restriction, including without limitation
77ec681f3Smrg# on the rights to use, copy, modify, merge, publish, distribute, sub
87ec681f3Smrg# license, and/or sell copies of the Software, and to permit persons to whom
97ec681f3Smrg# the Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg#
117ec681f3Smrg# The above copyright notice and this permission notice (including the next
127ec681f3Smrg# paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg# Software.
147ec681f3Smrg#
157ec681f3Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
187ec681f3Smrg# THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
197ec681f3Smrg# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
207ec681f3Smrg# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
217ec681f3Smrg# USE OR OTHER DEALINGS IN THE SOFTWARE.
227ec681f3Smrg#
237ec681f3Smrg"""
247ec681f3SmrgHelper script that was used during the generation of the JSON data.
257ec681f3Smrg
267ec681f3Smrg  usage: python3 canonicalize.py FILE
277ec681f3Smrg
287ec681f3SmrgReads the register database from FILE, performs canonicalization
297ec681f3Smrg(de-duplication of enums and register types, implicitly sorting JSON by name)
307ec681f3Smrgand attempts to deduce missing register types.
317ec681f3Smrg
327ec681f3SmrgNotes about deduced register types as well as the output JSON are printed on
337ec681f3Smrgstdout.
347ec681f3Smrg"""
357ec681f3Smrg
367ec681f3Smrgfrom collections import defaultdict
377ec681f3Smrgimport json
387ec681f3Smrgimport re
397ec681f3Smrgimport sys
407ec681f3Smrg
417ec681f3Smrgfrom regdb import RegisterDatabase, deduplicate_enums, deduplicate_register_types
427ec681f3Smrg
437ec681f3SmrgRE_number = re.compile('[0-9]+')
447ec681f3Smrg
457ec681f3Smrgdef deduce_missing_register_types(regdb):
467ec681f3Smrg    """
477ec681f3Smrg    This is a heuristic for filling in missing register types based on
487ec681f3Smrg    sequentially named registers.
497ec681f3Smrg    """
507ec681f3Smrg    buckets = defaultdict(list)
517ec681f3Smrg    for regmap in regdb.register_mappings():
527ec681f3Smrg        buckets[RE_number.sub('0', regmap.name)].append(regmap)
537ec681f3Smrg
547ec681f3Smrg    for bucket in buckets.values():
557ec681f3Smrg        if len(bucket) <= 1:
567ec681f3Smrg            continue
577ec681f3Smrg
587ec681f3Smrg        regtypenames = set(
597ec681f3Smrg            regmap.type_ref for regmap in bucket if hasattr(regmap, 'type_ref')
607ec681f3Smrg        )
617ec681f3Smrg        if len(regtypenames) == 1:
627ec681f3Smrg            regtypename = regtypenames.pop()
637ec681f3Smrg            for regmap in bucket:
647ec681f3Smrg                if not hasattr(regmap, 'type_ref'):
657ec681f3Smrg                    print('Deducing {0} -> {1}'.format(regmap.name, regtypename), file=sys.stderr)
667ec681f3Smrg                regmap.type_ref = regtypename
677ec681f3Smrg
687ec681f3Smrg
697ec681f3Smrgdef json_canonicalize(filp, chips = None):
707ec681f3Smrg    regdb = RegisterDatabase.from_json(json.load(filp))
717ec681f3Smrg
727ec681f3Smrg    if chips is not None:
737ec681f3Smrg        for regmap in regdb.register_mappings():
747ec681f3Smrg            assert not hasattr(regmap, 'chips')
757ec681f3Smrg            regmap.chips = [chips]
767ec681f3Smrg
777ec681f3Smrg    deduplicate_enums(regdb)
787ec681f3Smrg    deduplicate_register_types(regdb)
797ec681f3Smrg    deduce_missing_register_types(regdb)
807ec681f3Smrg    regdb.garbage_collect()
817ec681f3Smrg
827ec681f3Smrg    return regdb.encode_json_pretty()
837ec681f3Smrg
847ec681f3Smrg
857ec681f3Smrgdef main():
867ec681f3Smrg    print(json_canonicalize(open(sys.argv[1], 'r'), sys.argv[2]))
877ec681f3Smrg
887ec681f3Smrgif __name__ == '__main__':
897ec681f3Smrg    main()
907ec681f3Smrg
917ec681f3Smrg# kate: space-indent on; indent-width 4; replace-tabs on;
92