aot.h 7.0 KB

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