2
0

asm_offset.c 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2017-2018 The Khronos Group Inc.
  3. * Copyright (c) 2017-2018 Valve Corporation
  4. * Copyright (c) 2017-2018 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: Lenny Komow <[email protected]>
  19. */
  20. // This code generates an assembly file which provides offsets to get struct members from assembly code.
  21. #include <stdio.h>
  22. #include "loader.h"
  23. #if !defined(_MSC_VER) || (_MSC_VER >= 1900)
  24. #define SIZE_T_FMT "%-8zu"
  25. #else
  26. #define SIZE_T_FMT "%-8lu"
  27. #endif
  28. struct ValueInfo
  29. {
  30. const char *name;
  31. size_t value;
  32. const char *comment;
  33. };
  34. int main(int argc, char **argv) {
  35. const char *assembler = NULL;
  36. for (int i = 0; i < argc; ++i) {
  37. if (!strcmp(argv[i], "MASM")) {
  38. assembler = "MASM";
  39. } else if (!strcmp(argv[i], "GAS")) {
  40. assembler = "GAS";
  41. }
  42. }
  43. if (assembler == NULL) {
  44. return 1;
  45. }
  46. struct ValueInfo values[] = {
  47. { .name = "VK_DEBUG_REPORT_ERROR_BIT_EXT", .value = (size_t) VK_DEBUG_REPORT_ERROR_BIT_EXT,
  48. .comment = "The numerical value of the enum value 'VK_DEBUG_REPORT_ERROR_BIT_EXT'" },
  49. { .name = "PTR_SIZE", .value = sizeof(void*),
  50. .comment = "The size of a pointer" },
  51. { .name = "HASH_SIZE", .value = sizeof(struct loader_dispatch_hash_entry),
  52. .comment = "The size of a 'loader_dispatch_hash_entry' struct" },
  53. { .name = "HASH_OFFSET_INSTANCE", .value = offsetof(struct loader_instance, phys_dev_ext_disp_hash),
  54. .comment = "The offset of 'phys_dev_ext_disp_hash' within a 'loader_instance' struct" },
  55. { .name = "PHYS_DEV_OFFSET_INST_DISPATCH", .value = offsetof(struct loader_instance_dispatch_table, phys_dev_ext),
  56. .comment = "The offset of 'phys_dev_ext' within in 'loader_instance_dispatch_table' struct" },
  57. { .name = "PHYS_DEV_OFFSET_PHYS_DEV_TRAMP", .value = offsetof(struct loader_physical_device_tramp, phys_dev),
  58. .comment = "The offset of 'phys_dev' within a 'loader_physical_device_tramp' struct" },
  59. { .name = "ICD_TERM_OFFSET_PHYS_DEV_TERM", .value = offsetof(struct loader_physical_device_term, this_icd_term),
  60. .comment = "The offset of 'this_icd_term' within a 'loader_physical_device_term' struct" },
  61. { .name = "PHYS_DEV_OFFSET_PHYS_DEV_TERM", .value = offsetof(struct loader_physical_device_term, phys_dev),
  62. .comment = "The offset of 'phys_dev' within a 'loader_physical_device_term' struct" },
  63. { .name = "INSTANCE_OFFSET_ICD_TERM", .value = offsetof(struct loader_icd_term, this_instance),
  64. .comment = "The offset of 'this_instance' within a 'loader_icd_term' struct" },
  65. { .name = "DISPATCH_OFFSET_ICD_TERM", .value = offsetof(struct loader_icd_term, phys_dev_ext),
  66. .comment = "The offset of 'phys_dev_ext' within a 'loader_icd_term' struct" },
  67. { .name = "FUNC_NAME_OFFSET_HASH", .value = offsetof(struct loader_dispatch_hash_entry, func_name),
  68. .comment = "The offset of 'func_name' within a 'loader_dispatch_hash_entry' struct" },
  69. { .name = "EXT_OFFSET_DEVICE_DISPATCH", .value = offsetof(struct loader_dev_dispatch_table, ext_dispatch),
  70. .comment = "The offset of 'ext_dispatch' within a 'loader_dev_dispatch_table' struct" },
  71. };
  72. FILE *file = fopen("gen_defines.asm", "w");
  73. fprintf(file, "\n");
  74. if (!strcmp(assembler, "MASM")) {
  75. for (size_t i = 0; i < sizeof(values)/sizeof(values[0]); ++i) {
  76. fprintf(file, "%-32s equ " SIZE_T_FMT "; %s\n", values[i].name, values[i].value, values[i].comment);
  77. }
  78. } else if (!strcmp(assembler, "GAS")) {
  79. #ifdef __x86_64__
  80. fprintf(file, ".set X86_64, 1\n");
  81. #endif // __x86_64__
  82. for (size_t i = 0; i < sizeof(values)/sizeof(values[0]); ++i) {
  83. fprintf(file, ".set %-32s, " SIZE_T_FMT "# %s\n", values[i].name, values[i].value, values[i].comment);
  84. }
  85. }
  86. return fclose(file);
  87. }