aot.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. #ifndef AOT_FUNC_INTERNAL_PREFIX
  18. #define AOT_FUNC_INTERNAL_PREFIX "aot_func_internal#"
  19. #endif
  20. #ifndef AOT_STACK_SIZES_NAME
  21. #define AOT_STACK_SIZES_NAME "aot_stack_sizes"
  22. #endif
  23. extern const char *aot_stack_sizes_name;
  24. #ifndef AOT_STACK_SIZES_ALIAS_NAME
  25. #define AOT_STACK_SIZES_ALIAS_NAME "aot_stack_sizes_alias"
  26. #endif
  27. extern const char *aot_stack_sizes_alias_name;
  28. #ifndef AOT_STACK_SIZES_SECTION_NAME
  29. #define AOT_STACK_SIZES_SECTION_NAME ".aot_stack_sizes"
  30. #endif
  31. extern const char *aot_stack_sizes_section_name;
  32. typedef InitializerExpression AOTInitExpr;
  33. typedef WASMType AOTType;
  34. typedef WASMFuncType AOTFuncType;
  35. #if WASM_ENABLE_GC != 0
  36. typedef WASMStructType AOTStructType;
  37. typedef WASMArrayType AOTArrayType;
  38. #endif
  39. typedef WASMExport AOTExport;
  40. typedef WASMMemory AOTMemory;
  41. typedef WASMMemoryType AOTMemoryType;
  42. #if WASM_ENABLE_DEBUG_AOT != 0
  43. typedef void *dwarf_extractor_handle_t;
  44. #endif
  45. typedef enum AOTIntCond {
  46. INT_EQZ = 0,
  47. INT_EQ,
  48. INT_NE,
  49. INT_LT_S,
  50. INT_LT_U,
  51. INT_GT_S,
  52. INT_GT_U,
  53. INT_LE_S,
  54. INT_LE_U,
  55. INT_GE_S,
  56. INT_GE_U
  57. } AOTIntCond;
  58. typedef enum AOTFloatCond {
  59. FLOAT_EQ = 0,
  60. FLOAT_NE,
  61. FLOAT_LT,
  62. FLOAT_GT,
  63. FLOAT_LE,
  64. FLOAT_GE,
  65. FLOAT_UNO
  66. } AOTFloatCond;
  67. /**
  68. * Import memory
  69. */
  70. typedef struct AOTImportMemory {
  71. char *module_name;
  72. char *memory_name;
  73. AOTMemoryType mem_type;
  74. } AOTImportMemory;
  75. /**
  76. * A segment of memory init data
  77. */
  78. typedef struct AOTMemInitData {
  79. #if WASM_ENABLE_BULK_MEMORY != 0
  80. /* Passive flag */
  81. bool is_passive;
  82. /* memory index */
  83. uint32 memory_index;
  84. #endif
  85. /* Start address of init data */
  86. AOTInitExpr offset;
  87. /* Byte count */
  88. uint32 byte_count;
  89. /* Byte array */
  90. uint8 bytes[1];
  91. } AOTMemInitData;
  92. /**
  93. * Import table
  94. */
  95. typedef struct AOTImportTable {
  96. char *module_name;
  97. char *table_name;
  98. uint8 elem_type;
  99. uint8 table_flags;
  100. bool possible_grow;
  101. uint32 table_init_size;
  102. uint32 table_max_size;
  103. #if WASM_ENABLE_GC != 0
  104. WASMRefType *elem_ref_type;
  105. #endif
  106. } AOTImportTable;
  107. /**
  108. * Table
  109. */
  110. typedef struct AOTTable {
  111. uint8 elem_type;
  112. uint8 table_flags;
  113. bool possible_grow;
  114. uint32 table_init_size;
  115. uint32 table_max_size;
  116. #if WASM_ENABLE_GC != 0
  117. WASMRefType *elem_ref_type;
  118. /* init expr for the whole table */
  119. InitializerExpression init_expr;
  120. #endif
  121. } AOTTable;
  122. /**
  123. * A segment of table init data
  124. */
  125. typedef struct AOTTableInitData {
  126. /* 0 to 7 */
  127. uint32 mode;
  128. /* funcref or externref, elemkind will be considered as funcref */
  129. uint32 elem_type;
  130. #if WASM_ENABLE_GC != 0
  131. WASMRefType *elem_ref_type;
  132. #endif
  133. /* optional, only for active */
  134. uint32 table_index;
  135. /* Start address of init data */
  136. AOTInitExpr offset;
  137. /* Function index count */
  138. uint32 value_count;
  139. /* Function index array */
  140. InitializerExpression init_values[1];
  141. } AOTTableInitData;
  142. /**
  143. * Import global variable
  144. */
  145. typedef struct AOTImportGlobal {
  146. char *module_name;
  147. char *global_name;
  148. WASMGlobalType type;
  149. uint32 size;
  150. /* The data offset of current global in global data */
  151. uint32 data_offset;
  152. #if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
  153. /*
  154. * The data size and data offset of a wasm global may vary
  155. * in 32-bit target and 64-bit target, e.g., the size of a
  156. * GC obj is 4 bytes in the former and 8 bytes in the
  157. * latter, the AOT compiler needs to use the correct data
  158. * offset according to the target info.
  159. */
  160. uint32 size_64bit;
  161. uint32 size_32bit;
  162. uint32 data_offset_64bit;
  163. uint32 data_offset_32bit;
  164. #endif
  165. /* global data after linked */
  166. WASMValue global_data_linked;
  167. bool is_linked;
  168. } AOTImportGlobal;
  169. /**
  170. * Global variable
  171. */
  172. typedef struct AOTGlobal {
  173. WASMGlobalType type;
  174. uint32 size;
  175. /* The data offset of current global in global data */
  176. uint32 data_offset;
  177. #if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
  178. /* See comments in AOTImportGlobal */
  179. uint32 size_64bit;
  180. uint32 size_32bit;
  181. uint32 data_offset_64bit;
  182. uint32 data_offset_32bit;
  183. #endif
  184. AOTInitExpr init_expr;
  185. } AOTGlobal;
  186. /**
  187. * Import function
  188. */
  189. typedef struct AOTImportFunc {
  190. char *module_name;
  191. char *func_name;
  192. AOTFuncType *func_type;
  193. uint32 func_type_index;
  194. /* function pointer after linked */
  195. void *func_ptr_linked;
  196. /* signature from registered native symbols */
  197. const char *signature;
  198. /* attachment */
  199. void *attachment;
  200. bool call_conv_raw;
  201. bool call_conv_wasm_c_api;
  202. bool wasm_c_api_with_env;
  203. } AOTImportFunc;
  204. /**
  205. * Function
  206. */
  207. typedef struct AOTFunc {
  208. AOTFuncType *func_type;
  209. uint32 func_type_index;
  210. uint32 local_count;
  211. uint8 *local_types_wp;
  212. uint16 param_cell_num;
  213. uint16 local_cell_num;
  214. uint32 max_stack_cell_num;
  215. uint32 code_size;
  216. uint8 *code;
  217. /* offset of each local, including function parameters
  218. and local variables */
  219. uint16 *local_offsets;
  220. } AOTFunc;
  221. typedef struct AOTCompData {
  222. /* Import memories */
  223. uint32 import_memory_count;
  224. AOTImportMemory *import_memories;
  225. /* Memories */
  226. uint32 memory_count;
  227. AOTMemory *memories;
  228. /* Memory init data info */
  229. uint32 mem_init_data_count;
  230. AOTMemInitData **mem_init_data_list;
  231. /* Import tables */
  232. uint32 import_table_count;
  233. AOTImportTable *import_tables;
  234. /* Tables */
  235. uint32 table_count;
  236. AOTTable *tables;
  237. /* Table init data info */
  238. uint32 table_init_data_count;
  239. AOTTableInitData **table_init_data_list;
  240. /* Import globals */
  241. uint32 import_global_count;
  242. AOTImportGlobal *import_globals;
  243. /* Globals */
  244. uint32 global_count;
  245. AOTGlobal *globals;
  246. /* Function types */
  247. uint32 type_count;
  248. AOTType **types;
  249. /* Import functions */
  250. uint32 import_func_count;
  251. AOTImportFunc *import_funcs;
  252. /* Functions */
  253. uint32 func_count;
  254. AOTFunc **funcs;
  255. /* Custom name sections */
  256. const uint8 *name_section_buf;
  257. const uint8 *name_section_buf_end;
  258. uint8 *aot_name_section_buf;
  259. uint32 aot_name_section_size;
  260. uint32 global_data_size_64bit;
  261. uint32 global_data_size_32bit;
  262. uint32 start_func_index;
  263. uint32 malloc_func_index;
  264. uint32 free_func_index;
  265. uint32 retain_func_index;
  266. uint32 aux_data_end_global_index;
  267. uint64 aux_data_end;
  268. uint32 aux_heap_base_global_index;
  269. uint64 aux_heap_base;
  270. uint32 aux_stack_top_global_index;
  271. uint64 aux_stack_bottom;
  272. uint32 aux_stack_size;
  273. #if WASM_ENABLE_STRINGREF != 0
  274. uint32 string_literal_count;
  275. uint32 *string_literal_lengths_wp;
  276. const uint8 **string_literal_ptrs_wp;
  277. #endif
  278. WASMModule *wasm_module;
  279. #if WASM_ENABLE_DEBUG_AOT != 0
  280. dwarf_extractor_handle_t extractor;
  281. #endif
  282. } AOTCompData;
  283. typedef struct AOTNativeSymbol {
  284. bh_list_link link;
  285. char symbol[32];
  286. int32 index;
  287. } AOTNativeSymbol;
  288. AOTCompData *
  289. aot_create_comp_data(WASMModule *module, const char *target_arch,
  290. bool gc_enabled);
  291. void
  292. aot_destroy_comp_data(AOTCompData *comp_data);
  293. char *
  294. aot_get_last_error();
  295. void
  296. aot_set_last_error(const char *error);
  297. void
  298. aot_set_last_error_v(const char *format, ...);
  299. #if BH_DEBUG != 0
  300. #define HANDLE_FAILURE(callee) \
  301. do { \
  302. aot_set_last_error_v("call %s failed in %s:%d", (callee), \
  303. __FUNCTION__, __LINE__); \
  304. } while (0)
  305. #else
  306. #define HANDLE_FAILURE(callee) \
  307. do { \
  308. aot_set_last_error_v("call %s failed", (callee)); \
  309. } while (0)
  310. #endif
  311. static inline uint32
  312. aot_get_imp_tbl_data_slots(const AOTImportTable *tbl, bool is_jit_mode)
  313. {
  314. #if WASM_ENABLE_MULTI_MODULE != 0
  315. if (is_jit_mode)
  316. return tbl->table_max_size;
  317. #else
  318. (void)is_jit_mode;
  319. #endif
  320. return tbl->possible_grow ? tbl->table_max_size : tbl->table_init_size;
  321. }
  322. static inline uint32
  323. aot_get_tbl_data_slots(const AOTTable *tbl, bool is_jit_mode)
  324. {
  325. #if WASM_ENABLE_MULTI_MODULE != 0
  326. if (is_jit_mode)
  327. return tbl->table_max_size;
  328. #else
  329. (void)is_jit_mode;
  330. #endif
  331. return tbl->possible_grow ? tbl->table_max_size : tbl->table_init_size;
  332. }
  333. #ifdef __cplusplus
  334. } /* end of extern "C" */
  335. #endif
  336. #endif /* end of _AOT_H_ */