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// __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__) 36void 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 53static const uint32_t PTR_SIZE = sizeof(void *); 54static const uint32_t CHAR_PTR_SIZE = sizeof(char *); 55static const uint32_t FUNCTION_OFFSET_INSTANCE = offsetof(struct loader_instance, phys_dev_ext_disp_functions); 56static const uint32_t PHYS_DEV_OFFSET_INST_DISPATCH = offsetof(struct loader_instance_dispatch_table, phys_dev_ext); 57static const uint32_t PHYS_DEV_OFFSET_PHYS_DEV_TRAMP = offsetof(struct loader_physical_device_tramp, phys_dev); 58static const uint32_t ICD_TERM_OFFSET_PHYS_DEV_TERM = offsetof(struct loader_physical_device_term, this_icd_term); 59static const uint32_t PHYS_DEV_OFFSET_PHYS_DEV_TERM = offsetof(struct loader_physical_device_term, phys_dev); 60static const uint32_t INSTANCE_OFFSET_ICD_TERM = offsetof(struct loader_icd_term, this_instance); 61static const uint32_t DISPATCH_OFFSET_ICD_TERM = offsetof(struct loader_icd_term, phys_dev_ext); 62static 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 75struct ValueInfo { 76 const char *name; 77 size_t value; 78 const char *comment; 79}; 80 81// This file can both be executed to produce gen_defines.asm and contains all the relevant data which 82// the parse_asm_values.py script needs to write gen_defines.asm, necessary for cross compilation 83int main(int argc, char **argv) { 84 const char *assembler = NULL; 85 for (int i = 0; i < argc; ++i) { 86 if (!strcmp(argv[i], "MASM")) { 87 assembler = "MASM"; 88 } else if (!strcmp(argv[i], "GAS")) { 89 assembler = "GAS"; 90 } 91 } 92 if (assembler == NULL) { 93 return 1; 94 } 95 96 struct ValueInfo values[] = { 97 // clang-format off 98 { .name = "VULKAN_LOADER_ERROR_BIT", .value = (size_t) VULKAN_LOADER_ERROR_BIT, 99 .comment = "The numerical value of the enum value 'VULKAN_LOADER_ERROR_BIT'" }, 100 { .name = "PTR_SIZE", .value = sizeof(void*), 101 .comment = "The size of a pointer" }, 102 { .name = "CHAR_PTR_SIZE", .value = sizeof(char *), 103 .comment = "The size of a 'const char *' struct" }, 104 { .name = "FUNCTION_OFFSET_INSTANCE", .value = offsetof(struct loader_instance, phys_dev_ext_disp_functions), 105 .comment = "The offset of 'phys_dev_ext_disp_functions' within a 'loader_instance' struct" }, 106 { .name = "PHYS_DEV_OFFSET_INST_DISPATCH", .value = offsetof(struct loader_instance_dispatch_table, phys_dev_ext), 107 .comment = "The offset of 'phys_dev_ext' within in 'loader_instance_dispatch_table' struct" }, 108 { .name = "PHYS_DEV_OFFSET_PHYS_DEV_TRAMP", .value = offsetof(struct loader_physical_device_tramp, phys_dev), 109 .comment = "The offset of 'phys_dev' within a 'loader_physical_device_tramp' struct" }, 110 { .name = "ICD_TERM_OFFSET_PHYS_DEV_TERM", .value = offsetof(struct loader_physical_device_term, this_icd_term), 111 .comment = "The offset of 'this_icd_term' within a 'loader_physical_device_term' struct" }, 112 { .name = "PHYS_DEV_OFFSET_PHYS_DEV_TERM", .value = offsetof(struct loader_physical_device_term, phys_dev), 113 .comment = "The offset of 'phys_dev' within a 'loader_physical_device_term' struct" }, 114 { .name = "INSTANCE_OFFSET_ICD_TERM", .value = offsetof(struct loader_icd_term, this_instance), 115 .comment = "The offset of 'this_instance' within a 'loader_icd_term' struct" }, 116 { .name = "DISPATCH_OFFSET_ICD_TERM", .value = offsetof(struct loader_icd_term, phys_dev_ext), 117 .comment = "The offset of 'phys_dev_ext' within a 'loader_icd_term' struct" }, 118 { .name = "EXT_OFFSET_DEVICE_DISPATCH", .value = offsetof(struct loader_dev_dispatch_table, ext_dispatch), 119 .comment = "The offset of 'ext_dispatch' within a 'loader_dev_dispatch_table' struct" }, 120 // clang-format on 121 }; 122 123 FILE *file = loader_fopen("gen_defines.asm", "w"); 124 fprintf(file, "\n"); 125 if (!strcmp(assembler, "MASM")) { 126 for (size_t i = 0; i < sizeof(values) / sizeof(values[0]); ++i) { 127 fprintf(file, "%-32s equ " SIZE_T_FMT "; %s\n", values[i].name, values[i].value, values[i].comment); 128 } 129 } else if (!strcmp(assembler, "GAS")) { 130#if defined(__x86_64__) || defined(__i386__) 131 const char *comment_delimiter = "#"; 132#if defined(__x86_64__) 133 fprintf(file, ".set X86_64, 1\n"); 134#endif // defined(__x86_64__) 135#elif defined(__aarch64__) 136 const char *comment_delimiter = "//"; 137 fprintf(file, ".set AARCH_64, 1\n"); 138#else 139 // Default comment delimiter 140 const char *comment_delimiter = "#"; 141#endif 142 for (size_t i = 0; i < sizeof(values) / sizeof(values[0]); ++i) { 143 fprintf(file, ".set %-32s, " SIZE_T_FMT "%s %s\n", values[i].name, values[i].value, comment_delimiter, 144 values[i].comment); 145 } 146 } 147 return fclose(file); 148} 149