aot.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 table_flags;
  88. uint32 table_init_size;
  89. uint32 table_max_size;
  90. bool possible_grow;
  91. } AOTImportTable;
  92. /**
  93. * Table
  94. */
  95. typedef struct AOTTable {
  96. uint32 elem_type;
  97. uint32 table_flags;
  98. uint32 table_init_size;
  99. uint32 table_max_size;
  100. bool possible_grow;
  101. } AOTTable;
  102. /**
  103. * A segment of table init data
  104. */
  105. typedef struct AOTTableInitData {
  106. /* 0 to 7 */
  107. uint32 mode;
  108. /* funcref or externref, elemkind will be considered as funcref */
  109. uint32 elem_type;
  110. bool is_dropped;
  111. /* optional, only for active */
  112. uint32 table_index;
  113. /* Start address of init data */
  114. AOTInitExpr offset;
  115. /* Function index count */
  116. uint32 func_index_count;
  117. /* Function index array */
  118. uint32 func_indexes[1];
  119. } AOTTableInitData;
  120. /**
  121. * Import global variable
  122. */
  123. typedef struct AOTImportGlobal {
  124. char *module_name;
  125. char *global_name;
  126. /* VALUE_TYPE_I32/I64/F32/F64 */
  127. uint8 type;
  128. bool is_mutable;
  129. uint32 size;
  130. /* The data offset of current global in global data */
  131. uint32 data_offset;
  132. /* global data after linked */
  133. WASMValue global_data_linked;
  134. } AOTImportGlobal;
  135. /**
  136. * Global variable
  137. */
  138. typedef struct AOTGlobal {
  139. /* VALUE_TYPE_I32/I64/F32/F64 */
  140. uint8 type;
  141. bool is_mutable;
  142. uint32 size;
  143. /* The data offset of current global in global data */
  144. uint32 data_offset;
  145. AOTInitExpr init_expr;
  146. } AOTGlobal;
  147. /**
  148. * Import function
  149. */
  150. typedef struct AOTImportFunc {
  151. char *module_name;
  152. char *func_name;
  153. AOTFuncType *func_type;
  154. uint32 func_type_index;
  155. /* function pointer after linked */
  156. void *func_ptr_linked;
  157. /* signature from registered native symbols */
  158. const char *signature;
  159. /* attachment */
  160. void *attachment;
  161. bool call_conv_raw;
  162. bool call_conv_wasm_c_api;
  163. bool wasm_c_api_with_env;
  164. } AOTImportFunc;
  165. /**
  166. * Function
  167. */
  168. typedef struct AOTFunc {
  169. AOTFuncType *func_type;
  170. uint32 func_type_index;
  171. uint32 local_count;
  172. uint8 *local_types;
  173. uint16 param_cell_num;
  174. uint16 local_cell_num;
  175. uint32 code_size;
  176. uint8 *code;
  177. } AOTFunc;
  178. typedef struct AOTCompData {
  179. /* Import memories */
  180. uint32 import_memory_count;
  181. AOTImportMemory *import_memories;
  182. /* Memories */
  183. uint32 memory_count;
  184. AOTMemory *memories;
  185. /* Memory init data info */
  186. uint32 mem_init_data_count;
  187. AOTMemInitData **mem_init_data_list;
  188. /* Import tables */
  189. uint32 import_table_count;
  190. AOTImportTable *import_tables;
  191. /* Tables */
  192. uint32 table_count;
  193. AOTTable *tables;
  194. /* Table init data info */
  195. uint32 table_init_data_count;
  196. AOTTableInitData **table_init_data_list;
  197. /* Import globals */
  198. uint32 import_global_count;
  199. AOTImportGlobal *import_globals;
  200. /* Globals */
  201. uint32 global_count;
  202. AOTGlobal *globals;
  203. /* Function types */
  204. uint32 func_type_count;
  205. AOTFuncType **func_types;
  206. /* Import functions */
  207. uint32 import_func_count;
  208. AOTImportFunc *import_funcs;
  209. /* Functions */
  210. uint32 func_count;
  211. AOTFunc **funcs;
  212. /* Custom name sections */
  213. const uint8 *name_section_buf;
  214. const uint8 *name_section_buf_end;
  215. uint8 *aot_name_section_buf;
  216. uint32 aot_name_section_size;
  217. uint32 global_data_size;
  218. uint32 start_func_index;
  219. uint32 malloc_func_index;
  220. uint32 free_func_index;
  221. uint32 retain_func_index;
  222. uint32 aux_data_end_global_index;
  223. uint32 aux_data_end;
  224. uint32 aux_heap_base_global_index;
  225. uint32 aux_heap_base;
  226. uint32 aux_stack_top_global_index;
  227. uint32 aux_stack_bottom;
  228. uint32 aux_stack_size;
  229. WASMModule *wasm_module;
  230. #if WASM_ENABLE_DEBUG_AOT != 0
  231. dwar_extractor_handle_t extractor;
  232. #endif
  233. } AOTCompData;
  234. typedef struct AOTNativeSymbol {
  235. bh_list_link link;
  236. char symbol[32];
  237. int32 index;
  238. } AOTNativeSymbol;
  239. AOTCompData *
  240. aot_create_comp_data(WASMModule *module);
  241. void
  242. aot_destroy_comp_data(AOTCompData *comp_data);
  243. char *
  244. aot_get_last_error();
  245. void
  246. aot_set_last_error(const char *error);
  247. void
  248. aot_set_last_error_v(const char *format, ...);
  249. #if BH_DEBUG != 0
  250. #define HANDLE_FAILURE(callee) \
  251. do { \
  252. aot_set_last_error_v("call %s failed in %s:%d", (callee), \
  253. __FUNCTION__, __LINE__); \
  254. } while (0)
  255. #else
  256. #define HANDLE_FAILURE(callee) \
  257. do { \
  258. aot_set_last_error_v("call %s failed", (callee)); \
  259. } while (0)
  260. #endif
  261. static inline uint32
  262. aot_get_tbl_data_slots(const AOTTable *tbl)
  263. {
  264. return tbl->possible_grow ? tbl->table_max_size : tbl->table_init_size;
  265. }
  266. static inline uint32
  267. aot_get_imp_tbl_data_slots(const AOTImportTable *tbl)
  268. {
  269. return tbl->possible_grow ? tbl->table_max_size : tbl->table_init_size;
  270. }
  271. #ifdef __cplusplus
  272. } /* end of extern "C" */
  273. #endif
  274. #endif /* end of _AOT_H_ */