Home | History | Annotate | Download | only in loader
      1 /*
      2  * Copyright (c) 2017 The Khronos Group Inc.
      3  * Copyright (c) 2017 Valve Corporation
      4  * Copyright (c) 2017 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 <lenny (at) lunarg.com>
     19  */
     20 
     21 // This code generates an assembly file which provides offsets to get struct members from assembly code.
     22 
     23 #include <stdio.h>
     24 #include "loader.h"
     25 
     26 #if !defined(_MSC_VER) || (_MSC_VER >= 1900)
     27 #define SIZE_T_FMT "%-8zu"
     28 #else
     29 #define SIZE_T_FMT "%-8lu"
     30 #endif
     31 
     32 struct ValueInfo
     33 {
     34     const char *name;
     35     size_t value;
     36     const char *comment;
     37 };
     38 
     39 int main(int argc, char **argv) {
     40     const char *assembler = NULL;
     41     for (int i = 0; i < argc; ++i) {
     42         if (!strcmp(argv[i], "MASM")) {
     43             assembler = "MASM";
     44         } else if (!strcmp(argv[i], "GAS")) {
     45             assembler = "GAS";
     46         }
     47     }
     48     if (assembler == NULL) {
     49         return 1;
     50     }
     51 
     52     struct loader_instance instance;
     53     size_t hash_offset = ((size_t) &instance.phys_dev_ext_disp_hash) - ((size_t) &instance);
     54 
     55     struct loader_instance_dispatch_table disp;
     56     size_t phys_dev_offset_dispatch = ((size_t) &disp.phys_dev_ext) - ((size_t) &disp);
     57 
     58     struct loader_physical_device_tramp phys_dev_tramp;
     59     size_t phys_dev_offset_tramp = ((size_t) &phys_dev_tramp.phys_dev) - ((size_t) &phys_dev_tramp);
     60 
     61     struct loader_physical_device_term phys_dev_term;
     62     size_t icd_term_offset = ((size_t) &phys_dev_term.this_icd_term) - ((size_t) &phys_dev_term);
     63     size_t phys_dev_offset_term = ((size_t) &phys_dev_term.phys_dev) - ((size_t) &phys_dev_term);
     64 
     65     struct loader_icd_term icd_term;
     66     size_t instance_offset = ((size_t) &icd_term.this_instance) - ((size_t) &icd_term);
     67     size_t dispatch_offset = ((size_t) &icd_term.phys_dev_ext) - ((size_t) &icd_term);
     68 
     69     struct loader_dispatch_hash_entry hash;
     70     size_t func_name_offset = ((size_t) &hash.func_name) - ((size_t) &hash);
     71 
     72     struct loader_dev_dispatch_table dev_disp;
     73     size_t dev_disp_offset = ((size_t) &dev_disp.ext_dispatch) - ((size_t) &dev_disp);
     74 
     75     struct ValueInfo values[] = {
     76         { .name = "VK_DEBUG_REPORT_ERROR_BIT_EXT", .value = (size_t) VK_DEBUG_REPORT_ERROR_BIT_EXT,
     77             .comment = "The numerical value of the enum value 'VK_DEBUG_REPORT_ERROR_BIT_EXT'" },
     78         { .name = "PTR_SIZE", .value = sizeof(void*),
     79             .comment = "The size of a pointer" },
     80         { .name = "HASH_SIZE", .value = sizeof(struct loader_dispatch_hash_entry),
     81             .comment = "The size of a 'loader_dispatch_hash_entry' struct" },
     82         { .name = "HASH_OFFSET_INSTANCE", .value = hash_offset,
     83             .comment = "The offset of 'phys_dev_ext_disp_hash' within a 'loader_instance' struct" },
     84         { .name = "PHYS_DEV_OFFSET_INST_DISPATCH", .value = phys_dev_offset_dispatch,
     85             .comment = "The offset of 'phys_dev_ext' within in 'loader_instance_dispatch_table' struct" },
     86         { .name = "PHYS_DEV_OFFSET_PHYS_DEV_TRAMP", .value = phys_dev_offset_tramp,
     87             .comment = "The offset of 'phys_dev' within a 'loader_physical_device_tramp' struct" },
     88         { .name = "ICD_TERM_OFFSET_PHYS_DEV_TERM", .value = icd_term_offset,
     89             .comment = "The offset of 'this_icd_term' within a 'loader_physical_device_term' struct" },
     90         { .name = "PHYS_DEV_OFFSET_PHYS_DEV_TERM", .value = phys_dev_offset_term,
     91             .comment = "The offset of 'phys_dev' within a 'loader_physical_device_term' struct" },
     92         { .name = "INSTANCE_OFFSET_ICD_TERM", .value = instance_offset,
     93             .comment = "The offset of 'this_instance' within a 'loader_icd_term' struct" },
     94         { .name = "DISPATCH_OFFSET_ICD_TERM", .value = dispatch_offset,
     95             .comment = "The offset of 'phys_dev_ext' within a 'loader_icd_term' struct" },
     96         { .name = "FUNC_NAME_OFFSET_HASH", .value = func_name_offset,
     97             .comment = "The offset of 'func_name' within a 'loader_dispatch_hash_entry' struct" },
     98         { .name = "EXT_OFFSET_DEVICE_DISPATCH", .value = dev_disp_offset,
     99             .comment = "The offset of 'ext_dispatch' within a 'loader_dev_dispatch_table' struct" },
    100     };
    101 
    102     FILE *file = fopen("gen_defines.asm", "w");
    103     fprintf(file, "\n");
    104     if (!strcmp(assembler, "MASM")) {
    105         for (size_t i = 0; i < sizeof(values)/sizeof(values[0]); ++i) {
    106             fprintf(file, "%-32s equ " SIZE_T_FMT "; %s\n", values[i].name, values[i].value, values[i].comment);
    107         }
    108     } else if (!strcmp(assembler, "GAS")) {
    109 #ifdef __x86_64__
    110         fprintf(file, ".set X86_64, 1\n");
    111 #endif // __x86_64__
    112         for (size_t i = 0; i < sizeof(values)/sizeof(values[0]); ++i) {
    113             fprintf(file, ".set %-32s, " SIZE_T_FMT "# %s\n", values[i].name, values[i].value, values[i].comment);
    114         }
    115     }
    116     return fclose(file);
    117 }
    118