aot.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _AOT_H_
  6. #define _AOT_H_
  7. #include "bh_platform.h"
  8. #include "bh_assert.h"
  9. #include "../common/wasm_runtime_common.h"
  10. #include "../interpreter/wasm.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. #ifndef AOT_FUNC_PREFIX
  15. #define AOT_FUNC_PREFIX "aot_func#"
  16. #endif
  17. #ifndef AOT_FUNC_INTERNAL_PREFIX
  18. #define AOT_FUNC_INTERNAL_PREFIX "aot_func_internal#"
  19. #endif
  20. #ifndef AOT_STACK_SIZES_NAME
  21. #define AOT_STACK_SIZES_NAME "aot_stack_sizes"
  22. #endif
  23. extern const char *aot_stack_sizes_name;
  24. #ifndef AOT_STACK_SIZES_ALIAS_NAME
  25. #define AOT_STACK_SIZES_ALIAS_NAME "aot_stack_sizes_alias"
  26. #endif
  27. extern const char *aot_stack_sizes_alias_name;
  28. #ifndef AOT_STACK_SIZES_SECTION_NAME
  29. #define AOT_STACK_SIZES_SECTION_NAME ".aot_stack_sizes"
  30. #endif
  31. extern const char *aot_stack_sizes_section_name;
  32. typedef InitializerExpression AOTInitExpr;
  33. typedef WASMType AOTFuncType;
  34. typedef WASMExport AOTExport;
  35. #if WASM_ENABLE_DEBUG_AOT != 0
  36. typedef void *dwarf_extractor_handle_t;
  37. #endif
  38. typedef enum AOTIntCond {
  39. INT_EQZ = 0,
  40. INT_EQ,
  41. INT_NE,
  42. INT_LT_S,
  43. INT_LT_U,
  44. INT_GT_S,
  45. INT_GT_U,
  46. INT_LE_S,
  47. INT_LE_U,
  48. INT_GE_S,
  49. INT_GE_U
  50. } AOTIntCond;
  51. typedef enum AOTFloatCond {
  52. FLOAT_EQ = 0,
  53. FLOAT_NE,
  54. FLOAT_LT,
  55. FLOAT_GT,
  56. FLOAT_LE,
  57. FLOAT_GE,
  58. FLOAT_UNO
  59. } AOTFloatCond;
  60. /**
  61. * Import memory
  62. */
  63. typedef struct AOTImportMemory {
  64. char *module_name;
  65. char *memory_name;
  66. uint32 memory_flags;
  67. uint32 num_bytes_per_page;
  68. uint32 mem_init_page_count;
  69. uint32 mem_max_page_count;
  70. } AOTImportMemory;
  71. /**
  72. * Memory information
  73. */
  74. typedef struct AOTMemory {
  75. /* memory info */
  76. uint32 memory_flags;
  77. uint32 num_bytes_per_page;
  78. uint32 mem_init_page_count;
  79. uint32 mem_max_page_count;
  80. } AOTMemory;
  81. /**
  82. * A segment of memory init data
  83. */
  84. typedef struct AOTMemInitData {
  85. #if WASM_ENABLE_BULK_MEMORY != 0
  86. /* Passive flag */
  87. bool is_passive;
  88. /* memory index */
  89. uint32 memory_index;
  90. #endif
  91. /* Start address of init data */
  92. AOTInitExpr offset;
  93. /* Byte count */
  94. uint32 byte_count;
  95. /* Byte array */
  96. uint8 bytes[1];
  97. } AOTMemInitData;
  98. /**
  99. * Import table
  100. */
  101. typedef struct AOTImportTable {
  102. char *module_name;
  103. char *table_name;
  104. uint32 elem_type;
  105. uint32 table_flags;
  106. uint32 table_init_size;
  107. uint32 table_max_size;
  108. bool possible_grow;
  109. } AOTImportTable;
  110. /**
  111. * Table
  112. */
  113. typedef struct AOTTable {
  114. uint32 elem_type;
  115. uint32 table_flags;
  116. uint32 table_init_size;
  117. uint32 table_max_size;
  118. bool possible_grow;
  119. } AOTTable;
  120. /**
  121. * A segment of table init data
  122. */
  123. typedef struct AOTTableInitData {
  124. /* 0 to 7 */
  125. uint32 mode;
  126. /* funcref or externref, elemkind will be considered as funcref */
  127. uint32 elem_type;
  128. /* optional, only for active */
  129. uint32 table_index;
  130. /* Start address of init data */
  131. AOTInitExpr offset;
  132. /* Function index count */
  133. uint32 func_index_count;
  134. /* Function index array */
  135. uint32 func_indexes[1];
  136. } AOTTableInitData;
  137. /**
  138. * Import global variable
  139. */
  140. typedef struct AOTImportGlobal {
  141. char *module_name;
  142. char *global_name;
  143. /* VALUE_TYPE_I32/I64/F32/F64 */
  144. uint8 type;
  145. bool is_mutable;
  146. uint32 size;
  147. /* The data offset of current global in global data */
  148. uint32 data_offset;
  149. /* global data after linked */
  150. WASMValue global_data_linked;
  151. bool is_linked;
  152. } AOTImportGlobal;
  153. /**
  154. * Global variable
  155. */
  156. typedef struct AOTGlobal {
  157. /* VALUE_TYPE_I32/I64/F32/F64 */
  158. uint8 type;
  159. bool is_mutable;
  160. uint32 size;
  161. /* The data offset of current global in global data */
  162. uint32 data_offset;
  163. AOTInitExpr init_expr;
  164. } AOTGlobal;
  165. /**
  166. * Import function
  167. */
  168. typedef struct AOTImportFunc {
  169. char *module_name;
  170. char *func_name;
  171. AOTFuncType *func_type;
  172. uint32 func_type_index;
  173. /* function pointer after linked */
  174. void *func_ptr_linked;
  175. /* signature from registered native symbols */
  176. const char *signature;
  177. /* attachment */
  178. void *attachment;
  179. bool call_conv_raw;
  180. bool call_conv_wasm_c_api;
  181. bool wasm_c_api_with_env;
  182. } AOTImportFunc;
  183. /**
  184. * Function
  185. */
  186. typedef struct AOTFunc {
  187. AOTFuncType *func_type;
  188. uint32 func_type_index;
  189. uint32 local_count;
  190. uint8 *local_types;
  191. uint16 param_cell_num;
  192. uint16 local_cell_num;
  193. uint32 code_size;
  194. uint8 *code;
  195. } AOTFunc;
  196. typedef struct AOTCompData {
  197. /* Import memories */
  198. uint32 import_memory_count;
  199. AOTImportMemory *import_memories;
  200. /* Memories */
  201. uint32 memory_count;
  202. AOTMemory *memories;
  203. /* Memory init data info */
  204. uint32 mem_init_data_count;
  205. AOTMemInitData **mem_init_data_list;
  206. /* Import tables */
  207. uint32 import_table_count;
  208. AOTImportTable *import_tables;
  209. /* Tables */
  210. uint32 table_count;
  211. AOTTable *tables;
  212. /* Table init data info */
  213. uint32 table_init_data_count;
  214. AOTTableInitData **table_init_data_list;
  215. /* Import globals */
  216. uint32 import_global_count;
  217. AOTImportGlobal *import_globals;
  218. /* Globals */
  219. uint32 global_count;
  220. AOTGlobal *globals;
  221. /* Function types */
  222. uint32 func_type_count;
  223. AOTFuncType **func_types;
  224. /* Import functions */
  225. uint32 import_func_count;
  226. AOTImportFunc *import_funcs;
  227. /* Functions */
  228. uint32 func_count;
  229. AOTFunc **funcs;
  230. /* Custom name sections */
  231. const uint8 *name_section_buf;
  232. const uint8 *name_section_buf_end;
  233. uint8 *aot_name_section_buf;
  234. uint32 aot_name_section_size;
  235. uint32 global_data_size;
  236. uint32 start_func_index;
  237. uint32 malloc_func_index;
  238. uint32 free_func_index;
  239. uint32 retain_func_index;
  240. uint32 aux_data_end_global_index;
  241. uint32 aux_data_end;
  242. uint32 aux_heap_base_global_index;
  243. uint32 aux_heap_base;
  244. uint32 aux_stack_top_global_index;
  245. uint32 aux_stack_bottom;
  246. uint32 aux_stack_size;
  247. WASMModule *wasm_module;
  248. #if WASM_ENABLE_DEBUG_AOT != 0
  249. dwarf_extractor_handle_t extractor;
  250. #endif
  251. } AOTCompData;
  252. typedef struct AOTNativeSymbol {
  253. bh_list_link link;
  254. char symbol[32];
  255. int32 index;
  256. } AOTNativeSymbol;
  257. AOTCompData *
  258. aot_create_comp_data(WASMModule *module);
  259. void
  260. aot_destroy_comp_data(AOTCompData *comp_data);
  261. char *
  262. aot_get_last_error();
  263. void
  264. aot_set_last_error(const char *error);
  265. void
  266. aot_set_last_error_v(const char *format, ...);
  267. #if BH_DEBUG != 0
  268. #define HANDLE_FAILURE(callee) \
  269. do { \
  270. aot_set_last_error_v("call %s failed in %s:%d", (callee), \
  271. __FUNCTION__, __LINE__); \
  272. } while (0)
  273. #else
  274. #define HANDLE_FAILURE(callee) \
  275. do { \
  276. aot_set_last_error_v("call %s failed", (callee)); \
  277. } while (0)
  278. #endif
  279. static inline uint32
  280. aot_get_imp_tbl_data_slots(const AOTImportTable *tbl, bool is_jit_mode)
  281. {
  282. #if WASM_ENABLE_MULTI_MODULE != 0
  283. if (is_jit_mode)
  284. return tbl->table_max_size;
  285. #else
  286. (void)is_jit_mode;
  287. #endif
  288. return tbl->possible_grow ? tbl->table_max_size : tbl->table_init_size;
  289. }
  290. static inline uint32
  291. aot_get_tbl_data_slots(const AOTTable *tbl, bool is_jit_mode)
  292. {
  293. #if WASM_ENABLE_MULTI_MODULE != 0
  294. if (is_jit_mode)
  295. return tbl->table_max_size;
  296. #else
  297. (void)is_jit_mode;
  298. #endif
  299. return tbl->possible_grow ? tbl->table_max_size : tbl->table_init_size;
  300. }
  301. #ifdef __cplusplus
  302. } /* end of extern "C" */
  303. #endif
  304. #endif /* end of _AOT_H_ */