1 /*
2  * Copyright (c) 2017-2021 The Khronos Group Inc.
3  * Copyright (c) 2017-2021 Valve Corporation
4  * Copyright (c) 2017-2021 LunarG, Inc.
5  * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Author: Lenny Komow <lenny@lunarg.com>
20  * Author: Charles Giessen <charles@lunarg.com>
21  */
22 
23 // This code generates an assembly file which provides offsets to get struct members from assembly code.
24 
25 #include <stdio.h>
26 #include "loader.h"
27 #include "log.h"
28 
29 #if !defined(_MSC_VER) || (_MSC_VER >= 1900)
30 #define SIZE_T_FMT "%-8zu"
31 #else
32 #define SIZE_T_FMT "%-8lu"
33 #endif
34 
35 struct ValueInfo {
36     const char *name;
37     size_t value;
38     const char *comment;
39 };
40 
main(int argc,char ** argv)41 int main(int argc, char **argv) {
42     const char *assembler = NULL;
43     for (int i = 0; i < argc; ++i) {
44         if (!strcmp(argv[i], "MASM")) {
45             assembler = "MASM";
46         } else if (!strcmp(argv[i], "GAS")) {
47             assembler = "GAS";
48         }
49     }
50     if (assembler == NULL) {
51         return 1;
52     }
53 
54     struct ValueInfo values[] = {
55         // clang-format off
56         { .name = "VK_DEBUG_REPORT_ERROR_BIT_EXT", .value = (size_t) VK_DEBUG_REPORT_ERROR_BIT_EXT,
57             .comment = "The numerical value of the enum value 'VK_DEBUG_REPORT_ERROR_BIT_EXT'" },
58         { .name = "VULKAN_LOADER_ERROR_BIT", .value = (size_t) VULKAN_LOADER_ERROR_BIT,
59             .comment = "The numerical value of the enum value 'VULKAN_LOADER_ERROR_BIT'" },
60         { .name = "PTR_SIZE", .value = sizeof(void*),
61             .comment = "The size of a pointer" },
62         { .name = "HASH_SIZE", .value = sizeof(struct loader_dispatch_hash_entry),
63             .comment = "The size of a 'loader_dispatch_hash_entry' struct" },
64         { .name = "HASH_OFFSET_INSTANCE", .value = offsetof(struct loader_instance, phys_dev_ext_disp_hash),
65             .comment = "The offset of 'phys_dev_ext_disp_hash' within a 'loader_instance' struct" },
66         { .name = "PHYS_DEV_OFFSET_INST_DISPATCH", .value = offsetof(struct loader_instance_dispatch_table, phys_dev_ext),
67             .comment = "The offset of 'phys_dev_ext' within in 'loader_instance_dispatch_table' struct" },
68         { .name = "PHYS_DEV_OFFSET_PHYS_DEV_TRAMP", .value = offsetof(struct loader_physical_device_tramp, phys_dev),
69             .comment = "The offset of 'phys_dev' within a 'loader_physical_device_tramp' struct" },
70         { .name = "ICD_TERM_OFFSET_PHYS_DEV_TERM", .value = offsetof(struct loader_physical_device_term, this_icd_term),
71             .comment = "The offset of 'this_icd_term' within a 'loader_physical_device_term' struct" },
72         { .name = "PHYS_DEV_OFFSET_PHYS_DEV_TERM", .value = offsetof(struct loader_physical_device_term, phys_dev),
73             .comment = "The offset of 'phys_dev' within a 'loader_physical_device_term' struct" },
74         { .name = "INSTANCE_OFFSET_ICD_TERM", .value = offsetof(struct loader_icd_term, this_instance),
75             .comment = "The offset of 'this_instance' within a 'loader_icd_term' struct" },
76         { .name = "DISPATCH_OFFSET_ICD_TERM", .value = offsetof(struct loader_icd_term, phys_dev_ext),
77             .comment = "The offset of 'phys_dev_ext' within a 'loader_icd_term' struct" },
78         { .name = "FUNC_NAME_OFFSET_HASH", .value = offsetof(struct loader_dispatch_hash_entry, func_name),
79             .comment = "The offset of 'func_name' within a 'loader_dispatch_hash_entry' struct" },
80         { .name = "EXT_OFFSET_DEVICE_DISPATCH", .value = offsetof(struct loader_dev_dispatch_table, ext_dispatch),
81             .comment = "The offset of 'ext_dispatch' within a 'loader_dev_dispatch_table' struct" },
82         // clang-format on
83     };
84 
85     FILE *file = fopen("gen_defines.asm", "w");
86     fprintf(file, "\n");
87     if (!strcmp(assembler, "MASM")) {
88         for (size_t i = 0; i < sizeof(values) / sizeof(values[0]); ++i) {
89             fprintf(file, "%-32s equ " SIZE_T_FMT "; %s\n", values[i].name, values[i].value, values[i].comment);
90         }
91     } else if (!strcmp(assembler, "GAS")) {
92 #if defined(__x86_64__) || defined(__i386__)
93         const char* comment_delimiter = "#";
94 #if defined(__x86_64__)
95         fprintf(file, ".set X86_64, 1\n");
96 #endif // defined(__x86_64__)
97 #elif defined(__aarch64__)
98         const char* comment_delimiter = "//";
99         fprintf(file, ".set AARCH_64, 1\n");
100 #else
101         // Default comment delimiter
102         const char* comment_delimiter = "#";
103 #endif
104         for (size_t i = 0; i < sizeof(values) / sizeof(values[0]); ++i) {
105             fprintf(file, ".set %-32s, " SIZE_T_FMT "%s %s\n", values[i].name, values[i].value, comment_delimiter, values[i].comment);
106         }
107     }
108     return fclose(file);
109 }
110