#!/usr/bin/env python3
#
# SPDX-License-Identifier: MIT
#
# Copyright © 2024 Intel Corporation

import argparse
import os
import sys
import textwrap

import codegen

h = None
c = None


def generate_register_configs(set):
    register_types = {
        'FLEX': 'flex_regs',
        'NOA': 'mux_regs',
        'OA': 'b_counter_regs',
    }

    c("void %s_%s_add_registers(struct intel_xe_perf *perf, struct intel_xe_perf_metric_set *metric_set)" %
      (set.gen.chipset, set.underscore_name))
    c("{")
    c.indent(4)

    # fill in register/values
    register_configs = set.findall('register_config')
    for register_config in register_configs:
        t = register_types[register_config.get('type')]

        availability = register_config.get('availability')
        if availability:
            set.gen.output_availability(set, availability, register_config.get('type') + ' register config')
            c.indent(4)

        c("{")
        c.indent(4)
        c("static const struct intel_xe_perf_register_prog _%s[] = {" % t)
        c.indent(4)
        for register in register_config.findall('register'):
            c("{ .reg = %s, .val = %s }," %
              (register.get('address'), register.get('value')))
        c.outdent(4)
        c("};")
        c("metric_set->%s = _%s;" % (t, t))
        c("metric_set->n_%s = sizeof(_%s) / sizeof(_%s[0]);" % (t, t, t))
        c.outdent(4)
        c("}")

        if availability:
            c.outdent(4)
            c("}")
        c("\n")

    c.outdent(4)
    c("}")


def main():
    global c
    global h
    global xml_equations

    parser = argparse.ArgumentParser()
    parser.add_argument("--header", help="Header file to write")
    parser.add_argument("--code", help="C file to write")
    parser.add_argument("--xml-file", help="Xml file to generate register configurations from")

    args = parser.parse_args()

    # Note: either arg may == None
    h = codegen.Codegen(args.header)
    c = codegen.Codegen(args.code)

    gen = codegen.Gen(args.xml_file, c)

    copyright = textwrap.dedent("""\
        /* Autogenerated file, DO NOT EDIT manually! generated by {} */
        // SPDX-License-Identifier: MIT
        /*
         * Copyright © 2024 Intel Corporation
         */

        """).format(os.path.basename(__file__))


    header_file = os.path.basename(args.header)
    header_define = "__%s__" % header_file.replace('.', '_').upper()

    h(copyright)
    h("#ifndef %s" % header_define)
    h("#define %s" % header_define)
    h("\n")
    h("struct intel_xe_perf;")
    h("struct intel_xe_perf_metric_set;")
    h("\n")
    for set in gen.sets:
        h("void %s_%s_add_registers(struct intel_xe_perf *perf, struct intel_xe_perf_metric_set *metric_set);" %
          (gen.chipset, set.underscore_name))
    h("\n")
    h("#endif /* %s */" % header_define)

    c(copyright)
    c("\n")
    c("#include \"%s\"" % header_file)
    c("#include \"xe/xe_oa.h\"")

    for set in gen.sets:
        c("\n")
        generate_register_configs(set)


if __name__ == '__main__':
    main()
