aot.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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_FEATURE_ENABLE_DYNAMIC_LINKING 0x1
  15. #define AOT_FEATURE_ENABLE_XIP_MODE (0x1 << 1)
  16. #define AOT_FUNC_PREFIX "aot_func#"
  17. typedef InitializerExpression AOTInitExpr;
  18. typedef WASMType AOTFuncType;
  19. typedef WASMExport AOTExport;
  20. struct AOTCompContext;
  21. #if WASM_ENABLE_DEBUG_AOT != 0
  22. typedef void *dwar_extractor_handle_t;
  23. #endif
  24. typedef enum AOTIntCond {
  25. INT_EQZ = 0,
  26. INT_EQ,
  27. INT_NE,
  28. INT_LT_S,
  29. INT_LT_U,
  30. INT_GT_S,
  31. INT_GT_U,
  32. INT_LE_S,
  33. INT_LE_U,
  34. INT_GE_S,
  35. INT_GE_U
  36. } AOTIntCond;
  37. typedef enum AOTFloatCond {
  38. FLOAT_EQ = 0,
  39. FLOAT_NE,
  40. FLOAT_LT,
  41. FLOAT_GT,
  42. FLOAT_LE,
  43. FLOAT_GE,
  44. FLOAT_UNO
  45. } AOTFloatCond;
  46. /**
  47. * Import memory
  48. */
  49. typedef struct AOTImportMemory {
  50. char *module_name;
  51. char *memory_name;
  52. uint32 memory_flags;
  53. uint32 num_bytes_per_page;
  54. uint32 mem_init_page_count;
  55. uint32 mem_max_page_count;
  56. } AOTImportMemory;
  57. /**
  58. * Memory information
  59. */
  60. typedef struct AOTMemory {
  61. /* memory info */
  62. uint32 memory_flags;
  63. uint32 num_bytes_per_page;
  64. uint32 mem_init_page_count;
  65. uint32 mem_max_page_count;
  66. } AOTMemory;
  67. /**
  68. * A segment of memory init data
  69. */
  70. typedef struct AOTMemInitData {
  71. #if WASM_ENABLE_BULK_MEMORY != 0
  72. /* Passive flag */
  73. bool is_passive;
  74. /* memory index */
  75. uint32 memory_index;
  76. #endif
  77. /* Start address of init data */
  78. AOTInitExpr offset;
  79. /* Byte count */
  80. uint32 byte_count;
  81. /* Byte array */
  82. uint8 bytes[1];
  83. } AOTMemInitData;
  84. /**
  85. * Import table
  86. */
  87. typedef struct AOTImportTable {
  88. char *module_name;
  89. char *table_name;
  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. const ConstStrDescription *module_name;
  128. const ConstStrDescription *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. const ConstStrDescription * module_name;
  155. const ConstStrDescription *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. const char *symbol;
  240. int32 index;
  241. } AOTNativeSymbol;
  242. AOTCompData*
  243. aot_create_comp_data(WASMModule *module, uint32 pointer_size);
  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_tbl_data_slots(const AOTTable *tbl)
  266. {
  267. return tbl->possible_grow ? tbl->table_max_size : tbl->table_init_size;
  268. }
  269. static inline uint32
  270. aot_get_imp_tbl_data_slots(const AOTImportTable *tbl)
  271. {
  272. return tbl->possible_grow ? tbl->table_max_size : tbl->table_init_size;
  273. }
  274. #ifdef __cplusplus
  275. } /* end of extern "C" */
  276. #endif
  277. #endif /* end of _AOT_H_ */