1 /*
2 * Copyright (c) 2017-2024 The Khronos Group Inc.
3 * Copyright (c) 2017-2024 Valve Corporation
4 * Copyright (c) 2017-2024 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 // __USE_MINGW_ANSI_STDIO is needed to use the %zu format specifier with mingw-w64.
26 // Otherwise the compiler will complain about an unknown format specifier.
27 #if defined(__MINGW32__) && !defined(__USE_MINGW_ANSI_STDIO)
28 #define __USE_MINGW_ANSI_STDIO 1
29 #endif
30
31 #include <stdio.h>
32 #include "loader_common.h"
33 #include "log.h"
34
35 #if defined(__GNUC__) || defined(__clang__)
produce_asm_define()36 void produce_asm_define() {
37 // GCC and clang make it easy to print easy to regex for values
38 __asm__("# VULKAN_LOADER_ERROR_BIT = %c0" : : "i"(VULKAN_LOADER_ERROR_BIT));
39 __asm__("# PTR_SIZE = %c0" : : "i"(sizeof(void *)));
40 __asm__("# CHAR_PTR_SIZE = %c0" : : "i"(sizeof(char *)));
41 __asm__("# FUNCTION_OFFSET_INSTANCE = %c0" : : "i"(offsetof(struct loader_instance, phys_dev_ext_disp_functions)));
42 __asm__("# PHYS_DEV_OFFSET_INST_DISPATCH = %c0" : : "i"(offsetof(struct loader_instance_dispatch_table, phys_dev_ext)));
43 __asm__("# PHYS_DEV_OFFSET_PHYS_DEV_TRAMP = %c0" : : "i"(offsetof(struct loader_physical_device_tramp, phys_dev)));
44 __asm__("# ICD_TERM_OFFSET_PHYS_DEV_TERM = %c0" : : "i"(offsetof(struct loader_physical_device_term, this_icd_term)));
45 __asm__("# PHYS_DEV_OFFSET_PHYS_DEV_TERM = %c0" : : "i"(offsetof(struct loader_physical_device_term, phys_dev)));
46 __asm__("# INSTANCE_OFFSET_ICD_TERM = %c0" : : "i"(offsetof(struct loader_icd_term, this_instance)));
47 __asm__("# DISPATCH_OFFSET_ICD_TERM = %c0" : : "i"(offsetof(struct loader_icd_term, phys_dev_ext)));
48 __asm__("# EXT_OFFSET_DEVICE_DISPATCH = %c0" : : "i"(offsetof(struct loader_dev_dispatch_table, ext_dispatch)));
49 }
50 #elif defined(_WIN32)
51 // MSVC will print the name of the value and the value in hex
52 // Must disable optimization for this translation unit, otherwise the compiler strips out the variables
53 static const uint32_t PTR_SIZE = sizeof(void *);
54 static const uint32_t CHAR_PTR_SIZE = sizeof(char *);
55 static const uint32_t FUNCTION_OFFSET_INSTANCE = offsetof(struct loader_instance, phys_dev_ext_disp_functions);
56 static const uint32_t PHYS_DEV_OFFSET_INST_DISPATCH = offsetof(struct loader_instance_dispatch_table, phys_dev_ext);
57 static const uint32_t PHYS_DEV_OFFSET_PHYS_DEV_TRAMP = offsetof(struct loader_physical_device_tramp, phys_dev);
58 static const uint32_t ICD_TERM_OFFSET_PHYS_DEV_TERM = offsetof(struct loader_physical_device_term, this_icd_term);
59 static const uint32_t PHYS_DEV_OFFSET_PHYS_DEV_TERM = offsetof(struct loader_physical_device_term, phys_dev);
60 static const uint32_t INSTANCE_OFFSET_ICD_TERM = offsetof(struct loader_icd_term, this_instance);
61 static const uint32_t DISPATCH_OFFSET_ICD_TERM = offsetof(struct loader_icd_term, phys_dev_ext);
62 static const uint32_t EXT_OFFSET_DEVICE_DISPATCH = offsetof(struct loader_dev_dispatch_table, ext_dispatch);
63 #else
64 #warning asm_offset.c variable declarations need to be defined for this platform
65 #endif
66
67 #if !defined(_MSC_VER) || (_MSC_VER >= 1900)
68 #define SIZE_T_FMT "%-8zu"
69 #elif defined(__GNUC__) || defined(__clang__)
70 #define SIZE_T_FMT "%-8lu"
71 #else
72 #warning asm_offset.c SIZE_T_FMT must be defined for this platform
73 #endif
74
75 struct ValueInfo {
76 const char *name;
77 size_t value;
78 const char *comment;
79 };
80
81 enum Assembler {
82 UNKNOWN = 0,
83 MASM = 1,
84 MARMASM = 2,
85 GAS = 3,
86 };
87
88 // This file can both be executed to produce gen_defines.asm and contains all the relevant data which
89 // the parse_asm_values.py script needs to write gen_defines.asm, necessary for cross compilation
main(int argc,char ** argv)90 int main(int argc, char **argv) {
91 enum Assembler assembler = UNKNOWN;
92 for (int i = 0; i < argc; ++i) {
93 if (!strcmp(argv[i], "MASM")) {
94 assembler = MASM;
95 } else if (!strcmp(argv[i], "MARMASM")) {
96 assembler = MARMASM;
97 } else if (!strcmp(argv[i], "GAS")) {
98 assembler = GAS;
99 }
100 }
101 if (assembler == UNKNOWN) {
102 return -1;
103 }
104
105 struct ValueInfo values[] = {
106 // clang-format off
107 { .name = "VULKAN_LOADER_ERROR_BIT", .value = (size_t) VULKAN_LOADER_ERROR_BIT,
108 .comment = "The numerical value of the enum value 'VULKAN_LOADER_ERROR_BIT'" },
109 { .name = "PTR_SIZE", .value = sizeof(void*),
110 .comment = "The size of a pointer" },
111 { .name = "CHAR_PTR_SIZE", .value = sizeof(char *),
112 .comment = "The size of a 'const char *' struct" },
113 { .name = "FUNCTION_OFFSET_INSTANCE", .value = offsetof(struct loader_instance, phys_dev_ext_disp_functions),
114 .comment = "The offset of 'phys_dev_ext_disp_functions' within a 'loader_instance' struct" },
115 { .name = "PHYS_DEV_OFFSET_INST_DISPATCH", .value = offsetof(struct loader_instance_dispatch_table, phys_dev_ext),
116 .comment = "The offset of 'phys_dev_ext' within in 'loader_instance_dispatch_table' struct" },
117 { .name = "PHYS_DEV_OFFSET_PHYS_DEV_TRAMP", .value = offsetof(struct loader_physical_device_tramp, phys_dev),
118 .comment = "The offset of 'phys_dev' within a 'loader_physical_device_tramp' struct" },
119 { .name = "ICD_TERM_OFFSET_PHYS_DEV_TERM", .value = offsetof(struct loader_physical_device_term, this_icd_term),
120 .comment = "The offset of 'this_icd_term' within a 'loader_physical_device_term' struct" },
121 { .name = "PHYS_DEV_OFFSET_PHYS_DEV_TERM", .value = offsetof(struct loader_physical_device_term, phys_dev),
122 .comment = "The offset of 'phys_dev' within a 'loader_physical_device_term' struct" },
123 { .name = "INSTANCE_OFFSET_ICD_TERM", .value = offsetof(struct loader_icd_term, this_instance),
124 .comment = "The offset of 'this_instance' within a 'loader_icd_term' struct" },
125 { .name = "DISPATCH_OFFSET_ICD_TERM", .value = offsetof(struct loader_icd_term, phys_dev_ext),
126 .comment = "The offset of 'phys_dev_ext' within a 'loader_icd_term' struct" },
127 { .name = "EXT_OFFSET_DEVICE_DISPATCH", .value = offsetof(struct loader_dev_dispatch_table, ext_dispatch),
128 .comment = "The offset of 'ext_dispatch' within a 'loader_dev_dispatch_table' struct" },
129 // clang-format on
130 };
131
132 FILE *file = loader_fopen("gen_defines.asm", "w");
133 fprintf(file, "\n");
134 if (assembler == MASM) {
135 for (size_t i = 0; i < sizeof(values) / sizeof(values[0]); ++i) {
136 fprintf(file, "%-32s equ " SIZE_T_FMT "; %s\n", values[i].name, values[i].value, values[i].comment);
137 }
138 } else if (assembler == MARMASM) {
139 fprintf(file, " AREA loader_structs_details, DATA,READONLY\n");
140 #if defined(__aarch64__) || defined(_M_ARM64)
141 fprintf(file, "AARCH_64 EQU 1\n");
142 #else
143 fprintf(file, "AARCH_64 EQU 0\n");
144 #endif
145 for (size_t i = 0; i < sizeof(values) / sizeof(values[0]); ++i) {
146 fprintf(file, "%-32s EQU " SIZE_T_FMT "; %s\n", values[i].name, values[i].value, values[i].comment);
147 }
148 fprintf(file, " END\n");
149 } else if (assembler == GAS) {
150 #if defined(__x86_64__) || defined(__i386__)
151 const char *comment_delimiter = "#";
152 #if defined(__x86_64__)
153 fprintf(file, ".set X86_64, 1\n");
154 #endif // defined(__x86_64__)
155 #elif defined(__aarch64__) || defined(__arm__)
156 const char *comment_delimiter = "//";
157 #if defined(__aarch64__)
158 fprintf(file, ".set AARCH_64, 1\n");
159 #else
160 fprintf(file, ".set AARCH_64, 0\n");
161 #endif
162 #else
163 // Default comment delimiter
164 const char *comment_delimiter = "#";
165 #endif
166 for (size_t i = 0; i < sizeof(values) / sizeof(values[0]); ++i) {
167 fprintf(file, ".set %-32s, " SIZE_T_FMT "%s %s\n", values[i].name, values[i].value, comment_delimiter,
168 values[i].comment);
169 }
170 }
171 return fclose(file);
172 }
173