1/*
2    Copyright (c) 2014 Intel Corporation.  All Rights Reserved.
3
4    Redistribution and use in source and binary forms, with or without
5    modification, are permitted provided that the following conditions
6    are met:
7
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.
16
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.
28*/
29
30
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
39
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
47
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.
52extern "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
57int main(void)
58{
59    OFFLOAD_TARGET_MAIN();
60    return 0;
61}
62
63#pragma weak MAIN__
64extern "C" int MAIN__(void)
65{
66    OFFLOAD_TARGET_MAIN();
67    return 0;
68}
69#endif // HOST_LIBRARY
70
71// offload section prolog
72ALLOCATE(OFFLOAD_ENTRY_TABLE_SECTION_START)
73#ifdef TARGET_WINNT
74__declspec(align(sizeof(FuncTable::Entry)))
75#endif // TARGET_WINNT
76static FuncTable::Entry __offload_entry_table_start = { 0 };
77
78// list element for the current module
79static FuncList::Node __offload_entry_node = {
80    { &__offload_entry_table_start + 1, -1 },
81    0, 0
82};
83
84// offload fp section prolog
85ALLOCATE(OFFLOAD_FUNC_TABLE_SECTION_START)
86#ifdef TARGET_WINNT
87__declspec(align(sizeof(FuncTable::Entry)))
88#endif // TARGET_WINNT
89static FuncTable::Entry __offload_func_table_start = { 0 };
90
91// list element for the current module
92static FuncList::Node __offload_func_node = {
93    { &__offload_func_table_start + 1, -1 },
94    0, 0
95};
96
97// offload fp section prolog
98ALLOCATE(OFFLOAD_VAR_TABLE_SECTION_START)
99#ifdef TARGET_WINNT
100__declspec(align(sizeof(VarTable::Entry)))
101#endif // TARGET_WINNT
102static VarTable::Entry __offload_var_table_start = { 0 };
103
104// list element for the current module
105static VarList::Node __offload_var_node = {
106    { &__offload_var_table_start + 1 },
107    0, 0
108};
109
110#ifdef MYO_SUPPORT
111
112// offload myo shared var section prolog
113ALLOCATE(OFFLOAD_MYO_SHARED_TABLE_SECTION_START)
114#ifdef TARGET_WINNT
115__declspec(align(sizeof(SharedTableEntry)))
116#endif // TARGET_WINNT
117static SharedTableEntry __offload_myo_shared_table_start = { 0 };
118
119#if HOST_LIBRARY
120// offload myo shared var init section prolog
121ALLOCATE(OFFLOAD_MYO_SHARED_INIT_TABLE_SECTION_START)
122#ifdef TARGET_WINNT
123__declspec(align(sizeof(InitTableEntry)))
124#endif // TARGET_WINNT
125static InitTableEntry __offload_myo_shared_init_table_start = { 0 };
126#endif
127
128// offload myo fptr section prolog
129ALLOCATE(OFFLOAD_MYO_FPTR_TABLE_SECTION_START)
130#ifdef TARGET_WINNT
131__declspec(align(sizeof(FptrTableEntry)))
132#endif // TARGET_WINNT
133static FptrTableEntry __offload_myo_fptr_table_start = { 0 };
134
135#endif // MYO_SUPPORT
136
137// init/fini code which adds/removes local lookup data to/from the global list
138
139static void offload_fini();
140
141#ifndef TARGET_WINNT
142static void offload_init() __attribute__((constructor(101)));
143#else // TARGET_WINNT
144static void offload_init();
145
146// Place offload initialization before user constructors
147ALLOCATE(OFFLOAD_CRTINIT_SECTION_START)
148static void (*addressof_offload_init)() = offload_init;
149#endif // TARGET_WINNT
150
151static void offload_init()
152{
153    // register offload tables
154    __offload_register_tables(&__offload_entry_node,
155                              &__offload_func_node,
156                              &__offload_var_node);
157
158#if HOST_LIBRARY
159    __offload_register_image(&__offload_target_image);
160    atexit(offload_fini);
161#endif // HOST_LIBRARY
162
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
170    );
171#endif // MYO_SUPPORT
172}
173
174static void offload_fini()
175{
176#if HOST_LIBRARY
177    __offload_unregister_image(&__offload_target_image);
178#endif // HOST_LIBRARY
179
180    // unregister offload tables
181    __offload_unregister_tables(&__offload_entry_node,
182                                &__offload_func_node,
183                                &__offload_var_node);
184}
185