Jim Cownie | 33f7b24 | 2014-04-09 15:40:23 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.txt for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | |
| 11 | #if HOST_LIBRARY |
| 12 | #include "offload_host.h" |
| 13 | #include "offload_myo_host.h" |
| 14 | #else |
| 15 | #include "compiler_if_target.h" |
| 16 | #include "offload_target.h" |
| 17 | #include "offload_myo_target.h" |
| 18 | #endif |
| 19 | |
| 20 | #ifdef TARGET_WINNT |
| 21 | #define ALLOCATE(name) __declspec(allocate(name)) |
| 22 | #define DLL_LOCAL |
| 23 | #else // TARGET_WINNT |
| 24 | #define ALLOCATE(name) __attribute__((section(name))) |
| 25 | #define DLL_LOCAL __attribute__((visibility("hidden"))) |
| 26 | #endif // TARGET_WINNT |
| 27 | |
| 28 | #if HOST_LIBRARY |
| 29 | // the host program/shared library should always have __offload_target_image |
| 30 | // symbol defined. This symbol specifies the beginning of the target program |
| 31 | // image. |
| 32 | extern "C" DLL_LOCAL const void* __offload_target_image; |
| 33 | #else // HOST_LIBRARY |
| 34 | // Define a weak main which would be used on target side in case usere's |
| 35 | // source file containing main does not have offload code. |
| 36 | #pragma weak main |
| 37 | int main(void) |
| 38 | { |
| 39 | OFFLOAD_TARGET_MAIN(); |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | #pragma weak MAIN__ |
| 44 | extern "C" int MAIN__(void) |
| 45 | { |
| 46 | OFFLOAD_TARGET_MAIN(); |
| 47 | return 0; |
| 48 | } |
| 49 | #endif // HOST_LIBRARY |
| 50 | |
| 51 | // offload section prolog |
| 52 | ALLOCATE(OFFLOAD_ENTRY_TABLE_SECTION_START) |
| 53 | #ifdef TARGET_WINNT |
| 54 | __declspec(align(sizeof(FuncTable::Entry))) |
| 55 | #endif // TARGET_WINNT |
| 56 | static FuncTable::Entry __offload_entry_table_start = { 0 }; |
| 57 | |
| 58 | // list element for the current module |
| 59 | static FuncList::Node __offload_entry_node = { |
| 60 | { &__offload_entry_table_start + 1, -1 }, |
| 61 | 0, 0 |
| 62 | }; |
| 63 | |
| 64 | // offload fp section prolog |
| 65 | ALLOCATE(OFFLOAD_FUNC_TABLE_SECTION_START) |
| 66 | #ifdef TARGET_WINNT |
| 67 | __declspec(align(sizeof(FuncTable::Entry))) |
| 68 | #endif // TARGET_WINNT |
| 69 | static FuncTable::Entry __offload_func_table_start = { 0 }; |
| 70 | |
| 71 | // list element for the current module |
| 72 | static FuncList::Node __offload_func_node = { |
| 73 | { &__offload_func_table_start + 1, -1 }, |
| 74 | 0, 0 |
| 75 | }; |
| 76 | |
| 77 | // offload fp section prolog |
| 78 | ALLOCATE(OFFLOAD_VAR_TABLE_SECTION_START) |
| 79 | #ifdef TARGET_WINNT |
| 80 | __declspec(align(sizeof(VarTable::Entry))) |
| 81 | #endif // TARGET_WINNT |
| 82 | static VarTable::Entry __offload_var_table_start = { 0 }; |
| 83 | |
| 84 | // list element for the current module |
| 85 | static VarList::Node __offload_var_node = { |
| 86 | { &__offload_var_table_start + 1 }, |
| 87 | 0, 0 |
| 88 | }; |
| 89 | |
| 90 | #ifdef MYO_SUPPORT |
| 91 | |
| 92 | // offload myo shared var section prolog |
| 93 | ALLOCATE(OFFLOAD_MYO_SHARED_TABLE_SECTION_START) |
| 94 | #ifdef TARGET_WINNT |
| 95 | __declspec(align(sizeof(SharedTableEntry))) |
| 96 | #endif // TARGET_WINNT |
| 97 | static SharedTableEntry __offload_myo_shared_table_start = { 0 }; |
| 98 | |
| 99 | #if HOST_LIBRARY |
| 100 | // offload myo shared var init section prolog |
| 101 | ALLOCATE(OFFLOAD_MYO_SHARED_INIT_TABLE_SECTION_START) |
| 102 | #ifdef TARGET_WINNT |
| 103 | __declspec(align(sizeof(InitTableEntry))) |
| 104 | #endif // TARGET_WINNT |
| 105 | static InitTableEntry __offload_myo_shared_init_table_start = { 0 }; |
| 106 | #endif |
| 107 | |
| 108 | // offload myo fptr section prolog |
| 109 | ALLOCATE(OFFLOAD_MYO_FPTR_TABLE_SECTION_START) |
| 110 | #ifdef TARGET_WINNT |
| 111 | __declspec(align(sizeof(FptrTableEntry))) |
| 112 | #endif // TARGET_WINNT |
| 113 | static FptrTableEntry __offload_myo_fptr_table_start = { 0 }; |
| 114 | |
| 115 | #endif // MYO_SUPPORT |
| 116 | |
| 117 | // init/fini code which adds/removes local lookup data to/from the global list |
| 118 | |
| 119 | static void offload_fini(); |
| 120 | |
| 121 | #ifndef TARGET_WINNT |
| 122 | static void offload_init() __attribute__((constructor(101))); |
| 123 | #else // TARGET_WINNT |
| 124 | static void offload_init(); |
| 125 | |
| 126 | // Place offload initialization before user constructors |
| 127 | ALLOCATE(OFFLOAD_CRTINIT_SECTION_START) |
| 128 | static void (*addressof_offload_init)() = offload_init; |
| 129 | #endif // TARGET_WINNT |
| 130 | |
| 131 | static void offload_init() |
| 132 | { |
| 133 | // register offload tables |
| 134 | __offload_register_tables(&__offload_entry_node, |
| 135 | &__offload_func_node, |
| 136 | &__offload_var_node); |
| 137 | |
| 138 | #if HOST_LIBRARY |
| 139 | __offload_register_image(&__offload_target_image); |
| 140 | atexit(offload_fini); |
| 141 | #endif // HOST_LIBRARY |
| 142 | |
| 143 | #ifdef MYO_SUPPORT |
| 144 | __offload_myoRegisterTables( |
| 145 | #if HOST_LIBRARY |
| 146 | &__offload_myo_shared_init_table_start + 1, |
| 147 | #endif // HOST_LIBRARY |
| 148 | &__offload_myo_shared_table_start + 1, |
| 149 | &__offload_myo_fptr_table_start + 1 |
| 150 | ); |
| 151 | #endif // MYO_SUPPORT |
| 152 | } |
| 153 | |
| 154 | static void offload_fini() |
| 155 | { |
| 156 | #if HOST_LIBRARY |
| 157 | __offload_unregister_image(&__offload_target_image); |
| 158 | #endif // HOST_LIBRARY |
| 159 | |
| 160 | // unregister offload tables |
| 161 | __offload_unregister_tables(&__offload_entry_node, |
| 162 | &__offload_func_node, |
| 163 | &__offload_var_node); |
| 164 | } |