ofldbegin.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. Copyright (c) 2014 Intel Corporation. All Rights Reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Intel Corporation nor the names of its
  12. contributors may be used to endorse or promote products derived
  13. from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #if HOST_LIBRARY
  27. #include "offload_host.h"
  28. #include "offload_myo_host.h"
  29. #else
  30. #include "compiler_if_target.h"
  31. #include "offload_target.h"
  32. #include "offload_myo_target.h"
  33. #endif
  34. #ifdef TARGET_WINNT
  35. #define ALLOCATE(name) __declspec(allocate(name))
  36. #define DLL_LOCAL
  37. #else // TARGET_WINNT
  38. #define ALLOCATE(name) __attribute__((section(name)))
  39. #define DLL_LOCAL __attribute__((visibility("hidden")))
  40. #endif // TARGET_WINNT
  41. #if HOST_LIBRARY
  42. // the host program/shared library should always have __offload_target_image
  43. // symbol defined. This symbol specifies the beginning of the target program
  44. // image.
  45. extern "C" DLL_LOCAL const void* __offload_target_image;
  46. #else // HOST_LIBRARY
  47. // Define a weak main which would be used on target side in case usere's
  48. // source file containing main does not have offload code.
  49. #pragma weak main
  50. int main(void)
  51. {
  52. OFFLOAD_TARGET_MAIN();
  53. return 0;
  54. }
  55. #pragma weak MAIN__
  56. extern "C" int MAIN__(void)
  57. {
  58. OFFLOAD_TARGET_MAIN();
  59. return 0;
  60. }
  61. #endif // HOST_LIBRARY
  62. // offload section prolog
  63. ALLOCATE(OFFLOAD_ENTRY_TABLE_SECTION_START)
  64. #ifdef TARGET_WINNT
  65. __declspec(align(sizeof(FuncTable::Entry)))
  66. #endif // TARGET_WINNT
  67. static FuncTable::Entry __offload_entry_table_start = { 0 };
  68. // list element for the current module
  69. static FuncList::Node __offload_entry_node = {
  70. { &__offload_entry_table_start + 1, -1 },
  71. 0, 0
  72. };
  73. // offload fp section prolog
  74. ALLOCATE(OFFLOAD_FUNC_TABLE_SECTION_START)
  75. #ifdef TARGET_WINNT
  76. __declspec(align(sizeof(FuncTable::Entry)))
  77. #endif // TARGET_WINNT
  78. static FuncTable::Entry __offload_func_table_start = { 0 };
  79. // list element for the current module
  80. static FuncList::Node __offload_func_node = {
  81. { &__offload_func_table_start + 1, -1 },
  82. 0, 0
  83. };
  84. // offload fp section prolog
  85. ALLOCATE(OFFLOAD_VAR_TABLE_SECTION_START)
  86. #ifdef TARGET_WINNT
  87. __declspec(align(sizeof(VarTable::Entry)))
  88. #endif // TARGET_WINNT
  89. static VarTable::Entry __offload_var_table_start = { 0 };
  90. // list element for the current module
  91. static VarList::Node __offload_var_node = {
  92. { &__offload_var_table_start + 1 },
  93. 0, 0
  94. };
  95. #ifdef MYO_SUPPORT
  96. // offload myo shared var section prolog
  97. ALLOCATE(OFFLOAD_MYO_SHARED_TABLE_SECTION_START)
  98. #ifdef TARGET_WINNT
  99. __declspec(align(sizeof(SharedTableEntry)))
  100. #endif // TARGET_WINNT
  101. static SharedTableEntry __offload_myo_shared_table_start = { 0 };
  102. #if HOST_LIBRARY
  103. // offload myo shared var init section prolog
  104. ALLOCATE(OFFLOAD_MYO_SHARED_INIT_TABLE_SECTION_START)
  105. #ifdef TARGET_WINNT
  106. __declspec(align(sizeof(InitTableEntry)))
  107. #endif // TARGET_WINNT
  108. static InitTableEntry __offload_myo_shared_init_table_start = { 0 };
  109. #endif
  110. // offload myo fptr section prolog
  111. ALLOCATE(OFFLOAD_MYO_FPTR_TABLE_SECTION_START)
  112. #ifdef TARGET_WINNT
  113. __declspec(align(sizeof(FptrTableEntry)))
  114. #endif // TARGET_WINNT
  115. static FptrTableEntry __offload_myo_fptr_table_start = { 0 };
  116. #endif // MYO_SUPPORT
  117. // init/fini code which adds/removes local lookup data to/from the global list
  118. static void offload_fini();
  119. #ifndef TARGET_WINNT
  120. static void offload_init() __attribute__((constructor(101)));
  121. #else // TARGET_WINNT
  122. static void offload_init();
  123. // Place offload initialization before user constructors
  124. ALLOCATE(OFFLOAD_CRTINIT_SECTION_START)
  125. static void (*addressof_offload_init)() = offload_init;
  126. #endif // TARGET_WINNT
  127. static void offload_init()
  128. {
  129. // register offload tables
  130. __offload_register_tables(&__offload_entry_node,
  131. &__offload_func_node,
  132. &__offload_var_node);
  133. #if HOST_LIBRARY
  134. __offload_register_image(&__offload_target_image);
  135. atexit(offload_fini);
  136. #endif // HOST_LIBRARY
  137. #ifdef MYO_SUPPORT
  138. __offload_myoRegisterTables(
  139. #if HOST_LIBRARY
  140. &__offload_myo_shared_init_table_start + 1,
  141. #endif // HOST_LIBRARY
  142. &__offload_myo_shared_table_start + 1,
  143. &__offload_myo_fptr_table_start + 1
  144. );
  145. #endif // MYO_SUPPORT
  146. }
  147. static void offload_fini()
  148. {
  149. #if HOST_LIBRARY
  150. __offload_unregister_image(&__offload_target_image);
  151. #endif // HOST_LIBRARY
  152. // unregister offload tables
  153. __offload_unregister_tables(&__offload_entry_node,
  154. &__offload_func_node,
  155. &__offload_var_node);
  156. }