1#!/usr/bin/python3 -i
2#
3# Copyright (c) 2022 The Khronos Group Inc.
4# Copyright (c) 2022 LunarG, Inc.
5
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18# Author: Charles Giessen <charles@lunarg.com>
19
20# This script reads in the 'intermediate output' of a compiler to look for sizeof/offsetof information
21# necessary for the assembler portions of the loader. This is achieved by forcing the compiler to output
22# the intermediate assembly output and looking for specific patterns which contain the relevant information
23
24import sys
25import os.path
26from os.path import exists
27import re
28
29
30# Where to write the "gen_defines.asm" file
31destination_file = sys.argv[1]
32# The location the build system puts the intermediate asm file which depends on the compiler
33source_asm_file = sys.argv[2]
34# Whether we are using "MASM" or "GAS" for the assembler
35assembler_type = sys.argv[3]
36# Whether we are using gcc, clang, or msvc
37compiler = sys.argv[4]
38# taken from CMAKE_SYSTEM_PROCESSOR - x86_64, aarch64, or x86
39# Only used with GAS - MASM doesn't need this, as it has its own way to determine x86 vs x64
40arch = sys.argv[5]
41
42POSIX_COMPILERS = ["GNU", "Clang", "AppleClang"]
43
44if destination_file is None or source_asm_file is None or assembler_type is None or compiler is None or arch is None:
45    print("Required command line arguments were not provided")
46    sys.exit(1)
47
48defines = ["VULKAN_LOADER_ERROR_BIT",
49            "PTR_SIZE",
50            "CHAR_PTR_SIZE",
51            "FUNCTION_OFFSET_INSTANCE",
52            "PHYS_DEV_OFFSET_INST_DISPATCH",
53            "PHYS_DEV_OFFSET_PHYS_DEV_TRAMP",
54            "ICD_TERM_OFFSET_PHYS_DEV_TERM",
55            "PHYS_DEV_OFFSET_PHYS_DEV_TERM",
56            "INSTANCE_OFFSET_ICD_TERM",
57            "DISPATCH_OFFSET_ICD_TERM",
58            "EXT_OFFSET_DEVICE_DISPATCH" ]
59
60try:
61    with open(source_asm_file, 'r') as f:
62        asm_intermediate_file = f.read()
63except IOError:
64    print("Could not open assembler file:", source_asm_file)
65    sys.exit(1)
66
67with open(destination_file, "w", encoding="utf-8") as dest:
68    if assembler_type == "MASM":
69        # 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
70        dest.write("VULKAN_LOADER_ERROR_BIT equ 8;\n")
71    elif assembler_type == "GAS":
72        # let the assembler know which platform to use
73        if arch == "x86_64":
74            dest.write(".set X86_64, 1\n")
75        elif arch == "aarch64" or arch == "arm64":
76            dest.write(".set AARCH_64, 1\n")
77        # Nothing to write in the x86 case
78
79    for d in defines:
80        match = None
81        if compiler == "MSVC":
82            if d == "VULKAN_LOADER_ERROR_BIT":
83                continue # skip due to special case
84            match = re.search(d + " DD [ ]*([0-9a-f]+)H", asm_intermediate_file)
85        elif compiler in POSIX_COMPILERS:
86            match = re.search(d + " = ([0-9]+)", asm_intermediate_file)
87
88        if match:
89            if compiler == "MSVC":
90                value = str(int(match.group(1), 16))
91            elif compiler in POSIX_COMPILERS:
92                value = match.group(1)
93            if assembler_type == "MASM":
94            # MASM uses hex values, decode them here
95                dest.write(d + " equ " + value +";\n")
96            elif assembler_type == "GAS":
97                dest.write(".set " + d + ", " + value + "\n")
98        else:
99            print("Couldn't find ", d)
100            sys.exit(1)
101
102