#!/usr/bin/python
#
# Copyright (C) 2025 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Generate device_insn_info files out of the definition file.

See the full description of format in gen_device_insn_info_lib.py

"""

import gen_device_insn_info_lib
import sys


INDENT = '  '
AUTOGEN = """\
// This file automatically generated by gen_device_insn_info.py
// DO NOT EDIT!
"""


def main(argv):
  # Usage:
  #   gen_device_insn_info.py <device_insn_info-inl.h>
  #                           ...
  #                           <def>
  #                           ...
  #        Note: there should be equal number of inputs and outputes,
  #              one file on input is translated to one file in output.

  assert len(argv) % 2 == 1
  filenames = argv[1:]
  filename_pairs = ((filenames[i], filenames[len(filenames)//2 + i])
                    for i in range(0, len(filenames)//2))

  assemblers = 0
  for out_filename, input_filename in filename_pairs:
    with open(out_filename, 'w') as out_file:
      print(AUTOGEN, file=out_file)
      insns = gen_device_insn_info_lib._load_lir_def(input_filename, assemblers)
      gen_device_insn_info_lib._gen_device_insn_info(out_file, insns)
    assemblers += 1

  return 0


if __name__ == '__main__':
  sys.exit(main(sys.argv))
