• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_common.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 = "VULKAN_LOADER_ERROR_BIT", .value = (size_t) VULKAN_LOADER_ERROR_BIT,
57             .comment = "The numerical value of the enum value 'VULKAN_LOADER_ERROR_BIT'" },
58         { .name = "PTR_SIZE", .value = sizeof(void*),
59             .comment = "The size of a pointer" },
60         { .name = "CHAR_PTR_SIZE", .value = sizeof(char *),
61             .comment = "The size of a 'const char *' struct" },
62         { .name = "FUNCTION_OFFSET_INSTANCE", .value = offsetof(struct loader_instance, phys_dev_ext_disp_functions),
63             .comment = "The offset of 'phys_dev_ext_disp_functions' within a 'loader_instance' struct" },
64         { .name = "PHYS_DEV_OFFSET_INST_DISPATCH", .value = offsetof(struct loader_instance_dispatch_table, phys_dev_ext),
65             .comment = "The offset of 'phys_dev_ext' within in 'loader_instance_dispatch_table' struct" },
66         { .name = "PHYS_DEV_OFFSET_PHYS_DEV_TRAMP", .value = offsetof(struct loader_physical_device_tramp, phys_dev),
67             .comment = "The offset of 'phys_dev' within a 'loader_physical_device_tramp' struct" },
68         { .name = "ICD_TERM_OFFSET_PHYS_DEV_TERM", .value = offsetof(struct loader_physical_device_term, this_icd_term),
69             .comment = "The offset of 'this_icd_term' within a 'loader_physical_device_term' struct" },
70         { .name = "PHYS_DEV_OFFSET_PHYS_DEV_TERM", .value = offsetof(struct loader_physical_device_term, phys_dev),
71             .comment = "The offset of 'phys_dev' within a 'loader_physical_device_term' struct" },
72         { .name = "INSTANCE_OFFSET_ICD_TERM", .value = offsetof(struct loader_icd_term, this_instance),
73             .comment = "The offset of 'this_instance' within a 'loader_icd_term' struct" },
74         { .name = "DISPATCH_OFFSET_ICD_TERM", .value = offsetof(struct loader_icd_term, phys_dev_ext),
75             .comment = "The offset of 'phys_dev_ext' within a 'loader_icd_term' struct" },
76         { .name = "EXT_OFFSET_DEVICE_DISPATCH", .value = offsetof(struct loader_dev_dispatch_table, ext_dispatch),
77             .comment = "The offset of 'ext_dispatch' within a 'loader_dev_dispatch_table' struct" },
78         // clang-format on
79     };
80 
81     FILE *file = fopen("gen_defines.asm", "w");
82     fprintf(file, "\n");
83     if (!strcmp(assembler, "MASM")) {
84         for (size_t i = 0; i < sizeof(values) / sizeof(values[0]); ++i) {
85             fprintf(file, "%-32s equ " SIZE_T_FMT "; %s\n", values[i].name, values[i].value, values[i].comment);
86         }
87     } else if (!strcmp(assembler, "GAS")) {
88 #if defined(__x86_64__) || defined(__i386__)
89         const char *comment_delimiter = "#";
90 #if defined(__x86_64__)
91         fprintf(file, ".set X86_64, 1\n");
92 #endif  // defined(__x86_64__)
93 #elif defined(__aarch64__)
94         const char *comment_delimiter = "//";
95         fprintf(file, ".set AARCH_64, 1\n");
96 #else
97         // Default comment delimiter
98         const char *comment_delimiter = "#";
99 #endif
100         for (size_t i = 0; i < sizeof(values) / sizeof(values[0]); ++i) {
101             fprintf(file, ".set %-32s, " SIZE_T_FMT "%s %s\n", values[i].name, values[i].value, comment_delimiter,
102                     values[i].comment);
103         }
104     }
105     return fclose(file);
106 }
107