aot.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. } AOTImportGlobal;
  138. /**
  139. * Global variable
  140. */
  141. typedef struct AOTGlobal {
  142. /* VALUE_TYPE_I32/I64/F32/F64 */
  143. uint8 type;
  144. bool is_mutable;
  145. uint32 size;
  146. /* The data offset of current global in global data */
  147. uint32 data_offset;
  148. AOTInitExpr init_expr;
  149. } AOTGlobal;
  150. /**
  151. * Import function
  152. */
  153. typedef struct AOTImportFunc {
  154. char *module_name;
  155. char *func_name;
  156. AOTFuncType *func_type;
  157. uint32 func_type_index;
  158. /* function pointer after linked */
  159. void *func_ptr_linked;
  160. /* signature from registered native symbols */
  161. const char *signature;
  162. /* attachment */
  163. void *attachment;
  164. bool call_conv_raw;
  165. bool call_conv_wasm_c_api;
  166. bool wasm_c_api_with_env;
  167. } AOTImportFunc;
  168. /**
  169. * Function
  170. */
  171. typedef struct AOTFunc {
  172. AOTFuncType *func_type;
  173. uint32 func_type_index;
  174. uint32 local_count;
  175. uint8 *local_types;
  176. uint16 param_cell_num;
  177. uint16 local_cell_num;
  178. uint32 code_size;
  179. uint8 *code;
  180. } AOTFunc;
  181. typedef struct AOTCompData {
  182. /* Import memories */
  183. uint32 import_memory_count;
  184. AOTImportMemory *import_memories;
  185. /* Memories */
  186. uint32 memory_count;
  187. AOTMemory *memories;
  188. /* Memory init data info */
  189. uint32 mem_init_data_count;
  190. AOTMemInitData **mem_init_data_list;
  191. /* Import tables */
  192. uint32 import_table_count;
  193. AOTImportTable *import_tables;
  194. /* Tables */
  195. uint32 table_count;
  196. AOTTable *tables;
  197. /* Table init data info */
  198. uint32 table_init_data_count;
  199. AOTTableInitData **table_init_data_list;
  200. /* Import globals */
  201. uint32 import_global_count;
  202. AOTImportGlobal *import_globals;
  203. /* Globals */
  204. uint32 global_count;
  205. AOTGlobal *globals;
  206. /* Function types */
  207. uint32 func_type_count;
  208. AOTFuncType **func_types;
  209. /* Import functions */
  210. uint32 import_func_count;
  211. AOTImportFunc *import_funcs;
  212. /* Functions */
  213. uint32 func_count;
  214. AOTFunc **funcs;
  215. /* Custom name sections */
  216. const uint8 *name_section_buf;
  217. const uint8 *name_section_buf_end;
  218. uint8 *aot_name_section_buf;
  219. uint32 aot_name_section_size;
  220. uint32 global_data_size;
  221. uint32 start_func_index;
  222. uint32 malloc_func_index;
  223. uint32 free_func_index;
  224. uint32 retain_func_index;
  225. uint32 aux_data_end_global_index;
  226. uint32 aux_data_end;
  227. uint32 aux_heap_base_global_index;
  228. uint32 aux_heap_base;
  229. uint32 aux_stack_top_global_index;
  230. uint32 aux_stack_bottom;
  231. uint32 aux_stack_size;
  232. WASMModule *wasm_module;
  233. #if WASM_ENABLE_DEBUG_AOT != 0
  234. dwar_extractor_handle_t extractor;
  235. #endif
  236. } AOTCompData;
  237. typedef struct AOTNativeSymbol {
  238. bh_list_link link;
  239. char symbol[32];
  240. int32 index;
  241. } AOTNativeSymbol;
  242. AOTCompData *
  243. aot_create_comp_data(WASMModule *module);
  244. void
  245. aot_destroy_comp_data(AOTCompData *comp_data);
  246. char *
  247. aot_get_last_error();
  248. void
  249. aot_set_last_error(const char *error);
  250. void
  251. aot_set_last_error_v(const char *format, ...);
  252. #if BH_DEBUG != 0
  253. #define HANDLE_FAILURE(callee) \
  254. do { \
  255. aot_set_last_error_v("call %s failed in %s:%d", (callee), \
  256. __FUNCTION__, __LINE__); \
  257. } while (0)
  258. #else
  259. #define HANDLE_FAILURE(callee) \
  260. do { \
  261. aot_set_last_error_v("call %s failed", (callee)); \
  262. } while (0)
  263. #endif
  264. static inline uint32
  265. aot_get_imp_tbl_data_slots(const AOTImportTable *tbl, bool is_jit_mode)
  266. {
  267. #if WASM_ENABLE_MULTI_MODULE != 0
  268. if (is_jit_mode)
  269. return tbl->table_max_size;
  270. #endif
  271. return tbl->possible_grow ? tbl->table_max_size : tbl->table_init_size;
  272. }
  273. static inline uint32
  274. aot_get_tbl_data_slots(const AOTTable *tbl, bool is_jit_mode)
  275. {
  276. #if WASM_ENABLE_MULTI_MODULE != 0
  277. if (is_jit_mode)
  278. return tbl->table_max_size;
  279. #endif
  280. return tbl->possible_grow ? tbl->table_max_size : tbl->table_init_size;
  281. }
  282. #ifdef __cplusplus
  283. } /* end of extern "C" */
  284. #endif
  285. #endif /* end of _AOT_H_ */