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