aot.h 7.6 KB

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