aot.h 6.7 KB

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