15db71995Sopenharmony_ci#!/usr/bin/python3 -i 25db71995Sopenharmony_ci# 35db71995Sopenharmony_ci# Copyright (c) 2022 The Khronos Group Inc. 45db71995Sopenharmony_ci# Copyright (c) 2022 LunarG, Inc. 55db71995Sopenharmony_ci 65db71995Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License"); 75db71995Sopenharmony_ci# you may not use this file except in compliance with the License. 85db71995Sopenharmony_ci# You may obtain a copy of the License at 95db71995Sopenharmony_ci# 105db71995Sopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0 115db71995Sopenharmony_ci# 125db71995Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software 135db71995Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS, 145db71995Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 155db71995Sopenharmony_ci# See the License for the specific language governing permissions and 165db71995Sopenharmony_ci# limitations under the License. 175db71995Sopenharmony_ci# 185db71995Sopenharmony_ci# Author: Charles Giessen <charles@lunarg.com> 195db71995Sopenharmony_ci 205db71995Sopenharmony_ci# This script reads in the 'intermediate output' of a compiler to look for sizeof/offsetof information 215db71995Sopenharmony_ci# necessary for the assembler portions of the loader. This is achieved by forcing the compiler to output 225db71995Sopenharmony_ci# the intermediate assembly output and looking for specific patterns which contain the relevant information 235db71995Sopenharmony_ci 245db71995Sopenharmony_ciimport sys 255db71995Sopenharmony_ciimport os.path 265db71995Sopenharmony_cifrom os.path import exists 275db71995Sopenharmony_ciimport re 285db71995Sopenharmony_ci 295db71995Sopenharmony_ci 305db71995Sopenharmony_ci# Where to write the "gen_defines.asm" file 315db71995Sopenharmony_cidestination_file = sys.argv[1] 325db71995Sopenharmony_ci# The location the build system puts the intermediate asm file which depends on the compiler 335db71995Sopenharmony_cisource_asm_file = sys.argv[2] 345db71995Sopenharmony_ci# Whether we are using "MASM" or "GAS" for the assembler 355db71995Sopenharmony_ciassembler_type = sys.argv[3] 365db71995Sopenharmony_ci# Whether we are using gcc, clang, or msvc 375db71995Sopenharmony_cicompiler = sys.argv[4] 385db71995Sopenharmony_ci# taken from CMAKE_SYSTEM_PROCESSOR - x86_64, aarch64, or x86 395db71995Sopenharmony_ci# Only used with GAS - MASM doesn't need this, as it has its own way to determine x86 vs x64 405db71995Sopenharmony_ciarch = sys.argv[5] 415db71995Sopenharmony_ci 425db71995Sopenharmony_ciPOSIX_COMPILERS = ["GNU", "Clang", "AppleClang"] 435db71995Sopenharmony_ci 445db71995Sopenharmony_ciif destination_file is None or source_asm_file is None or assembler_type is None or compiler is None or arch is None: 455db71995Sopenharmony_ci print("Required command line arguments were not provided") 465db71995Sopenharmony_ci sys.exit(1) 475db71995Sopenharmony_ci 485db71995Sopenharmony_cidefines = ["VULKAN_LOADER_ERROR_BIT", 495db71995Sopenharmony_ci "PTR_SIZE", 505db71995Sopenharmony_ci "CHAR_PTR_SIZE", 515db71995Sopenharmony_ci "FUNCTION_OFFSET_INSTANCE", 525db71995Sopenharmony_ci "PHYS_DEV_OFFSET_INST_DISPATCH", 535db71995Sopenharmony_ci "PHYS_DEV_OFFSET_PHYS_DEV_TRAMP", 545db71995Sopenharmony_ci "ICD_TERM_OFFSET_PHYS_DEV_TERM", 555db71995Sopenharmony_ci "PHYS_DEV_OFFSET_PHYS_DEV_TERM", 565db71995Sopenharmony_ci "INSTANCE_OFFSET_ICD_TERM", 575db71995Sopenharmony_ci "DISPATCH_OFFSET_ICD_TERM", 585db71995Sopenharmony_ci "EXT_OFFSET_DEVICE_DISPATCH" ] 595db71995Sopenharmony_ci 605db71995Sopenharmony_citry: 615db71995Sopenharmony_ci with open(source_asm_file, 'r') as f: 625db71995Sopenharmony_ci asm_intermediate_file = f.read() 635db71995Sopenharmony_ciexcept IOError: 645db71995Sopenharmony_ci print("Could not open assembler file:", source_asm_file) 655db71995Sopenharmony_ci sys.exit(1) 665db71995Sopenharmony_ci 675db71995Sopenharmony_ciwith open(destination_file, "w", encoding="utf-8") as dest: 685db71995Sopenharmony_ci if assembler_type == "MASM": 695db71995Sopenharmony_ci # special case vulkan error bit due to it not appearing in the asm - its defined in the header as 8 so it shouldn't change 705db71995Sopenharmony_ci dest.write("VULKAN_LOADER_ERROR_BIT equ 8;\n") 715db71995Sopenharmony_ci elif assembler_type == "GAS": 725db71995Sopenharmony_ci # let the assembler know which platform to use 735db71995Sopenharmony_ci if arch == "x86_64": 745db71995Sopenharmony_ci dest.write(".set X86_64, 1\n") 755db71995Sopenharmony_ci elif arch == "aarch64" or arch == "arm64": 765db71995Sopenharmony_ci dest.write(".set AARCH_64, 1\n") 775db71995Sopenharmony_ci # Nothing to write in the x86 case 785db71995Sopenharmony_ci 795db71995Sopenharmony_ci for d in defines: 805db71995Sopenharmony_ci match = None 815db71995Sopenharmony_ci if compiler == "MSVC": 825db71995Sopenharmony_ci if d == "VULKAN_LOADER_ERROR_BIT": 835db71995Sopenharmony_ci continue # skip due to special case 845db71995Sopenharmony_ci match = re.search(d + " DD [ ]*([0-9a-f]+)H", asm_intermediate_file) 855db71995Sopenharmony_ci elif compiler in POSIX_COMPILERS: 865db71995Sopenharmony_ci match = re.search(d + " = ([0-9]+)", asm_intermediate_file) 875db71995Sopenharmony_ci 885db71995Sopenharmony_ci if match: 895db71995Sopenharmony_ci if compiler == "MSVC": 905db71995Sopenharmony_ci value = str(int(match.group(1), 16)) 915db71995Sopenharmony_ci elif compiler in POSIX_COMPILERS: 925db71995Sopenharmony_ci value = match.group(1) 935db71995Sopenharmony_ci if assembler_type == "MASM": 945db71995Sopenharmony_ci # MASM uses hex values, decode them here 955db71995Sopenharmony_ci dest.write(d + " equ " + value +";\n") 965db71995Sopenharmony_ci elif assembler_type == "GAS": 975db71995Sopenharmony_ci dest.write(".set " + d + ", " + value + "\n") 985db71995Sopenharmony_ci else: 995db71995Sopenharmony_ci print("Couldn't find ", d) 1005db71995Sopenharmony_ci sys.exit(1) 1015db71995Sopenharmony_ci 102