1 /* 2 * Copyright (c) 2020 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef LITE_CORE_MAIN_H 16 #define LITE_CORE_MAIN_H 17 #ifdef __cplusplus 18 #if __cplusplus 19 extern "C" { 20 #endif 21 #endif 22 23 #define SYS_NAME(name, step) ".zinitcall.sys." #name #step ".init" 24 #define MODULE_NAME(name, step) ".zinitcall." #name #step ".init" 25 26 #define SYS_CALL(name, step) \ 27 do { \ 28 InitCall *initcall = (InitCall *)(SYS_BEGIN(name, step)); \ 29 InitCall *initend = (InitCall *)(SYS_END(name, step)); \ 30 for (; initcall < initend; initcall++) { \ 31 (*initcall)(); \ 32 } \ 33 } while (0) 34 35 #define MODULE_CALL(name, step) \ 36 do { \ 37 InitCall *initcall = (InitCall *)(MODULE_BEGIN(name, step)); \ 38 InitCall *initend = (InitCall *)(MODULE_END(name, step)); \ 39 for (; initcall < initend; initcall++) { \ 40 (*initcall)(); \ 41 } \ 42 } while (0) 43 44 #if (defined(__GNUC__) || defined(__clang__)) 45 46 #define SYS_BEGIN(name, step) \ 47 ({ extern InitCall __zinitcall_sys_##name##_start; \ 48 InitCall *initCall = &__zinitcall_sys_##name##_start; \ 49 (initCall); \ 50 }) 51 52 #define SYS_END(name, step) \ 53 ({ extern InitCall __zinitcall_sys_##name##_end; \ 54 InitCall *initCall = &__zinitcall_sys_##name##_end; \ 55 (initCall); \ 56 }) 57 58 #define MODULE_BEGIN(name, step) \ 59 ({ extern InitCall __zinitcall_##name##_start; \ 60 InitCall *initCall = &__zinitcall_##name##_start; \ 61 (initCall); \ 62 }) 63 #define MODULE_END(name, step) \ 64 ({ extern InitCall __zinitcall_##name##_end; \ 65 InitCall *initCall = &__zinitcall_##name##_end; \ 66 (initCall); \ 67 }) 68 69 #define SYS_INIT(name) \ 70 do { \ 71 SYS_CALL(name, 0); \ 72 } while (0) 73 74 #define MODULE_INIT(name) \ 75 do { \ 76 MODULE_CALL(name, 0); \ 77 } while (0) 78 79 #elif (defined(__ICCARM__)) 80 81 #define SYS_BEGIN(name, step) __section_begin(SYS_NAME(name, step)) 82 #define SYS_END(name, step) __section_end(SYS_NAME(name, step)) 83 #define MODULE_BEGIN(name, step) __section_begin(MODULE_NAME(name, step)) 84 #define MODULE_END(name, step) __section_end(MODULE_NAME(name, step)) 85 86 #pragma section = SYS_NAME(service, 0) 87 #pragma section = SYS_NAME(service, 1) 88 #pragma section = SYS_NAME(service, 2) 89 #pragma section = SYS_NAME(service, 3) 90 #pragma section = SYS_NAME(service, 4) 91 #pragma section = SYS_NAME(feature, 0) 92 #pragma section = SYS_NAME(feature, 1) 93 #pragma section = SYS_NAME(feature, 2) 94 #pragma section = SYS_NAME(feature, 3) 95 #pragma section = SYS_NAME(feature, 4) 96 #pragma section = MODULE_NAME(bsp, 0) 97 #pragma section = MODULE_NAME(bsp, 1) 98 #pragma section = MODULE_NAME(bsp, 2) 99 #pragma section = MODULE_NAME(bsp, 3) 100 #pragma section = MODULE_NAME(bsp, 4) 101 #pragma section = MODULE_NAME(device, 0) 102 #pragma section = MODULE_NAME(device, 1) 103 #pragma section = MODULE_NAME(device, 2) 104 #pragma section = MODULE_NAME(device, 3) 105 #pragma section = MODULE_NAME(device, 4) 106 #pragma section = MODULE_NAME(core, 0) 107 #pragma section = MODULE_NAME(core, 1) 108 #pragma section = MODULE_NAME(core, 2) 109 #pragma section = MODULE_NAME(core, 3) 110 #pragma section = MODULE_NAME(core, 4) 111 #pragma section = MODULE_NAME(run, 0) 112 #pragma section = MODULE_NAME(run, 1) 113 #pragma section = MODULE_NAME(run, 2) 114 #pragma section = MODULE_NAME(run, 3) 115 #pragma section = MODULE_NAME(run, 4) 116 117 #define SYS_INIT(name) \ 118 do { \ 119 SYS_CALL(name, 0); \ 120 SYS_CALL(name, 1); \ 121 SYS_CALL(name, 2); \ 122 SYS_CALL(name, 3); \ 123 SYS_CALL(name, 4); \ 124 } while (0) 125 126 #define MODULE_INIT(name) \ 127 do { \ 128 MODULE_CALL(name, 0); \ 129 MODULE_CALL(name, 1); \ 130 MODULE_CALL(name, 2); \ 131 MODULE_CALL(name, 3); \ 132 MODULE_CALL(name, 4); \ 133 } while (0) 134 135 #else 136 #error Not support current compiler! 137 #endif 138 139 #ifdef __cplusplus 140 #if __cplusplus 141 } 142 #endif 143 #endif 144 #endif // LITE_CORE_MAIN_H 145